Opengl立方体灯不正确
问题描述:
我编写了一个显示立方体的OpenGL程序。
但是光线不正确,因为当我旋转立方体时,并非所有面部都被正确点亮:有两个面部呈现黑色。
这是代码:
I wrote a OpenGL program that shows a cube.
But the light is not correct because when I rotate the cube, not all the faces are correctly lighted: there are two faces that appears black.
Here's the code:
#include<GL\glut.h><br />
#include<GL\GL.h>º<br />
<br />
float rotX = 0.0, rotY = 0.0;<br />
<br />
void light ()<br />
{<br />
static float Position [] = {0.0, 0.0, 1.0, 0.0};<br />
static float Diffuse [] = {0.8, 0.8, 0.8, 1.0};<br />
static float Ambient [] = {0.3, 0.3, 0.3, 1.0};<br />
static float Specular [] = {1.0, 1.0, 1.0, 1.0};<br />
<br />
glLightfv(GL_LIGHT0, GL_POSITION, Position);<br />
glLightfv(GL_LIGHT0, GL_DIFFUSE, Diffuse);<br />
glLightfv(GL_LIGHT0, GL_AMBIENT, Ambient);<br />
glLightfv(GL_LIGHT0, GL_SPECULAR, Specular);<br />
<br />
glEnable(GL_LIGHTING);<br />
glEnable(GL_LIGHT0);<br />
}<br />
<br />
void drawCube(void)<br />
{<br />
int i;<br />
<br />
GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */<br />
{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},<br />
{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };<br />
<br />
GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */<br />
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},<br />
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };<br />
<br />
GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */<br />
<br />
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;<br />
v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;<br />
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;<br />
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;<br />
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;<br />
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;<br />
<br />
for (i = 0; i < 6; i++) {<br />
glBegin(GL_QUADS);<br />
glNormal3fv(&n[i][0]);<br />
glVertex3fv(&v[faces[i][0]][0]);<br />
glVertex3fv(&v[faces[i][1]][0]);<br />
glVertex3fv(&v[faces[i][2]][0]);<br />
glVertex3fv(&v[faces[i][3]][0]);<br />
glEnd();<br />
}<br />
}<br />
<br />
void display ()<br />
{<br />
glClear(GL_COLOR_BUFFER_BIT);<br />
glClear(GL_DEPTH_BUFFER_BIT);<br />
<br />
glMatrixMode(GL_MODELVIEW);<br />
glLoadIdentity();<br />
<br />
float diffuse [] = {1.0, 1.0, 0.0};<br />
glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);<br />
<br />
glTranslatef(0.0,0.0,-5.0);<br />
glRotatef(rotX, 0.0, 1.0, 0.0);<br />
glRotatef(rotY, 1.0, 0.0, 0.0);<br />
<br />
drawCube();<br />
<br />
glFlush();<br />
}<br />
<br />
void reshape (int w, int h)<br />
{<br />
float aspect = (float)w/(float)h;<br />
<br />
glViewport(0, 0, w, h);<br />
<br />
glMatrixMode(GL_PROJECTION);<br />
glLoadIdentity();<br />
<br />
gluPerspective(60.0, aspect, 0.1, 10.0);<br />
}<br />
<br />
void special (int key, int, int)<br />
{<br />
switch (key)<br />
{<br />
case GLUT_KEY_UP: rotY -= 1.0; break;<br />
case GLUT_KEY_DOWN: rotY += 1.0; break;<br />
case GLUT_KEY_LEFT: rotX -= 1.0; break;<br />
case GLUT_KEY_RIGHT: rotX += 1.0; break;<br />
default: return;<br />
}<br />
<br />
glutPostRedisplay();<br />
}<br />
<br />
int main (int argc, char* argv [])<br />
{<br />
glutInit(&argc, argv);<br />
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE|GLUT_DEPTH);<br />
glutCreateWindow("Cube");<br />
glutReshapeWindow(640,480); <br />
<br />
glutDisplayFunc(display);<br />
glutReshapeFunc(reshape);<br />
glutSpecialFunc(special);<br />
<br />
glClearColor(0.0, 0.0, 0.0, 1.0);<br />
<br />
light();<br />
<br />
glEnable(GL_DEPTH_TEST);<br />
<br />
glutMainLoop();<br />
<br />
return 0;<br />
}
有人可以帮助我吗?
我尝试过:
我尝试纠正法线,然后从OpenGL读取CUBE.C教程。
Can someone help me?
What I have tried:
I tried to correct normals, and I read the CUBE.C tutorial from OpenGL.
答
更改遗漏曲面的坐标和/或尝试其他灯光位置。也许你的皮肤坐标是自下而上或有一些标志问题。
您的代码格式错误,带有一些HTML标签。
Change the coordinates of the missed surfaces and/or try other light position. Maybe the coordinates of your skin are bottom up or have some sign issues.
Your code is malformatted with some HTML tags.