简体   繁体   English

Java OpenGL顶点缓冲区对象不起作用

[英]Java OpenGL Vertex Buffer Objects not working

I tried to implement Open GLs Vertex Buffer Objects the first time, and all i get is a black screen. 我第一次尝试实现Open GLs Vertex Buffer Objects,而我得到的只是一个黑屏。

I tried it with glOrtho instead of glPerspective, but it didnt work as well. 我使用glOrtho而不是glPerspective进行了尝试,但效果也不佳。

thanks for helping 谢谢你的帮助

Heres my code: 这是我的代码:

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

You have depth-testing enabled, but you don't clear the z-buffer once per frame using glClear (at the beginning of your render method). 您已启用深度测试,但不会使用glClear (在render方法的开头)每帧清除一次z缓冲区。 Same for clearing the color buffer. 与清除颜色缓冲区相同。

EDIT: Also, initGl() seems to be never called? 编辑:而且, initGl()似乎从未被调用过?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM