1

经过大量的谷歌搜索后,在 LWJGL 中渲染 VBO 立方体时,我被这个问题难住了。基本上,当我启用法线时,JVM 会崩溃。这很可能与我在 Plane.java 中设置法线的方式有关。由于我仍在学习 VBO,我似乎不知道如何解决这个问题。你能看一下,让我知道我可能出错的地方吗?

立方体.java:

package net.contour.src.voxel;

import org.lwjgl.opengl.GL11;

public class Cube {
    public float x;
    public float y;
    public float z;

    float size = 1.0f;

    private Plane top;
    private Plane bottom;
    private Plane left;
    private Plane right;
    private Plane front;
    private Plane back;

    public Cube(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;

        this.top = new Plane(new float[]{
            size / 2, size / 2, size / 2,
            -size / 2, size / 2, size / 2,
            -size / 2, size / 2, -size / 2,
            size / 2, size / 2, -size / 2
        }, new float[]{
            0.0f, 1.0f, 0.0f
        });

        this.bottom = new Plane(new float[]{
            size / 2, -size / 2, size / 2,
            -size / 2, -size / 2, size / 2,
            -size / 2, -size / 2, -size / 2,
            size / 2, -size / 2, -size / 2
        }, new float[]{
            0.0f, -1.0f, 0.0f
        });

        this.front = new Plane(new float[]{
            size / 2, size / 2, -size / 2,
            -size / 2, size / 2, -size / 2,
            -size / 2, -size / 2, -size / 2,
            size / 2, -size / 2, -size / 2
        }, new float[]{
            0.0f, 0.0f, 1.0f
        });

        this.back = new Plane(new float[]{
            size / 2, size / 2, size / 2,
            -size / 2, size / 2, size / 2,
            -size / 2, -size / 2, size / 2,
            size / 2, -size / 2, size / 2
        }, new float[]{
            0.0f, 0.0f, -1.0f
        });

        this.left = new Plane(new float[]{
            -size / 2, size / 2, size / 2,
            -size / 2, size / 2, -size / 2,
            -size / 2, -size / 2, -size / 2,
            -size / 2, -size / 2, size / 2
        }, new float[]{
            -1.0f, 0.0f, 0.0f
        });

        this.right = new Plane(new float[]{
            size / 2, size / 2, -size / 2,
            size / 2, size / 2, size / 2,
            size / 2, -size / 2, size / 2,
            size / 2, -size / 2, -size / 2
        }, new float[]{
            1.0f, 0.0f, 0.0f
        });
    }

    public void render() {
        GL11.glPushMatrix();
        GL11.glTranslatef(x, y, z);

        top.render();
        bottom.render();
        front.render();
        back.render();
        left.render();
        right.render();

        GL11.glPopMatrix();
    }

    public void destroy() {
        top.destroy();
        bottom.destroy();
        front.destroy();
        back.destroy();
        left.destroy();
        right.destroy();
    }
}

平面.java:

package net.contour.src.voxel;

import static org.lwjgl.opengl.GL11.GL_COLOR_ARRAY;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL11.GL_NORMAL_ARRAY;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
import static org.lwjgl.opengl.GL11.glColorPointer;
import static org.lwjgl.opengl.GL11.glDisableClientState;
import static org.lwjgl.opengl.GL11.glDrawArrays;
import static org.lwjgl.opengl.GL11.glEnableClientState;
import static org.lwjgl.opengl.GL11.glNormalPointer;
import static org.lwjgl.opengl.GL11.glVertexPointer;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import static org.lwjgl.opengl.GL15.glBufferData;
import static org.lwjgl.opengl.GL15.glDeleteBuffers;
import static org.lwjgl.opengl.GL15.glGenBuffers;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;

public class Plane {
    float size = 0.5f;

    private final int amountOfVertices = 4;
    private final int vertexSize = 3;
    private final int colorSize = 3;
    private final int normalSize = 3;

    private FloatBuffer vertexData;
    private FloatBuffer colorData;
    private FloatBuffer normalData;

    private int vboVertexHandle;
    private int vboColorHandle;
    private int vboNormalHandle;

    public Plane(float[] v, float[] n) {
        vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(new float[] {
            v[0], v[1], v[2],
            v[3], v[4], v[5],
            v[6], v[7], v[8],
            v[9], v[10], v[11]
        });
        vertexData.flip();

        colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
        colorData.put(new float[] {
            1, 0, 0,
            1, 0, 0,
            1, 0, 0,
            1, 0, 0
        });
        colorData.flip();

        normalData = BufferUtils.createFloatBuffer(normalSize);
        normalData.put(new float[] {
                n[0], n[1], n[2]
        });
        normalData.flip();

        vboVertexHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
        glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        vboColorHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
        glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        vboNormalHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
        glBufferData(GL_ARRAY_BUFFER, normalData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    public void render() {
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
        glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);

        glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
        glColorPointer(colorSize, GL_FLOAT, 0, 0L);

        glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
        glNormalPointer(normalSize, GL_FLOAT, 0L);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glDrawArrays(GL_QUADS, 0, amountOfVertices);
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);
    }

    public void destroy() {
        glDeleteBuffers(vboVertexHandle);
        glDeleteBuffers(vboColorHandle);
    }
}

让我知道你们的想法。我已经在 3 台不同的计算机上测试了代码,每台计算机都有相同的结果。

4

1 回答 1

0

我相信您确实需要为每个顶点设置法线。 (在上面评论之后,我挖掘了类似的代码来验证这一点)。 您只提供一个法线坐标,代码需要 4 个(每个顶点一个)。

于 2013-07-15T17:35:06.087 回答