PDA

View Full Version : rotating spotlight



dark_paladin
03-09-2010, 10:36 AM
I'm currently having some issues creating a rotating spotlight that rotates around its y axis. I have a cone that represents the spotlight's position that rotates the same way and managed to get that part of it working. I just haven't figured out how to make the spotlight itself rotate. I'm using a Idle() function to handle the updates to the cone and spotlight. Another issue is with the postReDisplay() call in the Idle() function. When its not commented out the cone will spin, however the spotlight will not show up. I'm assuming that's because the main parts of the spotlight are in the initialization function. I tried to add the direction of the spotlight into the display function under the call to Idle() but didn't help much at all. Any ideas would help me a lot. I added in some comments around the Idle() function to help show some of the issues I'm having




#include <GL\glut.h>

//lIGHT1 is a spotlight

struct point3d
{
GLfloat x,y,z;
};


GLfloat mat_emission[] = {1.0, 1.0, 0.0, 1.0};
GLfloat no_mat[] = { 0, 0, 0, 0 };
GLfloat noAmbient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat redAmbient[] = {0.20, 0.0, 0.0, 1.0 };
GLfloat whiteDiffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat greenDiffuse[] = {0.0, 1.0, 0.0, 1.0};
GLfloat blackDiffuse[] = {0.0, 0.0, 0.0, 1.0};
//GLfloat light_pos[4] = {0.0, 0.0, 0.0, 1.0}; //array to set light pos at origin
GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat lmodel_ambient[] = { 0.0, 0.1, 0.0, 1.0 };

//spotlight
//float noAmbient[] = {0.0f, 0.0f, 0.2f, 1.0f};//low ambient light
GLfloat diffuse[] = {0.0f, 0.0f, 1.0f, 1.0f};
GLfloat position[] = {0.0f, 0.0f, 0.0f, 1.0f};


//light attenuation (default values used here : no attenuation with the distance)
GLfloat light_dir[4] = {1.0f, -20.0f, 0.0f, 0.0f};
GLfloat green_light[] = { 0.0, 1.0, 0.0, 1.0 }; //green
GLfloat direction[] = {0, 0, 1};

GLint i = 0;
GLint j = 0;
GLdouble spin = 0;

point3d rainGrid[20][20];


//float winWidth, winHeight;


void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT2);
glEnable(GL_BLEND);//enable blend
glEnable(GL_DEPTH_TEST);//enable depth
//glEnable(GL_COLOR_MATERIAL);

//function for alpha
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glShadeModel(GL_SMOOTH);

glLightfv(GL_LIGHT0, GL_AMBIENT, redAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, greenDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_dir);
glLightfv(GL_LIGHT0, GL_SPECULAR, whiteDiffuse);

//spot light
//properties of the light
glLightfv(GL_LIGHT2, GL_AMBIENT, noAmbient);
glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuse); //blue diffuse color
glLightfv(GL_LIGHT2, GL_SPECULAR, whiteDiffuse);
glLightfv(GL_LIGHT2, GL_POSITION, position);
glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 1.0f);
glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.0f);
glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0f);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 20.0);
//glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 15.0f);


//spot direction
// glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, direction);
//angle of the cone light emitted by the spot : value between 0 to 180


}

//having figured out what to add to make the spotlight
//rotate around its y axis.
void Idle()
{
if(spin < 360)
spin+=5;
else
spin = 0;

//with this uncommented the cone will spin, but the spotlight doesn't show up.
//with it commented the spotlight shows.
//glutPostRedisplay();

}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
{
rainGrid[i][j].x = i*2;
rainGrid[i][j].z = j*2;
rainGrid[i][j].y = 0;

glPushMatrix();
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);

//glColor4f(1,1,1,1);
glTranslatef(rainGrid[i][j].x*2, rainGrid[i][j].y, rainGrid[i][j].z);
glutSolidSphere(1, 16, 16); //draw sphere
glPopMatrix();
}

glPushMatrix();
glColor4f(0,1,0,1);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
//glutIdleFunc();
//glColor4f(0,0,1,1);
Idle();
glRotatef(spin,0,1.0,0);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 20.0);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, direction);
//glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 20.0);
//glColor4f(1,0,0,1);
//glTranslatef(5,0,0);
glutSolidCone(3,5,15,15);
glPopMatrix();

//glutPostRedisplay();
glutSwapBuffers();

}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 50.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

}

int main(int argc, char** argv)
{

glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //say we are using RGBA and DEPTH
glutInitWindowSize(900, 700);
glutInitWindowPosition(100, 100);
glutCreateWindow("JBOProj05A");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
//glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;

}

dark_paladin
03-09-2010, 12:34 PM
problem has been solved.