简体   繁体   English

在OpenGL中为3D对象添加光源

[英]Adding a Light Source to 3D objects in OpenGL

I was wondering if anyone could help me figure out how to add a light source to my 3D objects. 我想知道是否有人可以帮助我弄清楚如何为3D对象添加光源。 I have four objects that are rotating and I want the light source to be at a fixed position, and I want to be able to see lighting on the object. 我有四个正在旋转的对象,并且我希望光源在固定的位置,并且希望能够看到该对象上的照明。

I tried doing this (********): 我尝试这样做(********):

//*******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);

However when I do this and I run the entire program, my objects turn gray, and at certain points in the rotation they turn white. 但是,当我这样做并运行整个程序时,我的对象变为灰色,并且在旋转的某些点上它们变为白色。 And this isnt what I want. 这不是我想要的。 I want to keep my colorful objects, but I want to be able to see the light source on them. 我想保留彩色的物体,但我希望能够看到它们上的光源。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Also let me know if you need to see more of my code to figure out the issue. 如果需要查看更多我的代码以解决问题,也请告诉我。 Thanks 谢谢

When lighting ( GL_LIGHTING ) is enabled, then the color is taken from the material parameters ( glMaterial ). 启用照明( GL_LIGHTING )后,颜色将从材料参数( glMaterial )中获取。

If you still want to use the current color, the you have to enable GL_COLOR_MATERIAL and to set the color material paramters ( glColorMaterial ): 如果仍然要使用当前颜色,则必须启用GL_COLOR_MATERIAL并设置颜色材料参数( glColorMaterial ):

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

See also Basic OpenGL Lighting . 另请参见基本OpenGL照明


But note, that drawing by glBegin / glEnd sequences, the fixed function pipeline matrix stack and fixed function pipeline per vertex light model, is deprecated since decades. 但是请注意,使用glBegin / glEnd序列进行glBegin ,固定功能管线矩阵堆栈和每个顶点光模型的固定功能管线自数十年来就已弃用。 Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering. 阅读有关固定功能管线的信息,并参见“ 顶点规范”和“ 着色器”以了解最新的渲染方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM