Commit 0043e966d97d0f0732f5229807d5436cfdffc1c5

Authored by Jiaming Guo
1 parent 9f3f0602

fixed normal vector errors

Showing 1 changed file with 23 additions and 53 deletions   Show diff stats
stim/visualization/gl_network.h
... ... @@ -45,30 +45,17 @@ public:
45 45  
46 46 ///render cylinder based on points from the top/bottom hat
47 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) {
  48 + void renderCylinder(std::vector< stim::vec3<T> > C1, std::vector< stim::vec3<T> > C2, stim::vec3<T> P1, stim::vec3<T> P2) {
49 49 glBegin(GL_QUAD_STRIP);
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();
  50 + for (unsigned i = 0; i < C1.size(); i++) { // for every point on the circle
  51 + stim::vec3<T> n1 = C1[i] - P1; // set normal vector for every vertex on the quads
  52 + stim::vec3<T> n2 = C2[i] - P2;
  53 + n1 = n1.norm();
  54 + n2 = n2.norm();
  55 + glNormal3f(n1[0], n1[1], n1[2]);
65 56 glVertex3f(C1[i][0], C1[i][1], C1[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]);
  57 + glNormal3f(n2[0], n2[1], n2[2]);
  58 + glVertex3f(C2[i][0], C2[i][1], C2[i][2]);
72 59 }
73 60 glEnd();
74 61 }
... ... @@ -120,7 +107,6 @@ public:
120 107 yy[3] = radius * std::sin(angle_z) * std::sin(angle_xy + step_xy);
121 108 zz[3] = radius * std::cos(angle_z);
122 109  
123   -
124 110 for (unsigned k = 0; k < 4; k++) {
125 111 glVertex3f(x + xx[k], y + yy[k], z + zz[k]); // draw the floor plane
126 112 }
... ... @@ -187,37 +173,21 @@ public:
187 173 std::vector< stim::vec3<T> > Cp1 = C1.glpoints(20);// get 20 points on the circle plane
188 174 std::vector< stim::vec3<T> > Cp2 = C2.glpoints(20);
189 175 glBegin(GL_QUAD_STRIP);
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();
  176 + for (unsigned i = 0; i < Cp1.size(); i++) {
  177 + stim::vec3<T> n1 = Cp1[i] - E[e][p - 1]; // set normal vector for every vertex on the quads
  178 + stim::vec3<T> n2 = Cp2[i] - E[e][p];
  179 + n1 = n1.norm();
  180 + n2 = n2.norm();
204 181  
  182 + glNormal3f(n1[0], n1[1], n1[2]);
205 183 glTexCoord1f(E[e].r(p - 1));
206 184 glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]);
  185 + glNormal3f(n2[0], n2[1], n2[2]);
207 186 glTexCoord1f(E[e].r(p));
208 187 glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]);
209   - glNormal3f(normal[0], normal[1], normal[2]); // set normal vector for lighting
210 188 }
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   - //}
219 189 glEnd();
220   - } // set the texture coordinate based on the specified magnitude index
  190 + } // set the texture coordinate based on the specified magnitude index
221 191 }
222 192 for (unsigned n = 0; n < V.size(); n++) {
223 193 size_t num = V[n].e[0].size(); // store the number of outgoing edge of that vertex
... ... @@ -229,7 +199,7 @@ public:
229 199 unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex
230 200 glTexCoord1f(E[idx].r(E[idx].size() - 1)); // bind the texture as metric of last point on that edge
231 201 }
232   - renderBall(V[n][0], V[n][1], V[n][2], 2*radius, 20, 20);
  202 + renderBall(V[n][0], V[n][1], V[n][2], 2*radius, 20);
233 203 }
234 204 glEndList(); // end the display list
235 205 }
... ... @@ -267,7 +237,7 @@ public:
267 237 else {
268 238 unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex
269 239 }
270   - renderBall(V[n][0], V[n][1], V[n][2], 2 * radius, 20, 20);
  240 + renderBall(V[n][0], V[n][1], V[n][2], 2 * radius, 20);
271 241 }
272 242 glEndList();
273 243 }
... ... @@ -300,7 +270,7 @@ public:
300 270 C2.set_R(2*radius);
301 271 std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
302 272 std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
303   - renderCylinder(Cp1, Cp2);
  273 + renderCylinder(Cp1, Cp2, E[e][p - 1], E[e][p]);
304 274 }
305 275 }
306 276 else {
... ... @@ -312,7 +282,7 @@ public:
312 282 C2.set_R(2*radius);
313 283 std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
314 284 std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
315   - renderCylinder(Cp1, Cp2);
  285 + renderCylinder(Cp1, Cp2, E[e][p - 1], E[e][p]);
316 286 }
317 287 }
318 288 }
... ... @@ -332,7 +302,7 @@ public:
332 302 glNormal3f(normal[0], normal[1], normal[2]);// for every ball, we only have one normal vector
333 303 }
334 304 glColor3f(0.3, 0.3, 0.3); // gray
335   - renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20, 20);
  305 + renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20);
336 306 }
337 307 else { // if it is the terminal vertex
338 308 if (V[n].e[0].size() != 0) {
... ... @@ -342,7 +312,7 @@ public:
342 312 unsigned idx = V[n].e[1][0]; // find the index of first incoming edge of that vertex
343 313 }
344 314 glColor3f(0.6, 0.6, 0.6); // more white gray
345   - renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20, 20);
  315 + renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20);
346 316 }
347 317 }
348 318 glEndList();
... ...