PDA

View Full Version : Redbook Ex. 2-16/17



Banangroda
02-14-2009, 08:29 PM
I'm a bit confused about this for loop:

for (i = 0; i < 3; i++) {
v12[i] = v1[i]+v2[i];
v23[i] = v2[i]+v3[i];
v31[i] = v3[i]+v1[i];
}

If v1[i] is 10 and v2[i] is 15, doesn't adding v1[i] to v2[i] make the resulting coordinate equal 35? But the end result seems to be 12.5, somehow - why?

Example 2-16 : Single Subdivision


void drawtriangle(float *v1, float *v2, float *v3)
{
glBegin(GL_TRIANGLES);
glNormal3fv(v1); vlVertex3fv(v1);
glNormal3fv(v2); vlVertex3fv(v2);
glNormal3fv(v3); vlVertex3fv(v3);
glEnd();
}

void subdivide(float *v1, float *v2, float *v3)
{
GLfloat v12[3], v23[3], v31[3];
GLint i;

for (i = 0; i < 3; i++) {
v12[i] = v1[i]+v2[i];
v23[i] = v2[i]+v3[i];
v31[i] = v3[i]+v1[i];
}
normalize(v12);
normalize(v23);
normalize(v31);
drawtriangle(v1, v12, v31);
drawtriangle(v2, v23, v12);
drawtriangle(v3, v31, v23);
drawtriangle(v12, v23, v31);
}

for (i = 0; i < 20; i++) {
subdivide(&amp;vdata[tindices[i][0]][0],
&amp;vdata[tindices[i][1]][0],
&amp;vdata[tindices[i][2]][0]);
}


Also, might as well ask this now. How could this recursive subdivision below possibly work when it only creates a triangle when depth is zero?

Example 2-17 : Recursive Subdivision


void subdivide(float *v1, float *v2, float *v3, long depth)
{
GLfloat v12[3], v23[3], v31[3];
GLint i;

if (depth == 0) {
drawtriangle(v1, v2, v3);
return;
}
for (i = 0; i < 3; i++) {
v12[i] = v1[i]+v2[i];
v23[i] = v2[i]+v3[i];
v31[i] = v3[i]+v1[i];
}
normalize(v12);
normalize(v23);
normalize(v31);
subdivide(v1, v12, v31, depth-1);
subdivide(v2, v23, v12, depth-1);
subdivide(v3, v31, v23, depth-1);
subdivide(v12, v23, v31, depth-1);
}

dletozeun
02-15-2009, 02:18 AM
void drawtriangle(float *v1, float *v2, float *v3)
{
glBegin(GL_TRIANGLES);
glNormal3fv(v1); vlVertex3fv(v1);
glNormal3fv(v2); vlVertex3fv(v2);
glNormal3fv(v3); vlVertex3fv(v3);
glEnd();
}


What is vlVertex3f?? It is not opengl.

Rosario Leonardi
02-15-2009, 08:06 AM
vlVertex is a typo. :P

a - The normalized sum of the vertex to compute the median works only in case you want to draw a sphere centered in the origin.

b - It's a recursive code, it compute a finer triangle on every step but it draws only the last level.

Banangroda
02-15-2009, 10:12 AM
ahh I see now. Thx.