gr_tut3_screenshot.jpg

OtherMaps.cs

#region Using directives

using System;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

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

#endregion

namespace Tutorial
{
	/// <summary>
	/// This example will show you how to create objects and assign all the different map
	/// types that the engine supports (except the detail map).
	/// The tutorial uses the default first-person camera of the engine. Use keys WASD
	/// and the mouse to control it.
	/// Note: to see better the reflection map, lower the bumpiness value so there isn´t
	/// a high perturbation of the normal component.
	/// </summary>
	/// <remarks>
	/// This tutorial can crash if you try to activate all the options at the same time
	/// because it will need more number of operations than the supported in shaders 2.0.
	/// If you have a 3.0 graphic card it will play nice, but if not prepare for the crash 
	/// </remarks>
	public class OtherMaps : JApplication
	{
		#region Fields

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

		/// <summary>
		/// The material layer that contains the maps
		/// </summary>
		JMaterialLayer layer;

		/// <summary>
		/// Light of the scene
		/// </summary>
		JBaseLight light;

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

			// Move the camera a little behind of the plane
			Jad.Scene.Camera.Transform.Position = new Vector3(0, 0, -1.5f);
			((JCameraFirstPerson) Jad.Scene.Camera).Speed = 1.0f;
			//Jad.Scene.Camera.FarPlane = 300.0f;

			#region Objects

			// We create a JMesh: the object that has all the information about the vertices
			// and indices. In this example, a rectangle of 1.5x1.5 with 2 vertices (without subdivisions)
			// in the XY plane and the texture coordinates 1x1

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

			JMesh rectangleMesh = JMesh.CreateRectangle(10.0f, 2.0f, 2, 2, JCreateRectangle.XY, false, 5, 1);

			// 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 rectangle = Jad.Scene.MeshObjects.Create("wall", false);

			// Now, we associate the mesh with the meshobject

			rectangle.Mesh = rectangleMesh;

			#endregion

			#region Material

			// Create a materials 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
			layer = new JMaterialLayer();

			// Add it to the material
			material[0].AddLayer(layer);

			layer.VideoStates = new JVideoStates();
			for (int i = 0; i < 8; i++)
			{
				layer.VideoStates.SetSamplerState(i, SamplerStageStates.AddressU, (int) TextureAddress.Clamp);
				layer.VideoStates.SetSamplerState(i, SamplerStageStates.AddressV, (int) TextureAddress.Clamp);
			}

			// 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("Pared_Difusse", true);

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

			// ---------------------------------------------------------------------------
			// NORMAL
			// ---------------------------------------------------------------------------

