OpenGL冰淇淋三明治上的黑色纹理



我正在使用OpenGL ES 1.0创建一个游戏。我最近把我的平板电脑更新到了android 4.0.3,现在我所有的纹理都显示为黑色。我试着打开和关闭硬件加速,但似乎没有什么区别。

这是我加载纹理的代码。

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

这是我渲染它们的代码。

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

我已经解决了这个问题。我用以下代码加载位图

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

我把它换成

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

这允许渲染所有大小为2的幂的纹理。任何不是2次方大小的纹理都不会渲染。OpenGL ES 1.0/1.1似乎无法处理非二次方纹理。我仍然不确定为什么当我的平板电脑运行蜂窝时,我能够渲染它们。

编辑:

我找到了一种使用OpenGL ES 1.0渲染非二次方纹理的方法。您必须将以下功能设置为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);

最新更新