What if you want to have a game inside of a room? You will have to go INSIDE of a shape to do so! The process of creating a shape to use as your 3D world isn’t very difficult. You simply have to create a large version of the desired shape and then add a few commands to make things work. There are three new commands that we’ll use in this section of code: FitMesh, FlipMesh, and EntityFX. I’ll discuss each of these after we’ve entered the code.
Let’s create a program that allows us to go inside shapes! Demo09-05.bb shows how it’s done. Make sure that the file called wall.jpg is also placed in the same directory as demo09-05.bb.
;demo09-05.bb - Going inside a shape ; –––––––––––––––– Graphics3D 640,480 SetBuffer BackBuffer() Const ESC_KEY = 1 Const LEFT_KEY = 203 Const RIGHT_KEY = 205 Const UP_KEY = 200 Const DOWN_KEY = 208 ; Create camera camera=CreateCamera() PositionEntity camera,0,1,0 ; Create a light light=CreateLight() ;Create our Cube World cubeworld=CreateCube() FitMesh cubeworld,-250,0,-250,500,500,500 FlipMesh cubeworld tex=LoadTexture( "wall.jpg" ) ScaleTexture tex, 0.5,0.5 EntityTexture cubeworld,tex EntityFX cubeworld,1 ; Create the terrain ground=CreateTerrain(512) ; This following code deals with cameras and terrain While Not KeyDown(ESC_KEY) If KeyDown(RIGHT_KEY)=True Then TurnEntity camera,0,-1,0 If KeyDown(LEFT_KEY)=True Then TurnEntity camera,0,1,0 If KeyDown(DOWN_KEY)=True Then TurnEntity camera,-1,0,0 If KeyDown(UP_KEY)=True Then TurnEntity camera,1,0,0 RenderWorld Flip Wend End
Take a closer look at the important part of this program.
;Creating our Cube World cubeworld=CreateCube() FitMesh cubeworld,-250,0,-250,500,500,500 FlipMesh cubeworld tex=LoadTexture( "wall.jpg" ) ScaleTexture tex, 0.5,0.5 EntityTexture cubeworld,tex EntityFX cubeworld,1
Run the program now and have a look around by pressing the arrow keys. You’ll notice that you are now inside your cube world, which looks like a room made of hardwood walls, a ceiling, and a floor, as in Figure 9.11.
Now let’s look at the code you just entered line by line to figure out exactly how we did what we did.
cubeworld=CreateCube()—This code creates a standard cube at the default size. In this case, we called our cube cubeworld. You don’t have to specify a size or location for the cube because you do so in the next command.
FitMesh cubeworld,-250,0,-250,500,500,500—The Fit Mesh command creates a mesh that will fit inside the cube that we created. After the FitMesh command, you need to specify the location of the mesh on the x, y, and z axes and the dimensions. The first three numbers specify the position of the mesh. In this case, it is at position —250 on the x axis, 0 on the y axis, and —250 on the z axis. The next three numbers specify the width, height, and depth of the mesh. In this case, we’ve made all three 500.
FlipMesh cubeworld—When we apply a texture to an object, that texture is wrapped around the outside of the object, not the inside. If you were to go inside an object that has a texture applied to it, it would appear dark. Think of the example of a box that is covered in gift wrapping. If you were inside the box, you would be in the dark because the wrapping paper is on the outside. Blitz3D has a function called FlipMesh that allows you to turn the wrapped object inside out so that the wrapping paper appears on the inside. That’s exactly what we’ve done in this step. We’ve told Blitz3D that anything we wrap around the object cubeworld should be wrapped around the inside, not the outside.
tex=LoadTexture( "wall.jpg" )—This is the command we have used many times already to load a texture. In this case, we are using the image called wall.jpg as the texture that we are calling tex.
ScaleTexture tex, 0.5,0.5—We will reduce the scale of our texture by half by adding this command.
EntityTexture cubeworld,tex—This applies the texture to the cube. Remember, because we created a FlipMesh, the texture will be on the inside of the cube.
EntityFX cubeworld,1—It would be very dark inside our box if we didn’t have a way of brightening the inside. To do this, we use the EntityFX command. The EntityFX command has several different effects. In this case, the number 1 indicates that the area should be filled with full brightness. Run the program without this code and you’ll notice that the walls inside your cube are much darker.
18.226.98.181