I’m working adding lighting to a plane. I can’t seem to get spot lighting to work correctly.
Essentially, I have a plane made up of triangles. I have a spot positioned in the middle of the plane and pointing to the lower right corner of the plane. As soon as I change the light direction to point towards the z axis (so that it is projecting onto the plane instead of parallel to it, I lose the light source completely.
The code is fairly trivial, but my error is not obvious to me. Assistance would be greatly appreciated.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/freeglut_ext.h>
using namespace std;
const float xMin = 0.0;
const float xMax = 10000.0;
const float yMin = 0.0;
const float yMax = 15000.0;
const float zMin = 0.0;
const float zMax = 1600.0;
void init (void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glEnable(GL_NORMALIZE);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFrustum(-450,450,-450,450,450,20);
gluLookAt(xMax/1.9, yMax/2, 10*zMax, xMax/2, yMax/2, 0, 0, 0, 1);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
GLfloat l0_position[4] = {xMax/2,yMax/2,6000,1.0 };
GLfloat l0_spot_direction[4] = {1,1, 0.0, 0.0}; //changing this to 1,1,1 causes the light to disappear.
GLfloat l0_diffuse[4] = { 0.1,0.1,0.1,1.0};
GLfloat l0_ambient[4] = { 0.2, 0.2, 0.2, 0.5 };
GLfloat l0_specular[4] = { 0.2, 0.2, 0.2, 0.5 };
GLfloat l0_spot_exponent[1] = {50};
GLfloat l0_spot_cutoff[1] = {30};
glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient);
glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, l0_spot_direction);
glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT, l0_spot_exponent);
glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, l0_spot_cutoff);
glEnable(GL_LIGHT0);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, l0_ambient);
glPolygonMode(GL_FRONT,GL_FILL);
float fieldRows = 100;
float fieldRowSteps = xMax/fieldRows;
float fieldCols = 100;
float fieldColsSteps = yMax/fieldCols;
float * p1 = new float[3], * p2 = new float[3], * p3 = new float[3], * p4 = new float[3];
float resultingColor[] = {0.2,0.2,0.2,0.9};
float mat_ambient[] = {0.2,0.2,0.2,0.9};
float mat_diffuse[] = {0.1,0.1,0.1,1.0};
float mat_shininess[] = {50.0};
glColor3fv(resultingColor);
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
//draw field
for(int i = 0; i < fieldRows; i++){
for(int j = 0; j < fieldCols; j++){
p1[0] = fieldRowSteps * (i);
p1[1] = fieldColsSteps*(j);
p1[2] = 0;
p2[0] = fieldRowSteps * (i+1);
p2[1] = fieldColsSteps*(j);
p2[2] = 0;
p3[0] = fieldRowSteps * (i+1);
p3[1] = fieldColsSteps*(j+1);
p3[2] = 0;
p4[0] = fieldRowSteps * (i);
p4[1] = fieldColsSteps*(j+1);
p4[2] = 0;
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0.0,0.0,1.0);
glVertex3fv(p1);
glNormal3f(0.0,0.0,1.0);
glVertex3fv(p2);
glNormal3f(0.0,0.0,1.0);
glVertex3fv(p4);
glNormal3f(0.0,0.0,1.0);
glVertex3fv(p3);
glEnd();
}
}
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float) w / h, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (800, 800);
glutInitWindowPosition (100, 100);
glutCreateWindow ("lighting");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}