1

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;
}

2 Answers 2

1

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).

0

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

4
  • 512 is a power of 2. 2^9 = 512
    – NebulaFox
    Commented Dec 22, 2010 at 20:58
  • 1
    yes but it is stated it won't be 512x512.. so I was saying it whatever it will be, it should be a power of 2
    – Will Kru
    Commented Dec 22, 2010 at 21:01
  • if I don't put inDensity = 240, the image will not be 512x512
    – NebulaFox
    Commented Dec 22, 2010 at 21:17
  • 1
    Ok I misunderstood your comment, I see you updated it. You could also use options.inScaled = false; to prevent Android from rescaling your resources.
    – Will Kru
    Commented Dec 22, 2010 at 21:38

Your Answer

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

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