Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在OpenGL中将光源添加到三维对象_C++_Opengl_3d_Lighting_Opengl Compat - Fatal编程技术网

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

C++ 在OpenGL中将光源添加到三维对象,c++,opengl,3d,lighting,opengl-compat,C++,Opengl,3d,Lighting,Opengl Compat,我想知道是否有人能帮我弄清楚如何将光源添加到我的3D对象中。我有四个旋转的物体,我希望光源在一个固定的位置,我希望能够看到物体上的灯光 我试过这样做(*******): 但是,当我这样做并运行整个程序时,我的对象会变成灰色,在旋转的某些点上它们会变成白色。这不是我想要的。我想保留我的彩色物体,但我想能够看到它们上的光源 任何帮助都将不胜感激。如果您需要查看更多我的代码来解决问题,请告诉我。谢谢启用照明()时,颜色取自材质参数() 如果仍要使用当前颜色,则必须启用 和设置颜色材质参数(): 另见

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

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

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

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

启用照明()时,颜色取自材质参数()

如果仍要使用当前颜色,则必须启用 和设置颜色材质参数():

另见


但请注意,几十年来,由
glBegin
/
glEnd
序列绘制的固定功能管道矩阵堆栈和固定功能管道逐顶点光照模型一直被弃用。 阅读并查看和,了解最先进的渲染方式

//*******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);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);