working with openGL - opengl

im trying to do a little 3d scene with openGL and include a bit of lighting, im fine with my scene (although it isnt anything special) and im trying to add some lighting to give it some effect. however I can add material to my podium (which isnt textured) and that gives me light and anything that IS textured does not apply any material to it so it defies the point of having lights. Heres some of the code.
// Setup GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular); // Setup specular light
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, qa);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.0);
// Setup GL_LIGHT1
glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT1, GL_SPECULAR, lightSpecular); // Setup specular light
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, qa);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 0.0);
// Setup GL_LIGHT2
glLightfv(GL_LIGHT2, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT2, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT2, GL_SPECULAR, lightSpecular); // Setup specular light
glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, qa);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 0.0);
// Setup GL_LIGHT3
glLightfv(GL_LIGHT3, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT3, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT3, GL_SPECULAR, lightSpecular); // Setup specular light
glLightf(GL_LIGHT3, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT3, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT3, GL_QUADRATIC_ATTENUATION, qa);
glLightf(GL_LIGHT3, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT3, GL_SPOT_EXPONENT, 0.0);
// OpenGL provides a global ambient light component - we don't want this so set to zero
GLfloat global_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
And to render my room + 3d object.
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHT3);
// Define position and direction
// NOTE: Placing these commands AFTER the above viewing (camera) transformations means the light position and direction appear to have a set point / direction in the 3D environment. If these commands are placed BEFORE the above viewing transformations, then the light appears to move with the camera (as if it is attached to the camera!)
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDirection);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightDirection1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, lightDirection2);
glLightfv(GL_LIGHT2, GL_POSITION, lightPosition2);
glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, lightDirection3);
glLightfv(GL_LIGHT3, GL_POSITION, lightPosition3);
// setup materials for objects to draw
glMaterialfv(GL_FRONT, GL_AMBIENT, ambientMaterial);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularMaterial);
// 2. Draw scene
glPushMatrix();
drawRoom();
glTranslatef(0.0,-0.75,0.0);
glRotatef(90.0, 1.0,0.0, 0.0);
glRotatef(-spin, 0.0,0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
drawPodium();
glPopMatrix();
// Save transformations prior to rendering scene
glPushMatrix();
glColor3f(0.0, 0.0, 1.0);
// Setup texture state
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glRotatef(spin, 0.0,1.0, 0.0);
// Render model
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial2);
glTranslatef(0.0,0.8,0.0);
glRotatef(1.0,1.0,0.0,0.0);
myModel->renderTexturedModel();
// Reset texture state
glDisable(GL_TEXTURE_2D);
// restore transformations after rendering scene
glPopMatrix();
And the current outcome with my 4 lights, in a square shape above the helicopter all looking down towards 0.0 is:
If you can make out the white dotted lines they are the direction the lights are looking. the surface of the podium is lit and the sides of the podium are lit as it spins. However the rest of the room and helicopter does not respond to light and act like lighting isnt even enabled hence why i think its because everything except the podium is textured.

You need to setup glTexEnv to specify how lighting should work for the textured objects
See:
21.030 Why doesn't lighting work when I turn on texture mapping?
http://www.opengl.org/resources/faq/technical/texture.htm
Or to address your problem more directly, the line
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
is what causes the behaviour. As the faq explains, when you specify GL_REPLACE, you replace the primitive lighting colour with the texture colour, which overwrites any lighting calculations. You can remove this line completely as GL_MODULATE is the default behaviour.

I don't know if the above method works for you. I had the same problem. My problem was that I had not specified the normals of the object.
//specify normals
glNormal3f();
hopefully this will solve the problem for anyone (i tried every other method over the internet but nothing worked; only this method worked).

Related

OpenGL + GLUT Spot light

