Question

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.

Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top