PDA

View Full Version : can't get spotlight to work



dante
07-06-2001, 07:49 PM
i have been trying to get a spotlight to work on a sphere. My code for the spotlight is the same code on page 171 for multiple lights in the red book. I have enabled the lighting but it still doesn't work. Here is the code in C++.

#include <iostream>

#include <windows.h>

#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

using std::cout;
using std::endl;

static int year=0, day=0, rotate=0;

void CALLBACK dayAdd(void)
{
day=(day+10)%360;
}


void CALLBACK yearAdd(void)
{
year=(year+5)%360;
}


void CALLBACK both(void)
{
dayAdd();
yearAdd();
}


void CALLBACK display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear color for new color of other stuff | used for depth

glEnable(GL_COLOR_MATERIAL);




glPushMatrix();
glColor3f (0.0, 0.0, 1.0); //blue earth color 0, 0 ,1
glRotatef(90.0, 1.0, 0.0, 0.0);
glRotatef((GLfloat) day, 0.0, 0.0, 1.0);
auxSolidSphere(1.0);//draw earth


glPopMatrix();

auxSwapBuffers();

glPushMatrix();
glColor3f (0.0, 1.0, 0.0); // green satellite color
glRotatef((GLfloat) year, 0.0, 1.0, 0.0); //rotate sat around earth
glTranslatef (2.0, 0.0, 0.0); //puts sat in its own orbit
auxWireSphere(0.2); //makes sat
glPopMatrix();

glPushMatrix();
glColor3f (1.0, 1.0, 0.0); // yellow satellite color
glRotatef((GLfloat) year, 0.0, 0.0, 1.0); //rotate sat around earth
glTranslatef (0.0, -2.0, 0.0); //puts sat in its own orbit
auxWireSphere(0.2); //makes sat
glPopMatrix();

glPushMatrix();
glColor3f (0.0, 1.0, 1.0); // cyan satellite color
glRotatef((GLfloat) year, 1.0, 0.0, 0.0); //rotate sat around earth
glTranslatef (0.0, 2.0, 0.0); //puts sat in its own orbit
auxWireSphere(0.2); //makes sat
glPopMatrix();



glFlush();

}


void CALLBACK spinDisplay(void)
{
for (int i=0; i <= 7200; i++)
{

yearAdd();
auxSwapBuffers();
}

display();
}



void myinit(void)
{
glEnable(GL_DEPTH_TEST); //used for depth
glShadeModel(GL_SMOOTH);

glClearColor(0.5, 0.5, 0.5, 0.0); // background color


GLfloat mat_specular[] = {1.0, 1.0, 1.0, 0.0}; //color of light
GLfloat mat_shininess[] = {50.0};
// GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; //position of shining light

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// glLightfv(GL_LIGHT0, GL_POSITION, light_position);

GLfloat light1_ambient[]={0.2, 0.2, 0.2, 1.0};
GLfloat light1_diffuse[]={1.0, 1.0, 1.0, 1.0};
GLfloat light1_specular[]={1.0, 1.0, 1.0, 1.0};
GLfloat light1_position[]={3.0, 3.0, 1.0, .00}; //position of spotlight??????
GLfloat spot_direction[] = {-1.0, -1.0, 1.0};

glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);

glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);

glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 3.0);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 100.0);


glEnable(GL_LIGHTING);
// glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1); //spotlight
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);


}

void CALLBACK myReshape(GLsizei w, GLsizei h)
{


glViewport(0,0,w,h); // moves image of earth and stuff to left or right
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);

}

void main(int argc, char** argv)
{
auxInitDisplayMode(AUX_SINGLE | AUX_RGBA | AUX_DEPTH);
auxInitPosition(0, 0, 500, 500);
auxInitWindow("My super program");



myinit();

auxReshapeFunc(myReshape);

auxSwapBuffers();

// auxIdleFunc(spinDisplay);

auxKeyFunc(AUX_UP, both);

auxKeyFunc(AUX_DOWN, dayAdd);

auxKeyFunc(AUX_LEFT, yearAdd);

auxMainLoop(display);

}

DFrey
07-06-2001, 08:37 PM
A couple things I see out of wack are you are not setting the spotlight direction for light1, so it is using the default of (0,0,-1), and with your main sphere of radius 1 at (0,0,0), and the spotlight at (3,3,1,0) with an extremely small cutoff angle of 3 degrees, there is no way the light could hit the sphere. Also the other odd thing is that 4th component for the position. You have it set to 0 which means the light is a directional light source and not a positional light source. A spotlight is a positional light that has a spot direction. A directional light is a point source of light infinitely far away and the position says which direction the light is directed. So what you need to do at a minimum is set the spotlight direction, crank up the spot cutoff, to say 30 or so (large enough so that the sphere will intersect the cone of light), and set the 4th component of the position to 1.