java - LWJGL/OpenGL 顶点缓冲区对象

标签 java opengl lwjgl

我目前正在使用 LWJGL/OpenGL 创建 2D、自上而下的游戏,但在使用 Vertex 渲染实体后,在绘制实体、让它们四处移动时遇到一些问题缓冲区对象。这是渲染线程的 run() 方法,以及 setUp 方法:

// Render thread
public void run() {
    setUpDisplay();
    setUpOpenGL();
    setUpEntities();
    while (isRunning) {
        getFPS();
        drawEntities();
        Display.update();
        Display.sync(60);
        if (Display.isCloseRequested()) {
            isRunning = false;
        }
    }
    destroyEntities();
    Display.destroy();
    System.exit(0);
}   

// Initialisation method for the display
private void setUpDisplay() {
    try {
        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.setTitle("Roguelike");
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
    }
}

// Initialisation method for OpenGL
private void setUpOpenGL() {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 640, 480, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    lastFrame = Main.getTime();
}

实体从抽象父类(super class)继承这些方法来设置VBO并绘制实体(渲染线程中的drawEntities方法简单地调用该方法和实体更新方法(见下文),而setUpEntities调用setUp方法):

// Method to initialise VBOs for a given entity
public void setUp() {
    vertexData = BufferUtils.createFloatBuffer(12);
    vertexData.put(getVertices());
    vertexData.flip();

    textureData = BufferUtils.createFloatBuffer(12);
    textureData.put(textureVertices);
    textureData.flip();

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

    vboTextureCoordHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vboTextureCoordHandle);
    glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

// Method to draw the entity
public void draw() {
    glBindTexture(GL_TEXTURE_2D, loadTexture(this.textureKey).getTextureID());

    glBindBuffer(GL_ARRAY_BUFFER, this.vboVertexHandle);
    glVertexPointer(2, GL_FLOAT, 0, 0L);

    glBindBuffer(GL_ARRAY_BUFFER, this.vboTextureCoordHandle);
    glTexCoordPointer(2, GL_FLOAT, 0, 0L);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

到目前为止,我一直在尝试在屏幕上移动单个实体以使翻译正常工作,但无济于事 - 我尝试使用实体的以下更新方法来更新其位置,但它会导致屏幕上绘制的实体副本的痕迹,这显然不是我想要的:

public void update(int delta) { // Updates the position of the object
    this.x += this.dx * delta;
    this.y += this.dy * delta;
    this.setBounds();
    this.vertexData.rewind();
    this.vertexData.put(getVertices());
    this.vertexData.flip();
    glBindBuffer(GL_ARRAY_BUFFER, this.vboVertexHandle);
    glBufferData(GL_ARRAY_BUFFER, this.vertexData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

这是使用此方法的结果( Sprite 只是我为了演示问题而画的一个占位符)- Click

非常感谢任何帮助,如果您需要更多信息,我会更新。

提前致谢!

编辑:快速说明,getVertices():

public float[] getVertices() {  // Returns array of vertices (for vertex VBO)
    float[] vertices = {    this.bounds[0], this.bounds[3], this.bounds[2], this.bounds[3],
                            this.bounds[2], this.bounds[1], this.bounds[2], this.bounds[1],
                            this.bounds[0], this.bounds[1], this.bounds[0], this.bounds[3] };
    return vertices;
}

setBounds() 如下:

public void setBounds() {
    this.bounds[0] = this.x;
    this.bounds[1] = this.y;
    this.bounds[2] = this.x + this.width;
    this.bounds[3] = this.y + this.height;
}

调用该方法时,它会有效地更新为新的 x/y 值。

最佳答案

那是因为你没有清除缓冲区。

您需要清除缓冲区,否则新渲染的数据将添加到旧数据上。

要清除缓冲区,您需要执行以下操作。

glClear(GL_COLOR_BUFFER_BIT);

如果您使用GL_DEPTH_TEST,请记住也清除它。 (这只是让你知道)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

关于java - LWJGL/OpenGL 顶点缓冲区对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18312346/

相关文章:

java - 微服务 - RestTemplate UnknownHostException

java - Spring 是否支持 SAP JCo 事务?

linux - glUseProgram() GL_INVALID_OPERATION 1282 在 Ubuntu Gnome 17.04 Intel HD4000 上

c++ - 在 OpenGL 中构建网格

java - opengl/lwjgl 的透明度问题

java - 服务器不直接响应我的命令

java - 从 android 操作系统中检索国家/地区列表

python - 使用 gluLookAt() 会导致对象旋转

java - LWJGL OpenGL glbegin 和其他内容已弃用

java - LWJGL - 黑屏