0

現在、LWJGL/OpenGL を使用して 2D のトップダウン ゲームを作成中ですが、頂点バッファー オブジェクトを使用してレンダリングした後、エンティティを描画した後、エンティティを移動させる際にいくつか問題があります。これは、レンダリング スレッドの 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();
}

そして、エンティティはこれらのメソッドを抽象スーパークラスから継承して、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);
}

この方法を使用した結果は次のとおりです (スプライトは、問題を示すために 1 分で描いた単なるプレースホルダーです) -クリック

さらに情報が必要な場合は、これを更新します。

前もって感謝します!

編集:クイックノート、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 値に効果的に更新されます。

4

1 に答える 1