problem with my texture coordinates on a square.

Posted by Evan Kimia on Stack Overflow See other posts from Stack Overflow or by Evan Kimia
Published on 2010-12-25T01:43:31Z Indexed on 2010/12/25 1:54 UTC
Read the original article Hit count: 606

Im very new to OpenGL ES, and have been doing a tutorial to build a square. The square is made, and now im trying to map a 256 by 256 image onto it. The problem is, im only seeing a very zoomed in portion of this bitmap; Im fairly certain my texture coords are whats wrong here. Thanks!

package se.jayway.opengl.tutorial;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

public class Square {
// Our vertices.
private float vertices[] = {
          -1.0f,  1.0f, 0.0f,  // 0, Top Left
          -1.0f, -1.0f, 0.0f,  // 1, Bottom Left
           1.0f, -1.0f, 0.0f,  // 2, Bottom Right
           1.0f,  1.0f, 0.0f,  // 3, Top Right
    };

//Our texture.
private float texture[] = {
          0.0f,  0.0f, 0.0f,
          0.0f, 1.0f, 0.0f, 
           1.0f, 1.0f, 0.0f, 
           1.0f,  0.0f, 0.0f, 
};

// The order we like to connect them.
private short[] indices = { 0, 1, 2, 0, 2, 3 };

// Our vertex buffer.
private FloatBuffer vertexBuffer;

// Our index buffer.
private ShortBuffer indexBuffer;

//texture buffer.
private FloatBuffer textureBuffer;

//Our texture pointer.
private int[] textures = new int[1];

public Square() {
    // a float is 4 bytes, therefore we multiply the number if 
    // vertices with 4.
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    // a float is 4 bytes, therefore we multiply the number of 
    // vertices with 4.
    ByteBuffer tbb = ByteBuffer.allocateDirect(texture.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    textureBuffer = tbb.asFloatBuffer();
    textureBuffer.put(texture);
    textureBuffer.position(0);

    // short is 2 bytes, therefore we multiply the number if 
    // vertices with 2.
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);



}

/**
 * This function draws our square on screen.
 * @param gl
 */
public void draw(GL10 gl) {
    // Counter-clockwise winding.
    gl.glFrontFace(GL10.GL_CCW);
    // Enable face culling.
    gl.glEnable(GL10.GL_CULL_FACE);
    // What faces to remove with the face culling.
    gl.glCullFace(GL10.GL_BACK);
    //Bind our only previously generated texture in this case
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    // Enabled the vertices buffer for writing and to be used during 
    // rendering.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    //Enable texture buffer array
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // Specifies the location and data format of an array of vertex
    // coordinates to use when rendering.
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);


    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, 
            GL10.GL_UNSIGNED_SHORT, indexBuffer);

    // Disable the vertices buffer.
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    //Disable the texture buffer.
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    // Disable face culling.
    gl.glDisable(GL10.GL_CULL_FACE);
}
/**
 * Load the textures
 * 
 * @param gl - The GL Context
 * @param context - The Activity context
 */
public void loadGLTexture(GL10 gl, Context context) {
    //Get the texture from the Android resource directory
    InputStream is = context.getResources().openRawResource(R.drawable.test);
    Bitmap bitmap = null;
    try {
        //BitmapFactory is an Android graphics utility for images
        bitmap = BitmapFactory.decodeStream(is);

    } finally {
        //Always clear and close
        try {
            is.close();
            is = null;
        } catch (IOException e) {
        }
    }

    //Generate one texture pointer...
    gl.glGenTextures(1, textures, 0);
    //...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    //Create Nearest Filtered Texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    //Clean up
    bitmap.recycle();
}

}

© Stack Overflow or respective owner

Related posts about android

Related posts about opengl