Calculating the Normal Matrix in OpenGL - c++

The following site says to use the model_view matrix when computing the normal matrix (assuming we are not using the built in gl_NormalMatrix): (site)Light House. I have the following algorithm in my program:
//Calculate Normal matrix
// 1. Multiply the model matrix by the view matrix and then grab the upper left
// corner 3 x 3 matrix.
mat3x3 mv_orientation = glext::orientation_matrix<float, glext::column>(
glext::model_view<float, glext::column>(glext_model_, glext_view_));
// 2. Because openGL matrices use homogeneous coordinate an affine inversion
// should work???
mv_orientation.affine_invert();
// 3. The normal matrix is defined as the transpose of the inverse of the upper
// left 3 X 3 matrix
mv_orientation.transpose();
// 4. Place this into the shader
basic_shader_.set_uniform_3d_matrix("normal_matrix", mv_orientation.to_gl_matrix());
Assuming most statements above are correct in the aforementioned code. Do you not include the projection matrix in the computation of the normal matrix? If not why, does the projection matrix not affect the normals like they do points?

That's because projection is not an affine transformation. Projections don't maintain the inner product and then they don't maintain the angles. And the real angles that have effect on the light diffusion and reflection are the angles in the affine 3d space. So using also the projection matrix would get you different angles, wrong angles, and hence wrong lights.

Do you not include the projection matrix in the computation of the normal matrix?
No. Normals are required for calculations, like illumination, happening in world and/or view space. It doesn't make sense from a mathematical point of you to do this after projection.
If not why, does the projection matrix not affect the normals like they do points?
Because it would make no sense. That normals should not undergo projective transformation was the original reason to have a separate projection matrix. If you'd put normals through the projection they'd loose their meaning and usefullness.

Related

How to calculate a linear tapering transformation matrix

I need to calculate a 4x4 matrix (for OpenGL) that can transform a 3d object on the left to one on the right. Transformation applied only in one axis.
EDIT:
The inputs are a given 3d object (points) to be deformed and a single variable for the amount of deformation.
A picture represents a cube projected to plane showing only relevant changes. There are no changes in the axis perpendicular to view plane.
The relative position of these two objects is not relevant and used only to show "before and after" situation.
******* wrong answer was here *******
It's somewhat opposite to 2D perspective matrix with further perspective division. So, to do this "perspective" thing inversely, you need to do something opposite to perspective division then multiply the result by an inverted "perspective" matrix. And though the perspective matrix may be inverted, I have no idea what is "opposite to perspective division". I think you just can't do it with matrices. You'll have to transform Y coord of each vertex instead

Rotating a projection matrix

In an OpenGL game, I am trying to rotate the camera relative to the player's view. This rotation is not easily defined by relative angles, but rather easily defined by relative forward/up/left vectors.
How do I construct a matrix such that I can multiply it by the current projection matrix to achieve this rotation?

Projection of rotation matrix

I have a rotation matrix I use to show a 3D-vector with openGL. Now I would like to have a projection of this vector on the XY-plane. So I´m looking for the rotation matrix that I can use to do this. Any ideas on how to do this?
This will not be rotation matrix, but general 4x4 transformation matrix for doing a projection on a plane. It is often used to do a "shadow" matrix for flattening objects onto a floor.
See more here: http://www.opengl.org/archives/resources/features/StencilTalk/tsld021.htm
https://math.stackexchange.com/questions/320527/projecting-a-point-on-a-plane-through-a-matrix
Projected Shadow with shadow matrix, simple test fails

How perspective matrix works?

I started to read lesson 1 in learningwebgl blog, and I noticed this part:
var pMatrix = mat4.create();
mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
I roughly understand how matrices (translation/rotation/multiple) works, but I have no idea what mat4.perspective(...) means. What is it used for? What is the result, if I multiply a vector with this matrix?
The perspective matrix is used to scale, and possibly translate or flip the coordinate system in preparation for the perspective divide. Since the the perspective projection operation involves a divide, it cannot be represented by a linear matrix transformation alone.
In a programmable graphics pipeline (see pixel shaders) you cannot see the divide operation - it is still one of the fixed-function parts. The programmer controls it by tweaking the variables involved in the operation. In the case of the perspective divide it is the projection matrix that gives you this control.
The projection matrix is used to convert world-coordinates to screen coordinates.
The positions in your three-dimensional virtual world are triplets of x, y and z coordinates. When you want to draw something (or rather tell OpenGL to draw something) it needs to calculation where these coordinates are on the users screen.
This calculation is implemented with matrix multiplication.
A vector consisting of x, y and z (and a fourth value of 1 which is necessary to allow the matrix to do some operations like scaling) is multiplied with a matrix to receive a new set of x, y and z coordinates (4th value is discarded) which represent where this point is on the users screen (the z-coordinate is required to determine which objects are in front of others).
The function mat4.perspective generates a projection matrix which generates a matrix which does exactly that. The arguments are:
The field-of-view in degree (45)
the aspect ratio of the field of view (the aspect ratio of the viewport)
the minimal distance from the viewer which is still drawn (0.1 world units)
the maximum distance from the viewer which is still drawn (100.0 world units)
the array in which the generated matrix is stored (pMatrix)
When a point is multiplied with this matrix, the result are the screen coordinates where this point has to be drawn.

Normal model matrix calculation for normal mapping in GLSL

I need to calculate a normal model matrix for doing normal mapping in GLSL shader. I want to make sure I am right on this: When I multiply view (camera model) matrix with geometry model matrix, is the view matrix supposed to be already inverted? It is not clear from the online examples like those found here and here. Also, I see in some cases people also transpose the resulting matrix. Why? So what is the right way to build a normal model matrix in OpenGL?
Currently I do it this way:
glm::mat4 view = inverse(GetCameraModel());
glm::mat3 normalModelMatr= glm::mat3(view * mesh.GetModel());
Is this the way to go ?
The correct normal matrix is the inverse transpose of the model-view matrix. If you do not do any non-uniform scaling, that is scaling axises by different amounts, the inverse of the matrix is equal to its transpose because it is orthogonal.
Therefore, the two operations cancel out and it is just the original matrix.
If you do do non uniform scale, the matrix is not orthogonal and you must do the inverse transpose.
You take the top 3x3 matrix, because you only need to rotate and scale normals, not translate.
So your normal matrix is correct as long as you do not employ non-uniform scaling.