PDA

View Full Version : Cube and Lighting



aus79er
05-26-2002, 08:26 AM
Hello,
I am trying to draw a cube using OpenGl and MFC(VC++).I can see the cube on the screen ,problem is I am not able to understand lighting.
I have written the code is such a way that I can change the position of light from the front end.But ever after changing the lighting position or material properties..there is no effect on the cube..
I am not able to understand this for quite sometime...
Can anybody help me........

Thanks
aus


void CSim_4_26_02View::draw3dobject()
{

glClear ( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ) ;
glColor3f ( 1.0f, 1.0f, 1.0f ) ;
glPushMatrix();
gluLookAt(eye_x,eye_y,eye_z,center_x,center_y,cent er_z,vect_x,vect_y,vect_z);
gluPerspective(fovy,aspect,znear,zfar);
glMaterialfv(GL_FRONT,GL_AMBIENT,a_mat);
glMaterialfv(GL_FRONT,GL_DIFFUSE,d_mat);
glMaterialfv(GL_FRONT,GL_SPECULAR,s_mat);
glMaterialf(GL_FRONT,GL_SHININESS,sh_mat);

glLightfv(GL_LIGHT0,GL_AMBIENT,a_light0);
glLightfv(GL_LIGHT0,GL_DIFFUSE,d_light0);
glLightfv(GL_LIGHT0,GL_SPECULAR,s_light0);
glLightfv(GL_LIGHT0,GL_POSITION,p_light0);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_CULL_FACE);

glShadeModel(GL_SMOOTH);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);

glTranslatef(m_x_trans,m_y_trans,m_z_trans);
glScalef(m_x_scale,m_y_scale,m_z_scale);
glRotatef(m_ang,m_x_rot,m_y_rot,m_z_rot);
glRotatef(m_angx,1.0f,0.0f,0.0f);
glRotatef(m_angy,0.0f,1.0f,0.0f);



glEnable(GL_NORMALIZE) ;

glBegin(GL_QUADS); // Draw A Quad
glColor3f(0.0f,1.0f,0.0f);
// Set The Color To Blue
glNormal3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
// glColor3f(1.0f,0.5f,0.0f);
// Set The Color To Orange
glNormal3f(0.0f,-1.0f,0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
// glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glNormal3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
// glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
glNormal3f(0.0f,0.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
// glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glNormal3f(-1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
// glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
glNormal3f(1.0f,0.0f,0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd(); // Done Drawing The Quad
glPopMatrix();
glFlush( );


}

void CSim_4_26_02View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here

glViewport(0,0,cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fovy,(GLfloat)cx/(GLfloat)cy,znear,zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

Bob
05-26-2002, 09:56 AM
gluLookAt(eye_x,eye_y,eye_z,center_x,center_y,cent er_z,vect_x,vect_y,vect_z);
gluPerspective(fovy,aspect,znear,zfar);

gluLookAt should go in the modelview matrix, and gluPerspective should go in the projection matrix. Anything else will mess up the stuff that happen between the modelview and projection matrix. Lighing is one of those things that will be messed up.