OpenGL 3D Game Tutorial 8: Model, View & Projection Matrices

preview_player
Показать описание
LWJGL tutorial series on how to create a 3D Java game with OpenGL!

This week create a rotating 3D cube using model, view and projection matrices!

Full code download for this episode:

LWJGL Wiki:

Common Problems:

-Don't forget to multiply the transformation matrix with the vertex position in the vertex shader. This was shown at the end of the last tutorial.

-Calling the getAllUniformLocations() method: We did this in the last episode in the ShaderProgram class constructor, a lot of people leave this out accidentally.

- In the getAllUniformLocations() method make sure that you're setting the value of the "location_transformationMatrix" variable. Getting the location is not enough, you need to store it too!

-After adding the projection matrix your entity's z position needs to be more negative than -NEAR_PLANE, otherwise it will be outside the view of the "camera". Set your entity's z position to -1 like I did at 11:38 in the video.

-The order that you multiply the matrices together in the vertex shader matters! Matrices are not like normal numbers, and multiplying them together in a different order will give you a different result.

-The spelling of the uniform variable names: The names must be spelled EXACTLY the same in the StaticShader getAllUniformLocations() method as they are in the shader code.

-The projection matrix code: Double check the code for the projection matrix, I've had quite a few emails from people who have had errors, and it has ended up being a mistake in the createProjectionMatrix() method.

-When creating the transformation matrix make sure that you didn't copy and paste the 3 rotation lines and forget to change them for each axis. Make sure that it looks like this:
Check the vectors and the rx, ry, and rz!

End of video music- Kai Engel, "Waking Stars":
Рекомендации по теме
Комментарии
Автор

Quite a few common mistakes being made on this episode. Make sure to check the following things if your code doesn't work:

>The bit at the end of the last tutorial! A lot of people have missed this out for some reason =P

>Calling the getAllUniformLocations() method: We did this in the last episode in the ShaderProgram class constructor, a lot of people leave this out accidentally.

>The order that you multiply the matrices in the vertex shader matters! Make sure you do it in the order shown in the video.

>The spelling of the uniform variable names: The names must be spelled EXACTLY the same in the StaticShader getAllUniformLocations() method as they are in the shader code.

>The projection matrix code: Double check the code for the projection matrix, I've had quite a few emails from people who have had errors, and it has ended up being a mistake in the createProjectionMatrix() method.

>After adding the projection matrix your entity's z position needs to be more negative than -NEAR_PLANE, otherwise it will be outside the view of the "camera". Set your entity's z position to -1 like I did at 11:38 in the video.

>In the getAllUniformLocations() method make sure that you're setting the value of the variable. Getting the location is not enough, you need to store it too!

>If all else fails you can download my code from the description. Use it to find your error rather than just copy and pasting it all!

Hope that helps :)

ThinMatrix
Автор

I decided to test the transformations by spinning it 1 degree every frame. I have never spent so long playing a game with no features.

Double-Negative
Автор

You deserve so much more recognition for this series... I love it so far lol

ishansethi
Автор

LWGJL 3 is suggested to be used with JOML.

For those using JOML (in this time frame till it gets updated v 1.9.3), here's the createProjectMatrix method:

private void createProjectionMatrix() {
projectionMatrix = new Math.toRadians(FOV), 1.0f, NEAR_PLANE, FAR_PLANE);
}

Manxmanir
Автор

Great explanation on the different matrices, I'm glad you didn't just throw all three together and run it. You took the time to explain and show each one working. Great job, keep it up.

John-jbfw
Автор

This is by far the TOPMOST quality tutorial I've ever seen on earth! Nice pace, accurate operation without any hesitation, ultra clear and skilled elaboration. You are born to be a teacher!

jomo
Автор

for those which are using GLFW for inputs handling, in case of need, here is how i managed the thing.
create a Keyboard class for inputs handling:

public class Keyboard extends GLFWKeyCallback {
public static boolean[] keys = new boolean[65536];
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
keys[key] = action != GLFW.GLFW_RELEASE;
}
public static boolean isKeyDown(int keycode){
return keys[keycode];
}
}

