Opengl set model to world matarix - opengl

There is making viewport matrix using glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);
But how to set model to world matrix like glGetflatv?

The code example you gave is getting the current value of the model view matrix which represents the transformation from model space to world space. This code does not affect the viewport which is set using glviewport(x,y,width,height). Assuming you are using fixed function OpenGL as indicated by your use of GL_MODELVIEW_MATRIX, then you can manipulate the modelview matrix using the built in functions or by loading your own matrix. See this related question for more details on setting the modelview matrix: How to update opengl modelview matrix with my own 4x4 matrix?.

Related

Multiple modelview matrices

I'm following the OpenGL tutorials on opengl-tutorial.org, and in tutorial 4 the author proposed, as a side "project", to send two "objects" to OpenGL and we have only ever rendered one object before. Specifically, he asks this:
Draw the cube AND the triangle, at different locations. You will need to generate 2 MVP matrices, to make 2 draw calls in the main loop, but only 1 shader is required.
We define the MVP matrix like this:
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
glm::mat4 Projection = glm::perspective(90.0f, 4.0f / 3.0f, 0.1f, 100.0f);
// Camera matrix
glm::mat4 View = glm::lookAt(
glm::vec3(0,0,3), // Camera is at (4,3,3), in World Space
glm::vec3(0,0,0), // and looks at the origin
glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
);
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 MVP = Projection * View * Model;
Now I understand that only one MVP matrix is used per object. But I have a few questions.
How do I create a MVP matrix for each object? Do I have to re-create the View and Model matrices for every object (IE specify a new one for each object)?
I thought glm::lookAt was used as the "camera" of sorts, so I shouldn't have to create a new "Camera" for each object, should I?
What does the Model matrix do? The author says it is a identity matrix right now, but if I change that to say 100, nothing happens! I thought that the Model matrix was the matrix that defined where the model was rendered, but I can't seem to change where the model is rendered without modifying the View matrix (which makes sense).
Typically, one uses one projection and one view matrix per "camera". The view matrix basically represents the position and orientation of the camera, and the projection matrix represents the properties of the camera (field of view, aspect, etc. in some way like a "lens" of a real camera, but don't stretch that analogy too far).
A model matrix typically is used to place the model in some global "world cooridnate system". So, you need a model matrix per object, so each object can be placed independently.
The world coordiante system is not strictly needed at all. What matters to the resulting picture is how the objects are placed relative to the camera - that is there ModelView enters the picture - as the composition of the model and the view transform, directly going from object space to some camera-relative eye space, bypassing any "world space" completely.
In your case, you even further combine this with the projection matrix, so you go directly from object space to clip space.
As you can see, the MVP matrix needs to change if any of the distinct matrices it is composed of does change. You could in theory precalculate an MVP per object - but that is typically not done, since the camera is moveable and all those MVP matrices would have to be recalculated anyways - there is not much to save here. So one typically keeps these matrices separate and multiplies them together directly before the draw call.
What does the Model matrix do? The author says it is a identity matrix right now, but if I change that to say 100, nothing happens!
What does this even mean, "changing a matrix to 100"? From the above code snippets it can be seen that your model matrix is indeed used and affects the MVP matrix. So changing it should have an effect - depending on what you change, of course. If you just changed the line
glm::mat4 Model = glm::mat4(1.0f);
to
glm::mat4 Model = glm::mat4(100.0f);
then it would actually not lead to any observeable change. You would get different clip space coords, but the differences would cancel each other out and the produced image would have been the same. That would be due to how homogenous coordinates work, and is a different matter entirely.

Using OpenGL for 2D

I have started with OpenGL and learned about model,view,and the projection matrix. From my understanding the projection matrix is only needed to project a 3D entity onto a 2D surface(the screen). So if I want to create a 2D game would I even need to mess around with the projection matrix?
It can still be nice to use a projection matrix for defining your coordinate system. By default a window will be defined between [-1,1] for both x and y no matter what resolution and aspect ratio. If you don't fix this using a projection matrix, you'll have to compensate in other ways. You want a square to render as a square, not a rectangle.
Depending on your GL version you can call glOrtho, construct it manually or use glm::ortho.
In my experience, working on the default [-1,1] system is extremely unpractical. For example : You don't want rotations around the z axis to deform your geometry.
No. When dealing purely with two dimensions, you can leave the projection matrix as the identity matrix.

How to update opengl modelview matrix with my own 4x4 matrix?

I have 4x4 matrix for object's transformations.
float mat44[16];
But i don't know how to update OpenGL ModelView matrix using my matrix. should i use glTranslatef()/glRotatef() with relavant values from my matrix or should i use glLoadMatrix(),glMultMatrix() ? Pls help. Thanks.
If you want to apply your transformation to current transformation already in OpenGL matrix stack, then you should write:
glMultMatrixf(mat44);
But if you want to discard what's currently on top of OpenGL matrix stack and use your own transformation, then you should write:
glLoadMatrixf(mat44);
For OpenGL-2.1 and earlier use glLoadMatrix or glMultMatrix, depending on the task at hand.
For OpenGL-3 and later set them as the right uniform for your shader using one of the glUniformMatrix variants.

Transform camera position in OpenGL

I draw a sphere with OpenGL which will be transformed by glRotate(), glScale(), glTranslate().
How could I get the absolute Position of the Object without calculating all transformations on my own?
I need the coordinates for setting the camera eye position to this point.
When using those transformations, you are manipulating the modelviewmatrix
To get the current one you can use ( http://linux.die.net/man/3/glgetfloatv )
void glGetFloatv(
GLenum pname,
GLfloat * params
);
for example:
float modelview[16];
// save the current modelview matrix
glPushMatrix();
// get the current modelview matrix
glGetFloatv(GL_MODELVIEW_MATRIX , modelview);
should get you the current matrix, multiplying the starting vectors/vertices/coordinates with this matrix will give you the absolute world coordinates.
EDIT: When making an application you usually want control over the absolute coordinates of each object, manipulate them, and use the opengl transformations to draw them where they are defined.
Objects are often modeled in one coordinate system, then scaled, translated, and rotated into the world you're constructing. World Coordinates result from transforming Object Coordinates by the modelling transforms stored in the ModelView matrix. However, OpenGL has no concept of World Coordinates. World Coordinates are purely an application construct.
Object Coordinates are transformed by the ModelView matrix to produce Eye Coordinates.
From opengl.org:
9.120 How do I find the coordinates of a vertex transformed only by the ModelView matrix?
It's often useful to obtain the eye coordinate space value of a vertex (i.e., the object space vertex transformed by the ModelView matrix). You can obtain this by retrieving the current ModelView matrix and performing simple vector / matrix multiplication.
To get the matrix use something like this
float fvViewMatrix[ 16 ];
glGetFloatv( GL_MODELVIEW_MATRIX, fvViewMatrix );
You can read more about opengl transformations here: http://www.opengl.org/resources/faq/technical/transformations.htm

Preserving the original axis system while rotating with openGL

I'm implementing an arcball with openGL (on cpp).
Say, I have an object in the center of the axes system and i want to rotate in several times acording to the original (world) axes.
But, after the first rotation, the axes are changed and all further rotations goes wrong.
Any ideas?
Thanks.
Supply the object with it's own orientation axes (modelview matrix), and then multiply that by the rotation matrices. Check Wikipedia for info on how to construct rotation matrices.
I had to do the same thing myself in an OpenGL ES application, which I describe in a writeup about it here. The original crude approach read the current model view matrix and manipulated it to produce the desired effect:
GLfloat currentModelViewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, currentModelViewMatrix);
glRotatef(xRotation, currentModelViewMatrix[1], currentModelViewMatrix[5], currentModelViewMatrix[9]);
glGetFloatv(GL_MODELVIEW_MATRIX, currentModelViewMatrix);
glRotatef(yRotation, currentModelViewMatrix[0], currentModelViewMatrix[4], currentModelViewMatrix[8]);
This will work, but be aware that the two glGetFloatv() calls will slow your rendering by halting the pipeline. I've since replaced this code with calculations that I perform on my own internal copy of the model view matrix, then I simply write the internally manipulated model view matrix after each rotation. This removes the need to do the expensive matrix read operations.
Add xAngle and yAngle to the current matrix.
Matrix.rotateM(matrix, 0, xAngleADD, matrix[1], matrix[5], matrix[9]);
Matrix.rotateM(matrix, 0, yAngleADD, matrix[0], matrix[4], matrix[8]);
gl.glMultMatrixf(matrix, 0);