0

Using OpenTK, I've created a window (800x600) with a vertical FOV of 90°. I want to make a 2D game with a background image that fits on the whole screen. What I want is the plane at a variable z coordinate as a RectangleF.

Currently my code is:

var y = (float)(Math.Tan(Math.PI / 4) * z);
return new RectangleF(aspectRatio * -y, -y, 2 * aspectRatio * y, 2 * y);

The rectangle calculated by this is always a little to small, this effect seems to decrease with z increasing. Hoping someone will find my mistake.

genpfault
  • 51,148
  • 11
  • 85
  • 139
timedt
  • 1,302
  • 2
  • 9
  • 14

2 Answers2

0

I want to make a 2D game with a background image that fits on the whole screen.

Then don't bother with perspective calculations. Just switch to an orthographic projection for drawing the background, disabling depth writes. Then switch to a perspective projection for the rest.

OpenGL is not a scene graph, it's a statefull drawing API. Make use of that fact.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Well, I was actually planning on using the perspective for zoom and maybe some 3D-expansions, so switching to ortho isn't really the best solution. Also I'm more eager to know my mistake than to get a work-around. – timedt May 02 '13 at 17:41
  • 1
    @timedev Using an ortho projection to draw 2D isn't a workaround, it's the correct way of doing things. Even in 3D games 2D overlays are drawn using ortho projections. To zoom, you can change the ortho projection like so - http://www.opengl.org/discussion_boards/showthread.php/132061-help-with-orthographic-zoom – Ani May 02 '13 at 17:49
  • @timedev: It's not a workaround. It's the only proper way and the best solution to do this. Never try to overcomplicate things. OpenGL is not a scenegraph. You are *supposed* to switch projections where needed. – datenwolf May 02 '13 at 18:44
0

To make a 2D game using OpenGL, you should use an orthographic projection, like this tutorial shows.

Then its simple to fill the screen with whatever image you want because you aren't dealing with perspective.

However, IF you were to insist on doing things the way you say, then you'd have to gluProject the 4 corners of your screen using the current modelview matrix and then draw a quad in 3D space with those corners. Even with this method, it is likely that the quad might not cover the entire screen sometimes due to floating point errors.

Ani
  • 10,826
  • 3
  • 27
  • 46