在OpenGL中将光源添加到三维对象



我想知道是否有人能帮我找出如何将光源添加到我的3D对象中。我有四个正在旋转的物体,我希望光源在固定的位置,我希望能够看到物体上的照明。

我试过这样做(********(:

//*******Initializing the light position
GLfloat pos[] = {-2,4,5,1};
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glMatrixMode(GL_MODELVIEW);   
//*******adding the light to the display method
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, pos);
// rectangle
glPushMatrix();
glTranslatef(0.0f, 2.5f, -8.0f);  
glRotatef(angleRectangle, 0.0f, 1.0f, 0.0f);  
drawRectangle();
glPopMatrix();
//small cylinder
glPushMatrix();
glTranslatef(0.0f, 2.0f, -8.0f);  
glRotatef(90, 1, 0, 0);
glRotatef(anglePyramid, 0.0f, 0.0f, 1.0f);
drawCylinder(0.2, 0.7);
glPopMatrix();
//big cylinder
glPushMatrix();
glTranslatef(0.0f, 1.5f, -8.0f); 
glRotatef(90, 1, 0, 0);
glRotatef(anglePyramid, 0.0f, 0.0f, 1.0f);
drawCylinder(0.7, 2.7);
glPopMatrix();
//pyramid
glPushMatrix();
glTranslatef(0.0f, -2.2f, -8.0f);  
glRotatef(180, 1, 0, 0);
glRotatef(anglePyramid, 0.0f, 1.0f, 0.0f);  
drawPyramid();
glPopMatrix();
glutSwapBuffers(); 
anglePyramid += k * 0.2f;  //- is CW, + is CCW
angleRectangle += -k * 0.2f;
}
//******* Then i added these to the main method
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

然而,当我这样做并运行整个程序时,我的对象会变灰,在旋转的某些点上,它们会变白。这不是我想要的。我想保留我的彩色物体,但我想能够看到它们上的光源。

如有任何帮助,我们将不胜感激。如果您需要查看更多我的代码来解决问题,请告诉我。感谢

启用照明(GL_LIGHTING(时,颜色取自材质参数(glMaterial(。

如果仍要使用当前颜色,则必须启用GL_COLOR_MATERIAL并设置色材参数(glColorMaterial(:

glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

请参见基本OpenGL照明。


但请注意,用glBegin/glEnd序列绘制固定函数流水线矩阵堆栈和固定函数流水线逐顶点光模型,几十年来一直不受欢迎。阅读有关固定函数管道的信息,并参阅顶点规范和着色器以了解最先进的渲染方式。

最新更新