2010-01-17 111 views
2

我試圖首次實現Open GL頂點緩衝區對象,我得到的只是一個黑屏。Java OpenGL頂點緩衝區對象不起作用

我試着用glOrtho代替glPerspective,但它沒有工作。

感謝幫助

繼承人我的代碼:

public class VBufferTest { 

public static final int WIN_WIDTH = 640; 
public static final int WIN_HEIGHT = 480; 
public int vBufferId; 

public static void main(String args[]){ 
    VBufferTest foo = new VBufferTest(); 
    foo.initLWJGLFrame(); 
    foo.initBuffer(); 
    while (!Display.isCloseRequested()){ 
    foo.render(); 
    } 
} 

public void initLWJGLFrame(){ 
    try { 
     DisplayMode[] possible = Display.getAvailableDisplayModes(); 
     DisplayMode chosen = null; 
     for (int i = 0; i < possible.length; i += 1){ 
      if (possible[i].getWidth() == WIN_WIDTH && possible[i].getHeight() == WIN_HEIGHT){ 
      chosen = possible[i];   
      break; 
      } 
     } 
     if (chosen != null){ 
      Display.setDisplayMode(chosen); 
      Display.setTitle("TestFrame1"); 
      Display.create(); 

     } 
     else { 
      throw new LWJGLException("Couldn't find the appropriate display mode."); 
     } 
     } 
     catch (LWJGLException e){ 

     } 
} 

public void initGL(){ 
    GL11.glEnable(GL11.GL_DEPTH_TEST); 
    GL11.glEnable(GL11.GL_TEXTURE_2D); 
    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY); 
    GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY); 
    GL11.glClearColor(104.f/255.0f, 136.0f/255.0f, 252.0f/255.0f, 1.0f); 
    GL11.glMatrixMode(GL11.GL_PROJECTION); 
    GL11.glLoadIdentity(); 
    GLU.gluPerspective(50.0f, Display.getDisplayMode().getWidth()/Display.getDisplayMode().getHeight(), 0.5f, 1000.0f); 

    GL11.glViewport(0, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight()); 
    GL11.glMatrixMode(GL11.GL_MODELVIEW); 
    GL11.glLoadIdentity(); 
} 
    public void initBuffer(){ 
    FloatBuffer vertices = BufferUtils.createFloatBuffer(4*3); 
    vBufferId = genNewId(); 
    vertices.put(new float[]{-1.0f, -1.0f, 0.0f}); 
    vertices.put(new float[]{1.0f, -1.0f, 0.0f}); 
    vertices.put(new float[]{1.0f, 1.0f, 0.0f}); 
    vertices.put(new float[]{-1.0f, 1.0f, 0.0f}); 
    vertices.flip(); 
    bufferData(vBufferId, vertices); 
    } 

    public void render(){ 
    GL11.glColor3f(1.0f, 0.0f, 1.0f); 
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, this.vBufferId); 
    GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0); 
    GL11.glDrawArrays(GL11.GL_QUADS, 0, 4); 
    Display.update(); 
    } 

    public static int genNewId(){ 
     IntBuffer buffer = BufferUtils.createIntBuffer(1); 
     ARBVertexBufferObject.glGenBuffersARB(buffer); 
     return buffer.get(0); 
    } 


    public static void bufferData(int id, FloatBuffer buffer){ 
     ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id); 
     ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB); 
    } 
} 

回答

1

您已啓用了深度測試,但你不每幀一次使用glClear(之初清除z緩衝的render方法)。清除顏色緩衝區也一樣。

編輯:另外,initGl()似乎從來沒有被稱爲?

+0

它的一種混淆爲什麼我從來沒有調用initGL()... glClear幫助了我。謝謝 – Simiil 2010-01-17 12:32:05

相關問題