2013-07-08 168 views
0

我想了解頂點緩衝對象。我已經能夠在二維空間中成功渲染一個四元組(在初始化過程中使用glOrtho),但是在使用gluPerspective渲染時我無法使用VBOs。使用頂點緩衝對象與gluPerspective

我正在使用Java和LWJGL,並附上我的代碼如下。目前,除了黑色清晰之外,沒有任何東西可以呈現在窗戶上

public class GameWindow { 
    // Height and width of the viewport 
    private final int WIDTH = 800; 
    private final int HEIGHT = 600; 

    private final float zNear = 1f; 
    private final float zFar = 1000f; 

    long lastFrame, lastFps; 
    int fps; 

    public void start() { 
     try { 
      Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); 
      Display.create(); 
     } catch (LWJGLException e) { 
      e.printStackTrace(); 
     } 
     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     gluPerspective(90, WIDTH/HEIGHT, zNear, zFar); 
     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 

     float[] quadCoords = { 
       100, 100, -1, 
       300, 100, -1, 
       300, 300, -1, 
       100, 300, -1 
     }; 

     float[] colorCoords = { 
       1, 0, 0, 
       0, 1, 0, 
       0, 0, 1, 
       1, 0, 1 
     }; 

     FloatBuffer vertexData = BufferUtils.createFloatBuffer(4 * 3); 
     vertexData.put(quadCoords); 
     vertexData.flip(); 

     FloatBuffer colorData = BufferUtils.createFloatBuffer(4 * 3); 
     colorData.put(colorCoords); 
     colorData.flip(); 

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

     int vboColor = glGenBuffers(); 
     glBindBuffer(GL_ARRAY_BUFFER, vboColor); 
     glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW); 
     glBindBuffer(GL_ARRAY_BUFFER, 0); 

     getDelta(); 
     lastFps = TimeUtil.getTime(); 

     while (!Display.isCloseRequested()) { 
      int delta = getDelta(); 
      updateFPS(); 
      glClear(GL_COLOR_BUFFER_BIT); 

      glBindBuffer(GL_ARRAY_BUFFER, vboVertex); 
      glVertexPointer(3, GL_FLOAT, 0, 0L); 

      glBindBuffer(GL_ARRAY_BUFFER, vboColor); 
      glColorPointer(3, GL_FLOAT, 0, 0L); 

      glEnableClientState(GL_VERTEX_ARRAY); 
      glEnableClientState(GL_COLOR_ARRAY); 
      glDrawArrays(GL_QUADS, 0, 4); 
      glDisableClientState(GL_COLOR_ARRAY); 
      glDisableClientState(GL_VERTEX_ARRAY); 

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

    private int getDelta() { 
     long time = TimeUtil.getTime(); 
     int delta = (int) (time - lastFrame); 
     lastFrame = time; 

     return delta; 
    } 

    public void updateFPS() { 
     if (TimeUtil.getTime() - lastFps > 1000) { 
      Display.setTitle("FPS: " + fps); 
      fps = 0; 
      lastFps += 1000; 
     } 
     fps++; 
    } 

    public static void main(String[] args) { 
     GameWindow window = new GameWindow(); 
     window.start(); 
    } 
} 

回答

1

我認爲你的四邊形的頂點不在視野中。沒有設置視圖矩陣(例如通過gluLookAt),攝像機指向-Z軸。你問zNear和zFar是1.0和1000.0,所以z-座標-1.0和-1000.0之間的所有東西都會被繪製出來(zNear和zFar就像是相機的「距離」)。你還必須考慮到透視投影與正字法完全不同。我想說,請嘗試在Z軸移動你的頂點回來,就像這樣:

float[] quadCoords = { 
     100, 100, -500, 
     300, 100, -500, 
     300, 300, -500, 
     100, 300, -500 
}; 
0

你的近裁剪平面爲1,但你渲染所有的Z座標爲-1