Question

Currently I have a working system of VBO's where I have two buffers - one for position and one for color. However, I want to draw textures instead of colors. How can I draw textures over my VBO position buffer? This is in Java and LWJGL, but an example in any language helps.

Was it helpful?

Solution 2

Here is a simple code snippet to initialize and render a cube with a texture. Uses Slick2D library.

int vertexHandle;
int textureHandle;

private void init() throws IOException {
    Camera.init();
    texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("test.png"));
    this.initialize3D();
    vertexHandle = GL15.glGenBuffers();
    FloatBuffer positionData = BufferUtils.createFloatBuffer(72);
    // Initalize position data.
    positionData.flip();

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,vertexHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER,positionData,GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,0);

    FloatBuffer textureData = BufferUtils.createFloatBuffer(72);
    // Initialize texture data.
    textureBuffer.flip();

    textureHandle = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, textureHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, textureData, GL15.GL_STATIC_DRAW);
    GL11.glTexCoordPointer(3, GL11.GL_FLOAT, 0, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
}

public void render() {
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glLoadIdentity();
    texture.bind();
    GL11.glPushMatrix();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexHandle);
    GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, textureHandle);
    GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
    GL11.glDrawArrays(GL11.GL_QUADS, 0, 24);
    GL11.glPopMatrix();
}

In addition you may need to integrate parts of this method to initialize 3D rendering:

public void initialize3D() {
    GL11.glEnable(GL11.GL_TEXTURE_2D); // Allows 2D textures.
    GL11.glShadeModel(GL11.GL_SMOOTH); // Smoother textures.
    //GL11.glClearColor(0.4f,0.6f,1.0f,0.0f); // BG color. 6698FF
    GL11.glClearDepth(1.0); // Buffer depth, allows objects to draw over things behind them.
    GL11.glEnable(GL11.GL_DEPTH_TEST); // Depth testing (see above).
    GL11.glDepthFunc(GL11.GL_LEQUAL); // Type of depth testing.

    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    //GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
    GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

    GL11.glMatrixMode(GL11.GL_PROJECTION); // Sets matrix mode to displaying pixels.
    GL11.glLoadIdentity(); // Loads the above matrix mode.

    // Sets default perspective location.                       Render Distances: Min   Max
    GLU.gluPerspective(45.0f,(float)Display.getWidth()/(float)Display.getHeight(),0.1f,300.0f);

    GL11.glMatrixMode(GL11.GL_MODELVIEW); // Sets the matrix to displaying objects.
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT,GL11.GL_NICEST); // Something unimportant for quality.
}

OTHER TIPS

I want to add textures to my VBO. How would I go about doing that?

You don't. At least you're not adding texture images to the VBO. What you add is a new attribute, called the texture coordinate, that assigns each vertex the location of an texture image.

The texture itself is an independent object, created using glGenTextures, glBindTexture and glTexImage….

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top