glDrawTexfOES 在手机上绘制黑色纹理,并在模拟器中正确
我正在使用 OpenGL 编写一个 2D 游戏,使用存储在我的资源中的 png 图像(64x64 像素,具有透明度)。
我的代码如下所示:
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import javax.microedition.khronos.opengles.GL11Ext;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
public class TestGLRenderer implements Renderer {
private int mTexGLNames[];
private Context mContext;
public TestGLRenderer(Context ctx) {
mContext = ctx;
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// setup the gl renderer
gl.glClearColor(0.2f, 0.4f, 0.6f, 1.0f);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
// reserve GL texture names
mTexGLNames = new int[1];
gl.glGenTextures(1, mTexGLNames, 0);
// load image from resources
Bitmap b;
b = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.image);
// load image in opengl
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexGLNames[0]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, b, 0);
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexGLNames[0]);
int crop[] = new int[] { 0, 64, 64, -64 };
((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D,
GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);
((GL11Ext) gl).glDrawTexfOES(160, 240, 0, 64, 64);
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
}
}
结果在模拟器中按预期工作(运行 Android 2.2),但图像在我的手机(LG-P500、Android 2.2)上显示为黑色方块。附件是模拟器和我的手机的屏幕截图。
是我的代码有问题,还是我的手机有问题(我的手机可以运行其他3D游戏没有问题)?
I'm writing a 2D game using OpenGL, using png images (64x64 pixels, with transparency) stored in my resources.
My code looks like this :
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import javax.microedition.khronos.opengles.GL11Ext;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
public class TestGLRenderer implements Renderer {
private int mTexGLNames[];
private Context mContext;
public TestGLRenderer(Context ctx) {
mContext = ctx;
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// setup the gl renderer
gl.glClearColor(0.2f, 0.4f, 0.6f, 1.0f);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
// reserve GL texture names
mTexGLNames = new int[1];
gl.glGenTextures(1, mTexGLNames, 0);
// load image from resources
Bitmap b;
b = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.image);
// load image in opengl
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexGLNames[0]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, b, 0);
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexGLNames[0]);
int crop[] = new int[] { 0, 64, 64, -64 };
((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D,
GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);
((GL11Ext) gl).glDrawTexfOES(160, 240, 0, 64, 64);
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
}
}
The result is working as expected in the emulator (running Android 2.2), but the image appears as a black square on my phone (LG-P500, Android 2.2). Attached are screenshots from both the emulator and my phone.
Is there something wrong with my code, or is it a problem with my phone (my phone can run other 3D games without problems) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否与你相同,但请尝试一下。
我之前加载过纹理(和你一样的问题):
这在模拟器中工作得很好,但你可以设置更多参数来设置纹理的计算方式。我的猜测是,手机默认无法设置这些,这就是它不起作用的原因(但这只是猜测)。所以解决方案:
就是这样。如果您不知道,可以通过谷歌搜索这些参数(GL_LINEAR 和 GL_REPEAT)的更多信息。
我希望这会有所帮助 =)
I am not sure is it same reason than you but try it.
I loaded textures before (and same problem like you):
Thats works fine in emulator but you can set more parameters to texture how its will calculate. My guess is that phone can't set these by default and thats why it not work (but that just guess). So to the solution:
That's it. You can google more information from those parameters (GL_LINEAR and GL_REPEAT) if you don't know.
I hope this will help =)