I'm having a bit of trouble creating a nice spot light. So far, my lighting properties are like this :
/* Spot light */
glEnable(GL_LIGHT1);
GLfloat ambientColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Light
GLfloat lightColor0[] = {0.7f, 0.7f, 0.7f,0.0f};
GLfloat lightPos0[] = {0.0f, 3.0f, 0.8f, 0.0};
glLightfv(GL_LIGHT1, GL_SPECULAR, lightColor0);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos0);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 15.0);
GLfloat spot_direction[] = { 0.0, -1.0, 0.0 };
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.0);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
I'm getting a 'spot light' of sorts... at least, its a light that sits above my scene and shines down on the objects, but its not (as far as I can tell) appearing in a conical sort of way.
This is the scene I'm trying to create
And here's what I've got so far.
I know my textures are a little buggered too, if any one could help with that as well, that'd be great!
Your solution is here
C++ Opengl - lighting using spotlight
you need the last value of the position variable to be 1.0 for the light to be a spotlight.

C++ Opengl - lighting using spotlight

I have a model that needs to be under a spotlight/directional light,
Meaning, I need to switch between the modes (Spotlight and directional).
Here is the code with some explanation:
I can rotate the model / light source with mouse movements so I am using
glRotate and glTranslate for that.
Once the user pressed the 'L' key I'm supposed to switch between the modes.
here is the code for the lightning:
void LightBall::projectLight(void)
{
if(LIGHT == _lightMode){
printf("Entering LIGHT mode\n"); <--- Supposed to be a directional light
glDisable(GL_LIGHT1);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, _light_position);
}
if(SPOT_LIGHT == _lightMode){
printf("Entering SPOTLIGHT mode\n"); <--- Supposed to be a spotlight
glDisable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_POSITION, _light_position);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10.0);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,_spotlight_position);
}
}
The problem is I always get the same light mode when switching between them,
Which is the following:
And another example after switching between 2 light modes and still getting same light
source with light source rotation (the small ball):
How can I get the wanted result?
Here are the LIGHT0 & LIGHT1 definitions:
GLfloat light_ambient[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
_light_position[0] = 0.0;
_light_position[1] = 1.0;
_light_position[2] = 0.0;
_light_position[3] = 0.0;
_spotlight_position[0] = 0.0;
_spotlight_position[1] = -1.0;
_spotlight_position[2] = 0.0;
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
Thanks!
Whether a GL light is a directional light or a spotlight depends on the w (4th) component of its position. If the coordinate is 0, it's directional. If nonzero (usually 1), it's a spotlight. You'll have to modify _lightPosition accordingly before calling glLightfv(..., GL_POSITION, ...).

OpenGL Multiple Lights

I'm attempting to add a second light to my scene. I was under the impression that all I needed to do, was enable another light (LIGHT1 in this case), and set it's parameters in order for it to work alongside the existing light. With this in mind, this is my lighting initialization:
void ThemePark::lightInit(GLfloat sun_position[], GLfloat light1_position[])
{
// Enable Lighting
glEnable(GL_LIGHTING);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
glLightfv(GL_LIGHT0, GL_POSITION, sun_position); // Position the lights
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
// Set light intensity and color for each component
glLightf(GL_LIGHT0, GL_DIFFUSE, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT0, GL_AMBIENT, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT0, GL_SPECULAR, (1,1,1,1));
glLightf(GL_LIGHT1, GL_DIFFUSE, (0.7,0.7,0.7,1));
glLightf(GL_LIGHT1, GL_AMBIENT, (0.3,0.3,0.3,1));
glLightf(GL_LIGHT1, GL_SPECULAR, (1,1,1,1));
// Set attenuation
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.5);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, -1.0);
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.2);
// Enable Lights
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
}
I am also positioning my lights again in my display function as follows:
if(lighting)
{
glLightfv(GL_LIGHT0, GL_POSITION, sun_position);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
}
Their positions are:
GLfloat sun_position[] = {10, 10, -2, 1};
GLfloat light1_position[] = {1, 1, 1, 1};
However, when doing this I still only have a single active light, LIGHT0. Taking out the enable statement for LIGHT0 gives me a scene with no lights. As a test, I modified the lighting initialization function to the following, which essentially makes LIGHT1 the same as LIGHT0, but never enables or initializes LIGHT0.
void ThemePark::lightInit(GLfloat sun_position[], GLfloat light1_position[])
{
// Enable Lighting
glEnable(GL_LIGHTING);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
glLightfv(GL_LIGHT1, GL_POSITION, sun_position);
// Set light intensity and color for each component
glLightf(GL_LIGHT1, GL_DIFFUSE, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT1, GL_AMBIENT, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT1, GL_SPECULAR, (1,1,1,1));
// Set attenuation
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.5);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, -1.0);
// Enable Lights
glEnable(GL_LIGHT1);
}
I also modified my display function accordingly. However, I still see no lighting in my scene. Is there something I'm missing here?
GL_LIGHT1-7 actually have different defaults than GL_LIGHT0 ... so you will have to find the ones you forgot to set. Perhaps GL_QUADRATIC_ATTENUATION? If not try setting all the params in the spec Many of them default to zero/off states for GL_LIGHT1-7.

