镜面照明不起作用

时间:2017-05-06 22:13:54

标签: c++ qt opengl 3d

我创建了一个3D场景,我添加了一个光源和一个3D盒子,它应该被光源照亮。 Ambient和Diffuse颜色似乎都能正常工作,但镜面反射颜色似乎没有正确计算。使用光源在相机前方移动相机时,没有镜面反射点。看得见。但是当我将相机移向立方体的顶部并观察原点(0,0,0)时,一个微弱的镜面反射点#39;可以在立方体的照明一侧看到。

我的问题如下:我做错了什么?

以下代码中的所有内容都发生在modelMatrix中,_nav成员管理相机移动。

以下是代码:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(_nav->getCamPosx(), _nav->getCamPosy(), _nav->getCamPosz(),
          _nav->getCamViewx(), _nav->getCamViewy(), _nav->getCamViewz(),
          _nav->getCamUpx(), _nav->getCamUpy(), _nav->getCamUpz());

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

GLfloat qaAmbientLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
GLfloat qaDiffuseLight[] = {0.8f, 0.8f, 0.8f, 1.0f};
GLfloat qaSpecularLight[] = {1.0f, 0.0f, 0.0f, 1.0f}; //red for testing
glLightfv(GL_LIGHT0, GL_AMBIENT, qaAmbientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, qaSpecularLight);

// Set the light position
GLfloat qaLightPosition[] = {0.5f, 0.5f, 1.2f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);

GLfloat cyan[] = {0.f, .8f, .8f, 1.f};
GLfloat white[] = {0.8f, 0.8f, 0.8f, 1.0f};
//GLfloat dark[] = {0.0, 0.0, 0.0, 1.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, cyan);
glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan);
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
GLfloat shininess[] = {80};
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);

glBegin(GL_QUADS);

    glNormal3f(0.0f, -1.0f, 0.0f);
    glVertex3f(0.1f, 0.0f, 0.1f);
    glVertex3f(1.0f, 0.0f, 0.1f);
    glVertex3f(1.0f, 0.0f, 1.0f);
    glVertex3f(0.1f, 0.0f, 1.0f);

    glNormal3f(0.0f, 0.0f, -1.0f);
    glVertex3f(0.1f, 0.0f, 0.1f);
    glVertex3f(1.0f, 0.0f, 0.1f);
    glVertex3f(1.0f, 1.0f, 0.1f);
    glVertex3f(0.1f, 1.0f, 0.1f);

    glNormal3f(-1.0f, 0.0f, 0.0f);
    glVertex3f(0.1f, 0.0f, 0.1f);
    glVertex3f(0.1f, 0.0f, 1.0f);
    glVertex3f(0.1f, 1.0f, 1.0f);
    glVertex3f(0.1f, 1.0f, 0.1f);

    //face that gets illuminated
    glNormal3f(0.0f, 0.0f, 1.0f);
    glVertex3f(1.0f, 0.0f, 1.0f);
    glVertex3f(0.1f, 0.0f, 1.0f);
    glVertex3f(0.1f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    glNormal3f(1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.1f);
    glVertex3f(1.0f, 0.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 0.1f);

    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.1f, 1.0f, 0.1f);
    glVertex3f(1.0f, 1.0f, 0.1f);
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(0.1f, 1.0f, 1.0f);

glEnd();

使用光源面向立方体侧:

Facing cube side with light source

立方体的顶视图,淡淡的红色是高光的点'':

Top view of the cube, the faint red is the specular 'dot'

0 个答案:

没有答案