I'm using the vecmath library to help with matrix maths while I'm converting opengl program to utilize glsl
better.
After writing question I guess I've 3 small questions:
display()
? i.e every frameThis is how I calculate my projection
and view
matrices within the reshape
function of my GLEventListener
..
Matrix4f mProjectionMatrix = createPerspectiveProjection(
60.0f, width / height, 0.1f, 100.0f);
Matrix4f mViewMatrix = new Matrix4f();
mViewMatrix.setIdentity();
Helper function ..
private Matrix4f createPerspectiveProjection(float fov, float aspect, float zNear, float zFar){
Matrix4f mat = new Matrix4f();
float yScale = (float) (1 / (Math.tan(Math.toRadians(fov / 2))));
float xScale = yScale / aspect;
float frustrumLength = zFar - zNear;
mat.m00 = xScale;
mat.m11 = yScale;
mat.m22 = -((zFar + zNear) / frustrumLength);
mat.m23 = -1;
mat.m32 = -((2 * zFar * zNear) / frustrumLength);
mat.m33 = 0;
return mat;
}
Inside display()
of GLEventListener
, I'm unsure about next steps.
Conceptually I have following in my head, are these the right steps I need to perform? ..
public void display(GLAutoDrawable gLDrawable){
// reset view matrix, needed?
// Calculate the model view projection matrix .. where does model matrix get it's values from?
// translate to Square1's position?
// rotate to Square1's angle?
// draw Square1 .. passing MVP matrix
// translate to Square2's position?
// rotate to Square2's angle?
// draw Square2 .. passing MVP matrix
}
This is my vertex shader ..
uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
void main() {
gl_Position = uMVPMatrix * vPosition;
}
Model View Projection is a common series of matrix transformations that can be applied to a vertex defined in model space, transforming it into clip space, which can then be rasterized.
Setting up the perspective projection matrix in OpenGL was done through a call to glFrustum. The function took six arguments: glFrustum(float left, float right, float bottom, float top, float near, float far); The implementation of this function can be found in the code below (line 20).
The matrix M, that contains every translations, rotations or scaling, applied to an object is named the model matrix in OpenGL. Basically, instead of sending down the OpenGL pipeline two, or more, geometrical transformation matrices we'll send a single matrix for efficiency.
View Matrix defines the position(location and orientation) of the camera, while model matrix defines the frame's position of the primitives you are going to draw. Projection matrix defines the characteristics of your camera, such as clip planes, field of view, projection method etc.
I keep three matrices in my OpenGL applications. They are the Model, View and the Projection matrices. All these matrices are sent to the shader using the uniforms and the shader does this.
gl_Position = mProj * mView * mModel * vec4(position, 1.0);
That is, the position is by default in the local object space. Multiplying it with the Model matrix applies the translation, rotation and scaling for that model, and the transformed vertex is now in the world space.
Now the vertex is multiplied by the View matrix, which applies the transformations of the camera, like the camera position and the orientation. Now the vertex will be in the eye space.
Finally the vertex is multiplied with the Projection matrix, applying the effect of either perspective or orthographic, which depends on what I chose to use. Finally after the transformation, the vertex will be in normalized device coordinates (ranging from -1 to +1) and will be rendered by the OpenGL.
Where does the model matrix get it's value from?
The model matrix is by default, an identity matrix. Whenever you want to only transform the entity and not the entire world, you will multiply the transformation matrix with the model matrix and set the result as the new model matrix. This allows you to keep separate attributes for each instance of a model in the scene.
Do I need to reset all model, view and projection matrices inside
display()
? i.e every frame?
No you don't. You can create the matrices once, but whenever the value of the matrices change on the client side, that is, your program, you have to update the uniforms in your shader program.
Do I reset mode, view and projection matrices after drawing each object?
No you don't. Usually you will keep one projection and one view matrix, but several model matrices per each object you have in your scene. The projection usually won't change in the entire course of the application, so there is no need to re-update it. The view matrix, is with your camera, so usually it will also be one for the entire scene. The model matrix, however will be different for each object, so you have to update it every frame.
Where do you get your Model matrix values from? i.e what are they generated from?
There are certain formulae which creates those matrices, like the helper method you have above which generates a perspective projection matrix based on the field-of-view and the aspect-ratio of the camera lens. It is hard to explain all those formulae here, so you can see the source code of my Transforms
class which has such functions.
See the source code for my Transforms class on my GitHub repository. It's not completely documented, but I think you can use them.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With