LWJGL VBO JVM崩溃

时间:2013-07-15 17:12:23

标签: java opengl lwjgl

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

Cube.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();
    }
}

Plane.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台独立的计算机上测试了代码,每一台计算机都有相同的结果。

1 个答案:

答案 0 :(得分:0)

我相信你确实需要每个顶点都有一个法线。 (在上面评论之后,我挖掘了我用来验证这个的类似代码。)你只提供一个普通坐标,代码期望4(每个顶点一个)。 / p>