In OpenGL ES, how do I convert screen coordinates to world coordinates?

后端 未结 1 691
余生分开走
余生分开走 2020-12-16 08:46

This is for a 2D game so there are only an x and y axis. The game is in landscape mode on the iPhone. I wish to be able to set the screen x and y coordinates of where to ren

相关标签:
1条回答
  • 2020-12-16 09:27

    If you're doing a 2D game, you can set up your projection and model-view matrices so that you don't need to convert at all:

    // This goes in your init code somewhere
    // Set up 480x320 orthographic projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-240.0f, 240.0f, -160.0f, 160.0f, -1.0f, 1.0f);
    
    // Rotate into landscape mode
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);
    

    This makes it so that world coordinates (-240, -160) maps to the top-left of the screen (bottom-left in landscape mode), (240, 160) maps to the bottom-right (top-right in landscape), etc. Since the iPhone's screen is 480x320, you won't need to convert between world and screen coordinates with your matrices set up thusly.

    Of course, if you want to be able to move the camera, then you'll need to offset by the camera's location.

    0 讨论(0)
提交回复
热议问题