本文介绍了glDrawTexfOES借鉴了手机的质感的黑色,并在正确的模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写使用OpenGL存储在我的资源2D游戏,采用PNG图像(64×64像素,具有透明度)。

I'm writing a 2D game using OpenGL, using png images (64x64 pixels, with transparency) stored in my resources.

我的code是这样的:

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) {
    }

}

结果正按预期在模拟器(运行Android 2.2),但图像显示为我的手机(LG-P500的Andr​​oid 2.2)上一个黑色的正方形。附上从模拟器和我的电话都截图。

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.

是不是有什么毛病我code,或者是用我的手机有问题(我的手机可以运行没有问题等3D游戏)?

Is there something wrong with my code, or is it a problem with my phone (my phone can run other 3D games without problems) ?

推荐答案

我不知道是不是一样的道理比你,但尝试一下。

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

就是这样。你可以从谷歌这些参数(GL_LINEAR和GL_REPEAT)的详细信息,如果你不知道。

That's it. You can google more information from those parameters (GL_LINEAR and GL_REPEAT) if you don't know.

我希望这将有助于=)

这篇关于glDrawTexfOES借鉴了手机的质感的黑色,并在正确的模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:44