瀏覽代碼

Custom Shader tutorial (#2281) [ci skip]

Alec Jacobson 2 年之前
父節點
當前提交
0c78def493
共有 3 個文件被更改,包括 118 次插入1 次删除
  1. 116 0
      tutorial/114_CustomShader/main.cpp
  2. 1 1
      tutorial/906_TrimWithSolid/main.cpp
  3. 1 0
      tutorial/CMakeLists.txt

+ 116 - 0
tutorial/114_CustomShader/main.cpp

@@ -0,0 +1,116 @@
+#include <igl/read_triangle_mesh.h>
+#include <igl/opengl/glfw/Viewer.h>
+#include <igl/opengl/create_shader_program.h>
+#include <igl/opengl/destroy_shader_program.h>
+#include <Eigen/Core>
+
+int main(int argc, char *argv[])
+{
+  using namespace Eigen;
+  using namespace std;
+  Eigen::MatrixXd V;
+  Eigen::MatrixXi F;
+  igl::opengl::glfw::Viewer v;
+  igl::read_triangle_mesh(
+    argc>1?argv[1]: TUTORIAL_SHARED_PATH "/bunny.off", V, F);
+  v.data().set_mesh(V,F);
+  v.data().set_face_based(false);
+  v.data().show_lines = false;
+  v.launch_init();
+
+  v.data().meshgl.init();
+  igl::opengl::destroy_shader_program(v.data().meshgl.shader_mesh);
+
+  // Custom shader that adds a Fresnel effect
+  {
+    std::string mesh_vertex_shader_string =
+R"(#version 150
+uniform mat4 view;
+uniform mat4 proj;
+uniform mat4 normal_matrix;
+in vec3 position;
+in vec3 normal;
+out vec3 position_eye;
+out vec3 normal_eye;
+in vec4 Ka;
+in vec4 Kd;
+in vec4 Ks;
+in vec2 texcoord;
+out vec2 texcoordi;
+out vec4 Kai;
+out vec4 Kdi;
+out vec4 Ksi;
+
+void main()
+{
+  position_eye = vec3 (view * vec4 (position, 1.0));
+  normal_eye = vec3 (normal_matrix * vec4 (normal, 0.0));
+  normal_eye = normalize(normal_eye);
+  gl_Position = proj * vec4 (position_eye, 1.0); //proj * view * vec4(position, 1.0);
+  Kai = Ka;
+  Kdi = Kd;
+  Ksi = Ks;
+  texcoordi = texcoord;
+})";
+
+    std::string mesh_fragment_shader_string =
+R"(#version 150
+uniform mat4 view;
+uniform mat4 proj;
+uniform vec4 fixed_color;
+in vec3 position_eye;
+in vec3 normal_eye;
+uniform vec3 light_position_eye;
+vec3 Ls = vec3 (1, 1, 1);
+vec3 Ld = vec3 (1, 1, 1);
+vec3 La = vec3 (1, 1, 1);
+in vec4 Ksi;
+in vec4 Kdi;
+in vec4 Kai;
+in vec2 texcoordi;
+uniform sampler2D tex;
+uniform float specular_exponent;
+uniform float lighting_factor;
+uniform float texture_factor;
+out vec4 outColor;
+void main()
+{
+vec3 Ia = La * vec3(Kai);    // ambient intensity
+
+vec3 vector_to_light_eye = light_position_eye - position_eye;
+vec3 direction_to_light_eye = normalize (vector_to_light_eye);
+float dot_prod = dot (direction_to_light_eye, normalize(normal_eye));
+float clamped_dot_prod = max (dot_prod, 0.0);
+vec3 Id = Ld * vec3(Kdi) * clamped_dot_prod;    // Diffuse intensity
+
+vec3 reflection_eye = reflect (-direction_to_light_eye, normalize(normal_eye));
+vec3 surface_to_viewer_eye = normalize (-position_eye);
+float dot_prod_specular = dot (reflection_eye, surface_to_viewer_eye);
+dot_prod_specular = float(abs(dot_prod)==dot_prod) * max (dot_prod_specular, 0.0);
+float specular_factor = pow (dot_prod_specular, specular_exponent);
+vec3 Kfi = 0.5*vec3(Ksi);
+vec3 Lf = Ls;
+float fresnel_exponent = 2*specular_exponent;
+float fresnel_factor = 0;
+{
+  float NE = max( 0., dot( normalize(normal_eye), surface_to_viewer_eye));
+  fresnel_factor = pow (max(sqrt(1. - NE*NE),0.0), fresnel_exponent);
+}
+vec3 Is = Ls * vec3(Ksi) * specular_factor;    // specular intensity
+vec3 If = Lf * vec3(Kfi) * fresnel_factor;     // fresnel intensity
+vec4 color = vec4(lighting_factor * (If + Is + Id) + Ia +
+  (1.0-lighting_factor) * vec3(Kdi),(Kai.a+Ksi.a+Kdi.a)/3);
+outColor = mix(vec4(1,1,1,1), texture(tex, texcoordi), texture_factor) * color;
+if (fixed_color != vec4(0.0)) outColor = fixed_color;
+})";
+
+    igl::opengl::create_shader_program(
+      mesh_vertex_shader_string,
+      mesh_fragment_shader_string,
+      {},
+      v.data().meshgl.shader_mesh);
+  }
+
+  v.launch_rendering(true);
+  v.launch_shut();
+}

+ 1 - 1
tutorial/906_TrimWithSolid/main.cpp

@@ -86,7 +86,7 @@ int main(int argc, char *argv[])
   // set first mesh as selected
   vr.selected_data_index = 0;
 
-  vr.launch_init(false);
+  vr.launch_init();
   vr.core().draw(vr.data(),true);
   vr.callback_mouse_up(vr,0,0);
   vr.launch_rendering(true);

+ 1 - 0
tutorial/CMakeLists.txt

@@ -31,6 +31,7 @@ if(LIBIGL_TUTORIALS_CHAPTER1)
     igl_add_tutorial(111_MatCap igl::glfw igl::stb)
     igl_add_tutorial(112_Selection igl::imgui)
     igl_add_tutorial(113_Shadows igl::imgui igl::stb)
+    igl_add_tutorial(114_CustomShader igl::glfw)
 endif()
 
 # Chapter 2