gpt4 book ai didi

android - 为什么 OpenGL 纹理在 Eclipse 中使用调试器运行时呈现良好,但在单独运行应用程序时呈现白色?

转载 作者:行者123 更新时间:2023-11-29 22:11:58 26 4
gpt4 key购买 nike

我有一个 sprite 类和另一个使用 OpenGLSurvace View 来渲染扩展 sprite 类的类。看起来像这样

public class Invader extends Sprite {

static int mTextureId = -1;

Invader(Context context, float x, float y, float angle)
{
super(context, x, y, angle);
super.setSize(width, height);
// super.setPos(x, y);
// super.setAngle(angle);

// Check to see if the texture is loaded already. If it is a valid texture
// id, don't bother loading it again
if (mTextureId < 0)
{
super.loadBitmap(context.getResources(), R.drawable.piano_key_white);
}
else
{
super.setTexture(mTextureId);
}
}

@Override
protected int getTexture()
{
return mTextureId;
}

@Override
protected void setTexture(int textureId)
{
mTextureId = textureId;
}

}

如您所见,它使用静态指针指向纹理,该纹理通过调用 decodeResource 函数的 loadBitmap 函数加载来创建 OpenGL 纹理。当我使用 SDK 插件通过 Eclipse 从调试器运行应用程序时,一切看起来都很棒。当我在手机上正常运行该应用程序时,除了将 Invader 呈现为白色方 block (这是对象的颜色)的此类之外,一切都呈现良好。

根据我的发现,纹理可能没有正确加载,因此绘制了一个普通的白色正方形。我不明白为什么这在调试中有效,而不是在正常执行期间。有任何想法吗?当程序未通过远程调试运行时有什么不同?

最佳答案

这是你需要做的:

  1. 将纹理放在“res/drawable”文件夹中
  2. 在加载期间禁用纹理缩放,为您编写代码:

纹理数组分配(duh ;))

// Texture and geometry data
private int [] m_textureID = new int[1];

和加载:

        // Load the texture from resource
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeResource(m_resources, R.drawable.tex, options);

gl.glGenTextures(1, m_textureID, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, m_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);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

// Free resources
bitmap.recycle();

// Do some error checking
if(m_textureID[0] == 0)
throw new RuntimeException("Error loading texture!");

关于android - 为什么 OpenGL 纹理在 Eclipse 中使用调试器运行时呈现良好,但在单独运行应用程序时呈现白色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9407093/

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