这是我编写的用于显示3D金字塔并使用键盘旋转它的代码。我还提供了一个光源,但它随金字塔一起旋转。我希望光源保持固定。我该怎么办?我在CodeBlocks上进行了编程。

#include<windows.h>
#include<GL/glut.h>
#include<stdlib.h>

double rotate_y=0;
double rotate_x=0;
double rotate_z=0;

void specialkeys(int key, int x, int y)
{
if(key==GLUT_KEY_RIGHT)
    rotate_y-=5;
else if(key==GLUT_KEY_LEFT)
    rotate_y+=5;
else if(key==GLUT_KEY_UP)
    rotate_x+=5;
else if(key==GLUT_KEY_DOWN)
    rotate_x-=5;
else if(key==GLUT_KEY_PAGE_UP)
    rotate_z+=5;
else if(key==GLUT_KEY_PAGE_DOWN)
    rotate_z-=5;
glutPostRedisplay();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotate_x,1.0,0.0,0.0);
glRotatef(rotate_y,0.0,1.0,0.0);
glRotatef(rotate_z,0.0,0.0,1.0);

glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.0,0.0,0.0);
glVertex3f(-0.5,0.5,-0.5);
glEnd();

glBegin(GL_POLYGON);
glColor3f(   1.0,  0.0, 0.0 );
glVertex3f(  0.5, 0.5, 0.5 );
glVertex3f(  0.0,  0.0, 0.0 );
glVertex3f( -0.5,  0.5, 0.5 );
glEnd();

glBegin(GL_POLYGON);
glColor3f(  1.0,  0.0,  0.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.0,  0.0, 0.0);
glVertex3f( 0.5,  0.5, -0.5 );
glEnd();


glBegin(GL_POLYGON);
glColor3f(   1.0,  0.0,  0.0 );
glVertex3f( -0.5, 0.5,  0.5 );
glVertex3f( 0.0,  0.0,  0.0 );
glVertex3f( -0.5,  0.5, -0.5 );
glEnd();


glBegin(GL_POLYGON);
glColor3f(   1.0,  0.0,  0.0 );
glVertex3f(  0.5,  0.5,  0.5 );
glVertex3f(  0.5,  0.5, -0.5 );
glVertex3f( -0.5,  0.5, -0.5 );
glVertex3f( -0.5,  0.5,  0.5 );
glEnd();

glFlush();
glutSwapBuffers();
}

void init()
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lmodel_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}

int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("3D Pyramid!");
init();
glutDisplayFunc(display);
glutSpecialFunc(specialkeys);
glutMainLoop();
return 0;
}

最佳答案

光源位置也由模型视图矩阵转换。您必须使用正确的Modelview矩阵转换调用glLightfv(GL_LIGHT…, GL_POSITION, …);;在定义您的观点之后,但在应用模型转换之前。

09-07 04:24