-
OpenGL lighting problems
Well, after trying everything I could think of on this problem, I decided to try CBoard.
I've been trying to learn OpenGL for a little while now, and this is about my fifth stab at it.
All this program does is init OpenGL, load a cube model, print the cube, allow you to rotate the cube, and quit when <ESC> or <space> is pressed.
Now, when I try to add in lights, the color of the cube changes. Also, when I rorate the cube, the color of the cube changes. And, when there are no lights, the sides of the cube are multi-coloured. When I add in the lights, they are a solid color. Any ideas?
Thanks.
-
I think I'm having problems with the following:
Code:
pos4[0] = 0.5, pos4[1] = 0.5, pos4[2] = 0.5, pos4[3] = 1.0;
glLightfv(GL_LIGHT1, GL_AMBIENT, pos4);
pos4[0] = 1.0, pos4[1] = 1.0, pos4[2] = 1.0, pos4[3] = 1.0;
glLightfv(GL_LIGHT1, GL_DIFFUSE, pos4);
pos4[0] = 1.0, pos4[1] = 1.0, pos4[2] = -200.0, pos4[3] = 1.0;
glLightfv(GL_LIGHT1, GL_POSITION, pos4);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
Also, uncomment out that section on the above section in the .zip, I uncommented it to get the screenshot.
Thanks again.
-
I haven't done much with OpenGL lights, but what I can tell you is this, when the light is off, it is using the glcolor3dv which you set up in the gllib.c file, and when the light is off it uses a meterial which you define I think in the init_game() function, so that would explain why when the light is off the have different colours from when it is on, but I am not 100% and I can't say that I am very good either :P
-
Just reading my OpenGL book again, perhaps I need to define a material? Would that make a difference?
-
OK, so now that I set a material, it works. However, the light is a bit bright, any more suggestions?
-
-
To organize it a little better instead of reusing the pos array you should make an array for each color you want and make them global
ex
Code:
float squarePos[] = {3.0, 0.0, -4.0, 1.0};
float squareAmbient[] = {0.1, 0.2, 0.3};
float squareDiffuse[] = { 0.4, 0.4, 0.1};
float squareSpecular[] = {0.4, .04, 0.4};
also you need to at most position the light by
Code:
glLightfv{GL_LIGHT1, GL_POSITION, squarePos};
The first three values in the squarePos array are the x, y, z coordinates, and the third indicating whether it's positional or not. 0 indicate's it's infinitely far away, while 1 indicate's it has a finite distance.
-
I can't remember the parameters passed to a meterial creation function, but one of the parameters defines the brightness . . . Diffuse I think may be it, but I can't be sure
-
glMaterial and glColorMaterial are only used on the object. for glMaterial you specify a face (GL_FRONT_AND_BACK ususally), the lighting attribute(specular, ambient, diffuse), and the vector specifying' it's color.
A better way I think is to use glColorMaterial, which you have to enable. You specify it as such
Code:
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
glColor4f(1, 0, 0, .5);
glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
//..etc
//draw your object with those attributes.
-
Well, you see, when I rotate the cube, the _entire_ cube color changes. Not just the face facing the light. I'll post screenshots as soon as I can. I am working out of NeHe and a book called "OpenGL game development", by the creators of GameDev.
Thanks again.
-
I'm reading the same book.
The thing is your light source is positioned 200 units in the negative z direction and it's a positional light. A positional light's effect on an object depends on its distance from the object. You indicated it was a positional light by setting the 4th value to 1 with the GL_POSITION value. Plus the fact that it's behind the object being rendered you will not see the effect. Try changing the z value to 10, which brings it 10 units in front of the object toward the screen.
Code:
pos4[0] = 1.0, pos4[1] = 1.0, pos4[2] = -200.0, pos4[3] = 1.0;
glLightfv(GL_LIGHT1, GL_POSITION, pos4);
you should change that to
Code:
float light1Pos[] = { 1.0, 1.0, 10, 1.0 };
glLightfv(GL_LIGHT1, GL_POSITION, light1Pos);
you should try using a less verbose method of initializing/re-initializing arrays, because it can get pretty confusing quickly.
-
Thanks Indigo. I'll try that as soon as I can, probably later on today.