Jump to content

  • Log In with Google      Sign In   
  • Create Account

Getting Started With 3D


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Dr. T   Members   -  Reputation: 105

Like
0Likes
Like

Posted 26 May 2013 - 04:10 AM

Going to 3D

This tutorial is about getting started with 3D using GameMaker: Studio and the GML programming language. We will be creating a simple cube with a texture attached to it.

 

Create a script named 'scr_start3D' input the following code to switch to 3D mode:

//switch to 3D mode
d3d_start();
 

This will result in GM running in 3D mode with the ability to perform 2D overlay drawing. This is also known as orthographic projection. If you would want to leave 3D mode completely, you would use the following code:

 

//switch to 2D mode
d3d_end();
 

Switching to 3D mode will allow us to work in 3D perspective. This will allow us to do draw objects in perspective which makes things that are further away from the camera appear smaller, just like in the real world. So, add the following code to our script to switch perspective on:

 

//switch perspective on
d3d_set_perspective(true);
 

The things that are closer to us, usually block the view of things further away. Things that are hidden from our view, are not seen by our eyes. The same thing can be done in 3D mode. We will have to switch hidden surface removal on. We will add this to our code:

 

//remove hidden surfaces
d3d_set_hidden(true);
 

In the real world we can only see things if there is light and darkness or light sources and shadows. GM allows us to use lighting but we will switch if off for now, not to over-complicate things. To get started, you don't need lighting to be switched on. Let's turn it off and set the drawing color to white which has the effect of drawing everything as if it was lighted equally from all angles without shadows. We will add the following code to our initialization code:

 

//switch lighting off
d3d_set_lighting(false);

//set draw color
draw_set_color(c_white);
 

On a misty day, things will dissolve into the distance. This can also be simulated in out program by using fog. Let's add the following code to make our object disappear in the fog as it gets further away:

 

//draw_fog
d3d_set_fog(true,c_white,1,1024);
 

When you see an object, you usually only see the outside of it. The insides are invisible. We can use this same principle in our program. It is known as culling and can be used to speed up drawing because we only have to draw the side of objects the we can actually see. We will switch culling off for now because it can be a tricky thing in the beginning. If you want to speed things up later, you can switch it on.

 

//set culling to false
d3d_set_culling(false);
 

There are two other functions we will be adding that handle the way drawing looks. We will not go into them because they are not important at this time. You could say they are merely cosmetic and you can always experiment later.

 

//switch shading off
d3d_set_shading(false);

//switch interpolation on
texture_set_interpolation(true);
 

Our script should now look like this:

 

//switch to 3D mode
d3d_start();

//switch perspective on
d3d_set_perspective(true);

//remove hidden surfaces
d3d_set_hidden(true);

//switch lighting off
d3d_set_lighting(false);

//set draw color
draw_set_color(c_white);

//draw_fog
d3d_set_fog(true,c_white,1,1024);

//set culling to false
d3d_set_culling(false);

//switch shading off
d3d_set_shading(false);

//switch interpolation on
texture_set_interpolation(true);
 

Creating a 3D Block

We can draw all kinds if complicated shapes and models but let's start out with a simple cube. We will be creating a new script named 'scr_Block' that holds the following code:

 

//draw the cube
d3d_draw_block(0,0,0,32,32,32,background_get_texture(bac_block),1,1);
 

The first three values (0,0,0) hold the one corner of our block and the next three (32,32,32) hold the other corner of our block. We will need to add a background resource named 'bac_block'. The last two values (1,1) indicate that the texture is tiled just once horizontally and vertically. Now that we have a block to see, we will have to actually draw it as it is seen. 

 

Using a Virtual Camera

Let's create a new script that allows us to place a virtual camera in our 3D world. This camera will be the viewpoint from which we are going to look at our block. The camera is like an eye. It will allow us to view the 3D world as if we are inside it. The script we'll create is 'scr_Camera', containing the following code:

 

//draw what the camera sees
d3d_set_projection(128,96,64,16,16,16,0,0,1);
 

The first three values (128,96,64) are the position of the camera. It is the point from which the camera sees, while the next three (16,16,16) indicate the point which the camera will look in. The camera will look from a position (128,96,64) to the center of our block (16,16,16). The last three values (0,0,1) indicate that we want to have a camera in the normal orientation which is upside is up.

 

Running the 3D Program

We will have to create a room, or level, and put some objects in there that handle our code. Let's create a room named 'rm_tutorial'. We will create an object to run our camera script and one for our block script.

 

We will have to create a camera object (obj_camera) that will run the initialization script (scr_start3D) in its Creation Event. This camera object will also have to show us what the camera sees every time the screen is drawn. In other words, it will execute the camera script in the Draw Event. We will have to create a block object that will run our block script (scr_block) in the Draw Event.

 

You know what? Let’s have our camera object create the block object for us. We will create a script named ‘scr_Make’ that will make an instance of the block object. The code looks like this:

 

//create the block object
instance_create(0,0,obj_Block);
 

 

We’ll make the camera object execute the script (scr_Make) in its Creation event, right after it has run the script ‘scr_Start3D’. Now we only have to place a camera object in the room and it will create the block object for us (at location 0,0 in the 2D Room). We will place the camera object in the Room (rm_tutorial).
 
Remember, it doesn’t matter where you place the camera object in the Room editor because the scripts (scr_Camera and scr_Block) determine where the cube is drawn and how the camera views it.
 
Now, it’s time to hit the F5 key (or click the green Play button) to run the program and see what happens. We should see a colored cube in the middle of our screen, drawn in perfect perspective.
 
The End Result
The end result of the program should look like this:
15119690.png

Edited by Dr. T, 26 May 2013 - 04:31 AM.


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS