PDA

View Full Version : Surface of Revolution



baconbeastnz
09-06-2008, 04:51 PM
Hi there, I have a surface of revolution which draws half an egg. It's working but missing the last like 10 degrees of the egg and I cannot figure out why, have spent so much time trying to figure out. Figured I'd post on the advacned section and see if there are any legends out there.



float vertices[10][16][3]
Latitudes=10;
Longitudes=16;

for(latCount=0; latCount<Latitudes-1; latCount++){ // along z axis
for(longCount=0; longCount<=Longitudes; longCount++){ // x and y
z = sin(Pi*latCount/((float) (Latitudes-1))-Pi/2.0f)/5;
r = cos(Pi*latCount/((float) (Latitudes-1))-Pi/2.0f);
x = (cos(Pi*longCount/(float) Longitudes)*r) /5;
y = (sin(Pi*longCount/(float) Longitudes)*r *2) /5;

vertices[latCount][longCount][0] = x; //x
vertices[latCount][longCount][1] = y; //y
vertices[latCount][longCount][2] = z; //z

z = sin(Pi*(latCount+1)/((float) (Latitudes-1))-Pi/2.0f)/5;
r = cos(Pi*(latCount+1)/((float) (Latitudes-1))-Pi/2.0f);
x = (cos(Pi*longCount/(float) Longitudes)*r) /5;
y = (sin(Pi*longCount/(float) Longitudes)*r *2) /5;

vertices[latCount+1][longCount][0] = x; //x
vertices[latCount+1][longCount][1] = y; //y
vertices[latCount+1][longCount][2] = z; //z
}
}

for(int latCount=0; latCount<Latitudes-1; latCount++){ // along z axis
glBegin(GL_QUAD_STRIP);
for(int longCount=0; longCount<=Longitudes; longCount++){
glVertex3f(vertices[latCount][longCount][0], vertices[latCount][longCount][1], vertices[latCount][longCount][2]);
glVertex3f(vertices[latCount+1][longCount][0], vertices[latCount+1][longCount][1], vertices[latCount+1][longCount][2]);
}

glEnd();
}

trinitrotoluene
09-06-2008, 09:26 PM
Try this.



for(int latCount=0; latCount<Latitudes; latCount++)
{ // along z axis
for(int longCount=0; longCount<Longitudes; longCount++){ // x and y
z = sin(M_PI*latCount/((float) (Latitudes-1))-M_PI/2.0f)/5;
r = cos(M_PI*latCount/((float) (Latitudes-1))-M_PI/2.0f);
x = (cos(M_PI*longCount/(float) (Longitudes-1))*r) /5;
y = (sin(M_PI*longCount/(float) (Longitudes-1))*r *2) /5;

vertices[latCount][longCount][0] = x; //x
vertices[latCount][longCount][1] = y; //y
vertices[latCount][longCount][2] = z; //z
}}


For me this code is working fine. The number of iteration in your loops is incorrect and the calculation of x and y is wrong. Also
you calculate some value twice.

baconbeastnz
09-06-2008, 10:57 PM
Wicked!! Thanks that is a much neater solution. Cheers!

sdwwq
09-12-2008, 03:31 AM
Hi

i want the same thing, i followed your code but i cant rotate it after i draw it.

this is how i am drawing it.


int lt, lg, numLatitudes=10, numLongitudes=16;
double x,y,z,r;
for(lt=0; lt<numLatitudes-1; lt++){
glBegin(GL_QUAD_STRIP);
for(lg=0; lg<numLongitudes; lg++){

glVertex3fv(vertices[lt][lg]);
glVertex3fv(vertices[lt+1][lg]);



}
glEnd();
}