Skip to main content
GameDev.net gamedev.net
🔒 Locked

skybox?

Started by jagguy2 Dec 27, 2007 at 2:14 AM 14 replies 2.5k views
Original Post
jagguy2
jagguy2
is it common or not to create a skybox for a outdoor scene in directx? i can create a basic terrain and move about and have horrible background of blue or black all around. so what can i do?
fpsgamer
fpsgamer
Well if you want to create a sky you only have a few options:

- Sky box
- Sky dome
- Sky plane
MJP
MJP
A skybox will definitely be the easiest and simplest way for you to start out. Getting one working is pretty easy, just look at the source code for one of the many DX SDK samples that use one. You can even borrow the .X mesh and cube textures, if you like.

jagguy2
jagguy2
i googled skybox (got kind of confused)and i want to ask

q1) to create a skybox is just importing a X file? how do you place objects inside it if the 3D objects are seperate to the skybox?

q2) is a skybox just cube you create that you can enter , and all walls designed from the inside of the cube?
MJP
MJP
A skybox is just a giant box with a texture applied to it. You don't place objects inside of it, you simply draw it so that its always centered around your camera no matter how much the camera moves. You don't really have to do much with it in terms of scene management, you're either drawing it or not drawing it.

On the technical side, since its a cube you'll need a vertex buffer with 6 vertices at the 8 corners of a cube. Usually a unit cube is used, with vertices at <1,1,1>, <-1,-1,-1>, and so on. Then those vertices are typically scaled to the largest value you can have for a given projection setting, which is farZ / sqrt(3). FarZ is the distance to a camera's far clipping plane. You'll also need an index buffer too, that maps out the triangles. This is why I suggested using the X mesh from the SDK samples, it will set all of that up for you.

Are you using shaders? If you are, I can help you set up the HLSL code for a skybox.

jagguy2
jagguy2
ok i can draw a unit cube no problems. Is there directx api commands for such a skybox or am i drawing cube and repositioning the whole 6 sides on every movement so it stays in the middle?

On the SDK examples I see a cube that you can move around in and has 6 sides which you cant travel out of. You are never always in the center of it as you can touch the walls.
MJP
MJP
If you're not always centered in it then its not a skybox. Check out samples like PostProcess or HDRFormats, those use a skybox.
jagguy2
jagguy2
i cant move the background in those samples?

i thought a skybox was something like the samples SCRIPTING,SHADOWMAP,CONFIGSYSTEM where you are actually in a box or maybe STATEMANAGER?

I want to create background like in STATEMANAGER so is this a skybox? what process do i do to make this?
MJP
MJP
The StateManager sample is using a skybox. The skybox mesh is skybox01.x, and the effect is in skybox01.fx. You can really just copy that mesh and effect and use them with whatever cube texture you'd like. You can either draw the skybox first and then draw your scene, or draw your scene with z-writes enabled and render the skybox with z-buffering enabled.
Subotron
Subotron
Quote:
Original post by MJP
The StateManager sample is using a skybox. The skybox mesh is skybox01.x, and the effect is in skybox01.fx. You can really just copy that mesh and effect and use them with whatever cube texture you'd like. You can either draw the skybox first and then draw your scene, or draw your scene with z-writes enabled and render the skybox with z-buffering enabled.


actually I think you were meaning to say the skybox should be rendered with z-buffering disabled right?
jagguy2
jagguy2
ok i can draw a unit cube mesh and i would scale it up so i am in a big scene?

When placing textures on it how do place textures on the inside? Normally a texture appears on the outside of an object.

In the state manager you walk around and do hit the edge of the cube or scene. You never stay put in the middle of a cube becuase you need to walk around the terrain 'bottom side of the cube'.
So I am confused when you say the camera stays in the middle.


[Edited by - jagguy2 on December 30, 2007 5:56:22 PM]
MJP
MJP
Quote:
Original post by Subotron

actually I think you were meaning to say the skybox should be rendered with z-buffering disabled right?