OpenGL lighting don't work

I'm trying to setup lighting in my scene
but for some reason it doesn't work, i'm trying to figure out why
here's how I initialize OpenGL:
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHT0);
and here is how I try to draw with lighting on:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GLfloat diffuse0[]={1.0, 1.0, 1.0, 1.0};
GLfloat ambient0[]={1.0, 1.0, 1.0, 1.0};
GLfloat specular0[]={1.0, 1.0, 1.0, 1.0};
GLfloat light0_pos[]={1.0, 1.0, 1,0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 2.0);
object->draw();
the object is not affected by the lighting at all
all normals are set and all the information that opengl require for lighting a scene
is there... what am I missing?
EDIT:
OK! I finally found a solution to the problem.
my opengl init function was misplaced in the code
I had to call it after showing the window and not before
and that's what caused the problem...
Probably you should tell us the position of the object.
Suspect 1: The position of the object and the light are too far
Suspect 2: Hide all ATTENUATION settings or make LINEARATTENUATION to 0.0

OpenGL: lines dont get rendered

Using OpenGL I am trying to render a simple line between the points p1 and p2, but it doesnt work.
However, I am able to render cubes at these positions.
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0};
GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0};
GLfloat mat_flash_shiny[] = {50.0};
GLfloat light_position[] = {100.0,-200.0,200.0,0.0};
GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(p1.x, p1.y, p1.z);
glutSolidCube(20);
glLoadIdentity();
glTranslatef(p2.x, p2.y, p2.z);
glutSolidCube(20);
glDisable( GL_LIGHTING );
glDisable( GL_DEPTH_TEST );
I am using the following code for rendering the line. But I cant find the problem why it doesnt get rendered:
glDisable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glLoadIdentity();
glColor3f(1,0,0);
glLineWidth(10.0);
glBegin(GL_LINES); //doesnt work with GL_LINE either
glVertex3f(p1.x, p1.y, p1.z);
glVertex3f(p2.x, p2.y, p2.z);
glEnd();
I assume what happens is that the lines are actually rendered, but there's still a vertex normal set, pointing away from the light source, or being 0. Easy fix: Disable lighting when drawing lines.
While it is possible to "illuminate" lines, the whole operation makes no sense if one assumes the "regular" normal based illumination models. Lines don't have a normal direction, they have a normal plane (or in other words, a line it it's tangent itself).
So the proper way to illuminate a line is by tangent based shading. Fixed function OpenGL can't do it, but it is trivial to implement with a shader.
A couple of suggestions for you, w.r.t your second code snippet:
1) You probably don't need the 'glEnable(GL_COLOR_MATERIAL)', try commenting out that line.
2) You're passing int's to glColor3f(). Try using glColor3f(1.0f, 0.0f, 0.0f); instead.
Also, did you check the line drawing without the cube at the same position? (ie., is it being occluded or not)