#region Using directives

using System;
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 example will show you how to create a sphere object, assign it a material with
	/// normal mapping and add an omni light and a spot light that moves around the sphere.
	/// The tutorial uses the default first-person camera of the engine. Use keys WASD
	/// and the mouse to control it.
	/// </summary>
	public class NormalMaps : JApplication
		#region Fields

		/// <summary>
		/// Indicates if the help must be shown in the screen or not
		/// </summary>
		bool showHelp = true;

		/// <summary>
		/// The spot light that moves around the sphere
		/// </summary>
		JSpotLight spotLight;

		/// <summary>
		/// Elapsed time that we use to assign a path to the spot light
		/// </summary>
		float time = 0;


		#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,...

			#region Objects

			// We create a JMesh: the object that has all the information about the vertices
			// and indices.
			// In this example, a sphere, with a 1.5m radius and 20 vertical and horizontal cuts

			// It´s important to remember that in Jad, the distance measure unit is the METER

			JMesh cubeMesh = JMesh.CreateSphere(1.5f, 20, 20);

			// A JMeshObject is an object with the information about the meshes and the
			// materials. It´s the element we´ll use to render the meshes.

			// We create a new JMeshObject in the scene. We set that we don´t want to
			// create a new mesh object, as we have just done it.

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

			// Now, we associate the mesh with the meshobject

			cube.Mesh = cubeMesh;


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

			// Create the normal map texture
			layer.NormalMap.Texture = Jad.Video.Textures.Create2D("brick_n", true);

			// Activate the map. This is not necessary in the DiffuseMap, but it´s necessary
			// in the rest types of maps
			layer.NormalMap.Enabled = true;

			// Indicate a value of "bumpy quantity". Reasonable values range from 0 to 1.
			layer.NormalMap.Bumpiness = 1f;

			// We indicate that we want to use the Normal Mapping shader
			layer.NormalType = JNormalType.Mapping;

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


			#region Lights

			// Create an omni light

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

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

			// Intensity
			light.Multiplier = 1f;

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

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

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

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

			// Create a spot light

			spotLight = (JSpotLight) Jad.Scene.Lights.Create("spot", JLightType.Spot);

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

			// Intensity
			spotLight.Multiplier = 1f;

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

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

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

			// Color
			spotLight.Color = JColor.RedColor;

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

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

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

			// Cone aperture of the spot light
			spotLight.HotSpot = 25f;

			// Maximun cone aperture
			// la apertura máximo del cono
			spotLight.FallOff = 50f;

			// Put an ambient light, a little more darker than the one in Tutorial 1
			// so we can see the effect better
			Jad.Scene.Lights.Ambient = new JColor(0.2f, 0.2f, 0.2f);


			// Everything is set, start the engine

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

			return true;

		/// <summary>
		/// Moves the spot light around the sphere and rotates the sphere
		/// </summary>
		void MoveLightAndRotateObject()
			// We want that the light takes 3 seconds to turn around the sphere

			// The method Jad.AtXSecond allows to specify how much time
			// it takes to get to a certain value

			float velocity = Jad.AtXSecond(3, 2f * (float) Math.PI);

			time += velocity;

			spotLight.Transform.Position = new Vector3((float) Math.Cos(time), 0, (float) Math.Sin(time)) * 2f;

			// We have created only 1 object, so get it from the list
			JMeshObject m = Jad.Scene.MeshObjects[0];

			// Set the Y rotation values
			m.Transform.RotateRadiansY(time * 0.01f);

		/// <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 (Jad.Input.Keyboard[Key.F1].Pressed) showHelp = !showHelp;

			// The engine supports World Space Normals and Tangent Space Normals
			// The first one are needed for some specific shaders
			// The second one are more common, and fastest.
			// Yo can press F2 to see the change on fps. On this example there is no
			// need to have World Space Normals, because are not used.
			if (Jad.Input.Keyboard[Key.F2].Pressed)
				JMaterial mat = Jad.Scene.MeshObjects[0].Material[0];
				mat.WorldSpaceNormals = !mat.WorldSpaceNormals;

			// If G key was pressed we activate / deactivate the gizmos
			// The diference between KeyTouch and KeyPressed is that KeyTouch only returns
			// true if the key was pressed for the first time and not when it´s down as in
			// KeyPressed

			// KeyTouch is like normal writting, returning true only once for every key press

			if (Jad.Input.Keyboard[Key.G].Pressed)
				Jad.Scene.ShowGizmos = !Jad.Scene.ShowGizmos;

			// If keys + - (from the numeric keyboard) were pressed, we modify the general bumpiness value

			if (Jad.Input.Keyboard[Key.NumPadPlus].Pressed)
				Jad.Scene.MeshObjects.Bumpiness += 0.1f;

			if (Jad.Input.Keyboard[Key.NumPadMinus].Pressed)
				Jad.Scene.MeshObjects.Bumpiness -= 0.1f;


			// Everything was right, continue
			return true;

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


			if (showHelp)
				// 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. Press G to Show/Hide Gizmos. F1 show/hide text", 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);

				// Write text in the second line, position x=0, White color
				font.RenderLine("Bumpiness ( Numpad + - to change ): " + Jad.Scene.MeshObjects.Bumpiness.ToString("N2"), 0, JColor.White);

				JMaterial mat = Jad.Scene.MeshObjects[0].Material[0];

				// Write text in the third line, position x=0, white color
				font.RenderLine("World Space Normals(F2):" + mat.WorldSpaceNormals, 0, JColor.White);

				// End fonts rendering



Last edited Mar 8, 2007 at 10:54 AM by Vicente, version 5


No comments yet.