1
\$\begingroup\$

Some days ago, I've felt a need to add a camera to my game, it's a puzzle. That would let me move the board, zoom in and etc. This gave me control to do boards muuuch larger, soon, more complex puzzles.

But today when i was implementing such a toolbar, I've found which problems the camera gave me too.

In logic, it is simple. I want the toolbar to be fixed, and the board be free. That is, when I zoom in, the board zoom, but the toolbar stays.

My camera code look as follows:

    public void Camera::draw(GL10 gl) {
    gl.glMatrixMode(GL10.GL_PROJECTION); // Select the projection matrix
    gl.glLoadIdentity(); // Reset the projection matrix
    gl.glOrthof(0.0f, width * zoom, 0.0f, height * zoom, 0.0f, 1.0f);

    // Translating
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(-position.X, -position.Y, 0);
}

Very simple, but stills functional.

To draw a scene I do the following:

public void Game::draw(GL10 gl) {
    camera.draw(gl);
    currentLevel.board.draw(gl);
}

I've came with the problem to add the toolbar.

public void draw(GL10 gl) {
    toolbar.draw(gl);
    camera.draw(gl);
    currentLevel.board.draw(gl);
}

I've thought about letting it came first than the camera draw, so it would be fixed. I was wrong.

Nice images examples for you:

A normal board
A normal board

The effect expected for zoom in
The effect expected for zoom in, just the board zooms

The effect that i got when i zoom in
The effect that i got when i zoom in everything zooms, even the draw that came before the camera.

You can think of this as a camera that supports HUD. I saw people solving this by using a scale factor instead of using glOrthof. But this would mess all my coordinate system. Are there any other alternative for me? If so, how can I implement it?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

It looks like your problem is that your not resetting your GL_PROJECTION matrix after you draw the zoomed in board.

Then next render loop its still using the zoomed in projection matrix. When it draws the next board it will set the projection matrix to the same thing again.

Either set it to the default unzoomed matrix at the beginning of the loop or after you draw the board.

Otherwise you can use glPushMatrix and glPopMatrix to store and revert to the previous state.

EDIT: Also be aware that glMatrixMode, glLoadIdentity, glOrthof, glTranslatef, glPushMatrix, glPopMatrix are all fixed pipeline functions that are deprecated/removed in modern OpenGL versions and have been replaced by uploading matrices to shaders. Might be fine for learning (then again it might be best to learn the proper way from the start).

EDIT2:

public void Camera::draw(GL10 gl) {
    gl.glMatrixMode(GL10.GL_PROJECTION); // Select the projection matrix
    gl.glLoadIdentity(); // Reset the projection matrix
    gl.glOrthof(0.0f, width, 0.0f, height, 0.0f, 10000.0f); // <- No zoom here anymore, increase view distance
}

public void drawBoard(GL10 gl) {
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(-position.X, -position.Y, zoom); // <- Zoom goes here (possibly it should be negative)
    currentLevel.board.draw(gl);
    gl.glLoadIdentity();
}

public void draw(GL10 gl) {
    camera.draw(gl); // This should actually only need to be done once (and on window resize)
    toolbar.draw(gl);
    drawBoard(gl);
}
\$\endgroup\$
6
  • \$\begingroup\$ hmm, that is valid for the projection too? i thought it was just for model matrix, will test right now \$\endgroup\$ Commented Jan 22, 2012 at 6:08
  • \$\begingroup\$ "There is a stack of matrices for each of the matrix modes.". Just make sure you have called the correct glMatrixMode befoure the push/pop. \$\endgroup\$ Commented Jan 22, 2012 at 6:10
  • \$\begingroup\$ tried to do change to projection, load identity and go back to modelview. Now the toolbar is drawing at a very strange place, but it doesnt see to zoom whith the board. What might be? \$\endgroup\$ Commented Jan 22, 2012 at 6:12
  • \$\begingroup\$ You will also probably also need to reset the modelview matrix too since the board translation will effect the toolbar. Also it might be better to use another glOrtho (without zoom) rather than loading an identity matrix into your PROJECTION_MATRIX since it could be giving you a different coordinate system (ie -1.0 to 1.0 rather than 0.0 to whatever screen resolution you have). It also might depend on how your drawing your toolbar mainly the coordinate system your using). Verify that you can draw the toolbar without the board first. \$\endgroup\$ Commented Jan 22, 2012 at 6:28
  • \$\begingroup\$ Loaded identity on model matrix too. Same problem. Also, how can i achieve this thing of having another glOrtho? Care to edit your answer? Thanks! \$\endgroup\$ Commented Jan 22, 2012 at 6:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .