Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vicman28

macrumors newbie
Original poster
Nov 26, 2008
5
0
Hi everyone, i have been doing some openGL programming on my powerbook and when i started with the lighting i got some problems. First off i tried to illuminate a sphere with two lights to "mimic" a pool ball but there seemed to be a problem with the light, i had to rotate the sphere and only half of it hat the diffuse and specular lights on it, the other half was just plain color. On top of that when the sphere rotated the change between one illuminated side and the other came with a weird swoop thing that i did not like. My first impression was the normals that i forgot to calculate so i tried using the Solid Teapot that (so i have read) is the only primitive with precalculated normals. The teapot shows even weirder behavior. With the teapot not only lighting seems weird, but the teapot has a cut in the middle, and when it turns around you can see through it as if the handle of the teapot had no alpha but the body of the teapot had some, this happens with all the faces close to the camera and i don't know what is going on, any help would be appreciated. Below i add my display method, i tried disabling face culling but don't see any improvement.

****************************************************************
void Display()
{
rot+=.01;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

GLfloat light[]={1,1,1,1};
glLightfv(GL_LIGHT0,GL_DIFFUSE,light);
GLfloat light2[]={1,1,1,1};
glLightfv(GL_LIGHT0,GL_SPECULAR,light2);
GLfloat material[]={1,0,0
,1};
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,material);
GLfloat material2[]={1,1,1,1};
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,material2);
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,45);
GLfloat pos[]={13,5,-10,1};
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glLightfv(GL_LIGHT1,GL_DIFFUSE,light);
glLightfv(GL_LIGHT1,GL_SPECULAR,light2);

glDisable(GL_CULL_FACE);

GLfloat pos1[]={-5,-5,5,1};
glLightfv(GL_LIGHT1,GL_POSITION,pos1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(rot,1,1,0);
//glutSolidSphere(0.3,10,10);
glutSolidTeapot(.3);




glutSwapBuffers();
glutPostRedisplay();

}
****************************************************************
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi

The 'see through' problem sounds like you haven't enabled depth tests, ie add this:-

Code:
glEnable( GL_DEPTH_TEST ) ;

The lighting problems looks like it's because you haven't enabled light0 and light1:-

Code:
glEnable( GL_LIGHT0 ) ;
glEnable( GL_LIGHT1 ) ;

Also, you might want to change your colour material mode to GL_AMBIENT_AND_DIFFUSE.

b e n
 

vicman28

macrumors newbie
Original poster
Nov 26, 2008
5
0
Ty for the fast reply, that is on the Init() in my code, i do the depth test and the enable lights there as in :

****************************************************************

void Init()
{
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);

}

****************************************************************

sorry i just showed my display, tough everything was there. Also i'm using GL_PROJECTION in the reshape method. I would like to know why you chose the GL_AMBIENT_AND_DIFFUSE i mean i understand it gives a better color match but didn't know it existed :p, tried using it but have the same problem with the teapot.
 

vicman28

macrumors newbie
Original poster
Nov 26, 2008
5
0
Pics

Some pics of the behavior. Best regards.
 

Attachments

  • Picture 1.png
    Picture 1.png
    11.7 KB · Views: 472
  • Picture 2.png
    Picture 2.png
    12.1 KB · Views: 471
  • Picture 3.png
    Picture 3.png
    11.9 KB · Views: 474

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
How about your glutInitDisplayMode(), have you assigned a depth buffer?

Code:
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ) ;
The only other thing I can think of is you've set your camera up so that the part of the teapot falls behind your camera's near clip plane.

As for the lighting, I tried your code and it worked fine for me. Oh one thing I added and forgot to say was:-

Code:
glEnable( GL_COLOR_MATERIAL ) ;

b e n
 

vicman28

macrumors newbie
Original poster
Nov 26, 2008
5
0
Worked like a charm

Ty very much the problem was with the glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); didn't use the Depth buffer, never even crossed my mind thanks a lot for the help :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.