NebulaFox
NebulaFox

Reputation: 8303

Android OpenGL ES not drawing Texture

I have a very basic Activity at the moment. It creates a GLSurfaceView and sets the Renderer. The problem is all I see is red, which is from glClearColor, and no texture. Not even a white area. Also glGetError() is not reporting anything.

Here is the Renderer:

public class MyRenderer implements Renderer {

    public MyRenderer(Context context)
    {
        mContext = context;
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);

        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertex);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoords);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);

        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(-160.0f, 160.0f, -240.0f, 240.0f, 0.1f, 1.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        float vertexBuffer[] = {
                -160.0f, -240.0f,
                -160.0f, 240.0f,
                160.0f, -240.0f,
                160.0f, 240.0f
        };

        vertex = ByteBuffer.allocateDirect(8 * 4).asFloatBuffer().put(vertexBuffer);

        float texCoordsBuffer[] = {
                0.0f, 0.0f,
                0.0f, 480.0f/512.0f,
                320.0f/512.0f, 0.0f,
                320.0f/512.0f, 480.0f/512.0f
        };

        texCoords = ByteBuffer.allocateDirect(8 * 4).asFloatBuffer().put(texCoordsBuffer);

            BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDensity = 240; // needed so that the image will be 512x512
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image, options);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Log.i(TAG, "Bitmap:{w:" + width + " h:" + height + "}");

        gl.glEnable(GL10.GL_TEXTURE_2D);

        texture = new int[1];

        gl.glGenTextures(1, texture, 0);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);

        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        bitmap.recycle();

        int error = gl.glGetError();
        if (error != GL10.GL_NO_ERROR)
        { 
            Log.e(TAG, "GL Texture Load Error: " + error);

        }


    }

    private Context mContext;
    private int texture[];
    private FloatBuffer vertex;
    private FloatBuffer texCoords;
}

Upvotes: 1

Views: 2615

Answers (2)

svdree
svdree

Reputation: 13348

There are several problems with your code:

You need to set the byte order of your buffers to native:

vertex.order(ByteOrder.nativeOrder())

After copying data into your buffers, reset the position to 0:

vertex.position(0)

(Do both for texCoord as well).

It would probably also help to put your near clipping plane at -1.0 instead of .1 (in glOrthof).

Upvotes: 1

Will Kru
Will Kru

Reputation: 5212

// needed because the image won't be 512x512

To have OpenGL render the texture, the texture size needs to be a power of 2, ie 64x64, 128x32 or 256x1024

Upvotes: 0

Related Questions