2
0

main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <igl/read_triangle_mesh.h>
  2. #include <igl/opengl/glfw/Viewer.h>
  3. #include <igl/opengl/create_shader_program.h>
  4. #include <igl/opengl/destroy_shader_program.h>
  5. #include <Eigen/Core>
  6. int main(int argc, char *argv[])
  7. {
  8. using namespace Eigen;
  9. using namespace std;
  10. Eigen::MatrixXd V;
  11. Eigen::MatrixXi F;
  12. igl::opengl::glfw::Viewer v;
  13. igl::read_triangle_mesh(
  14. argc>1?argv[1]: TUTORIAL_SHARED_PATH "/bunny.off", V, F);
  15. v.data().set_mesh(V,F);
  16. v.data().set_face_based(false);
  17. v.data().show_lines = false;
  18. v.launch_init();
  19. v.data().meshgl.init();
  20. igl::opengl::destroy_shader_program(v.data().meshgl.shader_mesh);
  21. // Custom shader that adds a Fresnel effect
  22. {
  23. std::string mesh_vertex_shader_string =
  24. R"(#version 150
  25. uniform mat4 view;
  26. uniform mat4 proj;
  27. uniform mat4 normal_matrix;
  28. in vec3 position;
  29. in vec3 normal;
  30. out vec3 position_eye;
  31. out vec3 normal_eye;
  32. in vec4 Ka;
  33. in vec4 Kd;
  34. in vec4 Ks;
  35. in vec2 texcoord;
  36. out vec2 texcoordi;
  37. out vec4 Kai;
  38. out vec4 Kdi;
  39. out vec4 Ksi;
  40. void main()
  41. {
  42. position_eye = vec3 (view * vec4 (position, 1.0));
  43. normal_eye = vec3 (normal_matrix * vec4 (normal, 0.0));
  44. normal_eye = normalize(normal_eye);
  45. gl_Position = proj * vec4 (position_eye, 1.0); //proj * view * vec4(position, 1.0);
  46. Kai = Ka;
  47. Kdi = Kd;
  48. Ksi = Ks;
  49. texcoordi = texcoord;
  50. })";
  51. std::string mesh_fragment_shader_string =
  52. R"(#version 150
  53. uniform mat4 view;
  54. uniform mat4 proj;
  55. uniform vec4 fixed_color;
  56. in vec3 position_eye;
  57. in vec3 normal_eye;
  58. uniform vec3 light_position_eye;
  59. vec3 Ls = vec3 (1, 1, 1);
  60. vec3 Ld = vec3 (1, 1, 1);
  61. vec3 La = vec3 (1, 1, 1);
  62. in vec4 Ksi;
  63. in vec4 Kdi;
  64. in vec4 Kai;
  65. in vec2 texcoordi;
  66. uniform sampler2D tex;
  67. uniform float specular_exponent;
  68. uniform float lighting_factor;
  69. uniform float texture_factor;
  70. out vec4 outColor;
  71. void main()
  72. {
  73. vec3 Ia = La * vec3(Kai); // ambient intensity
  74. vec3 vector_to_light_eye = light_position_eye - position_eye;
  75. vec3 direction_to_light_eye = normalize (vector_to_light_eye);
  76. float dot_prod = dot (direction_to_light_eye, normalize(normal_eye));
  77. float clamped_dot_prod = max (dot_prod, 0.0);
  78. vec3 Id = Ld * vec3(Kdi) * clamped_dot_prod; // Diffuse intensity
  79. vec3 reflection_eye = reflect (-direction_to_light_eye, normalize(normal_eye));
  80. vec3 surface_to_viewer_eye = normalize (-position_eye);
  81. float dot_prod_specular = dot (reflection_eye, surface_to_viewer_eye);
  82. dot_prod_specular = float(abs(dot_prod)==dot_prod) * max (dot_prod_specular, 0.0);
  83. float specular_factor = pow (dot_prod_specular, specular_exponent);
  84. vec3 Kfi = 0.5*vec3(Ksi);
  85. vec3 Lf = Ls;
  86. float fresnel_exponent = 2*specular_exponent;
  87. float fresnel_factor = 0;
  88. {
  89. float NE = max( 0., dot( normalize(normal_eye), surface_to_viewer_eye));
  90. fresnel_factor = pow (max(sqrt(1. - NE*NE),0.0), fresnel_exponent);
  91. }
  92. vec3 Is = Ls * vec3(Ksi) * specular_factor; // specular intensity
  93. vec3 If = Lf * vec3(Kfi) * fresnel_factor; // fresnel intensity
  94. vec4 color = vec4(lighting_factor * (If + Is + Id) + Ia +
  95. (1.0-lighting_factor) * vec3(Kdi),(Kai.a+Ksi.a+Kdi.a)/3);
  96. outColor = mix(vec4(1,1,1,1), texture(tex, texcoordi), texture_factor) * color;
  97. if (fixed_color != vec4(0.0)) outColor = fixed_color;
  98. })";
  99. igl::opengl::create_shader_program(
  100. mesh_vertex_shader_string,
  101. mesh_fragment_shader_string,
  102. {},
  103. v.data().meshgl.shader_mesh);
  104. }
  105. v.launch_rendering(true);
  106. v.launch_shut();
  107. }