gpt4 book ai didi

android - Ice Cream Sandwich 上的 OpenGL 黑色纹理

转载 作者:行者123 更新时间:2023-11-29 16:16:02 25 4
gpt4 key购买 nike

我正在使用 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();
}

最佳答案

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

位图 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 似乎无法处理 2 纹理的非幂。我仍然不确定为什么我的平板电脑运行 honeycomb 时能够渲染它们。

编辑:

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

关于android - Ice Cream Sandwich 上的 OpenGL 黑色纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9474540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com