C++ Opengl - lighting using spotlight - c++

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, ...).

Related

How to achieve brighter shininess using GL_LIGHT1?

I have this image
Is there a way to achieve brighter look of the light without editing specular material part of the code?
This is my current code:
void drawTeapot() {
GLfloat mat_diffuse[] = {0.2, 1.0, 0.8, 1.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {128.0};
GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
GLfloat light_position[] = {1.1, 1.1, 1.0, 1.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT1, GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_SPECULAR, mat_specular);
glLightfv(GL_LIGHT1, GL_SHININESS, mat_shininess);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glutSolidTeapot(0.5);
}
I have to use GL_LIGHT1 because there is another function that reacts to mouse clicks and turns that light on and off. Therefore, I can't have the specular on the object before the light turns on. It has to remain matte, sort of. As you can see, shininess is on max, specular also, but it's not really what I'm going for. I basically want it to be tiny bit bigger that it is now, more dispersed.

Lighting not working on OpenGL - Solid color

The lighting doesn't work, the object appears with solid color. The color itself changes with the material or lighting parameters but no shadow or anything just solid grey.
Here's part of the code:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glFrontFace(GL_CCW);
glShadeModel(GL_FLAT);
glEnable(GL_LIGHTING);
GLfloat amb[] = {0.6, 0.6, 0.6, 1.0};
GLfloat dif[] = {0.8, 0.8, 0.8, 1.0};
GLfloat spec[] = {0.5, 0.5, 0.5, 1.0};
GLfloat pos[] = {0.0, 0.0, 30.0};
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
glLightfv(GL_LIGHT0, GL_SPECULAR, spec);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
GLfloat co[4]={0.5, 0.5, 0.5, 0.5};
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 1.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, co);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
for(int i=0; i < numofTr; i++)
{
glBegin(GL_TRIANGLES);
glNormal3f(tr[i].v1->n.x, tr[i].v1->n.y, tr[i].v1->n.z);
glVertex3f(tr[i].v1->x, tr[i].v1->y, tr[i].v1->z);
glNormal3f(tr[i].v2->n.x, tr[i].v2->n.y, tr[i].v2->n.z);
glVertex3f(tr[i].v2->x, tr[i].v2->y, tr[i].v2->z);
glNormal3f(tr[i].v3->n.x, tr[i].v3->n.y, tr[i].v3->n.z);
glVertex3f(tr[i].v3->x, tr[i].v3->y, tr[i].v3->z);
glEnd();
}
I don't know what I'm missing, the normals look good when displayed along with the object.
You are using dim light on a gray object. Obviously the result is gray.
If you mean that faces are flat shaded, that's because of glShadeModel(GL_FLAT);. Change it to GL_SMOOTH to enable interpolation.
If the object is in 3D, I think that the fact you are only passing 3 floats as position might be relevant; default value is [0,0,1,0], and the function expects four floats (4th being the W coordinate, which should in most cases be equal to 1, or 0 for lights in infinite distance).
Also double check that the data you give to glVertex and glNormal is correct. When rendering a sphere at 0,0,0, the normal of every vertex is the normalized position of such vertex (only holds for spheres, obviously).
As a side note, this code is obviously outdated, and as a disclaimer I would ditch it alltoghether, if you are only learning, and switch to something that's relatively modern and uses programmable pipeline.

OpenGL lighting: half of triangles not lit

I'm using the marching cubes algorithm described here http://paulbourke.net/geometry/polygonise/ to turn a scalar field into a triangle mesh, and then displaying the mesh using OpenGL's GL_TRIANGLES mode. However, I'm having trouble with the lighting.
It seems like half the triangles are not being lit at all, and it's occurring in a distinct pattern.
In the program depicted below I've set the background to red, the front face color to green, and the back face color to blue. There is one light source with position (1.0, 1.0, 1.0, 0.0) and a sphere included for reference.
http://i.imgur.com/JutXhbb.png
And here it is with the triangles flipped, as in the drawing order of the last two vertices are switched:
http://i.imgur.com/iUgqm3N.png (back face color is blue)
Whats really confusing me is why the flipped version has color everywhere whereas the normal version doesn't. I figured that even if I drew stuff wrong, at least the flipped version should reflect the same error. Any idea what's happening?
Here is the complete lighting initialization code:
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat mat_color1[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat mat_color2[] = { 0.0, 1.0, 0.0, 1.0 };
glMaterialfv(GL_BACK, GL_DIFFUSE, mat_color1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_color2);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_BACK, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

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.

Making a light source visible in OpenGL

I want to move my light source in my OpenGL-Scene, which is working. But to actually I want to see thee light source as well.
How can I do this?
...
glPushMatrix();
GLfloat lightPos[] = {0, 0, 200, 1};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
GLfloat ambientLight[] = { 0.2, 0.2, 0.2, 1.0};
GLfloat lightColor[] = { 0.5, 0.5, 0.5, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor);
glutSolidSphere(5,50,50);
glPopMatrix();
...
A light source is not a visible object, per se. If you want to "see" a light, you have to place some object at the position that your light is. Maybe use a sphere, a sprite, an arrow (pointing in the direction the light is pointing in, if it's a directional light), etc.