简体   繁体   English

照明不适用于OpenGL-纯色

[英]Lighting not working on OpenGL - Solid color

The lighting doesn't work, the object appears with solid color. 照明不起作用,对象以纯色显示。 The color itself changes with the material or lighting parameters but no shadow or anything just solid grey. 颜色本身随材质或照明参数而变化,但没有阴影或仅是纯灰色的任何东西。

Here's part of the code: 这是代码的一部分:

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glFrontFace(GL_CCW);
glShadeModel(GL_FLAT);
glEnable(GL_LIGHTING);

GLfloat amb[] = {0.6, 0.6, 0.6, 1.0};
GLfloat dif[] = {0.8, 0.8, 0.8, 1.0};
GLfloat spec[] = {0.5, 0.5, 0.5, 1.0};
GLfloat pos[] = {0.0, 0.0, 30.0};

glEnable(GL_LIGHT0);    

glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
glLightfv(GL_LIGHT0, GL_SPECULAR, spec);
glLightfv(GL_LIGHT0, GL_POSITION, pos);


GLfloat co[4]={0.5, 0.5, 0.5, 0.5};
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 1.0 }; 
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, co);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

for(int i=0; i < numofTr; i++)
{       
glBegin(GL_TRIANGLES);
  glNormal3f(tr[i].v1->n.x, tr[i].v1->n.y, tr[i].v1->n.z);
  glVertex3f(tr[i].v1->x, tr[i].v1->y, tr[i].v1->z);    
  glNormal3f(tr[i].v2->n.x, tr[i].v2->n.y, tr[i].v2->n.z);
  glVertex3f(tr[i].v2->x, tr[i].v2->y, tr[i].v2->z);    
  glNormal3f(tr[i].v3->n.x, tr[i].v3->n.y, tr[i].v3->n.z);
  glVertex3f(tr[i].v3->x, tr[i].v3->y, tr[i].v3->z);    
glEnd();
}

I don't know what I'm missing, the normals look good when displayed along with the object. 我不知道自己缺少什么,与对象一起显示时法线看起来不错。

You are using dim light on a gray object. 您正在灰色物体上使用暗光。 Obviously the result is gray. 显然结果是灰色的。

If you mean that faces are flat shaded, that's because of glShadeModel(GL_FLAT); 如果您是说脸部是平面阴影,那是因为glShadeModel(GL_FLAT); . Change it to GL_SMOOTH to enable interpolation. 将其更改为GL_SMOOTH以启用插值。

If the object is in 3D, I think that the fact you are only passing 3 floats as position might be relevant; 如果对象是3D,我认为您只需要传递3个浮点就可以了,因为位置可能很重要; default value is [0,0,1,0] , and the function expects four floats (4th being the W coordinate, which should in most cases be equal to 1, or 0 for lights in infinite distance). 默认值为[0,0,1,0] ,并且该函数期望有四个浮点数(第4个为W坐标,在大多数情况下应等于1,对于无限距离的灯光应等于0)。

Also double check that the data you give to glVertex and glNormal is correct. 还要仔细检查您提供给glVertexglNormal的数据是否正确。 When rendering a sphere at 0,0,0, the normal of every vertex is the normalized position of such vertex (only holds for spheres, obviously). 在0,0,0处渲染球体时,每个顶点的法线就是该顶点的归一化位置(显然,仅适用于球体)。


As a side note, this code is obviously outdated, and as a disclaimer I would ditch it alltoghether, if you are only learning, and switch to something that's relatively modern and uses programmable pipeline. 附带说明一下,这段代码显然已经过时了,作为免责声明,如果您只是学习而切换到相对较新的并且使用可编程管道的内容,那么我将全部放弃。

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

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