0
\$\begingroup\$

Using gluPerspective makes OpenGL's grid really messy and rather hard to use especially when trying to do mouse input. The grid that perspective uses is completely messed up compared to the grid you get with the mouse. When using glOrtho the grid is fixed to the mouse grid, but then you can only do 2D work, how would I set up perspective to get the same results as if I were using glOrtho?

\$\endgroup\$
6
  • \$\begingroup\$ What "grid" are you talking about? \$\endgroup\$ Jun 6, 2012 at 2:34
  • \$\begingroup\$ well in glOrtho and when you get mouse callback, it is done with a non-cartesian grid where (0,0) is the top right corner. gluPerspective sets up the screen to use a cartesian grid, but it's not a 1 to 1 scale with the pixels on the screen, I need a way to make one pixel equal one unit, so that translation of one will make the character move 1 pixel. \$\endgroup\$ Jun 6, 2012 at 3:02
  • \$\begingroup\$ What are you trying to do with a perspective projection? Are you trying to do some 2D thing with depth? \$\endgroup\$ Jun 6, 2012 at 3:03
  • \$\begingroup\$ it is a 3D top down shooter \$\endgroup\$ Jun 6, 2012 at 3:08
  • 3
    \$\begingroup\$ i think you should google something about unprojecting ;) \$\endgroup\$
    – Notabene
    Jun 6, 2012 at 7:46

2 Answers 2

4
\$\begingroup\$

If you want to use a perspective matrix for rendering, but an orthographic projection for retrieving mouse input, why not set up both?

// this example uses the free and open source OpenGL Mathematics library
// you can get it here: http://glm.g-truc.net/

glm::mat4x4 perspective = glm::perspective(90.f, 640.f / 480.f, 0.1f, 1000.f);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(perspective));

// <render game here>

// get mouse in world coordinates

glm::vec3 mouse = GetMousePosition3D();
glm::mat4x4 camera = GetCameraMatrix();
glm::mat4x4 orthographic = glm::ortho(0.f, 640.f, 480.f, 0.f);
glm::vec4 viewport(0.f, 640.f, 0.f, 480.f);

glm::vec3 mouse_world = glm::unProject(mouse, camera, orthographic, viewport);
\$\endgroup\$
0
1
\$\begingroup\$

Both glOrtho and gluPerspective are deprecated: OpenGL expects you to construct, manipulate, and use matrices in your own programs and shaders.

\$\endgroup\$
2
  • \$\begingroup\$ OpenGL 3.0+ also expects you to build VBO's (glBegin...glEnd and display lists are deprecated), connect a shader to everything and keep track of your own matrices, which can be a royal pain in the ass. \$\endgroup\$
    – knight666
    Jun 6, 2012 at 12:31
  • \$\begingroup\$ I guess I haven't done enough research yet, all this OpenGL 3 stuff is getting to my head, I hadn't realized that you have to setup all the stuff yourself. that also explains why I couldn't figure out how to change the depth buffer \$\endgroup\$ Jun 6, 2012 at 14:43

You must log in to answer this question.

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