0
\$\begingroup\$

I'm a beginner with OpenGl so please discount my ignorance. I've managed to create a skybox (with a texture mapped cube) and would like to rotate the view (eye) around the center of the box, so I can admire the view :). By changing the X/Y coordinates of eye/camera in GLM.LookAt() I'm able to see most of the faces of the skybox but I'm not able to see the top and bottom faces though I'm sure that they are rendered. In any case what's the best way to rotate around the skybox? What I want to do is exactly shown in this video

Thanks!

EDIT: Sorry for the lack of relevant information.

  1. I'm using OpenGL version 3.3.
  2. Not using immediate mode. Everything is via shaders and VBO/VAO
  3. I have already setup the projection, view and model matrices which I pass to the vertex shader, and obviously have the texture loading also working as I can render the skybox. At the moment i've set the projection and model matrices to Identity for simplicity.
  4. I'm deriving the view matrix from GLM.LookAt(), So initially i thought that to rotate around the view box, I'd just have to change the viewMatrix
  5. I'm working on C# using SharpGL
  6. To draw the skybox, I've set up a VAO to draw a cube with vertices in the range (-1,-1,-1) and (1,1,1). Then I load 6 textures for each cube face to 6 Texture Units. During the rendering I draw the cube and update the texture for each face using a sampler variable. All of this seems to be working OK. I verified this my scaling down the size of the cube and checking if all 6 faces were being rendered properly by rotating the ModelMatrix

I suspect one problem is that I'm not positioning the camera in the center of the cube. At the moment it seems to be viewing the cube from outside. I'm still trying to grasp the usage of GLM.LookAt().

This is how I'm rendering the skybox at the moment. And I can see the front face of the cube, but how do I go about placing the camera in the center of the box and rotating it?

 viewMatrix = mat4.identity();
 projectionMatrix = mat4.identity();
 modelMatrix = mat4.identity();
 viewMatrix = glm.lookAt(new vec3(0f, 0f, 1f), new vec3(0f, 0f, 0f), new vec3(0.0f, 1.0f, 0.0f));

        shaderProgram.Bind(GL);
        shaderProgram.SetUniformMatrix4(GL, "projectionMatrix", projectionMatrix.to_array());
        shaderProgram.SetUniformMatrix4(GL, "viewMatrix", viewMatrix.to_array());
        shaderProgram.SetUniformMatrix4(GL, "modelMatrix", modelMatrix.to_array());

skyBox.renderSkybox(GL);

EDIT #2 I managed to figure out my error. I was trying to look at the Top and Bottom faces by doing viewMatrix = glm.lookAt(new vec3(0f,-1f, 0f), new vec3(0f, 0f, 0f), new vec3(0.0f,1f, 0f)); and viewMatrix = glm.lookAt(new vec3(0f, 1f, 0f), new vec3(0f, 0f, 0f), new vec3(0.0f,1f, 0f)); which was giving me a blank screen. The reason being that (0,1,0) is collinear with my up-vector and LookAt() couldn't generate the viewMatrix in this case. Changing the UpVector makes it OK!

\$\endgroup\$
3
  • \$\begingroup\$ There are many ways to do that; we're unable to know what's the best for you because you provide so little information. \$\endgroup\$
    – Vaillancourt
    Commented Apr 10, 2015 at 17:23
  • \$\begingroup\$ For a start you could include what version of OpenGL are you using, on what framework, and how do you currently draw your skybox. \$\endgroup\$
    – akaltar
    Commented Apr 10, 2015 at 17:25
  • \$\begingroup\$ Sorry for the lack of information. I've edited the post for more details. Could you have a look at it? Please let me know if you need more information. Thanks a lot! \$\endgroup\$
    – Sredni
    Commented Apr 11, 2015 at 4:50

1 Answer 1

0
\$\begingroup\$

There are different ways to implement / render a skybox but one way is to render the skybox first, turning off depth writes and using only the rotation from the camera, no translation. Then, you render the rest of your scene as normal on top of that. This gives the appearance that the skybox is infinitely far away and that you can never reach it, but you can see it all around you as you look different directions.

\$\endgroup\$
5
  • \$\begingroup\$ I've managed to render the box, but I cant figure out how to position the camera in the center of the cube and rotate it so I can view the box from inside out so to speak. Please see the code above. Thanks! \$\endgroup\$
    – Sredni
    Commented Apr 11, 2015 at 6:42
  • \$\begingroup\$ The 3 parameters to look at are: eye, center (at) and up. In your case what you want is for the eye to be the origin always (0,0,0) and the at is what should change based on the angle that you are viewing. You can use what you have for up so long as you don't let the camera look straight up or straight down, and as long as you don't want to allow roll rotation! Digging up the details of how to calculate the proper "at" vector, one minute... \$\endgroup\$
    – Alan Wolfe
    Commented Apr 11, 2015 at 16:27
  • \$\begingroup\$ OK so to get the correct "at" value, you basically need to keep track of the vertical and horizontal angle that the camera has, and then use this formula to convert those two angles to a normalized vector that you can use as your "at" parameter. Theta is the yaw, or the rotation around the y (vertical) axis, so is what decides how far left or right you are looking. Phi is the pitch, or the rotation around the x (left) axis, so decides how far up or down you are looking. At.x = cos(theta) * cos(phi). At.y = sin(theta) * cos(phi). At.z = sin(phi). \$\endgroup\$
    – Alan Wolfe
    Commented Apr 11, 2015 at 16:39
  • \$\begingroup\$ Btw there are other ways to do this, but this is probably the quickest way to get working. More info about the details of those equations here if you want em: blog.demofox.org/2013/10/12/… \$\endgroup\$
    – Alan Wolfe
    Commented Apr 11, 2015 at 16:39
  • \$\begingroup\$ Awesome that's exactly what I was looking for. Thanks! \$\endgroup\$
    – Sredni
    Commented Apr 11, 2015 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .