10

I have a series of OpenGL-ES calls that properly render a triangle and texture it with alpha blending on the emulator (2.0.1). When I fire up the same code on an actual device (Droid 2.0.1), all I get are white squares.

This suggests to me that the textures aren't loading, but I can't figure out why they aren't loading. All of my textures are 32-bit PNGs with alpha channels, under res/raw so they aren't optimized per the sdk docs.

Here's how I am loading my textures:

private void loadGLTexture(GL10 gl, Context context, int reasource_id, int texture_id)
{
    //Get the texture from the Android resource directory
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), reasource_id, sBitmapOptions);

    //Generate one texture pointer...
    gl.glGenTextures(1, textures, texture_id);
    //...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[texture_id]);

    //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);

    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
    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);

    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    //Clean up
    bitmap.recycle();
}

Here's how I am rendering the texture:

        //Clear
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    //Enable vertex buffer
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
        //Push transformation matrix
        gl.glPushMatrix();
        //Transformation matrices
        gl.glTranslatef(x, y, 0.0f);
        gl.glScalef(scalefactor, scalefactor, 0.0f);
        gl.glColor4f(1.0f,1.0f,1.0f,1.0f);
        //Bind the texture
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[textureid]);
        //Draw the vertices as triangles
        gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
        //Pop the matrix back to where we left it
        gl.glPopMatrix();
            //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

And here are the options I have enabled:

        gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
        gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
        gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);

Edit: I just tried supplying a BitmapOptions to the BitmapFactory.decodeResource() call, but this doesn't seem to fix the issue, despite manually setting the same preferredconfig, density, and targetdensity.

Edit2: As requested, here is a screenshot of the emulator working. The underlaying triangles are shown with a circle texture rendered onto it, the transparency is working because you can see the black background.

removed dead ImageShack link

Here is a shot of what the droid does with the exact same code on it:

removed dead ImageShack link

Edit3: Here are my BitmapOptions, updated the call above with how I am now calling the BitmapFactory, still the same results as below: sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;

sBitmapOptions.inDensity = 160;
sBitmapOptions.inTargetDensity = 160;
sBitmapOptions.inScreenDensity = 160;
sBitmapOptions.inDither = false;
sBitmapOptions.inSampleSize = 1;
sBitmapOptions.inScaled = false;

Here are my vertices, texture coords, and indices:

    /** The initial vertex definition */
    private static final float vertices[] = { 
                                        -1.0f, -1.0f, 0.0f, 
                                        1.0f, -1.0f, 0.0f,
                                        -1.0f, 1.0f, 0.0f,
                                        1.0f, 1.0f, 0.0f
                                                        };
    /** The initial texture coordinates (u, v) */   
private static final float texture[] = {            
                                    //Mapping coordinates for the vertices
                                    0.0f, 1.0f,
                                    1.0f, 1.0f,
                                    0.0f, 0.0f,
                                    1.0f, 0.0f
                                    };
/** The initial indices definition */   
private static final byte indices[] = {
                                        //Faces definition
                                        0,1,3, 0,3,2
                                        };

Is there anyway to dump the contents of the texture once it's been loaded into OpenGL ES? Maybe I can compare the emulator's loaded texture with the actual device's loaded texture?

I did try with a different texture (the default android icon) and again, it works fine for the emulator but fails to render on the actual phone.

Edit4: Tried switching around when I do texture loading. No luck. Tried using a constant offset of 0 to glGenTextures, no change.

Is there something that I'm using that the emulator supports that the actual phone does not?

Edit5: Per Ryan below, I resized my texture from 200x200 to 256x256, and the issue was NOT resolved.

Edit: As requested, added the calls to glVertexPointer and glTexCoordPointer above. Also, here is the initialization of vertexBuffer, textureBuffer, and indexBuffer:

ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
loadGLTextures(gl, this.context);
7
  • Please supply a screenshot of it working on emulator, and if possible a screenshot of it not working.
    – Justicle
    Jan 22, 2010 at 0:25
  • Done, please let me know if there's anything else I can provide that will help unravel this mystery, been banging my head on the wall for awhile now.
    – Dinedal
    Jan 22, 2010 at 0:58
  • Can you show where you enable the Texture and Vertex pointers? Should be something like: gl.glVertexPointer(2, GL10.GL_FLOAT, 0, GLVertices); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, GLTextures); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    – CaseyB
    Jan 27, 2010 at 16:40
  • At work right now, but I will update the code above when I can. Basiclly it's right before the glPushMatrix call.
    – Dinedal
    Jan 27, 2010 at 17:59
  • @CaseyB Added calls as you requested. All calls were already in the existing code, just not up here.
    – Dinedal
    Jan 27, 2010 at 22:01

7 Answers 7

11

