Screen to OpenGL world transformations - need help, really!

Problems with Canvas, OpenGL, etc...

Screen to OpenGL world transformations - need help, really!

Postby afail » Mon Nov 15, 2010 3:29 pm


I have a problem with mapping the coordinates of the screen to the OpenGL Es world. I do the following:

Code: Select all
mButton1.setPosition(75.0f, 120.0f);
mButton1.setScale(.3f, .3f, 0f);

mButton1 is a Button (custom class) instance. There is a method 'setPosition(x, y)' that sets mX and mY members like this:

Code: Select all
public void setPosition(float x, float y) {
      mPos = getTransformation(x, y);
      mTransX = mPos.x;
      mTransY = mPos.y;

Then, I have getTransformation(float, float) method that looks like this:

Code: Select all
public static PointF getTransformation(float x, float y) {
      float[] pos = new float[4];
        float[] mModelView = new float[16];
        float[] mProjection = new float[16];
        int view[] = {0, 0, screenWidth, screenHeight};

        getMatrix(mGL, GL10.GL_MODELVIEW, mModelView);
        getMatrix(mGL, GL10.GL_PROJECTION, mProjection);       
      GLU.gluUnProject(x, y, 0, mModelView, 0, mProjection, 0, view, 0, pos, 0);

      return (new PointF(pos[0], (-1)*pos[1]));   

And getMatrix that is using MatrixTrackingGL from the Google Graphics API samples:

Code: Select all
private static void getMatrix(GL10 gl, int mode, float[] mat) {
        MatrixTrackingGL gl2 = (MatrixTrackingGL) gl;
        gl2.getMatrix(mat, 0);

But getTransformation(x, y) returns incorrect values :(

Where do I make a mistake?

ANY help is appreciated.
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm


Re: Screen to OpenGL world transformations - need help, real

Postby seed » Wed Nov 17, 2010 4:54 pm

You sent me a PM to take a look at this thread. I don't really understand any of this code ... sorry. Not sure why this should be so complicated.

I guess it depends on your application. If you are doing a basically static perspective, then the translation of screen coordinates to GL coordinates should be really easy to do and you shouldn't need anything more than some multiplication addition and subtraction. If you are using a more dynamic perspective like in a FPS where the perspective is always changing, then translating screen to world coordinates is probably pretty hard and you shouldn't be doing this.

If the latter case, then you really should be rendering your scene however you currently are and then resetting things, changing to ortho projection, and rendering your buttons.

I have heard advise that you shouldn't really change your perspective. Rather, you should translate your world relative to a static perspective. Not sure why this is recommended. Probably because it is easier to conceptualize. However, if this was true, then you push your translations for the stuff in your game and render them, then you pop it and push a simple translation for your buttons and render them. No need to get all messed up with the perspective and doing complicated backwards translations if you handle it this way.

Sorry, but my answer is pretty generic. Probably because I didn't really understand the code in your question.

Visit Exit 4 Gaming -
Home of LavaBall -
Home of Rebound -
Home of Tap Crazy -
Download "Caveman Pool" From the Market Today!
Senior Developer
Senior Developer
Posts: 103
Joined: Mon Mar 15, 2010 3:22 pm


Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests