# Odd problem with lighting...

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

## Recommended Posts

I have this code (it makes an earth and a moon that rotates around it):
// found this function on the internet, not taking credit for it
int CreateSphere(XYZ c, double r, int n)
{
int i, j;
double theta1, theta2, theta3;
XYZ e, p;
if (r < 0)
r = -r;
if (n < 0)
n = -n;
if (n < 4 || r <= 0)
{
glBegin(GL_POINTS);
glVertex3f(c.x,c.y,c.z);
glEnd();
return 0;
}
for (j=0;j<n/2;j++)
{
theta1 = j * TWOPI / n - PID2;
theta2 = (j + 1) * TWOPI / n - PID2;
for (i=0;i<=n;i++)
{
theta3 = i * TWOPI / n;
e.x = cos(theta2) * cos(theta3);
e.y = sin(theta2);
e.z = cos(theta2) * sin(theta3);
p.x = c.x + r * e.x;
p.y = c.y + r * e.y;
p.z = c.z + r * e.z;
glNormal3f(e.x,e.y,e.z);
glTexCoord2f(i/(double)n,2*(j+1)/(double)n);
glVertex3f(p.x,p.y,p.z);
e.x = cos(theta1) * cos(theta3);
e.y = sin(theta1);
e.z = cos(theta1) * sin(theta3);
p.x = c.x + r * e.x;
p.y = c.y + r * e.y;
p.z = c.z + r * e.z;
glNormal3f(e.x,e.y,e.z);
glTexCoord2f(i/(double)n,2*j/(double)n);
glVertex3f(p.x,p.y,p.z);
}
glEnd();
}
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
spin = (spin + .5);
if (spin > 360.0)
spin = spin - 360.0;
moonSpin = moonSpin + 1;
if (moonSpin > 360.0)
moonSpin = moonSpin - 360.0;
// draw the stars
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, star_mat_emission);
glCallList(starfield);
glutPostRedisplay();
// draw earth
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, earth_mat_emission);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glPushMatrix();
glTranslatef(400.0, 400.0, 1000.0);
glRotatef(90, 0.0, 1.0, 0.0);
glRotatef(-12, 1.0, 0.0, 0.0);
glRotatef(spin, 0.0, 1.0, 0.0);
CreateSphere(coords, 200, 50);
glPopMatrix();
// draw moon
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, moon_mat_emission);
glPushMatrix();
glTranslatef(400.0, 400.0, 1000.0);
glRotatef(spin, 0.0, 1.0, 0.0);
glTranslatef(300.0, 0.0, 0.0);
glRotatef(moonSpin, 0.0, 1.0, 0.0);
CreateSphere(coords, 40, 50);
glPopMatrix();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}


but when I run it the moon rotates fine but it has a problem with the lighting where one side is lit and one side is dark that does not change. When I get rid of this line:
 glPushMatrix();
glTranslatef(400.0, 400.0, 1000.0);
//glRotatef(spin, 0.0, 1.0, 0.0);
glTranslatef(300.0, 0.0, 0.0);
glRotatef(moonSpin, 0.0, 1.0, 0.0);
CreateSphere(coords, 40, 50);
glPopMatrix();


it still rotates in place but not around the earth. The odd thing is the lighting problem goes away. Does anyone know what is wrong? Thanks.

##### Share on other sites
to make the moon spin on the earth do this method be before drawing the moon
glLoadIdentity -> Rotate -> Translate -> Rotate
i hope its work with you :)
bye ff8!

##### Share on other sites
Nope, it is still doing it. However, I think it might be a texture problem instead because when I set the emmission spectrum of the moon to full it is still doing it (even though it is lit up).

##### Share on other sites
when you rotate object, you must rotate normal vector with object!!

##### Share on other sites
How do you do that?

##### Share on other sites
Baltor,I don't see anywhere in the code calls to glLightf() that sets the light position.Anyway,the general procedure of doing things is:

void Render(){...glMatrixMode(GL_MODELVIEW);glLoadIdentity();SetCamera();//Matrix transformations that set the camera(like gluLookAt or something like that)SetLightPos();//Set the light position//Now just render the objectsglPushMatrix();TransformObject();//Here,setup transformations for object,like translating,rotating,scaling...RenderObject();glPopMatrix();...}

##### Share on other sites
I put this code in at the beginning of the display() func:
 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION, light_position);

with light_position equal to (800.0, 800.0, -1000.0, 0.0). I enabled lighting and set the properties in my init() function. I'm still getting the problem though.

Anyone?

##### Share on other sites
you should use glEnable(GL_LIGHTING); to activate the GL_LIGHT0

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 13
• 14
• 45