DEV Community

Cover image for Learning OpenGl part 2
NoticeableSmeh
NoticeableSmeh

Posted on

Learning OpenGl part 2

So here were using GLM for our vector math, I am not going to go into detail around vector math nor how this works because honestly I can not explain it very well. But essentially here we are declaring a vector with the glm lirbrary and then we are declaring a matrix that represents the translation along the defined diagonal in glm::mat4. Then taking trans * vec actually applies the actual movement of the vector.

In our vertex shader we declare a uniform and * our ve4 position with it. then we run this code inside of our program to get it spinning, exciting. One thing i learned here is that its very important to declare your diagonal and reset it at the beginning of everyloop, otherwise it accumulates.

So here we declare our model that we want to view and our viewport aswell as our projection, were doing a perspective view because that makes sense yknow. Its important to move the camera back in order to view our actually model, yknow it has to be behind it. Honestly this is all pretty self explanatory at this point.

heres the result yippi.

Now I followed the learnOpenGL tutorial and created a Camera class and implemented it. Let me walk you through it.

the Camera header files defines an enum for camera movement, each one of these is named given what they should accomplish.

here are the object variables associated to the camera class. Its pretty self explanatory if youve played an FPS game how these variables are needed, but I will explain Yaw and Pitch here. Yaw is a way of rotation, essentially the way you turn your head is a yaw type rotation. Pitch however is how you look up and down, quite simple actually. So these variables get declared when the constructor is called, you have to define the cameras position, rotation etc etc.

heres where were checkng the state at which is present. Ifthe forward state is active, we move forward.

here we have the mouseMovement processor function, it essntiall takes the offset of how much your mouse has moved since last frame and then it shifts the yaw and pitch accordingly to that offset.


heres kind of were the magic happens, heres where the actual cameras rotations are updated. And all that annoying math is involved, first it builds a front vector from the yaww and pitch using sie aqnd cosine. Then it normalizes that front so it has a length of 1.
Taking the cross product of the front and the worlds up vector genereates a perpendicular strafe direction to the right. Then it makes its Up direction by crossing the right and the front. In the end we are left with three unit vectors, front right and up.

so remember those glfw callback functions from part 1? Well were doing that here again but checking the mouse cursor pos everyframe, we pass in this function to handle that

we begin y converting the doubles that glfw gives and cast them to floats to match or camera math.

On our first frame we dont want any big jumps in camera rotations, so we handle that with a bool that checks on the first frame.
then we calculate the offsets of our mousemovement and store those positions for the next frame. We apply te sensitivity scaling then we finally feed that offset into the camera in processmousemovement from earlier.

thats it for this part.

here weere drawing all the cubes, excting stuff. 36 vertices in total, a lot of cubes.

Top comments (0)