OpenGL灯不工作

时间:2011-05-30 19:00:02

标签: java opengl lighting

我使用此代码使用LWJGL绘制不同颜色的不同立方体:

    GL11.glBegin(GL11.GL_QUADS);
        GL11.glColor3f(rcol.x, rcol.y, rcol.z); // Color Vector
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Right Of The Quad (Top)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Left Of The Quad (Top)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Bottom Left Of The Quad (Top)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);             // Bottom Right Of The Quad (Top)

        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Top Right Of The Quad (Bottom)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Top Left Of The Quad (Bottom)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Left Of The Quad (Bottom)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Right Of The Quad (Bottom)

        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Top Right Of The Quad (Front)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Top Left Of The Quad (Front)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Bottom Left Of The Quad (Front)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Bottom Right Of The Quad (Front)

        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Left Of The Quad (Back)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Right Of The Quad (Back)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Right Of The Quad (Back)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Left Of The Quad (Back)

        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Top Right Of The Quad (Left)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);     // Top Left Of The Quad (Left)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Left Of The Quad (Left)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Bottom Right Of The Quad (Left)

        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Right Of The Quad (Right)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Top Left Of The Quad (Right)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Bottom Left Of The Quad (Right)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Right Of The Quad (Right)
    GL11.glEnd();

它将立方体绘制得很好,颜色也很完美,但只要我加上光线,一切都变成白色,看起来很糟糕。这是我的照明代码:

GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_LIGHT0);

float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };  // Ambient Light Values
float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };      // Diffuse Light Values
float lightPosition[] = { 20.0f, 15.0f, 20.0f, 1.0f }; // Light Position
float lightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Light Position

ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder())
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, (FloatBuffer)temp.asFloatBuffer().put(lightSpecular).flip());   
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());         // Position The Light

有谁知道我在做错了什么?这是示例图片: No Lighting Lighting

1 个答案:

答案 0 :(得分:6)

您需要使用glNormal3f为您的四边形定义normal vector。法线向量用于照明计算。

法线向量垂直于表面。因此,例如,使用您的第一个四元组:

GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Right Of The Quad (Top)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Left Of The Quad (Top)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Bottom Left Of The Quad (Top)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);             // Bottom Right Of The Quad (Top)

y坐标是常数,因此垂直于该表面的法向量可以是 是(0, 1, 0)(0, -1, 0)