#include
#include
#include "3dMath.h"
GLfloat xrot = 0.0f;
GLfloat yrot = 0.0f;
//Define my cube triangles, 6 sides to a cube, so 12 triangles in all (with 3 vertices each)
float cube[12][3][3] = {
{{-50, 50, -50}, {50, 50, -50}, {-50, -50, -50}}, //Triangle 1
{{50, 50, -50}, {50, -50, -50}, {-50, -50, -50}}, //Triangle 2
{{50, 50, -50}, {50, 50, 50}, {50, -50, -50}}, //Triangle 3
{{50, 50, 50}, {50, -50, 50}, {50, -50, -50}}, //Triangle 4
{{50, 50, 50}, {-50, -50, 50}, {50, -50, 50}}, //Triangle 5
{{50, 50, 50}, {-50, 50, 50}, {-50, -50, 50}}, //Triangle 6
{{-50, -50, 50}, {-50, 50, 50}, {-50, 50, -50}}, //Triangle 7
{{-50, -50, 50}, {-50, 50, -50}, {-50, -50, -50}}, //Triangle 8
{{50, 50, 50}, {50, 50, -50}, {-50, 50, -50}}, //Triangle 9
{{50, 50, 50}, {-50, 50, -50}, {-50, 50, 50}}, //Triangle 10
{{50, -50, 50}, {-50, -50, 50}, {-50, -50, -50}}, //Triangle 11
{{50, -50, -50}, {50, -50, 50}, {-50, -50, -50}}};//Triangle 12
float cubeNormals[12][3]; //Contains one normal for each triangle in the cube.
void ChangeSize(GLsizei w, GLsizei h) {
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w/(GLfloat)h, 1, 400);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void RenderScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f, 0.0f, -10.0f);
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);
//Draw the cube
/*glBegin(GL_TRIANGLES);
int i = 0;
for(i = 0; i<12; i++) {
glNormal3f(cubeNormals[0], cubeNormals[1], cubeNormals[2]);
glVertex3f(cube[0][0], cube[0][1], cube[0][2]);
glVertex3f(cube[1][0], cube[1][1], cube[1][2]);
glVertex3f(cube[2][0], cube[2][1], cube[2][2]);
}
glEnd();*/
glColor3f(1.0, 0.0, 0.0);
glutSolidCube(5.0);
glPopMatrix();
glutSwapBuffers();
}
void Keyboard(int key, int x, int y) {
if(key == GLUT_KEY_UP) xrot -= 5;
if(key == GLUT_KEY_DOWN) xrot += 5;
if(key == GLUT_KEY_RIGHT) yrot += 5;
if(key == GLUT_KEY_LEFT) yrot -= 5;
glutPostRedisplay();
}
void SetupRC() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//Setup a few flags
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
//Calculate all the normals in the cube
for(int i = 0; i < 12; i++) {
calcNormal(cube, cubeNormals);
ReduceToUnit(cubeNormals);
}
//Setup polygons and lights
glShadeModel(GL_SMOOTH);
/// Light values and coordinates
GLfloat ambientLight[] = { 0.0, 0.3f, 0.0f, 1.0f };
GLfloat diffuseLight[] = { 0.0f, 0.7f, 0.0f, 1.0f };
GLfloat lightPos[] = {0.f, 50.0f, 0.0f, 1.0f};
// Enable lighting
glEnable(GL_LIGHTING);
// Setup and enable light 0
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);
// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
}
void main(void) {
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Primitives");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutSpecialFunc(Keyboard);
SetupRC();
glutMainLoop();
}
</pre>
Edited by - Blackstream on July 9, 2001 8:06:38 PM </i>
Having a newbie light problem
I don't think I'm fully understanding how light in OpenGL works...
Anyways, I'm trying to get a colored light shining on a different colored object.
My light colors are set with
GLfloat ambientLight[] = { 0.0, 0.3f, 0.0f, 1.0f };
GLfloat diffuseLight[] = { 0.0f, 0.7f, 0.0f, 1.0f };
and my object color is set via color tracking with
glColor3f(1.0, 0.0, 0.0);
However, in my scene, I only get a dark red cube, not a red cube with a green light on it. Can somebody please tell me what I'm doing wrong?
Here is my code
Well, I''m still trying to figure out lighting and now something else weird is happening.
I changed my object to a torus (easier to see lighting effects), got rid of the color tracking thing and replaced part of my initialization code with this
But my diffuse color seems weird. If I have it set to something like 1.0, 1.0, 1.0, 1.0, my torus looks like a normal, red, lighted torus. But if I have it something like 0.0, 1.0, 0.0, 1.0, it looks like a solid dark red torus (no variation of color), with some specular highlights.... instead of a red torus with a green light shining on it. I''m very confused.
I changed my object to a torus (easier to see lighting effects), got rid of the color tracking thing and replaced part of my initialization code with this
/// Light values and coordinates GLfloat ambientLight[] = { 0.0f, 0.0f, 1.0f, 1.0f }; GLfloat diffuseLight[] = { 0.0f, 0.0f, 1.0f, 1.0f }; GLfloat ambient[] = {1.0, 0.0, 0.0, 0.0}; GLfloat diffuse[] = {1.0, 0.0, 0.0, 1.0}; GLfloat specular[] = {1.0, 1.0, 1.0, 0.0}; GLfloat lightPos[] = {0.f, 20.0f, 0.0f, 1.0f}; // Enable lighting glEnable(GL_LIGHTING); // Setup and enable light 0 glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); glLightfv(GL_LIGHT0,GL_POSITION,lightPos); glEnable(GL_LIGHT0); glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, specular); glMateriali(GL_FRONT, GL_SHININESS, 128);
But my diffuse color seems weird. If I have it set to something like 1.0, 1.0, 1.0, 1.0, my torus looks like a normal, red, lighted torus. But if I have it something like 0.0, 1.0, 0.0, 1.0, it looks like a solid dark red torus (no variation of color), with some specular highlights.... instead of a red torus with a green light shining on it. I''m very confused.
I''m wondering, could this actually be a hardware problem? I tried using the Material and Lighting studio from the OpenGL SuperBible, and it doesn''t seem to be able to do colored lights very well, if at all...
Hmm, I just messed with it some more... When I messed with the colors some more, it seemed that the objects material colors merely affected what colors of light affected it and how much. Like the redder the object, the more affected the color you see is by red light. And if the object has 0 blue and 0 green, then those color factors of the light don''t affect it at all.
Is that how light is supposed to work? I think what I have here is not bad code, but a misunderstanding of how light works in OpenGl.
Hmm, I just messed with it some more... When I messed with the colors some more, it seemed that the objects material colors merely affected what colors of light affected it and how much. Like the redder the object, the more affected the color you see is by red light. And if the object has 0 blue and 0 green, then those color factors of the light don''t affect it at all.
Is that how light is supposed to work? I think what I have here is not bad code, but a misunderstanding of how light works in OpenGl.
>>ike the redder the object, the more affected the color you see is by red light. And if the object has 0 blue and 0 green, then those color factors of the light don''t affect it at all.
Is that how light is supposed to work<<
i had a v quick read and Yes this is the way lighting works. exactlly the same as in real life go into a dark room and shine a red light on a blue piece of paper the paper should appear black.
http://www.frii.com/~martz/oglfaq/
http://members.xoom.com/myBollux
Is that how light is supposed to work<<
i had a v quick read and Yes this is the way lighting works. exactlly the same as in real life go into a dark room and shine a red light on a blue piece of paper the paper should appear black.
http://www.frii.com/~martz/oglfaq/
http://members.xoom.com/myBollux
Ahh thanks for confirming my suspision! It truely was an odd experience. In my mind I saw this red torus being lit up green like.
Actually, now that you speak of it, it makes a lot more sense. How can I see a green object with a red light, if there was never any green in the light to begin with?
Thanks for your help!
-Blackstream
Will you, won''t you, will you, won''t you, won''t you take my virus?
-The Mad Hacker
Blackstream''s Webpage
Actually, now that you speak of it, it makes a lot more sense. How can I see a green object with a red light, if there was never any green in the light to begin with?
Thanks for your help!
-Blackstream
Will you, won''t you, will you, won''t you, won''t you take my virus?
-The Mad Hacker
Blackstream''s Webpage
Well if your getting a dark reddish looking cube then its working just fine. If you knew the math OpenGL uses to calculate the color values for each vertex from the lighting info you would understand why. Its doing like in real life, the color of the light is getting scaled by the color of the cube. It just takes the material color which is the color of your cube and the light color and multiplies them together.
You could go get a red cube of some sort like a block and shine a green light on it and you will see what im talking about here. I would suggest reading up on OpenGL''s lighting model. The lighting section in the opengl superbible is pretty good.
This page will show you the math behind the scenes of what opengl does to calculate the vertex color. http://reality.sgi.com/blythe/sig99/advanced99/notes/node410.html
-SirKnight
You could go get a red cube of some sort like a block and shine a green light on it and you will see what im talking about here. I would suggest reading up on OpenGL''s lighting model. The lighting section in the opengl superbible is pretty good.
This page will show you the math behind the scenes of what opengl does to calculate the vertex color. http://reality.sgi.com/blythe/sig99/advanced99/notes/node410.html
-SirKnight
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement