Browse Source

lingering bug: per-vertex attributes handled incorrectly

Former-commit-id: b21106046bc6b9a59013ea57db354e0451dfa855
Alec Jacobson 7 years ago
parent
commit
3f85509cc6
3 changed files with 120 additions and 112 deletions
  1. 105 100
      include/igl/opengl/MeshGL.cpp
  2. 12 11
      include/igl/opengl/ViewerData.cpp
  3. 3 1
      include/igl/opengl/glfw/Viewer.cpp

+ 105 - 100
include/igl/opengl/MeshGL.cpp

@@ -165,112 +165,117 @@ IGL_INLINE void igl::opengl::MeshGL::init()
   }
   is_initialized = true;
   std::string mesh_vertex_shader_string =
-  "#version 150\n"
-  "uniform mat4 model;"
-  "uniform mat4 view;"
-  "uniform mat4 proj;"
-  "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 * model * vec4 (position, 1.0));"
-  "  normal_eye = vec3 (view * model * vec4 (normal, 0.0));"
-  "  normal_eye = normalize(normal_eye);"
-  "  gl_Position = proj * vec4 (position_eye, 1.0);" //proj * view * model * vec4(position, 1.0);"
-  "  Kai = Ka;"
-  "  Kdi = Kd;"
-  "  Ksi = Ks;"
-  "  texcoordi = texcoord;"
-  "}";
-
-  std::string mesh_fragment_shader_string =
-  "#version 150\n"
-  "uniform mat4 model;"
-  "uniform mat4 view;"
-  "uniform mat4 proj;"
-  "uniform vec4 fixed_color;"
-  "in vec3 position_eye;"
-  "in vec3 normal_eye;"
-  "uniform vec3 light_position_world;"
-  "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 light_position_eye = vec3 (view * vec4 (light_position_world, 1.0));"
-  "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 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 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 Is = Ls * vec3(Ksi) * specular_factor;"    // specular intensity
-  "vec4 color = vec4(lighting_factor * (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;"
-  "}";
+R"(#version 150
+  uniform mat4 model;
+  uniform mat4 view;
+  uniform mat4 proj;
+  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 * model * vec4 (position, 1.0));
+    normal_eye = vec3 (view * model * vec4 (normal, 0.0));
+    normal_eye = normalize(normal_eye);
+    gl_Position = proj * vec4 (position_eye, 1.0); //proj * view * model * vec4(position, 1.0);
+    Kai = Ka;
+    Kdi = Kd;
+    Ksi = Ks;
+    texcoordi = texcoord;
+  }
+)";
+
+  std::string mesh_fragment_shader_string = 
+R"(#version 150
+  uniform mat4 model;
+  uniform mat4 view;
+  uniform mat4 proj;
+  uniform vec4 fixed_color;
+  in vec3 position_eye;
+  in vec3 normal_eye;
+  uniform vec3 light_position_world;
+  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 light_position_eye = vec3 (view * vec4 (light_position_world, 1.0));
+    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 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 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 Is = Ls * vec3(Ksi) * specular_factor;    // specular intensity
+    vec4 color = vec4(lighting_factor * (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;
+  }
+  )";
 
   std::string overlay_vertex_shader_string =
-  "#version 150\n"
-  "uniform mat4 model;"
-  "uniform mat4 view;"
-  "uniform mat4 proj;"
-  "in vec3 position;"
-  "in vec3 color;"
-  "out vec3 color_frag;"
-
-  "void main()"
-  "{"
-  "  gl_Position = proj * view * model * vec4 (position, 1.0);"
-  "  color_frag = color;"
-  "}";
+R"(#version 150
+  uniform mat4 model;
+  uniform mat4 view;
+  uniform mat4 proj;
+  in vec3 position;
+  in vec3 color;
+  out vec3 color_frag;
+
+  void main()
+  {
+    gl_Position = proj * view * model * vec4 (position, 1.0);
+    color_frag = color;
+  }
+)";
 
   std::string overlay_fragment_shader_string =
-  "#version 150\n"
-  "in vec3 color_frag;"
-  "out vec4 outColor;"
-  "void main()"
-  "{"
-  "  outColor = vec4(color_frag, 1.0);"
-  "}";
+R"(#version 150
+  in vec3 color_frag;
+  out vec4 outColor;
+  void main()
+  {
+    outColor = vec4(color_frag, 1.0);
+  }
+)";
 
   std::string overlay_point_fragment_shader_string =
-  "#version 150\n"
-  "in vec3 color_frag;"
-  "out vec4 outColor;"
-  "void main()"
-  "{"
-  "  if (length(gl_PointCoord - vec2(0.5)) > 0.5)"
-  "    discard;"
-  "  outColor = vec4(color_frag, 1.0);"
-  "}";
+R"(#version 150
+  in vec3 color_frag;
+  out vec4 outColor;
+  void main()
+  {
+    if (length(gl_PointCoord - vec2(0.5)) > 0.5)
+      discard;
+    outColor = vec4(color_frag, 1.0);
+  }
+)";
 
   init_buffers();
   create_shader_program(

+ 12 - 11
include/igl/opengl/ViewerData.cpp

@@ -528,7 +528,9 @@ IGL_INLINE void igl::opengl::ViewerData::updateGL(
 
       // Texture coordinates
       if (meshgl.dirty & MeshGL::DIRTY_UV)
+      {
         meshgl.V_uv_vbo = data.V_uv.cast<float>();
+      }
     }
     else
     {
@@ -541,36 +543,35 @@ IGL_INLINE void igl::opengl::ViewerData::updateGL(
 
       if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
       {
-        meshgl.V_ambient_vbo.resize(4,data.F.rows()*3);
+        meshgl.V_ambient_vbo.resize(data.F.rows()*3,4);
         for (unsigned i=0; i<data.F.rows();++i)
           for (unsigned j=0;j<3;++j)
-            meshgl.V_ambient_vbo.col (i*3+j) = data.V_material_ambient.row(data.F(i,j)).transpose().cast<float>();
+            meshgl.V_ambient_vbo.row(i*3+j) = data.V_material_ambient.row(data.F(i,j)).cast<float>();
       }
       if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
       {
-        meshgl.V_diffuse_vbo.resize(4,data.F.rows()*3);
+        meshgl.V_diffuse_vbo.resize(data.F.rows()*3,4);
         for (unsigned i=0; i<data.F.rows();++i)
           for (unsigned j=0;j<3;++j)
-            meshgl.V_diffuse_vbo.col (i*3+j) = data.V_material_diffuse.row(data.F(i,j)).transpose().cast<float>();
+            meshgl.V_diffuse_vbo.row(i*3+j) = data.V_material_diffuse.row(data.F(i,j)).cast<float>();
       }
       if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
       {
-        meshgl.V_specular_vbo.resize(4,data.F.rows()*3);
+        meshgl.V_specular_vbo.resize(data.F.rows()*3,4);
         for (unsigned i=0; i<data.F.rows();++i)
           for (unsigned j=0;j<3;++j)
-            meshgl.V_specular_vbo.col(i*3+j) = data.V_material_specular.row(data.F(i,j)).transpose().cast<float>();
+            meshgl.V_specular_vbo.row(i*3+j) = data.V_material_specular.row(data.F(i,j)).cast<float>();
       }
 
       if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
       {
-        meshgl.V_normals_vbo.resize(3,data.F.rows()*3);
+        meshgl.V_normals_vbo.resize(data.F.rows()*3,3);
         for (unsigned i=0; i<data.F.rows();++i)
           for (unsigned j=0;j<3;++j)
-
-            meshgl.V_normals_vbo.col (i*3+j) =
+            meshgl.V_normals_vbo.row(i*3+j) =
                          per_corner_normals ?
-               data.F_normals.row(i*3+j).transpose().cast<float>() :
-               data.V_normals.row(data.F(i,j)).transpose().cast<float>();
+               data.F_normals.row(i*3+j).cast<float>() :
+               data.V_normals.row(data.F(i,j)).cast<float>();
 
 
         if (invert_normals)

+ 3 - 1
include/igl/opengl/glfw/Viewer.cpp

@@ -172,8 +172,8 @@ namespace glfw
       printf("Failed to load OpenGL and its extensions\n");
       return(-1);
     }
-    printf("OpenGL Version %d.%d loaded\n", GLVersion.major, GLVersion.minor);
     #if defined(DEBUG) || defined(_DEBUG)
+      printf("OpenGL Version %d.%d loaded\n", GLVersion.major, GLVersion.minor);
       int major, minor, rev;
       major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
       minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
@@ -413,6 +413,8 @@ namespace glfw
     data().uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
                    Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
                    Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
+
+    // Alec: why?
     if (data().V_uv.rows() == 0)
     {
       data().grid_texture();