This project is read-only.


#region Using directives

using System.Windows.Forms;

using Microsoft.DirectX;

using JadEngine;
using JadEngine.Video;
using JadEngine.Core;
using JadEngine.Scene;
using JadEngine.Input;


namespace Tutorial
	/// <summary>
	/// This tutorial will show you how to use the third person camera
	/// </summary>
	public class ThirdPersonCamTutorial : JApplication
		#region Fields

		// Saves the reference to the third person camera
		JCameraSimple3rdPerson camera;

		// Determinates the camera movement speed
		float camSpeedFactor = 0.005f;


		#region Methods

		/// <summary>
		/// The engine calls this method after creating the device
		/// </summary>
		/// <param name="form">Form used as window</param>
		/// <returns>True if everything went right</returns>
		public override bool InitGame(Form form)
			// Create the fonts and texture objects

			// Load default textures, scenes, shaders,...

			// Show the object names
			Jad.Scene.ShowNames = true;

			#region Objects

			// Create the objects
			JMesh cubeMesh = JMesh.CreateCube(3, 3, 3);
			JMesh sphereMesh = JMesh.CreateSphere(1.5f, 12, 24);

			JMeshObject cube = Jad.Scene.MeshObjects.Create("cube", false);
			JMeshObject sphere = Jad.Scene.MeshObjects.Create("sphere", false);

			// Now, we associate the mesh with the mesh object
			cube.Mesh = cubeMesh;
			sphere.Mesh = sphereMesh;

			// Position the objects
			sphere.Transform.Position = new Vector3(5, 0, 0);
			cube.Transform.Position = new Vector3(-5, 0, 0);


			#region Material

			// Create a material's array of 1 element.
			// An array is needed because meshes can have subsets:
			// information for more than 1 material.

			JMaterial[] material = new JMaterial[1];

			material[0] = Jad.Scene.Materials.Create();

			// A material is composed by layers, so we can do multilayer effects.
			// In this example we´ll use only one layer.

			// Create the layer
			JMaterialLayer layer = new JMaterialLayer();

			// Add it to the material

			// Set the diffuse texture. The engine detects if the texture doesn´t exists
			// and load it. If it exists, it only references it.
			// You must not indicate the file extension (.tga, .jpg, .bmp,...). The engine
			// we´ll search the different extensions until it finds the texture.

			// To search for textures, the engine will enter in the base/textures
			// directory, and we´ll move through it recursively until it finds the
			// texture. Thanks to this, you can create subfolders to order the textures
			// without needing to change the engine.

			layer.DiffuseMap.Texture = Jad.Video.Textures.Create2D("brick_d", true);

			// Set the material we have just created to our JMeshObject
			cube.Material = material;
			sphere.Material = material;


			#region Light

			// Create an omni light
			JOmniLight light = (JOmniLight) Jad.Scene.Lights.Create("light", JLightType.Omni);

			// Set its position
			light.Transform.Position = new Vector3(0, 0, 0f);

			// Intensity
			light.Multiplier = 1f;

			// Attenuation
			light.Attenuation.Type = JLightAttenuationType.DualRadius;

			// Distance to start attenuating the light
			light.Attenuation.Start = 5f;

			// Distance where the light is completely attenuated
			light.Attenuation.End = 10f;

			// Color
			light.Color = JColor.Yellow;

			// The light affects the diffuse component of the material
			light.AffectDiffuse = true;

			// The light affects the specular component of the material
			light.AffectSpecular = true;

			// Don´t generate shadows
			light.CastShadows = false;

			// Put ambient light
			Jad.Scene.Lights.Ambient = new JColor(0.6f, 0.6f, 0.6f);


			#region Camera

			// Create the third person camera = (JCameraSimple3rdPerson) Jad.Scene.Cameras.CreateSimple3rdPerson("thirdPersonCam", Jad.Scene.MeshObjects["cube"]);

			// Set the third person camera as active camera
			Jad.Scene.Camera =;


			// Everything is set, start the engine

			// Create input class to manage the keyboard and the mouse

			return true;

		/// <summary>
		/// This method is called every frame and it´s where the input
		/// events and the game action must be placed
		/// </summary>
		/// <returns>True to continue, false to exit the application</returns>
		public override bool Update()
			// Exit the app?
			if (Jad.Input.Keyboard[Key.Escape].Down) return false;

			// If the spcae key is pressed, switch the camera target
			if (Jad.Input.Keyboard[Key.Space].Pressed)
				if (Jad.Scene.Camera.Transform.Target.Name == "cube")
					Jad.Scene.Camera.Transform.Target = Jad.Scene.MeshObjects["sphere"];
					Jad.Scene.Camera.Transform.Target = Jad.Scene.MeshObjects["cube"];

			#region Camera Movement
			// Get the relative mouse movement values
			int mouseMoveX = Jad.Video.Views.CurrentView.Input.Mouse.X;
			int mouseMoveY = Jad.Video.Views.CurrentView.Input.Mouse.Y;
			int mouseMoveZ = Jad.Video.Views.CurrentView.Input.Mouse.Wheel;

			// If the camera was moved on the x and/or y axis, rotate the camera
			// around the target accordingly
			if (mouseMoveX != 0 || mouseMoveY != 0)
					mouseMoveX * this.camSpeedFactor,
					mouseMoveY * this.camSpeedFactor);

			// If the mouse wheel was scrolled, move the camera towards / away from the target
			if (mouseMoveZ != 0)
			{ * -this.camSpeedFactor);

			// Everything was right, continue
			return true;

		/// <summary>
		/// Render method
		/// </summary>
		public override void Render()
			JView backbuffer = Jad.Video.Views[0];

			// Render the Jad.Scene using the scene camera

			// Get access to the 1st font. The engine creates a font by default
			JFont font = Jad.Video.Fonts[0];

			// Start fonts rendering

			/// Write text in the first line, position x=0, yellow color
			font.RenderLine(Jad.Version + " Press ESC to exit", 0, JColor.Yellow);

			// Write text in the third linem, position x=0, yellow color
			font.RenderLine("Press Space to switch the camera target", 0, JColor.Yellow);

			// Write text in the second line, position x=0, white color
			font.RenderLine(Jad.Video.Views.CurrentView.Stats.Fps + " FPS", 0, JColor.White);

			// End fonts rendering

			// Show the backbuffer


Last edited Mar 8, 2007 at 9:18 PM by Vicente, version 2


No comments yet.