last day (16 days later) » 

8:48 PM
0
Q: OpenGL Projection Matrix showing Orthographic

Mike HayesI got an orthographic camera working however I wanted to try and implement a perspective camera so I can do some parallax effects later down the line. I am having some issues when trying to implement it. It seems like the depth is not working correctly. I am rotating a 2d image along the x-axis t...

 
Where is a code for projection matrix initialization?
 
@MichaelNastenko - It is first initialized in the list to just (1.0f) - identity matrix so it will not have a null value, then it is re-initialized in the init function - Forgot that I commented it out and redid it - updated my post to show it. - Also had the wrong view matrix, updated it.
 
FoV of 45 is too small. You won't notice much difference from ortho. Try 90.
 
@MichaelNastenko - changed to 90, it just made it look like it was further back. Still showing an ortho look. image to 90fov and rotation to 70
 
How are M,V,P defined? M needs to be _projectionMatrix
 
8:48 PM
It means that perspective projection works. What kind of object you try to render? It looks like a sprite. If it's screen aligned you won't see much difference from ortho. Also you can add stones more objects with different distance to camera. In ortho they'll have same size. In perspective further objects will be smaller.
 
Indeed, your multiplication order is most likely wrong. Furthermore, consider computing a combined model view projection matrix (P * V * M in your case I would guess) on the CPU and then only put that combined matrix into a uniform. There's no point in copying all three matrices to the GPU just so that you can then go about multiplying all three matrices over and over again for each vertex…
 
@SpentDeath - Ah, I have been going off of the opengl.com tutorials. The differentiate them and use M as a modelMatrix. learnopengl.com/Getting-started/Coordinate-Systems - So what would be the P matrix if _projectionMatrix should be M?
@MichaelNastenko - Yes, it is a sprite. From what I was going off of, it should show something like this - link to opengl site - Also, added another sprite and if I change the Z in either direction, it does not render to the screen.
 
@Applez I'm not sure about the convention you are using, I'm suggesting something more informative: instead of M * V * P use clip * camera * world. clip is _projectionMatrix, camera is _viewMatrix and world is _modelMatrix. I would suggest that _modelMatrix should be part of the model, not the camera class.
 
Oh, just noticed - you shouldn't modify gl_Position.w.
 
@MichaelNastenko - Winner winner! Thanks my man! Super big oversight on my end... Thanks again my man! edit - I am new, how do I mark yours as the answer?
 
8:48 PM
Nicely spotted @MichaelNastenko. I somehow completely overlooked that…
@Applez You should still check your multiplication order though, it's almost certainly wrong…
 
@MichaelKenzel - Alright, I will take a look! It seems I can't use world coordinates so I am assuming it's the multiplication.
 
It should most likely be P * V * M rather than M * V * P given that you're using GLSL and glm and are apparently following the Matrix × vector convention…
 
@MichaelKenzel - Got the P * V * M working, should this convert it to world space using my viewMatrix? Or is there other math that needs to do calculation there.
 
Look at the expression P * V * M * v. The vector v is first multiplied with M. The result of that (again a vector) is then multiplied with V. And the result of that (still a vector) is then multiplied with P. So you're transforming the vector v with the model matrix (which takes it from object to world space), then with the view matrix (which takes it from world to camera space) and finally with the projection matrix (which takes it from camera to projective space, effectively projecting it onto the screen)…
The trick is to realize that, due to the way matrix transforms are applied to vectors (matrix is multiplied onto the vector from the left), you have to read these from right to left. At least if you're following the common mathematical convention.
 
@MichaelKenzel - Sorta makes sense. I am going to do some research on matrices and try to get this working lol. Thanks for all the help everyone.
 
8:48 PM
What people often struggle with when going from 2D to 3D is that coordinates are really arbitrary. There is no inherent unit like "pixels". What coordinates mean in the end is just a result of how things are scaled in relation to each other. When you used an orthographic projection, you most likely used a matrix that scaled everything in precisely such a way that moving stuff by 1 unit meant it would move 1 pixel on the screen. You're now using a projection matrix that basically defines the screen to range from -1 to 1 (if we ignore aspect ratio) because that's what glm::perspective() does…
In theory, you could just apply a scaling that divides x and y by width/2 and height/2 before the projection matrix. Note, however, that, as soon as perspective enters the game, this won't be all that useful because stuff is generally going to be distorted in some way…
 
@MichaelKenzel - I guess a better question would be what is the most practical way to use screen coordinates to translate them to NDC (-1 -> 1) because programming a graphic to be at -0.03 etc. could be a pain and probably not very practical. I appreciate all the info by the way.
 
@MikeHayes "what is the most practical way to use screen coordinates to translate them to NDC" - inverse projection matrix :-)
 
 
1 hour later…
9:59 PM
@MichaelNastenko Awesome thanks!
 

  last day (16 days later) »