我有一个glutSolidTeapot(根据opengl.org自动生成其表面法线)和一个发出散射光的光源。问题出在我尝试旋转茶壶时:似乎光源也在旋转,而不是保持在我定义的位置(它基本上跟随茶壶)。如您在我的代码中看到的那样,我仅在初始化时修改照明位置,因此它不会受到glRotatef()的影响,因为它在设置照明位置之后被调用。
尽管花了许多时间来尝试解决此问题,但我真的不知道这种行为可以归因于什么。
粘贴glEnable(GL_NORMALIZE);在初始化中也不能解决问题。
我认为所需的输出应该是一个右侧有光泽的茶壶(因为光线来自该方向),无论茶壶旋转了什么角度。
如果要测试我的代码,请按Space旋转茶壶。
#include <math.h>
#include <stdlib.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void onInitialization( ) { //creating the light source
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
GLfloat diffuse[]={0.8, 0.8, 0.8, 1.0};
GLfloat pos[]={0.5, 0.0, 0.8, 0.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glRotatef(-90, 1, 0, 0); //we want to see the top of the teapot
}
void onDisplay( ) {
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//rotating on every frame (for testing purposes only)
glRotatef(5, 0,1,0);
glutSolidTeapot(0.4);
glFinish();
glutSwapBuffers();
}
void onKeyboard(unsigned char key, int x, int y) {
if (key==32){ //do rotation upon hitting the Space key
glutPostRedisplay();
}
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Teapot");
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
onInitialization();
glutDisplayFunc(onDisplay);
glutKeyboardFunc(onKeyboard);
glutMainLoop();
return 0;
}
最佳答案
我认为改变
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
至
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
是一个很好的起点。
关于opengl - 如何在旋转对象时在OpenGL中固定光源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1757958/