PDA

View Full Version : Lighting



aus79er
05-22-2002, 03:30 PM
Hi,
I am drawing a cube using MFC and opengl.
Now inspite of setting the normals to the cube I am not getting the 3D effects( I can see the object but it doesn't feel like a cube.. http://www.opengl.org/discussion_boards/ubb/smile.gif).
I have tried changing the setting of light and position ,but to no avail.
The code is as follows...

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

Can anybody tell me what am i doing wrong...
Note that the variables i am using for rotation ,light position ,color can be changed through the front end....SO it have tried various positions of light..

Thanks in advance
aus

aus79er
05-22-2002, 03:34 PM
Sorry I forgot to post complete code...
int CSim_4_26_02View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
PIXELFORMATDESCRIPTOR pfd =
{
sizeof ( PIXELFORMATDESCRIPTOR ),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
32,
0, 0,
PFD_MAIN_PLANE,
0,
0, 0, 0
} ;
m_d = new CClientDC ( this ) ;

int pixformat = ChoosePixelFormat ( m_d -> m_hDC, &pfd ) ;
SetPixelFormat ( m_d -> m_hDC, pixformat, &pfd ) ;

DescribePixelFormat ( m_d -> m_hDC, pixformat, sizeof ( pfd ), &pfd ) ;


m_hGRC = wglCreateContext ( m_d -> m_hDC ) ;
wglMakeCurrent ( m_d -> m_hDC, m_hGRC ) ;

glShadeModel(GL_SMOOTH);

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
a_light0[0]=1.0f;
a_light0[1]=1.0f;
a_light0[2]=1.0f;
a_light0[3]=1.0f;

d_light0[0]=1.0f;
d_light0[1]=1.0f;
d_light0[2]=1.0f;
d_light0[3]=1.0f;

s_light0[0]=1.0f;
s_light0[1]=1.0f;
s_light0[2]=1.0f;
s_light0[3]=1.0f;

p_light0[0]=0.0f;
p_light0[1]=0.0f;
p_light0[2]=1.0f;
p_light0[3]=1.0f;

a_mat[0]=0.0f;
a_mat[1]=0.5f;
a_mat[2]=1.0f;
a_mat[3]=1.0f;

d_mat[0]=0.0f;
d_mat[1]=0.0f;
d_mat[2]=0.0f;
d_mat[3]=1.0f;

s_mat[0]=1.0f;
s_mat[1]=0.0f;
s_mat[2]=0.0f;
s_mat[3]=1.0f;

sh_mat=120.0f;


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-13.0,13.0,-13.0,13.0,0.0,7.0);
glMatrixMode(GL_MODELVIEW);

m_x_trans = m_y_trans = 0.0f ;
m_z_trans = -3.5f ;
m_x_scale = m_y_scale = m_z_scale = 1.0f ;
m_x_rot = m_y_rot = m_z_rot = 0.0f ;
m_ang=0.0f;

center_x=center_y=center_z=0.0f;
eye_x=eye_y=eye_z=0.0f;
vect_x=vect_z=0.0f;
vect_y=1.0f;

fovy=10.0f;
aspect=1.0f;
znear=0.1f;
zfar=8.0f;




return 1;
}

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);
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);

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
// Done Drawing The Quad

.........so on.......

}
}

JeeTriX
05-23-2002, 12:55 AM
i've loaded your cube in a test and it looks ok from all angles, so your normals 'n points are right. i noticed your fovy is on the low side and you're using a lot of transformations to just draw a cube http://www.opengl.org/discussion_boards/ubb/smile.gif

aus79er
05-23-2002, 07:02 AM
HI,
I agree there are lot of transformations but I am using them for tranforming when I move the mouse on the screen,while others are used to change using the front end..

What i am trying to say is that lighting position has no effect on the cube...,why?
what am i doing wrong?..

Thanks,
aus

mproso
05-23-2002, 08:53 AM
You have to use glEnable(GL_NORMALIZE) because your normals after scaling
are not normalized.
Hope this helps.

aus79er
05-23-2002, 10:05 AM
Thanks,
where should i place glEnable(GL_NORMALIZE) in my code?

Bob
05-23-2002, 11:30 AM
Anywhere you like, as long as it's after the creation of the rendering context and before you draw your cube.

05-23-2002, 02:25 PM
Correct me if I am wrong, but your gluPerspective() is called when your current matrix is the modelview matrix. Shouldn't the gluPerspective() matrix be applied to the projection matrix?

aus79er
05-24-2002, 08:15 AM
HI,
Still I am not able to understand the problem with my code.
Even if i change the lighting position it has not effects on the object...why?
I have posted almost the entire GL related code...the only remaining code is
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);

//glFrustum(-1.0,1.0,-1.0,1.0,1.0,7.0);
glMatrixMode(GL_MODELVIEW);

// gluLookAt(0,0,3,0,0,0,0,1,0);
//glFrustum ( -5.0, 5.0, -5.0, 5.0, -2.0, 7.0 ) ;
//draw3dobject();
}

Please help me..

Thanks,
aus