PDA

View Full Version : Lighting a surface of revolution



cloudraven
11-07-2003, 05:54 PM
I wasnt sure of posting this here, since it is a very basic question, but it is math related so I guess it would be better to post it here.
I have to finish an OpenGL project for college, and my deadline is tomorrow at midnight, I have tried everything and can't find the solution.
I have to display a surface of revolution and add some lights (the surface of revolution happens to be a windmill), my classmates chose other objects such as hands or simple robots that were easier to light, so they couldn't help me.
My problem is about calculating the normal vectors of the surface.
The surface is drawn as follows

for(v=0;v<=1;v+=K)
{
glBegin(GL_QUAD_STRIP);
for(u=0;u<=sqrh;u+=K)
{
glVertex3f(top(u)*R*sin(v*2*PI),R*u*3,top(u)*R*cos (v*2*PI));
glVertex3f(top(u)*R*sin((v+K)*2*PI),R*u*3,top(u)*R *cos((v+K)*2*PI));
}
glEnd();
}

GLfloat top(GLfloat u)
{

return (float)height - (u*u);
}


The teacher told me to calculate the normals by using the vector product of the partial derivatives of the ecuation.
I wrote this

GLfloat t1[3],t2[3];


t1[0] = topd(u)*R*sin(v*2*PI);
t1[1] = 3*R;
t1[2] = topd(u)*R*cos(v*2*PI);

t2[0] = 2*PI * R * top(u) * cos(v*2*PI);
t2[1] = 0;
t2[2] = -R*top(u)*2*PI*sin(v*2*PI);

VectorProduct(normal,t2,t1);
Normalize(normal);

Where topd() is

GLfloat inline topd(GLfloat u)
{
return (float)-(2*u);
}

When i render it, almost the 90% of the object is completely white, and just a very small zone is shaded.
The teacher says that there is something wrong with the normal vectors, but I am completely clueless now.
Any help woulde be really appreciated

11-08-2003, 07:32 AM
If you want, you can tell OpenGL to calculate the normals for you. In your initialize function, add

glEnable(GL_NORMALIZE);

This is sort of slow and not recommended, but if you have a tight deadline, maybe you should try it.

satan
11-08-2003, 08:19 AM
Originally posted by while(fork()):
If you want, you can tell OpenGL to calculate the normals for you. In your initialize function, add

glEnable(GL_NORMALIZE);

This is sort of slow and not recommended, but if you have a tight deadline, maybe you should try it.

GL_NORMALIZE will scale you normals to unit length after transformation but it will not generate normals, as far as I know.

cloudraven
11-08-2003, 12:12 PM
while(fork()) thanks a lot!
As satan said, that does not do the whole normal calculation, (without my normal vector calculation routine it fails), but if I use both my routine and glEnable(GL_NORMALIZE) as you said, everything works incredibly great. Now I will be able to submit my project as scheduled, thanks!!!!!!