glDrawTexfOES 在手机上绘制黑色纹理,并在模拟器中正确

发布于 2024-11-08 14:23:12 字数 2167 浏览 0 评论 0原文

我正在使用 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) ?

Screenshot from the emulator Screenshot from my phone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何时共饮酒 2024-11-15 14:23:12

我不确定这是否与你相同,但请尝试一下。

我之前加载过纹理(和你一样的问题):

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[i]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmaps[i], 0); 

这在模拟器中工作得很好,但你可以设置更多参数来设置纹理的计算方式。我的猜测是,手机默认无法设置这些,这就是它不起作用的原因(但这只是猜测)。所以解决方案:

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[i]); //Same

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_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmaps[i], 0); //Same

就是这样。如果您不知道,可以通过谷歌搜索这些参数(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):

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[i]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmaps[i], 0); 

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:

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[i]); //Same

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_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmaps[i], 0); //Same

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 =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文