Skip to main content
GameDev.net gamedev.net
🔒 Locked

lights&materials question

Started by L_Nandor Jul 27, 2009 at 6:17 AM 4 replies 650+ views
Original Post
L_Nandor
L_Nandor
Hello all, I'm trying to use lights and materials in opengl. here is the code for the initialization:

        glShadeModel(GL_SMOOTH);						
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);	
        glClearDepth(1.0f);
        glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);							
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	float dif[]={1.0f,1.0f,1.0f,1.0f};
	float amb[]={0.5f,0.5f,0.5f,1.0f};
	glEnable(GL_LIGHTING);
	glLightfv(GL_LIGHT0,GL_POSITION,lpos);
	glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,dif);
	glEnable(GL_LIGHT0);
and for the scene:

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glLightfv(GL_LIGHT0,GL_POSITION,lpos);
	glTranslatef(0.0f,0.0f,-6.0f);
	float dif[]={1.0f,0.0f,0.0f};
	glMaterialfv(GL_FRONT,GL_DIFFUSE,dif);
	GLUquadric *q=gluNewQuadric();
	gluQuadricNormals(q,GL_SMOOTH);
	gluSphere(q,1.0f,64,64);
	glDisable(GL_LIGHTING);
	glTranslatef(lpos[0],lpos[1],lpos[2]);
	glColor3f(1.0f,1.0f,0.0f);
	gluSphere(q,0.1f,8,8);
	glEnable(GL_LIGHTING);
when I position the light inside the sphere,the outer faces get colored.Is that normal or if it's not,could anyone tell me what's wrong? Image Hosting Image Hosting Thanks for any advice
mov eax,ebx
Orbital Fan
Orbital Fan
Is your sphere actually rendering inside-out? Try reversing the vertex ordering.

Also try disabling two-sided lighting.

Hope you get it fixed...
wuwenye
wuwenye
seems you

1- set a light at world coord (lpos),

2- draw your sphere at world coord (0.0f,0.0f,-6.0f),

3- draw a sphere to show the light's pos, at world coord (0.0f,0.0f,-6.0f) + (lpos).

well, i think you should glTranslatef before glLightfv.
L_Nandor
L_Nandor
Iy might be a foolish question,but does the world translation affect glLightfv?
mov eax,ebx
Sneftel
Sneftel
Quote:
Original post by L_Nandor
Iy might be a foolish question,but does the world translation affect glLightfv?

Yes. When glLight is called with GL_POSITION or GL_SPOT_DIRECTION, the argument is transformed by the current (at the time of the glLight call) model-view matrix. glLight, in other words, acts very much like glVertex or glNormal.
L_Nandor
L_Nandor
Thanks for the help,the sphere gets shaded correcly now :)
mov eax,ebx

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.