Drawing textures using VBO

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Drawing textures using VBO

yuanxd
I was using this code to render my textures (2d texture, rendered as a rectangle, ortho2D projection):

public void render(GL2 gl) {
          gl.glBindTexture(GL2.GL_TEXTURE_2D, idTexture);
          gl.glBegin(GL2.GL_TRIANGLE_FAN);
                gl.glTexCoord2d(0, 1);
                gl.glVertex2d(0, 0);
                gl.glTexCoord2d(1, 1);
                gl.glVertex2d(width, 0);
                gl.glTexCoord2d(1, 0);
                gl.glVertex2d(width, height);
                gl.glTexCoord2d(0, 0);
                gl.glVertex2d(0, height);
            gl.glEnd();
}

           

It works fine, but I read this kind of implementation is outdated, so I tried to use it using VBOs

// Creating VBO

public void createVBO(GL2 gl) {
        // Rectangle
        int numVex = 4;
       
        FloatBuffer vex = Buffers.newDirectFloatBuffer(new float[]{
            0.0f, 0.0f, 0.0f,
            width, 0.0f, 0.0f,
            width, height, 0.0f,
            0.0f, height, 0.0f
        });
        FloatBuffer tex = Buffers.newDirectFloatBuffer(new float[]{
            0.0f, 1.0f,
            1.0f, 1.0f,
            1.0f, 0.0f,
            0.0f, 0.0f
        });
       
        gl2.glGenBuffers(1, vexVBO, 0);
        gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, vexVBO[0]);
        gl2.glBufferData(GL2.GL_ARRAY_BUFFER, numVex * 3 * Buffers.SIZEOF_FLOAT, vex, GL2.GL_STATIC_DRAW);
       
        gl2.glGenBuffers(1, texVBO, 0);
        gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, tex[0]);
        gl2.glBufferData(GL2.GL_ARRAY_BUFFER, numVex * 2 * Buffers.SIZEOF_FLOAT, tex, GL2.GL_STATIC_DRAW);
}

// Rendering

public void render(GL2 gl, int numVex) {
          gl.glBindTexture(GL2.GL_TEXTURE_2D, idTexture);
          gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
          gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vexVBO[0]);
          gl.glVertexPointer(4, GL2.GL_FLOAT, 0, 0l);
           
          gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
          gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, texVBO[0]);
          gl.glTexCoordPointer(3, GL2.GL_FLOAT, 0, 0l);

          gl.glDrawArrays(GL2.GL_TRIANGLE_FAN, 0, numVex);
}

And nothing is showed =\. Any help will be appreciated :)
Reply | Threaded
Open this post in threaded view
|

Re: Drawing textures using VBO

gouessej
Administrator
Hi

You should look at the examples in jogl-demos, especially those of the OpenGL red book. Look at the manual:
https://www.opengl.org/sdk/docs/man2/xhtml/glTexCoordPointer.xml
https://www.opengl.org/sdk/docs/man2/xhtml/glVertexPointer.xml

The "size" is probably wrong.

Edit.: Another example is here.
Julien Gouesse | Personal blog | Website