# Help setting up a projection matrix

This topic is 3513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i am trying to make a software renderer and i got the 2D working but now i am trying to work out the projection matrix. I have found alot of good stuff to calculate it but in all of them it dosnt say how you work out the FOV angle and distance from the camera to the near plane beacuse if i have that i can work out the far plane using similer triangles. Can you just set them to whatever values you want? or is there some way to work it out? Thanks

##### Share on other sites
Quote:
 Can you just set them to whatever values you want?
Yes. The field of view, aspect ratio, and near and far plane distances are usually input parameters; everything else you can work out from there.

Does that answer your question? Or are you asking how to pick good values for those parameters?

##### Share on other sites
ah yea it does answer it because i thought you might have to put in some specific values or work them out somehow but some good values for them would be really helpful
Thanks

##### Share on other sites
Quote:
 ah yea it does answer it because i thought you might have to put in some specific values or work them out somehow but some good values for them would be really helpfulThanks
The aspect ratio can be whatever you want (its usually chosen to match the viewport). A common horizontal field of view is 90 degrees (you can increase or decrease the FOV relative to its initial value to create 'zoom' effects).

As for the near and far planes, they're usually chosen so as to include as much geometry as possible while maximizing depth buffer precision; basically, the near plane is placed as far away as you can get away with, while the far plane is placed as close as you can get away with. (This is assuming that your pipeline is similar to those of graphics APIs such as D3D and OpenGL.)

##### Share on other sites
For aspect ratio go with 4/3 or 16/9, depending on screen, or just divide screen width by height. Playing around with FOV will give interesting results, I use 45 degrees because at 90 degrees my scenes look extremely bent, but you can just bind a key to it and change it around until you find a value that works for you.

At least in OpenGL, the depth buffer is not linear, so you will have more precision up close and less precision for things in the back. The situation is probably similar for D3D. What matters more than the actual numbers is the ratio between them, but anything less than 1.0 for near plane is asking for trouble. More info here.

Usually if you set it to "reasonable" values it's good enough. To figure out how much you need, think about how large your view distance should be and set the far plane a bit beyond that. You can easily tell if your precision isn't good - things in the distance will start to overlap and you can see objects through other objects. This even happens in many games with things such as carpets, although there the problem is most likely that the distance between the two objects is too small and gets dropped. Floating point math can be scary.

##### Share on other sites
Quote:
 Original post by lightbringerAt least in OpenGL, the depth buffer is not linear, so you will have more precision up close and less precision for things in the back. The situation is probably similar for D3D. What matters more than the actual numbers is the ratio between them, but anything less than 1.0 for near plane is asking for trouble. More info here.

There is absolutely nothing wrong by itself with near clip plane distances less than 1. You said it yourself; what matters is the ratio. A near clip plane of 0.00001 is perfectly fine, assuming your objects are around 0.0001 units large, placed at that distance away from the viewpoint, and with a far clip plane about 100 times that of the near clip plane. Similarly, a near clip plane of 100 is a complete disaster of the scale of your objects are several orders of magnitude larger than that.

It's all about reasonable relative scene scale values. Less that 1 is perfectly fine, as long as the ratios are reasonable.

##### Share on other sites
Yes, it's the ratio that's important. The 1.0 thing is my misinterpretation of the OpenGL faq, which I scanned rather cursorily just to be on the safe side when answering, combined with something I read somewhere else (which was either wrong or my miss). It felt weird typing it, and now I know why, heh.

As an aside, I've seen four strategies mentioned in various places about how to deal with the z-buffer precision loss (which for a few select cases, mostly dealing with rendering in space-themed games, is simply unavoidable):

1) Ask for a buffer with more precision when creating display (but this generally doesn't work in my experience).

2) Render to FBO with your own attached z-buffer (exact mechanics I am not sure of - it seems that you can attach a zbuffer with higher precision to the FBO than what is normally available. Maybe a float buffer?).

3) Scale down everything past (zfar-znear)/2 and move it in, to lie between that and zfar. This is the strategy from the "matters of scale" article on Gamasutra.

4) Sort your rendering by depth, partition it in N distance blocks, and render each part separately, back to front, clearing the z-buffer and setting new near/far planes each time. I think this is how swiftcoder does it, or maybe it was Ysaneya's idea.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634142
• Total Posts
3015764
×