1

我在让 VBO 参与我的游戏时遇到了一些困难。我环顾四周,检查了所有常见的错误,但似乎没有什么能让他们画画。

以下是按执行顺序排列的相关代码:

运行一次:

public void startLWJGL() {
    try {
        Display.setParent(game.getCanvas());
        Display.create();
    } catch (LWJGLException ex) {
        System.err.print("LWJGL failed to initialize.");
        System.exit(-1);
    }

    // Initialize
    GL11.glMatrixMode(GL11.GL_PROJECTION_MATRIX);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 800, 0, 600, 1, -1); // L, R, B, T, Near, Far
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glClearColor(0.01f,0.05f,0.1f,1f);

    GL11.glDisable(GL11.GL_DEPTH_TEST);
    //GL11.glDisable(GL11.GL_BLEND);

    GL11.glViewport(0, 0, 800, 600);

    Renderer.init();
}

public static void init() {
    BALL_GEO = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, BALL_GEO);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, makeBallGeometry(), GL15.GL_STATIC_DRAW);

    COLOR_BUF = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, COLOR_BUF);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, makeSolidColBuffer(1f, 0f, 1f, 1f, (CIRCLE_SEGMENTS + 2)), GL15.GL_STATIC_DRAW);
}

运行每一帧:

private void tick(long delta) {
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    Renderer.testRender();
}

    public static void testRender() {

    drawBall(new Ball(new Vector(200, 200), null, 50.0));

    // This is just test stuff that DOES work
    GL11.glColor4f(1f, 1f, 1f, 1f);
    GL11.glBegin(GL11.GL_QUADS);
    GL11.glVertex2f(100, 100);
    GL11.glVertex2f(100, 400);
    GL11.glVertex2f(200, 400);
    GL11.glVertex2f(200, 100);
    GL11.glEnd();

    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glPushMatrix();
    //GL11.glLoadIdentity();
    GL11.glTranslatef(400f, 200f, 1f);
    GL11.glScalef(0.5f, 0.5f, 0.5f);

    GL11.glColor4f(0f, 1f, 1f, 1f);
    GL11.glBegin(GL11.GL_QUADS);
    GL11.glVertex2f(100, 100);
    GL11.glVertex2f(100, 400);
    GL11.glVertex2f(200, 400);
    GL11.glVertex2f(200, 100);
    GL11.glEnd();

    GL11.glPopMatrix();

}

public static void drawBall(Ball o) {
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    float x = (float) o.getPos().x;
    float y = (float) o.getPos().y;
    float s = (float) o.getRadius();

    GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, BALL_GEO);
    GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, COLOR_BUF);
    GL11.glColorPointer(4, GL11.GL_FLOAT, 0, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    GL11.glPushMatrix();
    //GL11.glLoadIdentity();
    GL11.glTranslatef(x, y, 0f);
    GL11.glScalef(s, s, s);

    GL11.glDrawArrays(GL11.GL_TRIANGLE_FAN, 0, (CIRCLE_SEGMENTS + 2));

    GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
    GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glPopMatrix();
}

用英语术语来说,我在 2D 中渲染不同大小和位置的纯色圆圈。我用即时模式测试了几何,它工作得很好。这可能是非常愚蠢的事情,但我无法弄清楚我的生活是什么。我在另一台计算机上运行此代码,它吐出 0x05 错误硬崩溃,但在这台计算机上它根本不渲染。Ball 对象构造函数接受一个向量作为它的位置、一个速度向量和一个半径作为双精度数。

编辑:这里是那些询问的几何图形和颜色缓冲区代码:

private static FloatBuffer makeBallGeometry() {
    FloatBuffer buffer = BufferUtils.createFloatBuffer((CIRCLE_SEGMENTS + 2) * 3);

    float theta = 2f * (float) Math.PI / CIRCLE_SEGMENTS; // Divisions in Radians
    float tanFac = (float) Math.tan(theta); // Scalar for tangenial line
    float radFac = (float) Math.cos(theta); // Scalar for radial line
    float x = 50f;
    float y = 0f;
    float tx, ty;

    buffer.put(0f);
    buffer.put(0f);
    buffer.put(0f);
    for (int i = 0; i <= CIRCLE_SEGMENTS; i++) {
        buffer.put(x);
        buffer.put(y);
        buffer.put(0f);

        tx = -y; // Tangent
        ty = x;

        x += tx * tanFac; // Adjust over by tanFac
        y += ty * tanFac;

        x *= radFac; // Shrink by radFac
        y *= radFac;
    }

    return buffer;
}

private static FloatBuffer makeSolidColBuffer(float r, float g, float b, float a, int size) {
    FloatBuffer buffer = BufferUtils.createFloatBuffer(size * 4);
    for (int i = 0; i < size; i++) {
        buffer.put(r);
        buffer.put(g);
        buffer.put(b);
        buffer.put(a);
    }
    return buffer;
}
4

1 回答 1

1

正如Andon M. Coleman指出的那样,我的问题是我忽略了在缓冲区上调用 flip(),实际上并没有传递任何信息字节。在我返回它们之前,我将 buffer.flip() 添加到颜色和顶点缓冲区,现在一切似乎都正常工作。

于 2014-06-05T13:38:16.750 回答