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.
Related
I'm getting started with a OpenGL ES app (C++/SDL) running on a Raspberry PI, that I'll want to output through a pocket projector.
I want to give the user the ability to correct the distortion caused by aiming the projector from a direction that is non-normal to the surface. The surface will also be smaller than the projected area. To do this, the user will be able to "move" the 4 corners of the projection window independently to match the surface.
I was planning to do this by solving a simple linear system of equations that transforms the original corners (in the current projection matrix coordinates) to the corners set by the user, and just push this resulting matrix on top of the GL_PROJECTION matrix stack.
However... I found out that I can't "read" the projection matrix in OpenGL ES:
float matrix[16];
glGetFloatv(GL_PROJECTION_MATRIX, matrix);
In particular, the GL_PROJECTION_MATRIX symbol doesn't exist... and I've even read that there's no such thing as a projection matrix in OpenGL ES (something I find hard to believe, but I'm totally new to it, so...).
Any idea of a workaround for reading this matrix, or maybe an alternative approach to do what I'm trying to do?
Thanks!
OpenGL ES 1.x has a projection matrix, and you should be able to get the current value with exactly the code you have in your question. See http://www.khronos.org/opengles/sdk/1.1/docs/man/glGet.xml for the documentation confirming this, or the header file at http://www.khronos.org/registry/gles/api/GLES/gl.h.
ES 2.0 and higher are a different story. ES 2.0 eliminates most of the fixed pipeline that was present in ES 1.x, and replaces it with shader based rendering.
Therefore, concepts like a built-in projection matrix don't exist anymore in ES 2.0 and are replaced with the much more flexible concept of shader programs written in GLSL. If you want to use a projection matrix in ES 2.0, you have a matrix variable in your vertex shader code, pass a value for the matrix into the shader program (using the glUniformMatrix4fv API call), and the GLSL code you provide for the vertex shader performs the multiplication of vectors with the projection matrix.
So in the case of ES 2.0, there is no need for a glGet*() call for the projection matrix. If you have one, you calculated it, and passed it into the shader program. Which means that you already have the matrix in your code. Well, you could get any matrix value back with the glGetUniformfv() API call, but it's much easier and cleaner to just keep it around in your code.
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?.
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.
I know that the normal transformation matrix is the inverse of the transpose of the model/view/projection matrix, but it looks like "inverse" was only added in GLSL 1.4 and I can't find a "transpose". Do I have to just copy and paste a bunch of math into my GLSL? (If so, is there a nice authoritative open-source place I can copy from?)
To be clear, what I'm asking is "How do I calculate gl_NormalMatrix without using deprecated APIs"?
This is normally handled by computing the transpose of the inverse of the modelview matrix
N = (M^-1)^T
on the CPU, then uploading the matrix just like uploading any other matrix.
just to clarify there's also transpose and if you don't do any scale the normal matrix is the 3x3 submatrix, in glsl you can do
normal = mat3(model_matrix) * v_normal;
So I know that glDrawPixels is deprecated. Is there any function that does the same thing?
I thought of using textures, but they are modified by the current matrix, unlike pixels that are drawn by glDrawPixels.
I thought of using textures, but they are modified by the current matrix
The "current matrix" is deprecated in 3.0 and removed in 3.1+ as well. So if you're not using glDrawPixels, you wouldn't be using matrix functions either. So it's nothing to be concerned about.
You could use a fragment shader where a function of gl_FragCoord is used to sample a rectangular texture.
Alternatively, you could use a more traditional approach and just set up your transformation matrices to approximate the pixel coordinate system of your window and then draw a textured quad with your image.
You need to draw a quad with :
A specific ModelViewProjection Matrix which will place it where you want (as Nicol said, there is no "current" matrix anymore)
A simple vertex shader which will use said Matrix to actually transform the vertices
A simple fragment shader which will sample the texture
And of course, adequate texture coordinates.
For starters, use an Identity matrix, and a mesh with X and Y coords between 0 and 1.
You might want to use a mix of http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ and http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-11-2d-text/ (though the latter one should be improved regarding the matrix used)