This topic is now archived and is closed to further replies.

Please help me !!! Lighting problem...

This topic is 5142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone ! I have a project to do in opengl and i encounter some very big problems with lighting. We have to realize a representation of the solar system. I''ve almost done some things, such as planet creation and rotation, and i have also put some light. In fact the light comes from the sun, which is the center of my solar system (original, isn''t it ??? :D) But, as planets are rotating around the sun, when they come in front of it, they should be really dark. And my problem is that they remain lightened. What should i do ? I have almost disabled general ambient light and ambient light which comes from my sun. Please help me, and if you can, send me answers by mail Thanks ! Adrien D.

Share this post

Link to post
Share on other sites
I''m not sure what you mean by "come in front of it [the sun]". Do you mean that the side facing away from the sun is lit like the side facing towards the sun? The most likely cause of just about any lighting problem is the normals. Do you have normals defined for your models?

Share this post

Link to post
Share on other sites
It''s exactly what you mean ! The two faces are lightened the same way. Can you explain me how to use those normals ?
If you want, you can leave me your mail, and i can send you my source code.
Thx a lot.
Adrien D.

Share this post

Link to post
Share on other sites
The normals are part of each vertex of the model. Just like you have vertex position and texture information, you define a normal for each vertex which is how OpenGL computes lighting. What are you using to generate your models, a modeling tool such as 3D Studio Max, using already made models, procedurally creating the models in code? Most 3d modeling tools will give you the option to export vertex normals. It sounds like you are generating the models procedurally. In that case you can calculate the face normals by taking the cross product of two of the edges of each face. Then to find the vertex normals, you need to loop through each vertex and average all of the face normals that share this vertex. I hope that makes since.

Share this post

Link to post
Share on other sites
Holy cow, don't do that just to get the normals. If you are drawing your planets (spheres) manually (ie: computing each vertex & making quads/triangles) then do something like the code at the bottom of this reply.
If you are just making a GL quadric, then I think there is a command flag that generates smooth normals for that quadric. Try this...
//your quad pointer
GLUquadricObj *quadratic;
//this sets it up
//this next line is for lighting & textures:
gluQuadricNormals(quadratic, GLU_SMOOTH);
//only for textures:
gluQuadricTexture(quadratic, GL_TRUE);
//creates your planet

This is an example of manually generating your sphere:
//X,Y,Z are vertice locations. Nx,Ny,Nz are normal vectors.
//you can also think of X,Y,Z as vectors
//(from the sphere's center to that vertex)
R = Sphere_Radius;
for( theta=0.0 ; theta<=360.0 ; theta+=increment )
Rtheta = PI*theta/180.0f;
for( phi=0.0f; phi<180.0f ; phi+=increment)
Rphi = PI*phi/180.0f;
//compute the normals
nX1 = GLfloat(sin(Rphi));
nY1 = GLfloat(cos(Rphi));
nX2 = GLfloat(sin(Rphi+Rten));
nY2 = GLfloat(cos(Rphi+Rten));
nZ3 = GLfloat(sin(Rfive*sin(Rphi)));
nX3 = nX1*GLfloat(cos(Rfive*sin(Rphi)));
nY3 = nY1*GLfloat(cos(Rfive*sin(Rphi)));
nZ4 = GLfloat(sin(Rfive*sin(Rphi+Rten)));
nX4 = nX2*GLfloat(cos(Rfive*sin(Rphi+Rten)));
nY4 = nY2*GLfloat(cos(Rfive*sin(Rphi+Rten)));
//the actual coordinates are just Radius * Normals
X1 = R*nX1;
Y1 = R*nY1;
X2 = R*nX2;
Y2 = R*nY2;
Z3 = R*nZ3;
X3 = R*nX3;
Y3 = R*nY3;
Z4 = R*nZ4;
X4 = R*nX4;
Y4 = R*nY4;
//plot vertexes with normals
glNormal3f( nX1, nY1, nZ1);
glNormal3f( nX2, nY2, nZ2);
glNormal3f( nX4, nY4, nZ4);
glNormal3f( nX3, nY3, nZ3);

[edited by - Luke Miklos on November 13, 2003 8:05:12 PM]

Share this post

Link to post
Share on other sites