glEnable(GL_LIGHTING)
glRotatef(gem_rotate, 0.0, 1.0, 0.0)
glTranslatef(0.0, 0.2, 0.0)
glScalef(0.3, 0.3, 0.3)
glCallList(4)
glDisable(GL_LIGHTING)
🔒 Locked
Lighting on Objects
Advertisement
Original Post
Hi, Note: All code here will be Python. I'm making a game where the player runs through a maze collecting objects, (in this case money in the form of gemstones). The facets of the gems face different directions, and so a light should illuminate each one differently. Therefore, I am adding a light to make enable one to discern the stones' 3D shape. The stones also rotate by "gem_rotate". The rendering code:My problem is that the lighting appears to be lighting the stone unanimously, making seeing each individual facet impossible. Further, I infer that the light is rotating too, as the color appears to fluctuate with the stone's rotation. It also seems to make no difference where I place the light. So, can anyone help? Thanks in advance for comments, Geometrian
1st) How are you setting up your light, and where? What is probably happening is that the light is being set at the origin of the scene, and then being rotating, since OpenGL is a state machine and whatever happened before, will apply until it's been changed. Try encasing the Light Position code with
glPushMatrix
...
...
...
glPopMatrix
2nd) You have to renmember that the Fixed Function lighting in OpenGL, is calculated Per-Vertex, and then interpolated across the polygon (if GL_SMOOTH shademodel is enabled). So unless you have very high polygon models, this kind of lighting is never going to work brilliantly.
glPushMatrix
...
...
...
glPopMatrix
2nd) You have to renmember that the Fixed Function lighting in OpenGL, is calculated Per-Vertex, and then interpolated across the polygon (if GL_SMOOTH shademodel is enabled). So unless you have very high polygon models, this kind of lighting is never going to work brilliantly.
If you want your light to be static at a certain position
glLoadIdentity();
glLight(GL_LIGHT0, GL_POSITION, ....);
and if you want it to be transformed by the camera
you can either compute the new light position yourself or
glLoadIdentity();
applyCamera();
glLight(GL_LIGHT0, GL_POSITION, ....);
you also need to decide on point light, directional light, spot light.
It's better to use shaders and do your own custom lighting (phong lighting/shading).
glLoadIdentity();
glLight(GL_LIGHT0, GL_POSITION, ....);
and if you want it to be transformed by the camera
you can either compute the new light position yourself or
glLoadIdentity();
applyCamera();
glLight(GL_LIGHT0, GL_POSITION, ....);
you also need to decide on point light, directional light, spot light.
It's better to use shaders and do your own custom lighting (phong lighting/shading).
I copied the code from a program I wrote that draws atoms, and it worked fine. Here's the code:It's called once at the beginning of the program.
LightAmbient = ( (0.5, 0.5, 0.5, 1.0) );LightDiffuse = ( (1.0, 1.0, 1.0, 1.0) );LightPosition = ( (5.0, 5.0, 5.0, 1.0) );glLightfv( GL_LIGHT0, GL_AMBIENT, LightAmbient )glLightfv( GL_LIGHT0, GL_DIFFUSE, LightDiffuse )glLightfv( GL_LIGHT0, GL_POSITION, LightPosition )glEnable( GL_LIGHT0 )
OK, the light stays stationary! But, it doesn't fix the silhouette-ish problem.
The gem has 64 triangles. I'm getting a lighting effect I want with a sphere (80 divisions each way) substituted for the stone...
Are you rendering your gems with proper normals?
I don't know what you mean; I'm rather new to this, so examples would be helpful :-)
Normals are vectors that indicate to OpenGL the perpendicular direction of the polygon's plane (the normal direction). I think that if you aren't setting the normals, and just leaving them at a default value, then there will be no shading differentiation between your gem's facets, and since they will all be 'facing the same way' then they will pulse on and off as the gem rotates, just as an individual facet would (but all in unison).
If you didn't know this, then you really REALLY need to read some resources on OpenGL lighting before you do anything else with lights.
If you didn't know this, then you really REALLY need to read some resources on OpenGL lighting before you do anything else with lights.
I'll read up on it. Can you point me to some resources? The problem you described is exactly my problem--can you tell me how to set the normals?
http://www.lighthouse3d.com/opengl/terrain/index.php3?normals
I didn't spend much time searching for this, but it seems to be a short, clear guide to calculating normals for a polygon. You'll need to do a cross product to find the perpendicular, then normalize it (set it to length 1) for use in OpenGL.
http://www.glprogramming.com/red/chapter02.html#name5
Here's how the redbook says to declare normals. Their syntax here is a little confusing, but more simply: your typical command will be void glNormal3f(x, y, z);
You call this before each vertex, or just before each polygon for a flat polygon. In the case of faceted gems, the sides are flat and will have 1 normal per polygon, but for smooth objects you supply a normal per vertex, which is interpolated across the polygon and makes it look smoother.
I didn't spend much time searching for this, but it seems to be a short, clear guide to calculating normals for a polygon. You'll need to do a cross product to find the perpendicular, then normalize it (set it to length 1) for use in OpenGL.
http://www.glprogramming.com/red/chapter02.html#name5
Here's how the redbook says to declare normals. Their syntax here is a little confusing, but more simply: your typical command will be void glNormal3f(x, y, z);
You call this before each vertex, or just before each polygon for a flat polygon. In the case of faceted gems, the sides are flat and will have 1 normal per polygon, but for smooth objects you supply a normal per vertex, which is interpolated across the polygon and makes it look smoother.
You set the normal of a vertex with glNormal3d(), like so:
glNormal3d(0.0,0.0,1.0);
glVertex3d(123.0,123.0,123.0);
This Normal points in the z direction. The normal should be normalized for the lighting calculations to work as they should.
More on OpenGL lighting.
glNormal3d(0.0,0.0,1.0);
glVertex3d(123.0,123.0,123.0);
This Normal points in the z direction. The normal should be normalized for the lighting calculations to work as they should.
More on OpenGL lighting.
OK, give me a few minutes to implement it...
Excellent. That works! Thanks!
G
G
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement