openGL纹理在模拟器上运行,但不在真实设备上运行

时间:2012-08-08 17:11:14

标签: java android opengl-es

我正在尝试通过openGL显示纹理。在下面的代码中,我想用纹理绘制简单的正方形。当我在Android模拟器上运行它时,一切都很好,但是当我在真实设备上运行它时,我只能看到没有任何纹理的白色方块。

There is similar problem,但我不使用NDK。我只使用Java。

public class MainActivity extends Activity
{
    public static TextView t;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        LinearLayout l = new LinearLayout(this);
        t = new TextView(this);
        l.addView(t);
        l.setOrientation(1);
        l.addView(new ImageView(this));
        setContentView(l);

    }
    long last = System.currentTimeMillis();
    int FPS = 0;
    //Show FPS(+ some extraData)
    public void FPS(final String extraData)
    {
        if(System.currentTimeMillis()<last+1000)
        {
            FPS++;
        }
        else
        {
            runOnUiThread(new Runnable(){@Override public void run(){
                t.setText((double)FPS/(((double)System.currentTimeMillis()-last)/1000)+";"+extraData);
            }});
            FPS = 0;
            last = System.currentTimeMillis();
        }
    }

public class ImageView extends GLSurfaceView implements GLSurfaceView.Renderer
{
    MainActivity thiz;
    public ImageView(MainActivity thiz)
    {
        super(thiz);
        this.thiz = thiz;
        setRenderer(this);
    }


    FloatBuffer vertex;
    ShortBuffer texture;
    int[] textureID = new int[1];
    public void onDrawFrame(GL10 gl)
    {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertex);
        gl.glTexCoordPointer(2, GL10.GL_SHORT, 0, texture);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

        thiz.FPS("");
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig arg1)
    {
        vertex = FloatBuffer.wrap(new float[]{-0.5f, -0.5f,
                                               0.5f, -0.5f,
                                                -0.5f, 0.5f,
                                                  0.5f, 0.5f});

        texture = ShortBuffer.wrap(new short[]{0, 1, 
                                                1, 1,
                                                0, 0,
                                                 1, 0});


        Bitmap b = BitmapFactory.decodeResource(thiz.getResources(), R.drawable.nex2);
        gl.glGenTextures(1, textureID, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID[0]);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, b, 0);



        b.recycle();

        gl.glClearColor(0.1f, 0.5f, 1f, 1f);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

    public void onSurfaceChanged(GL10 gl, int w, int h)
    {
        gl.glViewport(0, 0, w, h);
    }

有人可以查看我的代码并告诉我问题出在哪里吗?

2 个答案:

答案 0 :(得分:2)

您还记得将缩小过滤器设置为不使用mipmap吗?如果你没有,我会很惊讶这会在模拟器上工作,但也许它可以出于某种原因。

E.g。

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

http://www.opengl.org/wiki/Common_Mistakes#Creating_a_Texture

答案 1 :(得分:1)

模拟器不会运行OpenGL ES,它会运行桌面驱动程序。意味着它可以支持纹理不是2 ^ n,其中你的手机只支持2 ^ n(又名方形)的纹理。

我会检查一下,如果你没有错误,很可能是你的问题。抓住2 ^ n图像,看看它是否适合你。

相关问题