Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to compute the matrix manually and then pass it to OpenGL.</p> <h2>Computing the matrix</h2> <p>This snippet of code is based on the <a href="https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml" rel="nofollow noreferrer">gluPerspective documentation</a>.</p> <pre><code> void BuildPerspProjMat(float *m, float fov, float aspect, float znear, float zfar) { float f = 1/tan(fov * PI_OVER_360); m[0] = f/aspect; m[1] = 0; m[2] = 0; m[3] = 0; m[4] = 0; m[5] = f; m[6] = 0; m[7] = 0; m[8] = 0; m[9] = 0; m[10] = (zfar + znear) / (znear - zfar); m[11] = -1; m[12] = 0; m[13] = 0; m[14] = 2*zfar*znear / (znear - zfar); m[15] = 0; } </code></pre> <p>There is a C++ library called <a href="http://glm.g-truc.net/" rel="nofollow noreferrer">OpenGL Mathematics</a> that may be useful.</p> <h2>Loading the Matrix in OpenGL 3.1</h2> <p>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 <a href="http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml" rel="nofollow noreferrer">glUniform4fv</a> onto the video card.</p> <pre><code>{ glUseProgram(shaderId); glUniformMatrix4fv(glGetUniformLocation(shaderId, "u_proj_matrix"), 1, GL_FALSE, theProjectionMatrix); RenderObject(); glUseProgram(0); } </code></pre> <p>A simple vertex shader from a random <a href="http://blog.mgrundmann.com/?p=4" rel="nofollow noreferrer">blog</a> (found through stack overflow).</p> <pre><code>attribute vec4 a_position; attribute vec4 a_color; varying vec4 v_color; uniform mat4 u_proj_matrix; uniform mat4 u_model_matrix; void main() { mat4 mvp_matrix = u_proj_matrix * u_model_matrix; v_color = a_color; gl_Position = mvp_matrix * a_position; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload