Sign in to follow this  

Odd problem with lighting...

This topic is 4840 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

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;
  glBegin(GL_QUAD_STRIP);
   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); 
   TextureLoadBitmap(texEarthFileName); 
   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);
   TextureLoadBitmap(texMoonFileName); 
   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);
   TextureLoadBitmap(texMoonFileName); 
   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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 objects
glPushMatrix();
TransformObject();//Here,setup transformations for object,like translating,rotating,scaling...
RenderObject();
glPopMatrix();



...
}

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

This topic is 4840 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this