OpenGL 光照不作用于某些形状

OpenGL lighting not acting on some shapes

我正在尝试两种制作立方体的方法。问题是,如果我手动绘制立方体,则不会显示灯光。我是初学者,所以这可能是微不足道的,但我很茫然。我的 window 中显示的是:

显示阴影的立方体是我用 GLUT 制作的立方体,另外两个是我通过绘制每个面手动制作的。这是我的显示方法:

public void display(GLAutoDrawable glautodrawable) {
            GL2 gl2 = glautodrawable.getGL().getGL2();
            gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
            gl2.glMatrixMode(GL2.GL_MODELVIEW);
                //              gl2.glLoadIdentity();
            gl2.glPushMatrix();

            gl2.glTranslatef(2f, -2f, -4f);
            gl2.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT, FloatBuffer.wrap(QA_WHITE));
            gl2.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE, FloatBuffer.wrap(QA_WHITE));
            gl2.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, FloatBuffer.wrap(QA_WHITE));
            gl2.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, FloatBuffer.wrap(new float[]{50f}));

            wallUnit(glautodrawable);
            gl2.glPopMatrix();

            gl2.glPushMatrix();
            gl2.glTranslatef(-2f, -2f, -10f);
            wallUnit(glautodrawable);
            gl2.glPopMatrix();

            gl2.glPushMatrix();
            gl2.glTranslatef(-4.5f, -2.5f, -10f);
            glut.glutSolidCube(2f);
            gl2.glPopMatrix();


            gl2.glFlush();

        }

还有我的 wallUnit() 方法(因为它应该被缩放、纹理化并变成一面墙……如果你想知道的话。)

private static void wallUnit(GLAutoDrawable glAutoDrawable) {
    GL2 gl2 = glAutoDrawable.getGL().getGL2();

    gl2.glBegin(gl2.GL_QUADS);    

    // Back face
    gl2.glColor3f(1,0,0);
    gl2.glVertex3f(-1.0f, -1.0f, -1.0f);            
    gl2.glVertex3f(1.0f, -1.0f, -1.0f);           
    gl2.glVertex3f(1.0f, 1f, -1.0f);  
    gl2.glVertex3f(-1.0f, 1f, -1.0f);        

    // Bottom face
    gl2.glColor3f(0,1,0);
    gl2.glVertex3f(-1.0f, -1.0f, -1.0f);
    gl2.glVertex3f(1.0f, -1.0f, -1.0f); 
    gl2.glVertex3f(1.0f, -1.0f, 1.0f);     
    gl2.glVertex3f(-1.0f, -1.0f, 1.0f);        

    // Right side
    gl2.glVertex3f(1.0f, -1.0f, -1.0f);          
    gl2.glVertex3f(1.0f, -1.0f, 1.0f);            
    gl2.glVertex3f(1.0f, 1.0f, 1.0f);         
    gl2.glVertex3f(1.0f, 1f, -1.0f);         

    // Top face
    gl2.glVertex3f(1.0f, 1.0f, 1.0f);        
    gl2.glVertex3f(1.0f, 1f, -1.0f);          
    gl2.glVertex3f(-1.0f, 1f, -1.0f);          
    gl2.glVertex3f(-1.0f, 1f, 1.0f);        

    // Left side
    gl2.glVertex3f(-1.0f, -1.0f, -1.0f);        
    gl2.glVertex3f(-1.0f, -1.0f, 1.0f);          
    gl2.glVertex3f(-1.0f, 1.0f, 1.0f);         
    gl2.glVertex3f(-1.0f, 1f, -1.0f);        

    // Front face
    gl2.glColor3f(1,0,0);
    gl2.glVertex3f(-1.0f, -1.0f, 1.0f);        
    gl2.glVertex3f(1.0f, -1.0f, 1.0f);         
    gl2.glVertex3f(1.0f, 1f, 1.0f);        
    gl2.glVertex3f(-1.0f, 1f, 1.0f);        

    gl2.glEnd();
}

好久没接触openGL了。我认为您需要为立方体的每一面定义法线。

OpenGL supports a fixed number of lights.  The number that are available depends on the implementation, but will always be at least 3.  Go to the line after glEnable(GL_CULL_FACE); and add the following:

// Enable lighting nullglEnable(GL_LIGHTING); nullglEnable(GL_LIGHT0);

When you compile, you'll discover that this does not really do anything different.  The light is on, but it's not being used, yet.  The reason is that OpenGL needs to know a "normal" for every vertex it is lighting.  OpenGL does its lighting computations at the vertices only for efficiency reasons.

https://www.cse.msu.edu/~cse872/tutorial3.html

例如。背板的法线(即垂直于平面的向量)为 <0, 0, -1>。

// Back face 
gl2.glColor3f(1,0,0);
gl2.glNormal3f(0.0f, 0.0f, -1.0f);
gl2.glVertex3f(-1.0f, -1.0f, -1.0f);
gl2.glVertex3f(1.0f, -1.0f, -1.0f);
gl2.glVertex3f(1.0f, 1f, -1.0f);
gl2.glVertex3f(-1.0f, 1f, -1.0f); 

对另一边重复。

您需要为四边形生成法线:

gl2.glBegin(gl2.GL_QUADS);    

// Back face
gl2.glColor3f(1,0,0);
gl2.glNormal3f(1.0f, 0.0f, 0.0f); // quad normal
gl2.glVertex3f(-1.0f, -1.0f, -1.0f);            
gl2.glVertex3f(1.0f, -1.0f, -1.0f);           
gl2.glVertex3f(1.0f, 1f, -1.0f);  
gl2.glVertex3f(-1.0f, 1f, -1.0f);

...