(if you have a better way for keep tracking of inputs pressed instead of declaring that huge boolean array please tell me, i didn't go deep in my research becouse of time. i'm just trying to learn and if it works so far, for me is not that big of a deal)

then in the Camera constructor add these lines:
Keyboard keyboard = new Keyboard();
, keyboard::invoke);

in the move method now you just have to call in the if statement: etc

hope this helps

ShinigamiDN
Автор

Dude can I please say huge +rep to you, this series has by far more than anything else I could find helped me tremendously to learn the basics of LWJGL and of game engine development, it's been a huge help so far :)
Thank you so much and I really appreciate the attention to detail towards explaining every little thing!

Azamorn
Автор

If the square didn't move after executing at 05:00, my mistake was i forgot to put gl_Position = transformationMatrix * vec4(position, 1.0);
in the vertexShader.txt file

Triplaglol
Автор

You are amazingly good at explaining everything! xD

TheEggHealer
Автор

Thank you so much for this tutorial series! I am currently watching it for the fith time probably because it is quite challenging to remember all of the OpenGL function calls. Your videos have helped me a lot and I find them quite enjoyable to watch.

weinsim
Автор

OH YEAH, finally got shaders working in python3 using pyglet! had to write my own matrix classes and stuff but oh well...
a few things you probably should note in your video (as an annotation):
1. judging from your code at 10:23 the transformation matrix should look like this (with the fov being radians):
r1: (1/tan(fov/2)) 0 0 0
r2: 0 (1/tan(fov/2))*a 0 0
r3: 0 0 -zp/zm -(2*Zfar*zNear)/zm
r4: 0 0 -1 0
notice how m00 and m11 are different from 7:38
2. make sure to *transpose your matrices* before handing them over to opengl! otherwise it'll look pretty... strange

thanks for your tutorials Karl! they were *really* helpful to me and made me understand how all of this mess actually works! and over the long time I've come back to your channel I've probably watched every single one of them multiple times -and then gave up on OpenGL over and over again LOL- :)
I'm not using LWJGL but of course this still applies to pretty much every language

ThunderDraws
Автор

11:07 "and then we never have to load that projection matrix up to the shader again"


_laughs in resizeable window that doesn't break the entities dimensions_

crypticant
Автор

ive not been more happy to see a spinning block with my profile picture on it. i love being happy at the simplest things in life. whether it be gta, or a spinning cube. i am at peace with this world.

ikyyntts
Автор

I like how we have to revisit and update a lot of the methods we are creating earlier in the series. Helps with memorization.

black_squall
Автор

For anyone having problems with the animation not working, It seems like lots of people missed 15:50-16:15 from the previous tutorial (OpenGL 3D Game Tutorial 7: Matrices & Uniform Variables) ... myself included!

garrettord
Автор

When you generated getters and setters for the Entity class, my first thought was: are you going to change them? Isn't it logical to make the fields public? But no. *GENIUS JAVA*

PS: Nothing personal Thin Matrix, I'm just a C++ programmer that can't look at Java code. Despite that, I still love your tutorials, well done!

goat--ru
Автор

For anyone trying to follow this tutorial in C++: If you try to call derived methods from the base class constructor, it will call the virtual methods from the base class. Because of this the transformation matrix will not be initialized and no rectangle will appear. I solved this by adding another method to the base class that calls the derived methods bindAttribute() and getAllUniformlocations() after the shader object is constructed. Hope this helps.

TheMrmeadow
Автор

loving you tutorials !! ive been following your dev logs for well over a year on a few different channels that ive had and as soon as i seen the first tutorial i had to restrain myself from watching any more because i wanted to watch them in bulk but i started a day or two ago and i just cant stop watching them i learn so much !! also seeing you reply to so many comments is a great thing !!! youre awseome thanks for the videos man :)

kyle
Автор

Another fix if the quad does not appear: Make sure that, in Main.java, "shader.start();" gets called BEFORE

You're welcome :)

DasEtwas