Просмотр исходного кода

Fix normal direction in the viewer.

Former-commit-id: b7b6fd4cead79120f07546760aa4b06e72699a1b
Jérémie Dumas 7 лет назад
Родитель
Сommit
1a139da04e
3 измененных файлов с 17 добавлено и 10 удалено
  1. 4 3
      include/igl/opengl/MeshGL.cpp
  2. 11 7
      include/igl/opengl/ViewerCore.cpp
  3. 2 0
      include/igl/opengl/ViewerCore.h

+ 4 - 3
include/igl/opengl/MeshGL.cpp

@@ -168,6 +168,7 @@ IGL_INLINE void igl::opengl::MeshGL::init()
   "#version 150\n"
   "uniform mat4 view;"
   "uniform mat4 proj;"
+  "uniform mat4 normal_matrix;"
   "in vec3 position;"
   "in vec3 normal;"
   "out vec3 position_eye;"
@@ -184,7 +185,7 @@ IGL_INLINE void igl::opengl::MeshGL::init()
   "void main()"
   "{"
   "  position_eye = vec3 (view * vec4 (position, 1.0));"
-  "  normal_eye = vec3 (view * vec4 (normal, 0.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;"
@@ -219,11 +220,11 @@ IGL_INLINE void igl::opengl::MeshGL::init()
 
   "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, normal_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, normal_eye);"
+  "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);"

+ 11 - 7
include/igl/opengl/ViewerCore.cpp

@@ -122,8 +122,9 @@ IGL_INLINE void igl::opengl::ViewerCore::draw(
 
   if(update_matrices)
   {
-    view  = Eigen::Matrix4f::Identity();
-    proj  = Eigen::Matrix4f::Identity();
+    view = Eigen::Matrix4f::Identity();
+    proj = Eigen::Matrix4f::Identity();
+    norm = Eigen::Matrix4f::Identity();
 
     float width  = viewport(2);
     float height = viewport(3);
@@ -134,6 +135,8 @@ IGL_INLINE void igl::opengl::ViewerCore::draw(
       * (trackball_angle * Eigen::Scaling(camera_zoom * camera_base_zoom)
       * Eigen::Translation3f(camera_translation + camera_base_translation)).matrix();
 
+    norm = view.inverse().transpose();
+
     // Set projection
     if (orthographic)
     {
@@ -152,8 +155,10 @@ IGL_INLINE void igl::opengl::ViewerCore::draw(
   // Send transformations to the GPU
   GLint viewi  = glGetUniformLocation(data.meshgl.shader_mesh,"view");
   GLint proji  = glGetUniformLocation(data.meshgl.shader_mesh,"proj");
+  GLint normi  = glGetUniformLocation(data.meshgl.shader_mesh,"normal_matrix");
   glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
   glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
+  glUniformMatrix4fv(normi, 1, GL_FALSE, norm.data());
 
   // Light parameters
   GLint specular_exponenti    = glGetUniformLocation(data.meshgl.shader_mesh,"specular_exponent");
@@ -163,8 +168,7 @@ IGL_INLINE void igl::opengl::ViewerCore::draw(
   GLint texture_factori       = glGetUniformLocation(data.meshgl.shader_mesh,"texture_factor");
 
   glUniform1f(specular_exponenti, data.shininess);
-  Vector3f rev_light = -1.*light_position;
-  glUniform3fv(light_position_eyei, 1, rev_light.data());
+  glUniform3fv(light_position_eyei, 1, light_position.data());
   glUniform1f(lighting_factori, lighting_factor); // enables lighting
   glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
 
@@ -338,7 +342,7 @@ IGL_INLINE igl::opengl::ViewerCore::ViewerCore()
   background_color << 0.3f, 0.3f, 0.5f, 1.0f;
 
   // Default lights settings
-  light_position << 0.0f, -0.30f, -5.0f;
+  light_position << 0.0f, 0.3f, 0.0f;
   lighting_factor = 1.0f; //on
 
   // Default trackball
@@ -352,8 +356,8 @@ IGL_INLINE igl::opengl::ViewerCore::ViewerCore()
   camera_view_angle = 45.0;
   camera_dnear = 1.0;
   camera_dfar = 100.0;
-  camera_base_translation << 0,0,0;
-  camera_translation << 0,0,0;
+  camera_base_translation << 0, 0, 0;
+  camera_translation << 0, 0, 0;
   camera_eye << 0, 0, 5;
   camera_center << 0, 0, 0;
   camera_up << 0, 1, 0;

+ 2 - 0
include/igl/opengl/ViewerCore.h

@@ -131,6 +131,7 @@ public:
   // Save the OpenGL transformation matrices used for the previous rendering pass
   Eigen::Matrix4f view;
   Eigen::Matrix4f proj;
+  Eigen::Matrix4f norm;
   public:
       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
 };
@@ -174,6 +175,7 @@ namespace igl {
       SERIALIZE_MEMBER(viewport);
       SERIALIZE_MEMBER(view);
       SERIALIZE_MEMBER(proj);
+      SERIALIZE_MEMBER(norm);
     }
 
     template<>