I had exactly this issue when I moved from SDK 3 to 4. My textures would display on the emulator but not on the real device (Motorola Milestone/Droid).

All your textures must be binary powers of two. Mine were 256x256. However the reason the textures don't display is that the OS is scaling them from their binary power sizes to display on the Milestone's high density screen.

The solution is easy - just move the textures to the drawable-nodpi directory and the OS won't scale them when it loads them.

1
  • Thanks a lot! I changed size to binary points of two and it works!
    – Tommz
    Jul 28, 2013 at 23:16
3

Im also new to Android OpenGL ES dev. I own a Milestone (euro version for Droid).

From what i can see so far, firing several tutorial apps from different books/websites on my milestone, it seems this phone handle OpenGL ES in a different way than all other android phones released so far.

I think its related to the OpenGL extensions supported on the device.

check this link for the list of the supported extensions, im pretty sure a code guru will find why exactly the droid is handling opengl es in such a weird way.

http://www.rbgrn.net/content/345-hands-on-motorola-droid-opengl-es-specs

Hope this helps.. a bit


EDITED :

I tweaked the manifest.xml and noticed when using

<uses-sdk android:minSdkVersion="6"/>

the resources aren't loading at all.

When using

<uses-sdk android:minSdkVersion="3"/>

resources are loading fine.

So i switched back to

<uses-sdk android:minSdkVersion="6"/>

and changed the way the bitmap is loaded.

Try to replace :

  //Get the texture from the Android resource directory 
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), reasource_id, sBitmapOptions); 

with :

InputStream is = context.getResources().openRawResource(your.resource.there);
    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeStream(is);

    } finally {
        //Always clear and close
        try {
            is.close();
            is = null;
        } catch (IOException e) {
        }
    }

Textures are loading fine on my milestone with that code, hope this work for yours !

1
  • would love to know if you can draw textures with my code.. :)
    – Dullahx
    Feb 5, 2010 at 11:15
1

Make sure you load your bitmaps with a appropriate BitmapConfig that is not scaling your image behind the scenes, because this happens.

Also, is there a reason you are storing your pngs in /raw/ instead of /drawable/?

1
  • I'd been having a similar problem, and this was exactly what was happening. Thanks for the help.
    – rascalking
    Dec 1, 2010 at 15:58
1

Dullahx's edited answer solved my problem on the same issue. thanks a lot, it was bugging me the last two days!

I was trying to map a texture on samsung galaxy tab, which was displayed successfully on the emulator but only the plane was displayed on the device.

I replaced my code:

plane.loadBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.jay));

with this one:

InputStream is = context.getResources().openRawResource(R.drawable.jay);

// Load the texture.
plane.loadBitmap(BitmapFactory.decodeStream(is));

and it worked on the device too!

But be careful about the minSdkVersion if you are also trying to map a texture on a fullscreen size rectangle like me. If I use

 <uses-sdk android:minSdkVersion="3"/>

on samsung galaxy tab, my plane was not rendered fullscreen. It was rendered according to the fullscreen size of a device using sdk version 3. I changed this to

<uses-sdk android:minSdkVersion="8"/>

and my fullscreen rectangle is back with its texture on.

Hope this helps, too.

0

I am unsure of what your problem is, I'm pretty new to development in general, but my OpenGL android problems with difference between the emulator and the phone were:

-Emulator can support non 2^x dimensions for bitmaps, phone cannot -Emulator flipped screen in NDK for openGL so I had to use negative coordinates to define the view in the emulator, while my G1 used the coordinates non-negated.

Hopefully this is able to help you in someway.

3
  • +1 for the non power of two issue. What size in your texture?
    – CaseyB
    Jan 22, 2010 at 19:46
  • It was 200x200, but I just resized it to 256x256, and it still has the same issue. That isn't it.
    – Dinedal
    Jan 22, 2010 at 21:14
  • for me, that was it, together with putting it into the "raw" folder instead of "drawable".
    – manmal
    Mar 20, 2012 at 13:55
0

I think that it maybe that you are having trouble loading the texture from the APK. Try copying the texture to the SDCard and see if you can load it from there.

0
0

are you using the GL11 interface somewhere? GL11 is not supported by all devices. and also remove the glColor line, perhaps your texture is displayed but over-painted by the color? here some code snippet how i display my texture-meshes:

void draw(GL10 gl) {

    // Enabled the vertices buffer for writing and to be used during
    // rendering.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    // Specifies the location and data format of an array of vertex
    // coordinates to use when rendering.
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    gl.glEnable(GL10.GL_TEXTURE_2D);

    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.glBindTexture(GL10.GL_TEXTURE_2D, myTextureId);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    gl.glDrawArrays(drawMode, 0, verticesCount);

    gl.glDisable(GL10.GL_TEXTURE_2D);
    // Disable the vertices buffer.
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.