Jan
31
GM6 D3D from the Ground Up - Chapter 2
2007 @ 10:17 PMChapter 2 - Introduction to D3D Programming
I'm sure you have a firm understanding of our we can accomplish 3D drawing now. So, let's actually get into the real action.
Getting into 3D Mode
To do any 3D drawing in Game Maker, we must first set it into 3D mode. We can accomplish this by using the
d3d_start() function. Use the
d3d_end() function to put GM back into 2D mode.
Upon calling
d3d_start(), Game Maker automatically enable hidden surface removal. Hidden surface removal is a way to ensure that everything is drawn in the correct order. Whenever a pixel is drawn, it's assigned a value in the z-buffer that describes its distance from the viewer's perspective. Whenever another pixel needs to be drawn in that same location, D3D tests to see if this new pixel's z-buffer value is closer of further away from the viewer. If it is closer, it overwrites the old pixel. Otherwise, the new pixel is never drawn. However, there sometimes arises a special case where the z-buffer values are the same. This is called z-clipping. It is undefined as to what D3D draws and therefore you get some ugly results. You can toggle the z-buffer and hidden surface removal via the function
d3d_set_hidden({true|false});
Next, Game Maker automatically turns off the
Orthographic projection and turns on the
Perspective projection. You can toggle between the orthographic and perspective projections via the function
d3d_set_perspective({true|false});
Hello, 3D World!
Let's write a skeleton 3D application. Our goal is to simply set Game Maker into 3D mode.
Start a new Game Maker project and save it as "hello.gm6". Create a new object and name it "d3dController." This object will be used to execute code to put GM into 3D mode.
Right-click on "d3dController" and select "Properties..." Click on the "Add Event" button in the Object Properties dialog and click on the "Create" event.
Next, select the "Control" tab. Drag and Drop the "Execute a piece of code" library action into the "Actions:" list.
The GM code editor should show itself. Add the following code:
d3d_start(); //Initializes 3D mode
Now create a new room and add this object to it.
Drawing Some Shapes
Add a "Draw" event to our object. Again, add a new Code Execution action. We're going to simply draw a rectangle on the screen.
d3d_set_projection_ortho(0,0,room_width,room_height,0);
draw_set_color(c_red);
draw_rectangle(30,30,60,60,true);
The only unfamiliar code should be the
d3d_set_project_ortho function. However, you should have an idea of what it does already. Hopefully, you've guessed that it defines an othographic projection for Direct3D to project our coordinates to. If you recall, orthographic projections are for drawing text and images. So, you can use the familiar draw_rectangle function here.
d3d_set_projection_ortho
Synopsis:
d3d_set_projection_ortho(x, y, w, h, angle);
The function creates an orthographic projection over the specified viewing volume in the room. It is then rotated over the given angle. In our example, we just used the room's width and height. However, you could bind it to only a certain part of the window.
<< Previous Chapter Next Chapter >>
This article hasn't been commented yet.