rotating and lighting
i want my object to rotate while the lighting is stationary...i have tried everything but when it rotates, the lighting goes all to hell. if anyone could help, i'd greatly appreciate it.....i suspect the problem is in display() or projection() but i figured id post the whole thing just in case.
Thanks.
Thanks.
Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
float xtheta, ytheta, ztheta;
float xdelta, ydelta, zdelta;
//GLfloat blue_light[] = { 0.8, 0.5, .5, 1};
//GLfloat red_light[] = { 0.5, 0.5, .8, 1};
//GLfloat light_position2[] = { -5.0, 0.0, -20.0, 1.0};
//GLfloat hull_specular[] = { 0.57, 0.57, 0.57, 1.0};
//GLfloat hull_diffuse[] = { 0.57, 0.57, 0.57, 1.0};
void init(void);
void reshape(int width, int height);
void display(void);
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void idle();
void clickedmotion(int x, int y);
void projection(int width, int height, int perspective);
void text(char* string);
void Render();
int main(int argc, char** argv)
{
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowPosition(50, 50);
glutInitWindowSize(512, 512);
glutInit(&argc, argv);
glutCreateWindow("BS 3D Engine (Alpha)");
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMotionFunc(clickedmotion);
glutMouseFunc(mouse);
glutIdleFunc(idle);
init();
glutMainLoop();
return 0;
}
void idle()
{
glutPostRedisplay();
}
void init(void)
{
//glLightfv(GL_LIGHT0, GL_POSITION, light_position1);
//glEnable(GL_COLOR_MATERIAL);
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_NORMALIZE);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,1);
//glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER,light_position1);
xtheta = ytheta = ztheta = 0.0;
xdelta = ydelta = zdelta = 0.0;
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Render()
{
glBegin(GL_QUAD_STRIP); //Main Hull Top
glVertex3f( 0.0, 5.0, 0.0); // 1
glVertex3f( 0.0, 2.0, 3.0); // 2
glVertex3f(-2.0, 5.0, 0.0); // 3
glVertex3f(-1.0, 2.0, 3.0); // 4
glVertex3f(-4.0, 4.0, 0.0); // 5
glVertex3f(-2.0, 1.5, 3.0); // 6
glVertex3f(-5.0, 3.0, 0.0); // 7
glVertex3f(-2.5, 1.0, 3.0); // 8
glVertex3f(-6.0, 1.0, 0.0); // 9
glVertex3f(-3.0, 0.5, 3.0); // 10
glVertex3f(-6.0,-1.0, 0.0); // 11
glVertex3f(-3.0,-0.5, 3.0); // 12
glVertex3f(-5.0,-3.0, 0.0); // 13
glVertex3f(-2.5,-1.0, 3.0); // 14
glVertex3f(-4.0,-4.0, 0.0); // 15
glVertex3f(-2.0,-1.5, 3.0); // 16
glVertex3f(-2.0,-5.0, 0.0); // 17
glVertex3f(-1.0,-2.0, 3.0); // 18
glVertex3f( 0.0,-5.0, 0.0); // 19
glVertex3f( 0.0,-2.0, 3.0); // 20
glEnd();
glBegin(GL_POLYGON); //Main Hull Top
glVertex3f( 0.0, 2.0, 3.0); // 2
glVertex3f(-1.0, 2.0, 3.0); // 4
glVertex3f(-2.0, 1.5, 3.0); // 6
glVertex3f(-2.5, 1.0, 3.0); // 8
glVertex3f(-3.0, 0.5, 3.0); // 10
glVertex3f(-3.0,-0.5, 3.0); // 12
glVertex3f(-2.5,-1.0, 3.0); // 14
glVertex3f(-2.0,-1.5, 3.0); // 16
glVertex3f(-1.0,-2.0, 3.0); // 18
glVertex3f( 0.0,-2.0, 3.0); // 20
glEnd();
glBegin(GL_QUAD_STRIP);//Main Hull Belly
glVertex3f( 0.0, 5.0, 0.0); // 1
glVertex3f( 0.0, 2.0,-1.0); // 2
glVertex3f(-2.0, 5.0, 0.0); // 3
glVertex3f(-1.0, 2.0,-1.0); // 4
glVertex3f(-4.0, 4.0, 0.0); // 5
glVertex3f(-2.0, 1.5,-1.0); // 6
glVertex3f(-5.0, 3.0, 0.0); // 7
glVertex3f(-2.5, 1.0,-1.0); // 8
glVertex3f(-6.0, 1.0, 0.0); // 9
glVertex3f(-3.0, 0.5,-1.0); // 10
glVertex3f(-6.0,-1.0, 0.0); // 11
glVertex3f(-3.0,-0.5,-1.0); // 12
glVertex3f(-5.0,-3.0, 0.0); // 13
glVertex3f(-2.5,-1.0,-1.0); // 14
glVertex3f(-4.0,-4.0, 0.0); // 15
glVertex3f(-2.0,-1.5,-1.0); // 16
glVertex3f(-2.0,-5.0, 0.0); // 17
glVertex3f(-1.0,-2.0,-1.0); // 18
glVertex3f( 0.0,-5.0, 0.0); // 19
glVertex3f( 0.0,-2.0,-1.0); // 20
glEnd();
glBegin(GL_POLYGON);//Main Hull Belly
glVertex3f( 0.0, 2.0,-1.0); // 2
glVertex3f(-1.0, 2.0,-1.0); // 4
glVertex3f(-2.0, 1.5,-1.0); // 6
glVertex3f(-2.5, 1.0,-1.0); // 8
glVertex3f(-3.0, 0.5,-1.0); // 10
glVertex3f(-3.0,-0.5,-1.0); // 12
glVertex3f(-2.5,-1.0,-1.0); // 14
glVertex3f(-2.0,-1.5,-1.0); // 16
glVertex3f(-1.0,-2.0,-1.0); // 18
glVertex3f( 0.0,-2.0,-1.0); // 20
glEnd();
}
void display(void)
{
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
GLfloat light_p[] = { 0.5, 0.5, 5.0, 0};
GLfloat light_a[] = { 1.0, 0.0, 0.0, 0};
GLfloat light_d[] = { 1.0, 0.0, 0.0, 0};
GLfloat light_s[] = { 1.0, 0.0, 0.0, 0};
xtheta+=xdelta;
ytheta+=ydelta;
ztheta+=zdelta;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
projection(width,height,1);
gluLookAt(0.0, 0.0, 12.0, 0.0, 0.0, 0.0, 0.0, 1.0, 2.0);
glPushMatrix();
glRotatef(xtheta, 1.0, 0.0, 0.0);
glRotatef(ytheta, 0.0, 1.0, 0.0);
glRotatef(ztheta, 0.0, 0.0, 1.0);
Render();
glPopMatrix();
glLightfv(GL_LIGHT0, GL_POSITION, light_p);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_a);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_d);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_s);
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void projection(int width, int height, int perspective) //keeps screen good after resizing
{
float ratio = (float)width/height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, ratio, 1, 256);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
case 'x':
xdelta += .1;
break;
case 'X':
xdelta -= .1;
break;
case 'c':
ydelta += .1;
break;
case 'C':
ydelta -= .1;
break;
case 'z':
zdelta += .1;
break;
case 'Z':
zdelta -= .1;
break;
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
// old_x = x;
// old_y = y;
// glutPostRedisplay();
}
void clickedmotion(int x, int y)
{
//spin_x = x - old_x;
//spin_y = y - old_y;
//glutPostRedisplay();
}
void text(char* string)
{
char* p;
for (p = string; *p; p++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *p);
}
Try moving the four glLightfv calls to the top of display... glLightfv is very sensitive to where it's called in relation to matrix manipulation (rotate, scale, lookat, &c)
Note also that if the lights don't move, you probably don't need to call glLightfv every frame. You can position the light once and then leave it alone.
A comment about the original question: I find it's easy to get confused and think that because the view is rotating or moving around the scene, that the lights need to move as well. it's just a case of mixing up two equally valid ways of thinking about the scene transformations. You can think of the camera/eye as fixed, and transform the scenery around it, in which case clearly you want the lighting to be fixed. Or you can think of the scene being fixed in place and move the camera (and lights) around it. It all comes out to the same OpenGL transformations in the end, but different problems lend themselves to thinking about it one way or the other. It helps to always be consistent about how you think about it; makes it easier to get the transformations right.
A comment about the original question: I find it's easy to get confused and think that because the view is rotating or moving around the scene, that the lights need to move as well. it's just a case of mixing up two equally valid ways of thinking about the scene transformations. You can think of the camera/eye as fixed, and transform the scenery around it, in which case clearly you want the lighting to be fixed. Or you can think of the scene being fixed in place and move the camera (and lights) around it. It all comes out to the same OpenGL transformations in the end, but different problems lend themselves to thinking about it one way or the other. It helps to always be consistent about how you think about it; makes it easier to get the transformations right.
Measure twice, cut once, curse three or four times.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Origin problems - rotating shapes | frances_farmer | 1 | 2,222 |
Feb 10, 2007 08:08 AM Last Post: unknown |
|
| Rotating a point in 3D | Joseph Duchesne | 1 | 2,243 |
Dec 19, 2006 12:43 PM Last Post: unknown |
|
| rotating objects | ferum | 11 | 3,541 |
Dec 19, 2005 02:28 PM Last Post: ferum |
|
| rotating... | MACnus | 4 | 3,388 |
Mar 25, 2005 07:46 PM Last Post: phydeaux |
|
| OpenGl and Rotating images | DukeLeto | 6 | 3,391 |
Oct 21, 2004 11:21 AM Last Post: PowerMacX |
|

