In order to use that library we just need to add another dependency to our pom.xml file. We will choose the easy path and use a specific library for dealing with math operations in LWJGL which is called JOML (Java OpenGL Math Library).
#Java lwjgl how to resize vao code#
We could choose to write all that code by our own from scratch or use an already existing library. So we need to handle a set of mathematical entities such as vectors, matrices and include the operations that can be done on them.
The result will be another vector that will contain the projected version. In order to obtain the projected coordinates of a given point we just need to multiply the projection matrix by the original coordinates. Where aspect ratio is the relation between our screen width and our screen height ( a = w i d t h / h e i g h t a=width/height a = w i d t h / h e i g h t). The mathematical representation of that matrix is as follows (don’t be scared). The following picture shows a top view of that area.Ī projection matrix will correctly map 3D coordinates so they can be correctly represented on a 2D screen. The vision area of that camera will be a truncated pyramid. You can think about the projection matrix as a camera, which has a field of view and a minimum and maximum distance. The main characteristics of matrices, related to 3D graphics, is that they are very useful to transform points in the space. Matrices have a number of basic operations that can be applied to them (such as addition, multiplication, etc.) that you can consult in any maths book. For instance, here you can see a 2x2 matrix (2 rows and 2 columns). A matrix order is the number of rows and columns. Each number inside a matrix is called an element. The projection matrix will also consider our field of view and how far the maximum distance is that should be displayed.įor those not familiar with matrices, a matrix is a bi-dimensional array of numbers arranged in columns and rows. It also will handle the distance so objects far away from us will be drawn smaller. The projection matrix will take care of the aspect ratio (the relation between size and height) of our drawing area so objects won’t be distorted. Now that we have diagnosed the problem, how do we do this? The answer is using a projection matrix or frustum.
We need to correctly project our coordinates. OpenGL just uses the coordinates we are passing, so we must take care of this. You could have two objects of different sizes, one closer and smaller and one bigger and further that could be projected correctly onto the screen with the same size (those would have same x and y coordinates but different z). The z coordinate tells OpenGL that an object is closer or farther away, but OpenGL does not know anything about the size of your object. Should this not be handled by the z coordinate? The answer is yes and no. But we are drawing them with the same x and y coordinates.īut, wait. Why is this happening? Objects that are further away should be drawn smaller than objects that are closer. What do you see? The quad is exactly drawn in the same place no matter if it’s displaced along the z axis. Modify the z coordinate of our quad from 0.0 to 1.0 and to -1.0. So, if our window has a size of 900x480, OpenGL coordinates (1,0) will be mapped to coordinates (900, 0) creating a rectangle instead of a quad.īut, the problem is more serious than that. Those coordinates are mapped, considering our window size, to window coordinates (which have the origin at the top-left corner of the previous figure). The following figure shows the OpenGL coordinates (just for x and y axis). That is, when we say that a vertex has a value for coordinate x of 0.5 we are saying to OpenGL to draw it at x position 0.5 on our screen. If you revisit our vertex shader code we are just passing our coordinates directly. You can even change the width of the window from 600 pixels to 900 and the distortion will be more evident.
If you look carefully at it, it more resembles a rectangle. Let’s get back to our nice coloured quad we created in the previous chapter.