OpenGL 灯光不工作

发布于 2024-11-10 16:11:01 字数 4223 浏览 3 评论 0原文

我使用这段代码使用 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

有谁知道我在这里做错了什么?这是示例图片: 无照明 灯光

I am using this code to draw different cubes with different colors using the 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();

It draws the cube fine and the color is perfect, but as soon as I add light to it, everything turns white and looks horrible. Here is my lighting code:

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

Does anyone know what I am doing wrong here? Here is come example pictures:
No Lighting
Lighting

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2024-11-17 16:11:01

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

法线矢量垂直于您的表面。因此,例如,对于您的第一个四边形:

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)

You need to define the normal vector to your quads with glNormal3f. The normal vector is used for lighting calculations.

The normal vector is perpendicular to your surface. So, for example, with your first quad:

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)

The y coordinate is constant, so a normal vector perpendicular to this surface could
be (0, 1, 0) or (0, -1, 0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文