使用OpenGL / LWJGL使用glDrawArrays绘制VBO的问题

时间:2014-06-05 00:58:38

标签: java opengl lwjgl vbo gldrawarrays

我在使用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;
}

1 个答案:

答案 0 :(得分:1)

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