OpenGL照明不会对某些形状起作用

时间:2015-03-08 03:25:40

标签: opengl lighting

我正在尝试两种制作立方体的方法。问题是,如果我手动绘制立方体,则不会显示照明。我是初学者,所以这可能是微不足道的,但我不知所措。在我的窗口中显示的是:

enter image description here

显示阴影的立方体是我用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();
}

2 个答案:

答案 0 :(得分:1)

自从我涉足openGL以来已经有很长一段时间了。我认为你需要为立方体的每一面定义法线。

  

OpenGL支持固定数量的灯光。可用的数量取决于实现,但始终至少为3.转到glEnable(GL_CULL_FACE)之后的行;并添加以下内容:

     

//启用lighting nullglEnable(GL_LIGHTING); nullglEnable(GL_LIGHT0);

     

编译时,你会发现这并没有真正发挥作用。 灯亮,但尚未使用。原因是OpenGL需要知道它正在照明的每个顶点的“正常”。 OpenGL仅在出于效率原因的情况下在顶点处进行光照计算。

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

重复其他方面。

答案 1 :(得分:1)

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

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

...