2

I have a world that is rendered in 2D and I'm looking at it from the top. Tjat looks like this (the floor tiles have no texture and only random green color yet):

2D look on the world

Before rendering my entities, I transform the model-view matrix like this (while position is the position and zoom the zoom of the camera, ROTATION is 45):

glScalef(this.zoom, this.zoom, 1);
glTranslatef(this.position.x, this.position.y, 0);
glRotatef(ROTATION, 0, 0, 1);

Now I want to calculate the world coordinates for the current position of my camera. What I'm trying is to create a new matrix with glPushMatrix, then transform it the same way that the camera is transformed, and then get the matrix and multiply the given camera coordinate with it:

private Vector2f toWorldCoordinates(Vector2f position) {

    glPushMatrix();

    // do the same as when rendering
    glScalef(this.zoom, this.zoom, 1);
    glTranslatef(this.position.x, this.position.y, 0);
    glRotatef(ROTATION, 0, 0, 1);

    // get the model-view matrix
    ByteBuffer m = ByteBuffer.allocateDirect(64);
    m.order(ByteOrder.nativeOrder());
    glGetFloatv(GL_MODELVIEW_MATRIX, m);

    // calculate transformed position
    float x = (position.x * m.getFloat(0)) + (position.y * m.getFloat(4)) + m.getFloat(12);
    float y = (position.x * m.getFloat(1)) + (position.y * m.getFloat(5)) + m.getFloat(13);
    System.out.println(x + "/" + y);

    glPopMatrix();
    return new Vector2f(x, y);
}

The problem now is: this works for the x coordinate, but the y coordinate is wrong and always 0. Have I misused the matrix somehow? Is there a "smoother" way of getting the world coordinates from the eye coordinates?

maxdev
  • 2,491
  • 1
  • 25
  • 50

1 Answers1

1

The problem is with the way you're calling getFloat(). When you call it with an index on a ByteBuffer, the index is the number of bytes into the buffer at which to start reading the float, not the number of floats. You need to multiply each of your indices by 4:

float x = (position.x * m.getFloat(0)) + (position.y * m.getFloat(16)) + m.getFloat(48);
float y = (position.x * m.getFloat(4)) + (position.y * m.getFloat(20)) + m.getFloat(52);

However given that x is working for you already, I suspect you might also need to transpose your matrix co-ordinates, and so the correct code is:

float x = (position.x * m.getFloat(0)) + (position.y * m.getFloat(4)) + m.getFloat(12);
float y = (position.x * m.getFloat(16)) + (position.y * m.getFloat(20)) + m.getFloat(28);

(By a co-incidence, transposing the first row of the matrix into the first column gives indices that are 4 times as great, so the 2 bugs cancel each other out in the case of x but not y).

If you're looking for a smoother way of doing it, look into using gluUnProject, although you may have to apply some additional transforms (it maps from window to object co-ordinates).

samgak
  • 23,944
  • 4
  • 60
  • 82
  • Oh, thanks a lot. I assumed that `getFloat` would work in a manner similar to indexing on pointers in C. To the transposing part; why do I have to transpose the matrix? It in fact works, but what is the logic behind this (could you add a source)? – maxdev May 06 '15 at 13:42
  • Also I have a follow-up question you may know an answer to: http://stackoverflow.com/questions/30080477/doing-a-matrix-multiplication-in-reverse :) – maxdev May 06 '15 at 15:00
  • To clarify: you don't have to transpose the matrix, whereas in your original code you were. world-to-camera is the inverse transform, but camera-to-world (or world-from-camera) is the forward direction, which is the one you are doing. So it's just a straightforward Matrix-Vector product, as shown here: https://open.gl/transformations – samgak May 07 '15 at 03:21