From 0043e966d97d0f0732f5229807d5436cfdffc1c5 Mon Sep 17 00:00:00 2001 From: Jiaming Guo Date: Thu, 26 Jan 2017 12:06:10 -0600 Subject: [PATCH] fixed normal vector errors --- stim/visualization/gl_network.h | 76 +++++++++++++++++++++++----------------------------------------------------- 1 file changed, 23 insertions(+), 53 deletions(-) diff --git a/stim/visualization/gl_network.h b/stim/visualization/gl_network.h index 562a16e..cb987f4 100644 --- a/stim/visualization/gl_network.h +++ b/stim/visualization/gl_network.h @@ -45,30 +45,17 @@ public: ///render cylinder based on points from the top/bottom hat ///@param C1 set of points from one of the hat - void renderCylinder(std::vector< stim::vec3 > C1, std::vector< stim::vec3 > C2) { + void renderCylinder(std::vector< stim::vec3 > C1, std::vector< stim::vec3 > C2, stim::vec3 P1, stim::vec3 P2) { glBegin(GL_QUAD_STRIP); - for (unsigned i = 0; i < C1.size() - 1; i++) { // for every point on the circle - stim::vec3 v1; - stim::vec3 v2; - stim::vec3 v3; - stim::vec3 v4; - - v1 = C2[i + 1] - C1[i + 1]; - v2 = C1[i] - C1[i + 1]; - v3 = C1[i] - C2[i]; - v4 = C2[i + 1] - C2[i]; - - stim::vec3 n1 = v1.cross(v2); - stim::vec3 n2 = v3.cross(v4); - stim::vec3 normal = n1 + n2; - normal = normal.norm(); + for (unsigned i = 0; i < C1.size(); i++) { // for every point on the circle + stim::vec3 n1 = C1[i] - P1; // set normal vector for every vertex on the quads + stim::vec3 n2 = C2[i] - P2; + n1 = n1.norm(); + n2 = n2.norm(); + glNormal3f(n1[0], n1[1], n1[2]); glVertex3f(C1[i][0], C1[i][1], C1[i][2]); - glVertex3f(C2[i][0], C2[i][1], C2[i][2]); - glNormal3f(normal[0], normal[1], normal[2]); // set normal vector for lighting - //glNormal3f(n1[0], n1[1], n1[2]); - //glVertex3f(C1[i][0], C1[i][1], C1[i][2]); - //glNormal3f(n2[0], n2[1], n2[2]); - //glVertex3f(C2[i][0], C2[i][1], C2[i][2]); + glNormal3f(n2[0], n2[1], n2[2]); + glVertex3f(C2[i][0], C2[i][1], C2[i][2]); } glEnd(); } @@ -120,7 +107,6 @@ public: yy[3] = radius * std::sin(angle_z) * std::sin(angle_xy + step_xy); zz[3] = radius * std::cos(angle_z); - for (unsigned k = 0; k < 4; k++) { glVertex3f(x + xx[k], y + yy[k], z + zz[k]); // draw the floor plane } @@ -187,37 +173,21 @@ public: std::vector< stim::vec3 > Cp1 = C1.glpoints(20);// get 20 points on the circle plane std::vector< stim::vec3 > Cp2 = C2.glpoints(20); glBegin(GL_QUAD_STRIP); - for (unsigned i = 0; i < Cp1.size() - 1; i++) { - stim::vec3 v1; - stim::vec3 v2; - stim::vec3 v3; - stim::vec3 v4; - v1 = Cp2[i + 1] - Cp1[i + 1]; - v2 = Cp1[i] - Cp1[i + 1]; - v3 = Cp1[i] - Cp2[i]; - v4 = Cp2[i + 1] - Cp2[i]; - - stim::vec3 n1 = v1.cross(v2); - stim::vec3 n2 = v3.cross(v4); - stim::vec3 normal = n1; - normal = normal.norm(); + for (unsigned i = 0; i < Cp1.size(); i++) { + stim::vec3 n1 = Cp1[i] - E[e][p - 1]; // set normal vector for every vertex on the quads + stim::vec3 n2 = Cp2[i] - E[e][p]; + n1 = n1.norm(); + n2 = n2.norm(); + glNormal3f(n1[0], n1[1], n1[2]); glTexCoord1f(E[e].r(p - 1)); glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]); + glNormal3f(n2[0], n2[1], n2[2]); glTexCoord1f(E[e].r(p)); glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]); - glNormal3f(normal[0], normal[1], normal[2]); // set normal vector for lighting } - //for (unsigned i = 0; i < Cp1.size(); i++) { // for every point on the circle(+1 means closing the circle) - // glTexCoord1f(E[e].r(p - 1)); - // glNormal3f(n1[0], n1[1], n1[2]); // set normal vector for lighting - // glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]); - // glTexCoord1f(E[e].r(p)); - // glNormal3f(n2[0], n2[1], n2[2]); - // glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]); - //} glEnd(); - } // set the texture coordinate based on the specified magnitude index + } // set the texture coordinate based on the specified magnitude index } for (unsigned n = 0; n < V.size(); n++) { size_t num = V[n].e[0].size(); // store the number of outgoing edge of that vertex @@ -229,7 +199,7 @@ public: unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex glTexCoord1f(E[idx].r(E[idx].size() - 1)); // bind the texture as metric of last point on that edge } - renderBall(V[n][0], V[n][1], V[n][2], 2*radius, 20, 20); + renderBall(V[n][0], V[n][1], V[n][2], 2*radius, 20); } glEndList(); // end the display list } @@ -267,7 +237,7 @@ public: else { unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex } - renderBall(V[n][0], V[n][1], V[n][2], 2 * radius, 20, 20); + renderBall(V[n][0], V[n][1], V[n][2], 2 * radius, 20); } glEndList(); } @@ -300,7 +270,7 @@ public: C2.set_R(2*radius); std::vector< stim::vec3 >Cp1 = C1.glpoints(20); std::vector< stim::vec3 >Cp2 = C2.glpoints(20); - renderCylinder(Cp1, Cp2); + renderCylinder(Cp1, Cp2, E[e][p - 1], E[e][p]); } } else { @@ -312,7 +282,7 @@ public: C2.set_R(2*radius); std::vector< stim::vec3 >Cp1 = C1.glpoints(20); std::vector< stim::vec3 >Cp2 = C2.glpoints(20); - renderCylinder(Cp1, Cp2); + renderCylinder(Cp1, Cp2, E[e][p - 1], E[e][p]); } } } @@ -332,7 +302,7 @@ public: glNormal3f(normal[0], normal[1], normal[2]);// for every ball, we only have one normal vector } glColor3f(0.3, 0.3, 0.3); // gray - renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20, 20); + renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20); } else { // if it is the terminal vertex if (V[n].e[0].size() != 0) { @@ -342,7 +312,7 @@ public: unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex } glColor3f(0.6, 0.6, 0.6); // more white gray - renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20, 20); + renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20); } } glEndList(); -- libgit2 0.21.4