3D_for_GML:_Backface_culling - hpgDesigns/hpgdesigns-dev.io GitHub Wiki

1.1 Setting culling to true or false

You may remember the following line of code from the script ini_camera that has been used in many of the previous tutorial gm6 files:

//set culling to false
d3d_set_culling(false)

It has also been used in the exact same way in the gm6 file that comes with this tutorial. But this time the script scr_culling_true, found in the camera object, has been added to execute the following code in the Press event:

//set culling to true
d3d_set_culling(true);

When the Space key is released, culling is set back to false. This means we can draw a 3D scene and by pressing Space we will see what culling actually does. The WASD keys and Up/Down arrow keys will be used to move the camera in the 3D room.

1.2 Using culling with different objects

If you take a look at the 3D scene, you’ll see four objects: a floor, sky, wall and ball object. In total this gm6 file uses five objects, one being the camera to view the other world objects. First, let’s explore the ball object. Move inside of it with the camera and you will see that it’s textured on the inside. Now, press and release the Space key a few times. Remember, pressing the Space key turns culling on. When culling is set to true, the inside of the ball is not drawn. The Space key code may not be the most effective or bug-free way of showing the result of culling but you will probably notice the effect of culling on the ellipsoid (ball object). If you were to draw a huge sky dome (or sky bubble) that surrounds the camera, you would have to set culling to false. Otherwise, the sky would not be drawn. Next, let’s take a look at the floor object. As long as the camera is floating above the floor, switching culling on and off has an effect. When the camera is below the floor, switching has no visible effect whatsoever. In code, the drawing of the floor looks like this:

//draw floor
d3d_draw_floor(0, 0, 0, room_width, room_height, 0, background_get_texture(bk_floor), 6, 6);

The most important thing you should know about culling is that it can speed up drawing processes. That’s why it’s important to learn how to use it and get to know its limitations. Before we go deeper into the speed issue, let’s take a look at the sky and the drawing code:

//draw sky
d3d_draw_floor(0, 0, 256, room_width, room_height, 256, background_get_texture(bk_sky), 4, 4);

It is drawn in the same manner as the floor and behaves accordingly. One object remains: the wall, which has this code drawing it:

//draw wall
d3d_draw_wall(128, 0, 0, room_width, 0, 256, background_get_texture(bk_wall), 8, 8);

When the camera’s at the left side of the wall, culling has no visible effect. When on the other side (right), switching culling on and off makes the wall appear and disappear.

1.4 Flipping the back face

So, what to make of all this? Well, if you want to use back face culling, you need to find out what the back face is. For every object you will have to ask yourself where the back face is because that side of the object will not be visible if you switch back face culling on. For instance, in a race game you would have a ground on which cars are driving around a course. In a typical race game, you would always see the race track from the top, not the bottom. Back face culling would be useful in such a game to speed things up a little, increasing the overall speed of the game. The trick is to put the back face in the right place. If you take a another look at the example gm6, you will see that back face culling would make the ground (floor) disappear. That would not be acceptable in a race game, would it? You would need to flip the floor so the back face would be on the other side, thus allowing for back face culling while keeping the ground visible. Let’s try that, let’s flip the ground. Let’s put the back face on the other side by changing the drawing code of the floor object to this:

//draw floor
d3d_draw_floor(0, room_height, 0, room_width, 0, 0, background_get_texture(bk_floor), 6, 6);

The back face will now be on the bottom side which would be what we would desire in a normal race game. Like I said, the main reason for using back face culling in your programs is to improve the drawing speed. The general rule of thumb in 3D games is to not draw what you don’t see. That’s where back face culling comes in as a handy tool. Not drawing back faces, that you don’t see anyway, can speed up your game by reducing drawing time.

⚠️ **GitHub.com Fallback** ⚠️