Commit 42d385468c42f34a768c3522656af8b902fbd601

Authored by Jiaming Guo
1 parent c8864f86

fixed normal vector errors

Showing 1 changed file with 50 additions and 30 deletions   Show diff stats
stim/visualization/gl_network.h
@@ -45,16 +45,32 @@ public: @@ -45,16 +45,32 @@ public:
45 45
46 ///render cylinder based on points from the top/bottom hat 46 ///render cylinder based on points from the top/bottom hat
47 ///@param C1 set of points from one of the hat 47 ///@param C1 set of points from one of the hat
48 - void renderCylinder(std::vector< stim::vec3<T> > C1, std::vector< stim::vec3<T> > C2, stim::vec3<T> n1, stim::vec3<T> n2) { 48 + void renderCylinder(std::vector< stim::vec3<T> > C1, std::vector< stim::vec3<T> > C2) {
49 glBegin(GL_QUAD_STRIP); 49 glBegin(GL_QUAD_STRIP);
50 - for (unsigned i = 0; i < C1.size(); i++) { // for every point on the circle  
51 - glNormal3f(n1[0], n1[1], n1[2]); 50 + for (unsigned i = 0; i < C1.size() - 1; i++) { // for every point on the circle
  51 + stim::vec3<T> v1;
  52 + stim::vec3<T> v2;
  53 + stim::vec3<T> v3;
  54 + stim::vec3<T> v4;
  55 +
  56 + v1 = C2[i + 1] - C1[i + 1];
  57 + v2 = C1[i] - C1[i + 1];
  58 + v3 = C1[i] - C2[i];
  59 + v4 = C2[i + 1] - C2[i];
  60 +
  61 + stim::vec3<T> n1 = v1.cross(v2);
  62 + stim::vec3<T> n2 = v3.cross(v4);
  63 + stim::vec3<T> normal = n1 + n2;
  64 + normal = normal.norm();
52 glVertex3f(C1[i][0], C1[i][1], C1[i][2]); 65 glVertex3f(C1[i][0], C1[i][1], C1[i][2]);
53 - glNormal3f(n2[0], n2[1], n2[2]);  
54 glVertex3f(C2[i][0], C2[i][1], C2[i][2]); 66 glVertex3f(C2[i][0], C2[i][1], C2[i][2]);
  67 + glNormal3f(normal[0], normal[1], normal[2]); // set normal vector for lighting
  68 + //glNormal3f(n1[0], n1[1], n1[2]);
  69 + //glVertex3f(C1[i][0], C1[i][1], C1[i][2]);
  70 + //glNormal3f(n2[0], n2[1], n2[2]);
  71 + //glVertex3f(C2[i][0], C2[i][1], C2[i][2]);
55 } 72 }
56 glEnd(); 73 glEnd();
57 - //glFlush();  
58 } 74 }
59 75
60 ///render the vertex as sphere based on glut build-in function 76 ///render the vertex as sphere based on glut build-in function
@@ -104,6 +120,7 @@ public: @@ -104,6 +120,7 @@ public:
104 yy[3] = radius * std::sin(angle_z) * std::sin(angle_xy + step_xy); 120 yy[3] = radius * std::sin(angle_z) * std::sin(angle_xy + step_xy);
105 zz[3] = radius * std::cos(angle_z); 121 zz[3] = radius * std::cos(angle_z);
106 122
  123 +
107 for (unsigned k = 0; k < 4; k++) { 124 for (unsigned k = 0; k < 4; k++) {
108 glVertex3f(x + xx[k], y + yy[k], z + zz[k]); // draw the floor plane 125 glVertex3f(x + xx[k], y + yy[k], z + zz[k]); // draw the floor plane
109 } 126 }
@@ -167,19 +184,38 @@ public: @@ -167,19 +184,38 @@ public:
167 stim::circle<T> C2 = E[e].circ(p); 184 stim::circle<T> C2 = E[e].circ(p);
168 C1.set_R(2*radius); // re-scale the circle to the same 185 C1.set_R(2*radius); // re-scale the circle to the same
169 C2.set_R(2*radius); 186 C2.set_R(2*radius);
170 - std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);// get 20 points on the circle plane  
171 - std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);  
172 - stim::vec3<T> n1 = C1.n(); // get the normal vector  
173 - stim::vec3<T> n2 = C2.n(); 187 + std::vector< stim::vec3<T> > Cp1 = C1.glpoints(20);// get 20 points on the circle plane
  188 + std::vector< stim::vec3<T> > Cp2 = C2.glpoints(20);
174 glBegin(GL_QUAD_STRIP); 189 glBegin(GL_QUAD_STRIP);
175 - for (unsigned i = 0; i < Cp1.size(); i++) { // for every point on the circle(+1 means closing the circle) 190 + for (unsigned i = 0; i < Cp1.size() - 1; i++) {
  191 + stim::vec3<T> v1;
  192 + stim::vec3<T> v2;
  193 + stim::vec3<T> v3;
  194 + stim::vec3<T> v4;
  195 + v1 = Cp2[i + 1] - Cp1[i + 1];
  196 + v2 = Cp1[i] - Cp1[i + 1];
  197 + v3 = Cp1[i] - Cp2[i];
  198 + v4 = Cp2[i + 1] - Cp2[i];
  199 +
  200 + stim::vec3<T> n1 = v1.cross(v2);
  201 + stim::vec3<T> n2 = v3.cross(v4);
  202 + stim::vec3<T> normal = n1;
  203 + normal = normal.norm();
  204 +
176 glTexCoord1f(E[e].r(p - 1)); 205 glTexCoord1f(E[e].r(p - 1));
177 - glNormal3f(n1[0], n1[1], n1[2]); // set normal vector for lighting  
178 glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]); 206 glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]);
179 glTexCoord1f(E[e].r(p)); 207 glTexCoord1f(E[e].r(p));
180 - glNormal3f(n2[0], n2[1], n2[2]);  
181 glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]); 208 glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]);
  209 + glNormal3f(normal[0], normal[1], normal[2]); // set normal vector for lighting
