gpt4 book ai didi

java - OpenGL es glDrawTexfOES() 2D纹理不渲染

转载 作者:行者123 更新时间:2023-12-02 00:28:18 27 4
gpt4 key购买 nike

首先,我知道这是一个重复的问题,我检查了另一个问题,但没有帮助!我正在尝试将我的 Android 游戏从 Canvas 系统转换为 GL10。经过大量谷歌搜索后,我决定为表演者做这件事。不管怎样,我尝试构建一个加载和绘制纹理的类。问题是纹理在我的蓝屏上显示为全黑。我不确定是否可以在不创建任何网格或类似内容的情况下绘制纹理,但这就是我想要做的。我只是想按照自己的尺寸绘制 2D 纹理,所以我认为不需要网格。这是我的代码:

onSurfaceCreated

    @Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 1.0f, 0.2f);
gl.glClearDepthf(1.0f);
gl.glViewport(0, 0, 800, 480);

gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
//gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glLoadIdentity();
// This is the class where I load and draw my textures.
textureStorage = new TextureStorage(gl);
//I'm loading a texture. **CODE A TEXTURE LOADER**
bgNightID = textureStorage.Load(BitmapFactory.decodeResource(context.getResources(),R.drawable.bg_game));
//This is where I load my units variables. Nothing to do with it.
RenderingFunctions.InitUnits(this);
}

onDrawFrame

@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
//gl.glTranslatef(0, 0, -4);
**CODE B DRAWING**
// bgNightID is the ID of texture was loaded.
textureStorage.DrawTexture(bgNightID, 50,50,0,(short)4000,(short)638);
// Drawing background right here which is shows up black.
}

CODE A TEXTURE LOADER

public int glLoad( Bitmap bitmap ){
if ( gl == null ){
Log.e(TAG, "Failed to load resource. Context/GL is NULL");return -1;
}
int textureName = -1;
gl.glGenTextures(1, TexIDStorage, 0);
textureName = TexIDStorage[0];

Log.d(TAG, "Generated texture: " + textureName);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureName);

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_MIN_FILTER, GL10.GL_NEAREST);
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);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

int error = gl.glGetError();
if (error != GL10.GL_NO_ERROR){
Log.e(TAG, "GL Texture Load Error: " + error);
}else
Log.d(TAG, "Loaded texture: " + textureName);
return textureName;
}

CODE B DRAWING

public void DrawTexture(int TexutreID, float X, float Y, float Z, short Width, short Height){
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
gl.glBindTexture(GL10.GL_TEXTURE_2D, TexIDStorage[TexutreID]);
((GL11Ext) gl).glDrawTexfOES(X, Y, Z, Width, Height);
}

我对 GL10 完全陌生,无法弄清楚问题是什么以及我错过了什么。所以如果有人能帮助我那就太好了。感谢您给我们时间!

最佳答案

我非常确定纹理的宽度和高度必须是 2 的幂才能正确显示。尝试缩放位图,使其达到 2 的幂。

关于java - OpenGL es glDrawTexfOES() 2D纹理不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9624260/

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