Getting started with mojo3d: Creating a scene!

This is a quick introduction to getting started with mojo3d.

You can download the source code for this tutorial at GitHub; if you don’t know how to use git, just click the big green Clone or download button and select Download ZIP. (You’ll also find a second source file, intended to form part of the next tutorial, that I’ve included in error, but what the hey! It may allow you to move on sooner anyway!)

It assumes you are comfortable with at least the basics of object-oriented programming, working with classes, fields, methods, and the like, but no more than that.

Here’s a simple program that implements just about the minimum necessary for a basic mojo3d application:

#Import "<std>"
#Import "<mojo>"
#Import "<mojo3d>"

Using std..
Using mojo..
Using mojo3d..

Class MyGame Extends Window
	
	Field scene:Scene
	Field camera:Camera
	
	Method New  (title:String = "My Game", width:Int = 640, height:Int = 480, flags:WindowFlags = WindowFlags.Resizable)
		Super.New (title, width, height, flags)
	End
	
	Method OnCreateWindow () Override
		
		scene				= New Scene
		camera				= New Camera (Self)
		
	End
	
	Method OnRender (canvas:Canvas) Override
		
		If Keyboard.KeyHit (Key.Escape) Then App.Terminate ()
		
		RequestRender ()
		
		scene.Update ()
		camera.Render (canvas)
		
	End
	
End

Function Main ()

	New AppInstance
	New MyGame
	
	App.Run ()
	
End

Imports

We start with a few imports — the standard library, the mojo graphics library, and the mojo3d library that sits ‘on top’ of mojo.

You will always need to import these three classes at a minimum.

We also specify that we are Using them, in order to avoid the need to prefix all class names; for instance, we can now use Camera instead of mojo3d.Camera.

Main Window class

Next, we create a new class that Extends mojo’s Window class, allowing us to implement our own functionality, and add more as needed, which includes a few fields and three methods: New, OnCreateWindow and OnRender. (Note that MyGame can be called whatever you like, and contains all of the features of the Window class.)

There are two fields present here, scene (of mojo3d’s Scene type) and camera (of mojo3d’s Camera type). These will be the minimum needed in order to create a technically working 3D environment.

The New method simply creates the game window, via the Window.New method (called via Super to refer to the ‘parent’ New method).

OnCreateWindow overrides the parent Window method and implements our required functionality, in this case assigning a new Scene to the scene field, and a Camera to the camera field.

The Scene class organises and manages the entire 3D world, including all entities (3D objects of the Entity class, of which more later).

The Camera class is a built-in class providing visibility into the 3D world. Cameras are a type of Entity class, too, which allows us to move them around, rotate them, etc, like any other 3D object.

OnRender is the final built-in class here, and is where the action takes place.

Here, we check for the Escape key being hit, in which case the application terminates.

We ask the application to update its view via RequestRender — this is required.

We call scene.Update in order to update the scene; this has no effect in this case, but later on it will become relevant and I would suggest adding it anyway. (It will handle physics and collision updates, among other things.)

Finally, we use the camera to render to a canvas. Note that the canvas reference is passed in automatically by mojo (see the OnRender parameter). Rendering to this canvas will result in our 3D view appearing on screen.

Main

The remainder of the code is the standard, required, Main function: it creates a new Application instance, creates a MyGame instance (our overridden Window class) and then runs the application.

Behold!

Run the code, and… oh! It’s a blue screen…

This is fine — we haven’t placed any entities into the scene yet, but this blue is the default Scene screen-clearing colour. (You can modify this using scene.ClearColor = Color.Black, or any of the other built-in colours — to see these, use the Color documentation by placing the cursor inside Color and hitting the F1 key twice. You can of course create your own colours, but we’ll keep things simple for now.)

The blue screen in fact shows that our scene is rendering correctly.

Next time, we’ll add something to look at! (See the GitHub note at the very start if you want to toy with the second source file in the meantime.)

Comments are closed.