182 } 210 }
  211 + //for (unsigned i = 0; i < Cp1.size(); i++) { // for every point on the circle(+1 means closing the circle)
  212 + // glTexCoord1f(E[e].r(p - 1));
  213 + // glNormal3f(n1[0], n1[1], n1[2]); // set normal vector for lighting
  214 + // glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]);
  215 + // glTexCoord1f(E[e].r(p));
  216 + // glNormal3f(n2[0], n2[1], n2[2]);
  217 + // glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]);
  218 + //}
183 glEnd(); 219 glEnd();
184 } // set the texture coordinate based on the specified magnitude index 220 } // set the texture coordinate based on the specified magnitude index
185 } 221 }
@@ -187,16 +223,10 @@ public: @@ -187,16 +223,10 @@ public:
187 size_t num = V[n].e[0].size(); // store the number of outgoing edge of that vertex 223 size_t num = V[n].e[0].size(); // store the number of outgoing edge of that vertex
188 if (num != 0) { // if it has outgoing edge 224 if (num != 0) { // if it has outgoing edge
189 unsigned idx = V[n].e[0][0]; // find the index of first outgoing edge of that vertex 225 unsigned idx = V[n].e[0][0]; // find the index of first outgoing edge of that vertex
190 - stim::circle<T> C = E[idx].circ(0);  
191 - stim::vec3<T> normal = C.n();  
192 - glNormal3f(normal[0], normal[1], normal[2]);// for every ball, we only have one normal vector  
193 glTexCoord1f(E[idx].r(0)); // bind the texture as metric of first point on that edge 226 glTexCoord1f(E[idx].r(0)); // bind the texture as metric of first point on that edge
194 } 227 }
195 else { 228 else {
196 unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex 229 unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex
197 - stim::circle<T> C = E[idx].circ(E[idx].size()-1);  
198 - stim::vec3<T> normal = C.n();  
199 - glNormal3f(normal[0], normal[1], normal[2]);// for every ball, we only have one normal vector  
200 glTexCoord1f(E[idx].r(E[idx].size() - 1)); // bind the texture as metric of last point on that edge 230 glTexCoord1f(E[idx].r(E[idx].size() - 1)); // bind the texture as metric of last point on that edge
201 } 231 }
202 renderBall(V[n][0], V[n][1], V[n][2], 2*radius, 20, 20); 232 renderBall(V[n][0], V[n][1], V[n][2], 2*radius, 20, 20);
@@ -270,9 +300,7 @@ public: @@ -270,9 +300,7 @@ public:
270 C2.set_R(2*radius); 300 C2.set_R(2*radius);
271 std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20); 301 std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
272 std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20); 302 std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
273 - stim::vec3<T> n1 = C1.n(); // get the normal vector  
274 - stim::vec3<T> n2 = C2.n();  
275 - renderCylinder(Cp1, Cp2, n1, n2); 303 + renderCylinder(Cp1, Cp2);
276 } 304 }
277 } 305 }
278 else { 306 else {
@@ -284,9 +312,7 @@ public: @@ -284,9 +312,7 @@ public:
284 C2.set_R(2*radius); 312 C2.set_R(2*radius);
285 std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20); 313 std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
286 std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20); 314 std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
287 - stim::vec3<T> n1 = C1.n(); // get the normal vector  
288 - stim::vec3<T> n2 = C2.n();  
289 - renderCylinder(Cp1, Cp2, n1, n2); 315 + renderCylinder(Cp1, Cp2);
290 } 316 }
291 } 317 }
292 } 318 }
@@ -311,15 +337,9 @@ public: @@ -311,15 +337,9 @@ public:
311 else { // if it is the terminal vertex 337 else { // if it is the terminal vertex
312 if (V[n].e[0].size() != 0) { 338 if (V[n].e[0].size() != 0) {
313 unsigned idx = V[n].e[0][0]; // find the index of first incoming edge of that vertex 339 unsigned idx = V[n].e[0][0]; // find the index of first incoming edge of that vertex
314 - stim::circle<T> C = E[idx].circ(0);  
315 - stim::vec3<T> normal = C.n();  
316 - glNormal3f(normal[0], normal[1], normal[2]);// for every ball, we only have one normal vector  
317 } 340 }
318 else { 341 else {
319 unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex 342 unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex
320 - stim::circle<T> C = E[idx].circ(E[idx].size() - 1);  
321 - stim::vec3<T> normal = C.n();  
322 - glNormal3f(normal[0], normal[1], normal[2]);// for every ball, we only have one normal vector  
323 } 343 }
324 glColor3f(0.6, 0.6, 0.6); // more white gray 344 glColor3f(0.6, 0.6, 0.6); // more white gray
325 renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20, 20); 345 renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20, 20);