OpenGL ES 纹理适用于模拟器,但不适用于设备

发布于 2024-11-24 12:24:17 字数 2688 浏览 3 评论 0原文

我尝试了所有方法,但无法使 OpenGL ES 纹理在我的设备中工作,即使它在模拟器上工作也是如此。

源代码:

纹理类:

public class XAndroidTexture 
{
    private int[] textures = new int[1];
    public int width, height;
    Bitmap bmp;

    public XAndroidTexture(GL10 gl, Bitmap bitmap) 
    {
        gl.glEnable(GL10.GL_TEXTURE_2D);
        this.bmp = bitmap;
        width = bmp.getWidth();
        height = bmp.getHeight();
        gl.glGenTextures(1, textures, 0);
        // ...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // create nearest filtered texture
        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);

        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_MODULATE);
        // Use Android GLUtils to specify a two-dimensional texture image from our bitmap 
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
         //GLUtils.texSubImage2D(GL10.GL_TEXTURE_2D, 0, 0, 0, bmp);

        // Clean up
        bitmap.recycle();

    }

    public void bind(GL10 gl)
    {
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    }

    public static XAndroidTexture createTextureFromBitmap(GL10 gl, Bitmap bmp) 
    {
        return new XAndroidTexture(gl, bmp);
    }
}

纹理加载功能:

public XAndroidTexture Textureload(String path)
    {
        InputStream is;
        Bitmap bmp = null;
        try 
            {
                is = this.getAssets().open(path);

                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inDither = true;
                Bitmap tBmp = BitmapFactory.decodeStream(is, null, opts);
                bmp = Bitmap.createBitmap(tBmp.getWidth(), tBmp.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bmp);
                canvas.drawBitmap(tBmp, 0, 0, null);
                canvas.save();
                tBmp.recycle();
                tBmp = null;
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
            if(bmp == null)
                return null;
            else
                return new XAndroidTexture(gl, bmp);
    }

它在模拟器上工作得很好,但在设备上却不行。 任何人都可以在这里发布适用于设备的纹理类吗?

I tried everything, but I can't make the OpenGL ES texture to work in my device, even when it works on emulator.

Source code:

Texture class:

public class XAndroidTexture 
{
    private int[] textures = new int[1];
    public int width, height;
    Bitmap bmp;

    public XAndroidTexture(GL10 gl, Bitmap bitmap) 
    {
        gl.glEnable(GL10.GL_TEXTURE_2D);
        this.bmp = bitmap;
        width = bmp.getWidth();
        height = bmp.getHeight();
        gl.glGenTextures(1, textures, 0);
        // ...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // create nearest filtered texture
        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);

        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_MODULATE);
        // Use Android GLUtils to specify a two-dimensional texture image from our bitmap 
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
         //GLUtils.texSubImage2D(GL10.GL_TEXTURE_2D, 0, 0, 0, bmp);

        // Clean up
        bitmap.recycle();

    }

    public void bind(GL10 gl)
    {
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    }

    public static XAndroidTexture createTextureFromBitmap(GL10 gl, Bitmap bmp) 
    {
        return new XAndroidTexture(gl, bmp);
    }
}

Texture loading function:

public XAndroidTexture Textureload(String path)
    {
        InputStream is;
        Bitmap bmp = null;
        try 
            {
                is = this.getAssets().open(path);

                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inDither = true;
                Bitmap tBmp = BitmapFactory.decodeStream(is, null, opts);
                bmp = Bitmap.createBitmap(tBmp.getWidth(), tBmp.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bmp);
                canvas.drawBitmap(tBmp, 0, 0, null);
                canvas.save();
                tBmp.recycle();
                tBmp = null;
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
            if(bmp == null)
                return null;
            else
                return new XAndroidTexture(gl, bmp);
    }

It is working on Emulator perfectly fine, but not on device.
Can anyone post here texture class that work on device?

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

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

发布评论

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

评论(1

生活了然无味 2024-12-01 12:24:17

检查下面的链接..我也遇到了同样的问题。我从链接中得到了解决方案。

尝试在设备出现故障,但模拟器可以工作。为什么?

Check the link below.. i too got same problem. i got solution from the link.

Trying to draw textured triangles on device fails, but the emulator works. Why?

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