			// Create the normal map texture
			layer.NormalMap.Texture = Jad.Video.Textures.Create2D("pom_test01", 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 = 0.8f;

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

			// ---------------------------------------------------------------------------
			// SPECULAR
			// ---------------------------------------------------------------------------

			// It´s not necessary to set a texture, we can use
			float m = 0.2f;
			layer.SpecularMap.Color = new JColor(0.9f * m, 0.376f * m, 0.125f * m);

			// Create the specular map texture
			layer.SpecularMap.Texture = Jad.Video.Textures.Create2D("Pared_Spec", true);

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

			// Specular power
			layer.SpecularMap.Power = 64;

			//layer.SpecularMap.Color = new Color(0.4f);

			// ---------------------------------------------------------------------------
			// HEIGHT MAP
			// ---------------------------------------------------------------------------

			// The Height map must be on tha alpha channel of the Normal map

			layer.HeightMap.Enabled = true;

			// Parallax amount
			layer.HeightMap.ParallaxAmount = 0.30f;
			layer.HeightMap.PerspectiveBias = 0.85f;

			layer.HeightMap.OcclusionMapping = true;

			// ---------------------------------------------------------------------------
			// Reflective
			// ---------------------------------------------------------------------------

			// Create the reflective map texture
			layer.ReflectionMap.Texture = Jad.Video.Textures.CreateCube("LobbyCube", true);

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

			// Reflection amount
			layer.ReflectionMap.Reflectivity = 0.5f;

			// ---------------------------------------------------------------------------
			// Refractive
			// ---------------------------------------------------------------------------

			// Create the refractive map texture
			layer.RefractionMap.Texture = Jad.Video.Textures.CreateCube("LobbyCube", true);

			// We DON´T activate the refraction, let the user do it
			layer.RefractionMap.Enabled = false;

			// Refraction amount
			layer.RefractionMap.Transmittance = 0.5f;

			#endregion

			#region Light

			// Create a directional light
			light = Jad.Scene.Lights.Create("light", JLightType.Directional);

			//light.Hide = true;

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

			// Intensity
			light.Multiplier = 1.0f;

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

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

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

			// Color
			light.Color = JColor.White;

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

			((JSpotLight) light).FallOff = 45.0f;
			((JSpotLight) light).HotSpot = 40.0f;

			light.Transform.LookAt(Jad.Scene.Root);

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

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

			// 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)
				layer.NormalMap.Bumpiness += 0.1f;

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

			// In this example a value greater than 0.06 starts to give visual errors
			if (Jad.Input.Keyboard[Key.PageUp].Pressed)
				layer.HeightMap.ParallaxAmount += 0.01f;

			if (Jad.Input.Keyboard[Key.PageDown].Pressed)
				layer.HeightMap.ParallaxAmount -= 0.01f;

			if (Jad.Input.Keyboard[Key.F3].Pressed)
				layer.HeightMap.PerspectiveBias += 0.01f;

			if (Jad.Input.Keyboard[Key.F4].Pressed)
				layer.HeightMap.PerspectiveBias -= 0.01f;

			// If keys End Home were pressed, we modify the reflectivity value
			if (Jad.Input.Keyboard[Key.End].Pressed)
				layer.ReflectionMap.Reflectivity += 0.1f;

			if (Jad.Input.Keyboard[Key.Home].Pressed)
				layer.ReflectionMap.Reflectivity -= 0.1f;

			if (Jad.Input.Keyboard[Key.D1].Pressed)
				layer.NormalMap.Enabled = !layer.NormalMap.Enabled;

			if (Jad.Input.Keyboard[Key.D2].Pressed)
				layer.SpecularMap.Enabled = !layer.SpecularMap.Enabled;

			if (Jad.Input.Keyboard[Key.D3].Pressed)
				layer.HeightMap.Enabled = !layer.HeightMap.Enabled;

			if (Jad.Input.Keyboard[Key.D4].Pressed)
				layer.ReflectionMap.Enabled = !layer.ReflectionMap.Enabled;

			if (Jad.Input.Keyboard[Key.D5].Pressed)
			{
				layer.RefractionMap.Enabled = !layer.RefractionMap.Enabled;

				// We use the same sample in the 2 cases, so we can´t reactivate 
				// the heightmap
				if (layer.RefractionMap.Enabled) layer.HeightMap.Enabled = false;
			}

			if (Jad.Input.Keyboard[Key.D6].Pressed)
				layer.HeightMap.OcclusionMapping = !layer.HeightMap.OcclusionMapping;

			float lSpeed = 2.0f;

			if (Jad.Input.Keyboard[Key.Up].Down)
				light.Transform.TranslateStepY(Jad.At1Second(lSpeed));

			else if (Jad.Input.Keyboard[Key.Down].Down)
				light.Transform.TranslateStepY(-Jad.At1Second(lSpeed));

			if (Jad.Input.Keyboard[Key.Left].Down)
				light.Transform.TranslateStepX(-Jad.At1Second(lSpeed));

			else if (Jad.Input.Keyboard[Key.Right].Down)
				light.Transform.TranslateStepX(Jad.At1Second(lSpeed));

			float radius = 1.7f;
			light.Transform.TranslateX(radius * (float)Math.Cos(Jad.Timer.Time));
			light.Transform.TranslateY(radius * (float)Math.Sin(Jad.Timer.Time));

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

			if (showHelp)
			{
				// 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. 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);

				font.RenderLine("Bumpiness ( Numpad + - to change ): " + layer.NormalMap.Bumpiness.ToString("N2"), 0, JColor.White);
				font.RenderLine("Parallax ( PgUp PgDown to change ): " + layer.HeightMap.ParallaxAmount.ToString("N2"), 0, JColor.White);
				font.RenderLine("Perspective(Occlusion) ( F3 F4 to change ): " + layer.HeightMap.PerspectiveBias.ToString("N2"), 0, JColor.White);
				font.RenderLine("Reflectivity ( Home End to change ): " + layer.ReflectionMap.Reflectivity.ToString("N2"), 0, JColor.White);

				font.RenderLine("Normal     (1):" + layer.NormalMap.Enabled.ToString(), 0, JColor.White);
				font.RenderLine("Specular   (2):" + layer.SpecularMap.Enabled.ToString(), 0, JColor.White);
				font.RenderLine("Height     (3):" + layer.HeightMap.Enabled.ToString(), 0, JColor.White);
				font.RenderLine("Reflection (4):" + layer.ReflectionMap.Enabled.ToString(), 0, JColor.White);
				font.RenderLine("Refraction (5):" + layer.RefractionMap.Enabled.ToString(), 0, JColor.White);
				font.RenderLine("Par. Occl. (6):" + layer.HeightMap.OcclusionMapping.ToString(), 0, JColor.White);

				// End fonts rendering
				Jad.Video.Fonts.End();
			}

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

		#endregion
	}
}

Last edited Mar 8, 2007 at 12:00 PM by Vicente, version 7

Comments

No comments yet.