No if you render your scene geometry first with z-writes enabled, you would want to render the skybox with z-culling enabled so the skybox isn't drawn over existing geometry. There usually isn't any need for z-writes to be enabled during this step, since the z-buffer is usually cleared to 1.0 and the skybox is supposed to be infinitely far away.
MJP
MJP
Quote:
Original post by jagguy2
ok i can draw a unit cube mesh and i would scale it up so i am in a big scene?

When placing textures on it how do place textures on the inside? Normally a texture appears on the outside of an object.

In the state manager you walk around and do hit the edge of the cube or scene. You never stay put in the middle of a cube becuase you need to walk around the terrain 'bottom side of the cube'.
So I am confused when you say the camera stays in the middle.


No you're confusing what's part of the skybox and what's part of the scene geometry. I apologize, I'll admit I'm not doing the best job of explaining this for you.

In that StateManager sample, the terrain is not part of the skybox. All you see that is part of the skybox is the background texture with the blue sky and clouds. This, as you'll notice, never moves no matter how much the camera moves. The snow-covered terrain, the gazebo, the trees, and the rocks are all separate from the skybox.

As for the textures, they will appear on both sides of a single-sided mesh (such as the skybox mesh that sample uses).

And yes, you will probably scale your skybox so that its as large as it can be for the projection matrix you're using to draw it. Keep in mind its not necessary to use the same projection matrix that you're using for the rest of the scene geometry, unless you want to use z-culling for rendering your skybox like I mentioned in my previous post. This means you could create a special projection matrix just for the skybox that has the same aspect ratio and FOV as your normal projection matrix, but has a Z-far value > sqrt(3) and Z-near < 1.

jagguy2
jagguy2
thanks for the replies so far as it has been a great help. I am just interested in knowing more .

Q1) I scale my unit cube this much so I assume my x-axis will extend to +/- 100 and y axis +/- 50. This is not so as I move my cube up 1 (y translation and i am at the bottom of the cube since the camera started at the origin.


        g_xtrans2=1; to move unit cube 'skybox' up 1 so my camera starts at bottom of the cube inside it.	g_xscale=100.0f;	g_yscale=50.0f;	g_zscale=100.0f;	D3DXMatrixTranslation( &matTrans, g_xtrans2, g_ytrans2, g_ztrans2);	D3DXMatrixScaling(&Scale, g_xscale, g_xscale, g_zscale);	D3DXMatrixMultiply(&combine,&matTrans,&Scale);	g_pd3dDevice->SetTransform(D3DTS_WORLD, &combine);

Is the scaling factor of 100 means X 100 in original size along an axis, so an intial length of 1 will be 100?

q2) for an indoor scene then i dont use a skybox but walls and this may not be in a cube shape, yes?



[Edited by - jagguy2 on December 30, 2007 11:21:57 PM]
ItsDan
ItsDan
Walls and such are typically interactive. The player can run into them, weapons can impact them, etc. That is not what a skybox is. A skybox (or a sky-whatever shape) is something the user is never intended to touch. The items drawn on it are assumed to be so far away that they wouldn't appear to move even if you ran towards them. Imagine mountains in the distance, you have to cover a large distance for them to look larger.

Even indoors you might want to render a skybox, since your building could have windows. Remember a box is only a handful of polygons. It's one of the least expensive pieces of geometry you can have in your scene.

In my last project I just reset the camera to the origin, matched the rotations of the current player, and drew the unit box around it with the zbuffer disabled. There's no reason to scale or translate the box this way. I would then reenable the zbuffer, and do my drawing as normal.



In your above example, reverse the multiplication. Scale, then translate.
-----------------------------------------------“The best, most affordable way to save the most lives and improve overall health is to increase the number of trained local, primary healthcare workers.”Learn how you can help at www.ghets.org
jagguy2
jagguy2
ok i understand, a unit cube will do and z-buffer turned off will do the trick, and move the unit cube with the camera.

I dont need it but why didnt the scaling work as i expected? scaling seems pointless as you lose detail with textures. If I want a bigger object then design it big and not a unit size and scale up.

q)since i didnt create a z-buffer why do i need to disable it? My unit cube gets drawn first but is over writes objects draw after it and rotating in such a small space 'unit cube ' i find i am always getting out of it.

[Edited by - jagguy2 on December 31, 2007 7:32:21 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.