JOGL绘制VBO黑屏

时间:2018-01-20 18:18:20

标签: java opengl jogl

我开始尝试使用JOGL绘制VBO。在此之前,我在glBegin和glEnd的帮助下绘制,一切正常。然后我看到只有黑屏。可能是什么问题呢?我在某处读到使用VBO进行绘图需要着色器。是这样吗? 代码:

public class Main implements GLEventListener {

       public static DisplayMode dm, dm_old;
       private GLU glu = new GLU();
       private float xrot,yrot,zrot;
       private int texture;
       Texture t;
       @Override
       public void display(GLAutoDrawable drawable) {
          final GL2 gl = drawable.getGL().getGL2();
          gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
          gl.glLoadIdentity(); // Reset The View
          gl.glTranslatef(0f, 0f, -5.0f);

          gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);

          final float[] coordData = {
                  0, 0, //
                  1, 0, //
                  0, 1, //
          };
          final float[] vertices = {
                  -1.0f, -1.0f, 1.0f,
                  1.0f, -1.0f, 1.0f,
                  -1.0f, 1.0f, 1.0f,
          };
          // Setup the vertices into the buffer
          FloatBuffer verts = Buffers.newDirectFloatBuffer(vertices.length);
          verts.put(vertices).position(0);

          // Setup the texture coordinates
          FloatBuffer coords = Buffers.newDirectFloatBuffer(coordData.length);
          coords.put(coordData).position(0);

          gl.glVertexPointer(3, GL2.GL_FLOAT, 0, verts);
          gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, coords);
          gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 3);

          //change the speeds here
          xrot += 5f;
       }

       @Override
       public void init(GLAutoDrawable drawable) {
          final GL2 gl = drawable.getGL().getGL2();
          gl.glShadeModel(GL2.GL_SMOOTH);
          gl.glClearColor(0f, 0f, 0f, 0f);
          gl.glClearDepth(1.0f);
          gl.glEnable(GL2.GL_DEPTH_TEST);
          gl.glDepthFunc(GL2.GL_LEQUAL);
          gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);

          gl.glEnable(GL2.GL_TEXTURE_2D);
          try {
             File im = new File("/home/congard/pic/t.jpeg");
             t = TextureIO.newTexture(im, true);
             texture= t.getTextureObject(gl);

          }catch(IOException e){
             e.printStackTrace();
          }
       }

       @Override
       public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
          // TODO Auto-generated method stub
       }

       public static void main(String[] args) {
            // some code
       }
}

1 个答案:

答案 0 :(得分:3)

您没有为顶点和纹理坐标启用客户端功能。见Client-Side Vertex Arrays  和glEnableClientState

将以下内容添加到您的代码中:

gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);

gl.glVertexPointer(3, GL2.GL_FLOAT, 0, verts);
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, coords);