1
\$\begingroup\$

I'm looking at this tutorial https://capnramses.github.io/opengl/cubemaps.html about half way down the webpage, there is a description of a vertex shader

#version 400

in vec3 vp;
uniform mat4 P, V;
out vec3 texcoords;

void main() {
  texcoords = vp;
  gl_Position = P * V * vec4(vp, 1.0);
}

The P and V matrices are my camera's projection, and view matrices, respectively. The view matrix here is a special version of the camera's view matrix that does not contain the camera translation. Inside my main loop I check if the camera has moved. If so I build a very simple view matrix and update its uniform for the cube map shader. This camera only can only rotate on the Y-Axis, but you might use a quaternion to generate a matrix for a free-look camera. Remember that view matrices use negated orientations so that the scene is rotated around the camera. Of course if you are using a different maths library then you will have different matrix functions here.

I'm having difficulty figuring out how to construct this matrix in my program. How would I construct it from a projection matrix, view matrix, or camera matrix? (These are the matrices already within my program)

\$\endgroup\$
  • \$\begingroup\$ That tutorial is pretty terrible in a number of ways. I ran into the same issue you did and was just as baffled. \$\endgroup\$ – user1118321 Dec 13 '16 at 17:40
1
\$\begingroup\$

Okay so I cannot open that link from where I am, but from what I can draw from your text, this is a shader for a cube map, right?

The point of a cube map is that you (the camera) are always at the center of it. You only change the part of the map you can see by rotating your camera. You still are able to move the camera, the skybox just does not change then.

So what you need for your program is two states of the view matrix - one with a possible translations (movement) and one without.
You use the one with translation for every asset in your scene that you want to change position relative to the camera when it moves.
You use the one without translation for the cube map, that is always centred on the camera. The latter one is the one your text talks about.

I can think of two approaches at getting those matrices:
1) Have 2 view matrices "a" and "b" at the start of the game loop. Apply rotations and translations on "a", use that for the first case. Apply only rotations on "b", use that for the skybox.
2) Have 1 view matrix. Apply rotations on it, update your cube map view matrix uniform, then apply translations and update the view matrix uniforms in your other shaders where needed.

\$\endgroup\$
  • \$\begingroup\$ It might be worth commenting on scale, as well. I would think that you wouldn't want the skybox to scale at all. Basically, the view transform for the skybox should only contain rotations, right? \$\endgroup\$ – user1118321 Dec 13 '16 at 17:49
  • \$\begingroup\$ @user1118321 As far as I know, view matrices usually made so that they don't change scale. \$\endgroup\$ – HolyBlackCat Dec 14 '16 at 9:12
4
\$\begingroup\$

In fact you don't need to create a second matrix separately. To remove any translation you just need to cast the matrix to mat3 and back to mat4.

It will change following numbers in the matrix

- - - 0
- - - 0
- - - 0
0 0 0 1

effectively removing any translation.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.