View Full Version : how to get modelMatrix?

cl781121

07-02-2016, 07:29 AM

glm::mat4 projectionMatrix = glm::perspective(camera.Zoom, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);

here ,i can get the "projectionMatrix " from "glGetDoublev(GL_PROJECTION_MATRIX,projectionMatrix );"

glm::mat4 viewMatrix;

viewMatrix = camera.GetViewMatrix();

i can get viewMatrix from "glGetDoublev(GL_MODELVIEW_MATRIX,modelViewMtx);"

glBindVertexArray(containerVAO);

glm::mat4 model;

glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

glDrawArrays(GL_TRIANGLES, 0, 36);

glBindVertexArray(0);

but i can not get the "model",i dont understand that mean

in glsl

#version 330 core

layout (location = 0) in vec3 position;

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

void main()

{

gl_Position = projection * view * model * vec4(position, 1.0f);

}

thanks

Alfonse Reinheart

07-02-2016, 07:37 AM

here ,i can get the "projectionMatrix " from "glGetDoublev(GL_PROJECTION_MATRIX,projectionMatri x);"

No, you can't. Ignoring the fact that your code already filled in `projectionMatrix` using GLM functions, your matrix is a float matrix. `glGetDoublev`, as the name suggests, fetches doubles. So you'd have to use `glGetFloatv`. Though why you would want to makes no sense, as you've already computed `projectionMatrix`.

but i can not get the "model",i dont understand that mean

Compatibility OpenGL has no such matrix. Nor does it need one. The reason its calls the matrix "modelview" is because it transforms from model space to view space. That is, to your GLSL code, the GL_MODELVIEW matrix is analogous to `view * model`. What your shader uses 2 matrices to do, compatibility GL only uses one.

BBeck1

07-02-2016, 10:59 AM

Sounds like the version of OpenGL you are using does not use matrices. I skipped straight to OGL 4, so I have no idea how it works before that.

But in OGL 4, you set your model matrix = an identity matrix. The model matrix's job is to hold the position, orientation, and scale of your model. So, you have one for every model (or possibly more than one but that's more advanced).

The view matrix holds the position and orientation of your camera (I'm not sure scale makes sense with a camera). Note that it is the same thing as a model (world) matrix except it is inverted (it does everything backwards).

The projection matrix is the odd ball. It's job is to hold all the math that is required to convert your 3D world into a 2D image that can be drawn to your 2D computer monitor.

The other matrices (translation, rotation, scale, etc.) are all to manipulate the model/world matrix (or the view matrix if you like but don't forget to invert).

An identity matrix is basically an empty matrix. So, set your model matrix to an identity matrix to place it exactly as it was placed in the 3D modeling program it was exported from. Then you can move it around from there.

cl781121

07-13-2016, 10:21 AM

thank for reply,and yes ,i set modelMatrix to a identity matrix,but should i upload modelMatrix to shader when i use like gltranslatef or glscale etc? or just upload a identity matrix as madelMatrix all the time

GClements

07-13-2016, 12:29 PM

thank for reply,and yes ,i set modelMatrix to a identity matrix,but should i upload modelMatrix to shader when i use like gltranslatef or glscale etc? or just upload a identity matrix as madelMatrix all the time

If you're using shaders, you shouldn't really be using glTranslatef() etc. You should construct the matrices in the application and pass them to OpenGL via glUniformMatrix4fv() etc.

BBeck1

07-13-2016, 10:14 PM

The projection matrix is the weird one; it holds the math to convert the 3D world to 2D. The model (or world) matrix holds the orientation, position, and scale of the model. The view matrix holds the orientation and position of the camera. The view matrix is an inverted matrix and is backwards compared to a model/world matrix.

So, you could tell your shader every frame the orientation and position of your model every frame. Or, you could have a model/world matrix that holds the orientation and position (and scale/skew) of your model and just give that to the shader.

The identity matrix is a good place to start. It says that the position is at the origin and the orientation is aligned with the Z axis. It's neutral. It's a given starting point. You can rotate from their.

Personally, I don't like the way GLM does some of the math because it seems to me to confuse the issue and do extra un-needed steps, but I'm still learning.

But think of it this way: the identity matrix is the "empty" matrix that will put your model in the exact same position and orientation it was when it was in the modeling program whether that was Blender, Max, or Maya. For a camera, the view matrix as an identity matrix will put it at the origin aligned with the Z axis. The view matrix is inverted: you have to do everything the opposite to make it function correctly. This gets into the theory of what the view matrix is and why, but the quick answer is that the view matrix is basically an inverted world matrix that defines the camera.

So, when you want to move something, you build a translation matrix that describes a change in position or a distance in a given direction. Then you combine that with the existing matrix whether it's a view (camera) or world/model matrix. So, I build a matrix that says "4 units along the Y axis" and if I combine (multiply) that with the original matrix the resulting matrix will give you the orientation and position that is the result of that movement, which you can store back as the original matrix.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.