I'm trying to read some OpenGL tutorials on the net. the problem is that I found some old ones that use gluPerspective()
. gluPerspective was deprecated in OpenGL 3.0 and removed in 3.1.
What function can I use instead?
I'm using C++ with latest FreeGlut installed.
This is a modified version of Dan's function, with simplified calculations from Unspecified Behavior.
Copied from one of my older projects:
You have to compute the matrix manually and then pass it to OpenGL.
Computing the matrix
This snippet of code is based on the gluPerspective documentation.
There is a C++ library called OpenGL Mathematics that may be useful.
Loading the Matrix in OpenGL 3.1
I am still new to the OpenGL 3.1 API, but you need to update a matrix on the GPU and then make use of it in your vertex shader to get the proper perspective. The following code just loads the matrix using glUniform4fv onto the video card.
A simple vertex shader from a random blog (found through stack overflow).
Use GLM.
Note, if you have
GLM_FORCE_RADIANS
defined then you should use radians in the perspective function, not degrees...The formula for various perspective matrices is documented in the Direct3D documentation.
Here is the formulation for
D3DXMatrixPerspectiveFovRH
, the right-handed perspective projection matrix: