Making our Skybox act like a Skybox

Now that we have a fully working camera (at least one we can use to move around and look at things) I have noticed that our skybox isn’t exactly acting like a real skybox should. A real skybox would follow the camera around so that it was always in the background of the scene being looked at, and it would also be much larger. These two changes are actually quite easy to implement.

Step 1: Making Our Skybox Bigger

For starters we can change the value in the skybox class for the size to be a much larger number since it has only been at 10 so far. Instead of changing the value of all of the vertices in the skybox class, we can more easily change the scaling value that is inherited from the HMObject class. This can be accomplished by adding the following code in the demo class:

sky1.SetScaling(new Vector3(100, 100, 100));

Step 2: Rendering at the Correct Position

Now, similar to the changes that we had to make to the Mesh class to take the transformation vectors into account, we must now add the functionality to do so to not only the skybox class, but to the VertexGroup class that it uses to render each of its sides. Here are the changes we have to make to the code to get all these transformations passed to their correct places and rendering correctly:

// In the HMObject class public virtual void SetPosition(Vector3 newPosition) { myPosition = newPosition; } public virtual void SetRotation(Vector3 newRotation) { myRotation = newRotation; } public virtual void SetScaling(Vector3 newScaling) { myScaling = newScaling; } // In the HMSkybox class public override void SetScaling(Vector3 newScaling) { foreach(HMVertexGroup vg in faces) { vg.SetScaling(newScaling); } } // In the HMVertexGroup class public override void Render(Device myDevice) { myDevice.Transform.World = Matrix.Scaling(myScaling) * Matrix.RotationYawPitchRoll(myRotation.Y, myRotation.X, myRotation.Z) * Matrix.Translation(myPosition); myDevice.VertexFormat = CustomVertex.PositionTextured.Format; myDevice.SetTexture(0, myTexture); myDevice.SetStreamSource(0, vb, 0); myDevice.Indices = ib; myDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, verts.Length, 0, inds.Length / 3); }

Step 3: Following the Camera Around

We will also need to make our skybox follow the camera around so it is always in the background and seems to be much farther away than it truly is. This is accomplished by changing the translation value for the skybox in the rendering function to the current position of the camera. To do this we will need to pass our camera into the Render function along with the device.

We will have to change the Render function for all of our object types and the call in our Scene class, but this is not that much trouble. Once we have gotten our camera passed on from the scene to each of our objects’ rendering functions, we can simply add a call to the skybox’s VertexGroup.SetPosition prior to each render so that it will adjust the boxes position when needed each frame. Here is what the code in HMSkybox looks like:

public override void Render(HMCamera myCamera, Device myDevice) { foreach(HMVertexGroup vg in faces) { vg.SetPosition(myCamera.Position); vg.Render(myCamera, myDevice); } }

Now our skybox is acting as it should, and if we need to change its size to accomodate a much larger world we can easily do so by changing one scaling value on the main skybox and it will automatically be adjusted for each of its parts and rendered correctly.

Leave a Reply

You must be logged in to post a comment.

Do NOT follow this link or you will be banned from the site!