user1234320
user1234320

Reputation: 71

OpenGL Black Textures on Ice Cream Sandwich

I'm creating a game using OpenGL ES 1.0. I recently updated my tablet to android 4.0.3 and now all my textures just show up black. I tried turning hardware acceleration on and off and it doesn't seem to make a difference.

Here is my code for loading textures.

        public void loadTexture(GL10 gl, Context context, int image, boolean has_alpha) {
            m_alpha = has_alpha;

            int [] textures = new int[1];
            gl.glDeleteTextures(1, textures, 0);
            gl.glGenTextures(1, textures, 0);
            m_textureid = textures[0];

            gl.glBindTexture(GL10.GL_TEXTURE_2D, m_textureid);

            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

            Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), image);
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
            bmp.recycle();

    }

Here is my code for rendering them.

        public void render(GL10 gl, float rotation) {
            gl.glPushMatrix();


            gl.glEnable(GL10.GL_TEXTURE_2D);
            // Tell OpenGL where our texture is located.
            gl.glBindTexture(GL10.GL_TEXTURE_2D, m_textureid);
            // Tell OpenGL to enable the use of UV coordinates.
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            // Telling OpenGL where our UV coordinates are.
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureVB);

            if(m_3d) {
            // 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);
            }

            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            // Specifies the location and data format of an array of vertex
            // coordinates to use when rendering.
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexVB);
            // Set flat color
            gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
            // Smooth color
            if (colorVB != null) {
                    if(m_alpha) {
                        gl.glEnable(GL10.GL_BLEND);
                        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
                    }
                    // Enable the color array buffer to be used during rendering.
                    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
                    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorVB);
            }

            gl.glTranslatef(m_a.m_pos.x + m_dist_x, m_a.m_pos.y + m_dist_y, m_a.m_pos.z + m_dist_z);
            gl.glRotatef(rotation, 0.0f, 0.0f, 1.0f);

            // Point out the where the color buffer is.
            gl.glDrawElements(GL10.GL_TRIANGLES, indicies.length,
                            GL10.GL_UNSIGNED_SHORT, indexVB);



            // Disable the vertices buffer.
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            if(m_alpha) {
                gl.glDisable(GL10.GL_BLEND);
            }

            if(m_3d) {
                    // Disable face culling.
                    gl.glDisable(GL10.GL_CULL_FACE);
            }

            // Disable the use of UV coordinates.
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            // Disable the use of textures.
            gl.glDisable(GL10.GL_TEXTURE_2D);
            //gl.glPopMatrix();
            gl.glPopMatrix();
    }

Upvotes: 1

Views: 1385

Answers (1)

user1234320
user1234320

Reputation: 71

I've solved the issue. I was loading the bitmaps with the following code

Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), image);

I replaced it with

            Bitmap bmp = null;
            InputStream is = context.getResources().openRawResource(image);
            try {
                bmp = BitmapFactory.decodeStream(is);
            } finally {
                try {
                    is.close();
                    is = null;
                } catch(IOException e) {

                }
            }

That allowed all textures that are power of 2 sizes to be rendered. Any textures that are not power of 2 sizes will not render. OpenGL ES 1.0/1.1 can't handle non-power of 2 textures it seems. I'm still not sure why I was able to render them when my tablet was running honeycomb.

EDIT:

I found a way to get non-power of 2 textures to render using OpenGL ES 1.0. You must have the following functions set to GL_CLAMP_TO_EDGE

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);

Upvotes: 4

Related Questions