0
votes

I'm learning Opengl for 2D, (in particular using the java slick2D library, but that might not be a factor for this question). I discovered I can change push a new transformation matrix, for instance to transform world coordinates into screen (view) coordinates.

I want to use this to zoom my view, increasing the distances between objects, but once this is done I want to paint images and shapes in screen coordinates, like a HUD. Basically, I want the coordinates to change, but the graphics to stay the same, so I can render custom images on each scale.

Can this be done with some clever transformation or some OpenGL option instead of manually calculating all coordinates?

2

2 Answers

5
votes
  1. Clear color and depth buffers

  2. Set projection/modelview matrices for zoomed 2D/3D rendering

  3. Enable depth test

  4. Render zoomed 2D/3D scene

  5. Reset projection/modelview for 2D overlay

  6. Disable depth test

  7. Draw 2D overlay

  8. Repeat

1
votes

Ok, figured it out, you have to use two separate projection:

1.- Scale the world coordinates, so that the objects are drawn on the correct scene locations:

    g.pushTransform();

    // Always zoom in the center of the screen.
    g.translate(resolution.getX(), resolution.getY());
    g.scale(scale, scale);

    // Move the view
    g.translate(-view.getX(), -view.getY());

2.- Push a new matrix to draw the object, inverting the scale transformation so the object's graphics don't change with scale:

    g.pushTransform();

    // Center on the current object.
    g.translate(pos.x, pos.y);

    // Undo scaling of graphics!
    g.scale(1.0f/scale, 1.0f/scale);