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 trackball camera
	/// </summary>
	public class TrackballCameraTutorial : JApplication
		#region Fields

		// Saves the reference to the trackball camera
		JCameraTrackball _camera;
		bool _lastTargetSphere;


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

			// Show the gizmos
			Jad.Scene.ShowGizmos = true;

			#region Objects

			// Create the objects
			JMesh cubeMesh = JMesh.CreateCube(3, 3, 3);
			JMesh sphereMesh = JMesh.CreateSphere(1.5f, 24, 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(0, 0, 0);
			cube.Transform.Position = new Vector3(0, 5, 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, 2.5f, 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 trackball camera
			this._camera = (JCameraTrackball) Jad.Scene.Cameras.CreateTrackball("trackballCamera");

			// Set the trackball camera as active camera
			Jad.Scene.Camera = this._camera;
			// Center our center of focus and rotation onto the cube object
			_camera.Target = cube;

			// Back us off a bit to start
			_camera.Radius = 25f;

			// Remove the default camera completely 
			// (Otherwise, you see it's frustrum when ShowGizmo's is true
			JCamera jad_camera = (JCamera) Jad.Scene.Root.SceneGraph.GetNodeByName("jad_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 (_lastTargetSphere)
					_camera.Target = Jad.Scene.MeshObjects["cube"];
					_camera.Target = Jad.Scene.MeshObjects["sphere"];
				_lastTargetSphere = !_lastTargetSphere;

			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 + ": FPS " + Jad.Video.Views.CurrentView.Stats.Fps + " - Press ESC to Exit", 0, JColor.White);

			// Write instructions
			font.RenderLine("Use left mouse button, arrow keys, or NumPad to rotate", 0, JColor.Yellow);
			font.RenderLine("Use mouse wheel to zoom: Radius = " + _camera.Radius.ToString(), 0, JColor.Yellow);
			font.RenderLine("Use right mouse button to translate", 0, JColor.Yellow);
			font.RenderLine("Use middle mouse button to shift center of rotation", 0, JColor.Yellow);
			font.RenderLine("Press Space to switch the camera target", 0, JColor.Yellow);

			// End fonts rendering

			// Show the backbuffer


Last edited Mar 8, 2007 at 11:24 PM by Vicente, version 4


No comments yet.