can't get texture to work

Posted by user583713 on Stack Overflow See other posts from Stack Overflow or by user583713
Published on 2012-11-27T17:02:36Z Indexed on 2012/11/27 17:03 UTC
Read the original article Hit count: 288

Filed under:
|

It been a while since I use android opengl but for what ever reason I get a white squre box and not the texture I what on the screen. Oh I do not think this would matter but just in case I put a linerlayout view first then the surfaceview on but anyway Here my code:

public class GameEngine {

private float vertices[];
private float textureUV[];

private int[] textureId = new int[1];

private FloatBuffer vertextBuffer;
private FloatBuffer textureBuffer;

private short indices[] = {0,1,2,2,1,3};
private ShortBuffer indexBuffer;

private float x, y, z;
private float rot, rotX, rotY, rotZ; 


public GameEngine() {

}

public void setEngine(float x, float y, float vertices[]){
    this.x = x;
    this.y = y;
    this.vertices = vertices;

    ByteBuffer vbb = ByteBuffer.allocateDirect(this.vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());

    vertextBuffer = vbb.asFloatBuffer();
    vertextBuffer.put(this.vertices);
    vertextBuffer.position(0);
    vertextBuffer.clear();


    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());

    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);
    indexBuffer.clear();
}



public void draw(GL10 gl){
    gl.glLoadIdentity();
    gl.glTranslatef(x, y, z);
    gl.glRotatef(rot, rotX, rotY, rotZ);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId[0]);

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


    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertextBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);



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


    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

}

public void LoadTexture(float textureUV[], GL10 gl, InputStream is) throws IOException{

    this.textureUV = textureUV;

    ByteBuffer tbb = ByteBuffer.allocateDirect(this.textureUV.length * 4);
    tbb.order(ByteOrder.nativeOrder());
    textureBuffer = tbb.asFloatBuffer();
    textureBuffer.put(this.textureUV);
    textureBuffer.position(0);
    textureBuffer.clear();

    Bitmap bitmap = null;

    try{
        bitmap = BitmapFactory.decodeStream(is);

    }finally{
        try{
            is.close();
            is = null;
            gl.glGenTextures(1, textureId,0);   
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId[0]);


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


            gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

            //Clean up
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId[0]);

            bitmap.recycle();
        }catch(IOException e){

        }

    }

}



public void setVector(float x, float y, float z){
    this.x = x;
    this.y = y;
    this.z = z;
}

public void setRot(float rot, float x, float y, float z){
    this.rot = rot;
    this.rotX = x;
    this.rotY = y;
    this.rotZ = z;
}

public float getZ(){
    return z;
}

}

© Stack Overflow or respective owner

Related posts about android

Related posts about opengl