www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Perspective Projection

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
I again am having issues with OpenGL, this time with the 
projection matrix.  Using gl3n, I have the following code:

```d
     // model matrix
     mat4 trans = mat4(0f);
     trans.make_identity();
     trans = trans.rotatex(radians(-55));

     // view matrix:
     mat4 view = mat4(0f);
     view.make_identity();
     view = view.translation(0f ,0f, -3f);

     // projection matrix
     mat4 projection;
     projection.make_identity();
     projection = projection.perspective(800f, 600f, radians(45f), 
.1f, 100f);
```

I am binding all of these matrices to the correct locations, and 
this seems to be the gl3n equivalent to what is given in the 
OpenGL tutorial:

```c
         // create transformations
         glm::mat4 model         = glm::mat4(1.0f); // make sure 
to initialize matrix to identity matrix first
         glm::mat4 view          = glm::mat4(1.0f);
         glm::mat4 projection    = glm::mat4(1.0f);
         model = glm::rotate(model, glm::radians(-55.0f), 
glm::vec3(1.0f, 0.0f, 0.0f));
         view  = glm::translate(view, glm::vec3(0.0f, 0.0f, 
-3.0f));
         projection = glm::perspective(glm::radians(45.0f), 800f / 
600f, 0.1f, 100.0f);
```

Everything displays fine (with orthographic projection, of 
course) if you leave the projection as the identity matrix, but 
setting it as I have done results in a blank screen.  I assume it 
has to do with the values of

`(projection * view * trans * vec4(vertex, 1)).w` not being 1.  
Should I just use a different library, and if so, how to use it 
to generate a perspective matrix?
Jul 28 2023
parent reply Dennis <dkorpel gmail.com> writes:
On Friday, 28 July 2023 at 16:08:43 UTC, Ruby The Roobster wrote:
 Everything displays fine (with orthographic projection, of 
 course) if you leave the projection as the identity matrix, but 
 setting it as I have done results in a blank screen.
How do you pass the matrix to OpenGL? Be careful that gl3n uses row major matrices, but OpenGL uses column major matrices, so you either need to transpose it yourself, or pass `true` to the `transpose` argument in `glUniformMatrix4fv`.
Jul 28 2023
parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Friday, 28 July 2023 at 16:20:26 UTC, Dennis wrote:
 On Friday, 28 July 2023 at 16:08:43 UTC, Ruby The Roobster 
 wrote:
 Everything displays fine (with orthographic projection, of 
 course) if you leave the projection as the identity matrix, 
 but setting it as I have done results in a blank screen.
How do you pass the matrix to OpenGL? Be careful that gl3n uses row major matrices, but OpenGL uses column major matrices, so you either need to transpose it yourself, or pass `true` to the `transpose` argument in `glUniformMatrix4fv`.
Thank you very much! Changing GL_FALSE to GL_TRUE in all of the `glUniformMatrix4fv` calls resulted in something being displayed, though very incorrectly. As it turns out, there is an inconsistency in the gl3n API, that you write a rotation matrix in radians, but you must write the FOV in degrees. After that, it worked as it was supposed to.
Jul 28 2023