Browse Source

3DFileViewer: Enable smooth shading in viewer + teapot model

Sahan Fernando 3 years ago
parent
commit
05f9146af8
2 changed files with 3662 additions and 10 deletions
  1. 3652 1
      Base/home/anon/Documents/3D Models/teapot.obj
  2. 10 9
      Userland/Applications/3DFileViewer/Mesh.cpp

File diff suppressed because it is too large
+ 3652 - 1
Base/home/anon/Documents/3D Models/teapot.obj


+ 10 - 9
Userland/Applications/3DFileViewer/Mesh.cpp

@@ -51,29 +51,30 @@ void Mesh::draw(float uv_scale)
             m_vertex_list.at(triangle.c).y,
             m_vertex_list.at(triangle.c).z);
 
-        FloatVector3 normal;
+        FloatVector3 normal_a, normal_b, normal_c;
         if (has_normals()) {
-            const FloatVector3 normal_a(
+            normal_a = FloatVector3(
                 m_normal_list.at(triangle.normal_index0).x,
                 m_normal_list.at(triangle.normal_index0).y,
                 m_normal_list.at(triangle.normal_index0).z);
 
-            const FloatVector3 normal_b(
+            normal_b = FloatVector3(
                 m_normal_list.at(triangle.normal_index1).x,
                 m_normal_list.at(triangle.normal_index1).y,
                 m_normal_list.at(triangle.normal_index1).z);
 
-            const FloatVector3 normal_c(
+            normal_c = FloatVector3(
                 m_normal_list.at(triangle.normal_index2).x,
                 m_normal_list.at(triangle.normal_index2).y,
                 m_normal_list.at(triangle.normal_index2).z);
 
-            normal = (normal_a + normal_b + normal_c).normalized();
         } else {
             // Compute the triangle normal
             const FloatVector3 vec_ab = vertex_b - vertex_a;
             const FloatVector3 vec_ac = vertex_c - vertex_a;
-            normal = vec_ab.cross(vec_ac).normalized();
+            normal_a = vec_ab.cross(vec_ac).normalized();
+            normal_b = normal_a;
+            normal_c = normal_a;
         }
 
         glBegin(GL_TRIANGLES);
@@ -81,10 +82,8 @@ void Mesh::draw(float uv_scale)
         if (is_textured())
             glTexCoord2f(m_tex_coords.at(triangle.tex_coord_index0).u * uv_scale, (1.0f - m_tex_coords.at(triangle.tex_coord_index0).v) * uv_scale);
 
-        // Upload the face normal
-        glNormal3f(normal.x(), normal.y(), normal.z());
-
         // Vertex 1
+        glNormal3f(normal_a.x(), normal_a.y(), normal_a.z());
         glVertex3f(
             m_vertex_list.at(triangle.a).x,
             m_vertex_list.at(triangle.a).y,
@@ -94,6 +93,7 @@ void Mesh::draw(float uv_scale)
             glTexCoord2f(m_tex_coords.at(triangle.tex_coord_index1).u * uv_scale, (1.0f - m_tex_coords.at(triangle.tex_coord_index1).v) * uv_scale);
 
         // Vertex 2
+        glNormal3f(normal_b.x(), normal_b.y(), normal_b.z());
         glVertex3f(
             m_vertex_list.at(triangle.b).x,
             m_vertex_list.at(triangle.b).y,
@@ -103,6 +103,7 @@ void Mesh::draw(float uv_scale)
             glTexCoord2f(m_tex_coords.at(triangle.tex_coord_index2).u * uv_scale, (1.0f - m_tex_coords.at(triangle.tex_coord_index2).v) * uv_scale);
 
         // Vertex 3
+        glNormal3f(normal_c.x(), normal_c.y(), normal_c.z());
         glVertex3f(
             m_vertex_list.at(triangle.c).x,
             m_vertex_list.at(triangle.c).y,

Some files were not shown because too many files changed in this diff