2013-08-19 192 views
0

我目前正在使用LWJGL/OpenGL創建一個2D自上而下的遊戲,但我在繪製實體後讓它們移動,已使用頂點緩衝區對象呈現。這是渲染線程的run()方法,以設置方法一起:LWJGL/OpenGL頂點緩衝區對象

// 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(); 
} 

和實體繼承一個抽象超這些方法來設置駐維也納各組織,並提請在呈現實體(drawEntities方法線程簡單地調用該方法,與實體更新方法(見下文),而setUpEntities調用設置方法):

// 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); 
} 

下面是使用這種方法的結果(精靈只是一個佔位符我在一分鐘內展示畫問題) - 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值。

+0

你需要清除緩存之間的幀渲染 – arynaq

+0

*捂臉*我知道這會是愚蠢的東西一樣,得益於哈哈,它現在固定。 – ACraftyMarmoset

回答

1

這是因爲您沒有清除緩衝區。

您需要清除緩衝區或新的渲染數據將被添加到舊數據。

要清除緩衝區,您需要執行以下操作。

glClear(GL_COLOR_BUFFER_BIT); 

如果您使用的是GL_DEPTH_TEST,那麼請記得清除它。 (這更多的只是讓你知道)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
+0

是啊,謝謝哈哈,我簡直忘了在渲染循環中添加那行......不會再犯這個錯誤了。 – ACraftyMarmoset

+0

適用於我們大多數人! ;) – Vallentin

相關問題