0

I'm trying to show textures by openGL. In following code I want to draw simple square with texture. When I run it on android emulator, everything is OK, but when I run it on real device, I can see only white square without any texture.

There is similar problem, but I don't use NDK. I use only 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);
    }

Can someone look at my code and tell me where is the problem?

Community
  • 1
  • 1
InflexCZE
  • 722
  • 1
  • 14
  • 30
  • Have you copied the texture resource to your device? Guess you would get a runtime error if you hadn't... I'm not familiar with Android GL10, but you could try calling glEnable(GL_TEXTURE_2D) before glDrawArrays, incase some other app or part of your app deactivated it. – torbjoernwh Aug 08 '12 at 17:40
  • Texture is in .apk file. I load it like this. "Bitmap b = BitmapFactory.decodeResource(thiz.getResources(), R.drawable.nex2);" And if i move glEnable(GL10.GL_TEXTURE_2D) before glDrawArrays it makes no change. – InflexCZE Aug 08 '12 at 17:44

2 Answers2

2

Do you remember to set the minification filter to not use mipmaps? I'm kind of surprised this would work on the emulator if you didn't, but maybe it allows it for some reason.

E.g.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

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

Tim
  • 35,413
  • 11
  • 95
  • 121
1

The emulator does NOT run OpenGL ES afaik, it runs off of the desktop driver. Meaning it can support textures are not 2^n, where your phone only supports textures that are 2^n(aka square).

I would check this, it is most likely your issue if you are getting no errors. Grab a 2^n image and see if it works for you.

Jug6ernaut
  • 8,219
  • 2
  • 26
  • 26
  • Possible, though if a device doesn't support npot textures it's supposed to post a glError on glTexImage2d : *GL_INVALID_VALUE is generated if non-power-of-two textures are not supported ...* – Tim Aug 08 '12 at 20:28
  • I am just trying to thing of a reason why it would work on one and not the other, you are probably correct though, I have not tested. – Jug6ernaut Aug 08 '12 at 20:42
  • I use 254*254 pixels picture. So this is OK. When i use another size like 300*300, square is black not white. In every way thanks for effort. – InflexCZE Aug 09 '12 at 00:10
  • sorry. i use 256*256 not 254*254 – InflexCZE Aug 09 '12 at 10:09