首先,我知道这是一个重复的问题,我查看了另一个问题,但没有帮助!我正在尝试将我的安卓游戏从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;
}
代码B绘图
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的新手,无法弄清楚问题是什么,我错过了什么。所以如果有人能帮助我那将是伟大的。谢谢givim时间!
答案 0 :(得分:2)
我很确定纹理的宽度和高度必须是2的幂才能显示出来。尝试缩放您的位图,使其具有2的幂。
答案 1 :(得分:1)
嗯,我也不是一个主要的OpenGL专家,但是从我在你的代码中看到的唯一有点奇怪的是你如何加载你的图像,即这部分:
bgNightID = textureStorage.Load(BitmapFactory.decodeResource(context.getResources(),R.drawable.bg_game));
我所做的只是创建一个像这样的Bitmap实例:
InputStream is = appContext.getResources().openRawResource(R.drawable.whateverId);
Bitmap bitmap = = BitmapFactory.decodeStream(is);
然后将其传递给GLUtils:
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap , 0);
我也从不打电话给glTexEnv:
//I don't do this:
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);