gr_tut30_screenshot.jpg

ThirdPersonCamTutorial.cs

#region Using directives

using System.Windows.Forms;

using Microsoft.DirectX;

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

#endregion

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;

		#endregion

		#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
			Jad.AfterCreateDevice();

			// Load default textures, scenes, shaders,...
			Jad.LoadCreateInHouse();

			// 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);

			#endregion

			#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
			material[0].AddLayer(layer);

			// 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;

			#endregion

			#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);

			#endregion

			#region Camera

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

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

			#endregion

			// Everything is set, start the engine
			Jad.Begin();

			// Create input class to manage the keyboard and the mouse
			Jad.CreateInput();

			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"];
				else
					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)
			{
				this.camera.RotateAroundTarget(
					mouseMoveX * this.camSpeedFactor,
					mouseMoveY * this.camSpeedFactor);
			}

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

			// 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
			backbuffer.Render();

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

			// Start fonts rendering
			Jad.Video.Fonts.Begin();

			/// 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
			Jad.Video.Fonts.End();

			// Show the backbuffer
			backbuffer.Present();
		}

		#endregion
	}
}

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

Comments

No comments yet.