0

我目前一直在学习 Vertex Arrays 和 VBO 的 b/c 我目前了解到它比“立即”渲染(如使用 glBegin)更好/更快,尽管这要容易得多。我做了一个小程序(实际上只是一个测试),它创建了一个按钮,当你将鼠标悬停在按钮上时,它会改变颜色,但是每当我启动它时,程序就会崩溃并显示以下错误消息:

A fatal error has been detected by the Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006875c490, pid=5708, tid=7148

JRE version: 7.0_21-b11
Java VM: Java HotSpot(TM) 64-Bit Server VM (23.21-b01 mixed mode windows-amd64 compressed oops)
Problematic frame:
C  [atio6axx.dll+0x20c490]  DrvPresentBuffers+0x1cb680

Failed to write core dump. Minidumps are not enabled by default on client versions of   Windows

 An error report file with more information is saved as:
C:\Users\UserNameHere\Desktop\Java\workspace\LWJGL Tutorials\hs_err_pid5708.log

If you would like to submit a bug report, please visit:
http://bugreport.sun.com/bugreport/crash.jsp
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.

我的代码

package testandothertutorials;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class VBOMenu {

int WIDTH = 640;
int HEIGHT = 480;
float button[] = {
        //Left bottom triangle
        30, 60, 0,
        30, 30, 0,
        60, 30, 0,
        //Right top triangle
        60, 30, 0,
        60, 60, 0,
        30, 60, 0
};

public VBOMenu() {
    try {
        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.setTitle("Vertex Arrays Demo");
        Display.create();
    } catch(LWJGLException e) {
        Display.destroy();
    }

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 640, 480, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    final int amountOfVertices = 4;
    final int colorSize = 4;
    final int vertexSize = 4;

    FloatBuffer firstVertexData = BufferUtils.createFloatBuffer(button.length);
    firstVertexData.put(button);
    firstVertexData.flip();

    FloatBuffer unselColorData = BufferUtils.createFloatBuffer(colorSize); //The color that will be set when the menu is launched
    unselColorData.put(new float[] {0, 1, 0});
    unselColorData.flip();

    FloatBuffer selColorData = BufferUtils.createFloatBuffer(colorSize); //The color that will be set when the mouse hovers over the button
    selColorData.put(new float[] {0, 2, 0});
    selColorData.flip();

    int firstVertexHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, firstVertexHandle);
    glBufferData(GL_ARRAY_BUFFER, firstVertexData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    int unselColorHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, unselColorHandle);
    glBufferData(GL_ARRAY_BUFFER, unselColorData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    int selColorHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, selColorHandle);
    glBufferData(GL_ARRAY_BUFFER, selColorData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);


    boolean isRunning = true;

    while(isRunning) {
        glClear(GL_COLOR_BUFFER_BIT);

        glBindBuffer(GL_ARRAY_BUFFER, firstVertexHandle);
        glVertexPointer(vertexSize, GL_FLOAT, 0, 0l);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);

        Display.update();
        Display.sync(60);

        if(Display.isCloseRequested())
            isRunning = false;
    }

    glDeleteBuffers(firstVertexHandle);

    Display.destroy();
}

public static void main(String[] args) {
    new VBOMenu();
}

}

4

1 回答 1

1

那是因为您告诉 OpenGL 您要使用颜色数组,因此当 OpenGL 尝试使用顶点和颜色缓冲区进行渲染时,它会失败,因为您没有绑定颜色缓冲区。

这就是你所缺少的。

glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(vertexSize, GL_FLOAT, 0, 0l);

您需要将 替换为vbo_color_handle您自己的颜色句柄 ID。

更好的例子

也只是通过查看您的代码,我可以看到您并不完全理解 VBO 的工作原理,所以这里有一个 VBO 存储三角形的顶点和颜色并渲染它的小例子!

创建 VBO。

这是您创建实际顶点和颜色缓冲区并将它们绑定到 VBO 的代码。

int vertices = 3;

int vertex_size = 3; // X, Y, Z
int color_size = 3; // R, G, B

FloatBuffer vertex_data = BufferTools.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, 1f, 0f, });
vertex_data.flip();

FloatBuffer color_data = BufferTools.createFloatBuffer(vertices * color_size);
color_data.put(new float[] { 1f, 0f, 0f, });
color_data.put(new float[] { 0f, 1f, 0f, });
color_data.put(new float[] { 0f, 0f, 1f, });
color_data.flip();

vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

渲染 VBO。

这是您需要调用的代码,以呈现 VBO。

glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);

glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(color_size, GL_FLOAT, 0, 0l);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLES, 0, vertices);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
于 2013-08-08T12:48:17.947 回答