Showing posts from February, 2009

How to load a texture in Android OpenGL ES

Here is how you can easily load a texture from any image format - PNG, BMP, JPG etc.. in OpenGL ES in Android. **Before you begin the code, you need to import your images into your project's /res/drawable/ folder. Just right click the project in eclipse and select import to do that.** //First setup the integer array to hold texture numbers which OpenGL generates int texture[] = new int[1]; //Generate and bind to the texture (gl is my GL10 object) gl.glGenTextures(1, texture, 0); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); //Setup the texture co-ordinates float texCoords[] = { 0.0f, 0.0f, 1.0f, 0.0f,.......}; FloatBuffer texcoords = FloatBuffer.wrap(texCoords); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texcoords); Bitmap wood = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.myimagefilenamewithoutextension); //Setup optional texture parameters

Turning on Depth Buffer makes everything disappear !

I finally got to use textures on Android using OpenGL ES, and I found that its turn I started using the Depth buffer for some proper rendering. The moment I uncommented gl.glEnable(GL10.GL_DEPTH_BUFFER); The object disappeared and became invisible. Not even a pixel of the object was visible. Then a colleague of mine gave me a hint about clearing buffers ! Uffff ! I corrected the line "gl.glClear(GL10.GL_COLOR_BUFFER_BIT);" --to-- "gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);" and everything was fine again !