Edit 15/03/05 : Question answered, code updated.
I’ve been going insane over this little problem, which is that I can’t fix a GL_POSITION light at a point relative to my scene.
The light seems to transform with objects. However I don’t think it’s moving with the camera. I want the light to stay in a fixed point in the scene, independent of everything else.
Here’s a screencap of the problem: Position Lights
In this vid, as the cubes rotate the sides should change lightness as they get closer/further from the light.
And here’s the code:
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
static GLfloat spin = 0.0;
static GLfloat osc = 0.0;
static GLfloat i = 0.0;
static GLfloat j = 0.15; //Increment: Adjusts horse bobbing speed
static GLfloat k = 6.28;
static GLfloat l = 0.8; //Increment: Adjusts rotation speed
GLint height = 50, width = 50;
GLint globalX, globalY;
// COORDINATE COLUMNS :: X Y Z
// base coordinates (left)
static GLint baseverts[] = { -50, -40, 50, //point 1
50, -40, 50, //point 2
50, -30, 50, //etc
-50, -30, 50,
-50, -40, -50,
50, -40, -50,
50, -30, -50,
-50, -30, -50
};
// base coordinates (left)
static GLint topverts[] = { -50, 30, 50, //point 1
50, 30, 50, //point 2
50, 40, 50, //etc
-50, 40, 50,
-50, 30, -50,
50, 30, -50,
50, 40, -50,
-50, 40, -50
};
// box 1 coordinates (left)
static GLint box1verts[] = { -50, -10, 10, //point 1
-30, -10, 10, //point 2
-30, 10, 10, //etc
-50, 10, 10,
-50, -10, -10,
-30, -10, -10,
-30, 10, -10,
-50, 10, -10
};
// box 2 coordinates (right)
static GLint box2verts[] = { 30, -10, 10, //point 1
50, -10, 10, //point 2
50, 10, 10, //etc
30, 10, 10,
30, -10, -10,
50, -10, -10,
50, 10, -10,
30, 10, -10
};
// box 3 coordinates (middle)
static GLint box3verts[] = { -10, -30, 10, //point 1
10, -30, 10, //point 2
10, 30, 10, //etc
-10, 30, 10,
-10, -30, -10,
10, -30, -10,
10, 30, -10,
-10, 30, -10
};
// NORMALS :: X Y Z
static GLfloat normals[]= { -1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0};
// COLOUR COLUMNS :: R G B
static GLfloat colors[]= { 1.0, 0.0, 0.0, //point 1 colors
0.0, 1.0, 0.0, //etc
0.0, 0.0, 1.0,
0.5, 0.0, 0.0,
0.0, 0.5, 0.0,
0.0, 0.0, 0.5,
1.0, 1.0, 1.0,
1.0, 0.0, 1.0};
//cube indices refs in one array
// 3___________2
// / /|
// / / |
// 7/___________/6 | RIGHT
// | | | |
// | 0 _ | |1
// | / | /
// | | /
// |__________|/
// 4 FRONT 5
static GLubyte boxIndices[]= {4,5,6,7, 1,2,6,5, 0,1,5,4, 0,3,2,1, 0,4,7,3, 2,3,7,6};
GLfloat posone [] = { 80.0, 20.0, -80.0, 0.0 };
GLfloat colone [] = { 1.0, 0.0, 0.0 };
GLfloat postwo [] = { -80.0, 200.0, 80.0, 0.0 };
GLfloat coltwo [] = { 0.0, 1.0, 0.0 };
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLdouble eyeX = globalX-(width/2);
GLdouble eyeZ = cos(eyeX/width)*2000-1820;
//printf("%4.2f
",eyeZ);
gluLookAt(eyeX/2,globalY-30,eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_COLOR, colone);
glLightfv(GL_LIGHT1, GL_COLOR, coltwo);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glColorPointer(3,GL_FLOAT,0,colors);
glPushMatrix();
glVertexPointer(3,GL_INT,0,baseverts);
glNormalPointer(GL_FLOAT,0,normals);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, boxIndices);
glVertexPointer(3,GL_INT,0,topverts);
glNormalPointer(GL_FLOAT,0,normals);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, boxIndices);
glRotatef(spin,0.0,1.0,0.0);
// draw the lot at once
glVertexPointer(3,GL_INT,0,box3verts);
glNormalPointer(GL_FLOAT,0,normals);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, boxIndices);
glTranslatef(0.0,osc,0.0);
glVertexPointer(3,GL_INT,0,box1verts);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, boxIndices);
glVertexPointer(3,GL_INT,0,box2verts);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, boxIndices);
glPopMatrix();
glutSwapBuffers();
}
void oscDisplay(void)
{
osc = sin(i)*20;
i = i + j;
if (i >k)
i = 0;
glutPostRedisplay();
}
void animDisplay(void)
{
osc = sin(i)*20;
i = i + j;
if (i >k)
i = 0;
spin = spin + l;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
void spinDisplay(void)
{
spin = spin + l;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
// Mouse Control :: Starts and stops animation
void mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
printf("Left Mouse Button Pressed:
Horses Bobbing at increments of %f
",j);
glutIdleFunc(oscDisplay);
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
printf("Middle Mouse Button Pressed:
Horses Rotating at increments of %f
",l);
glutIdleFunc(spinDisplay);
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
printf("Right Mouse Button Pressed:
Horses Bobbing at increments of %f
Horses Rotating at increments of %f
",j,l);
glutIdleFunc(animDisplay);
break;
default:
break;
}
}
void eyePosition(int x, int y)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80,1.0,1,1000);
glMatrixMode(GL_MODELVIEW);
glLightfv(GL_LIGHT0, GL_POSITION, posone);
glLightfv(GL_LIGHT1, GL_POSITION, postwo);
glutPostRedisplay();
globalX = x;
globalY = y;
}
void reshape(int w, int h)
{
width = w;
height = h;
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,(GLsizei) w, (GLsizei) h);
printf("Window Resized: Width=%d Height=%d
",w,h);
glutPassiveMotionFunc(eyePosition);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Window");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Any ideas? Thanks in advance.