gr_tut14_screenshot.jpg

ShadowMap.cs

#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

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

using JadEngine;
using JadEngine.Video;
using JadEngine.Core;
using JadEngine.Mathematics;
using JadEngine.Scene;
using JadEngine.Scene.MD5;
using JadEngine.Physics;
using JadEngine.Particles;
using JadEngine.Sound;
using JadEngine.Tools;
using JadEngine.Input;

#endregion

namespace Tutorial
{
	/// <summary>
	/// This tutorial will show you the shadowmap caps.
	/// </summary>
	public class ShadowMap : JApplication
	{
		#region Fields

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

		/// <summary>
		/// TODO
		/// </summary>
		JSprite sprite;

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

			#region Creation

			CreateRoom();

			#endregion

			#region Light

			// Create a spot light

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

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

			// By default, it is created looking at the Z axis. We want that light
			// goes in diagonal
			spotLight.Transform.Angles = new Vector3(Geometry.DegreeToRadian(35f), 0, 0);

			// Intensity
			spotLight.Multiplier = 1f;

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

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

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

			// Color
			spotLight.Color = JColor.White;

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

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

			// We want shadows
			spotLight.CastShadows = true;

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

			// Maximum cone aperture
			spotLight.FallOff = 50f;

			#endregion

			Jad.Scene.Camera.Transform.Position = new Vector3(-1.5f, 0, -3f);

			Jad.Scene.Camera.FarPlane = 40f;

			// Set the shadowmap technique
			Jad.Scene.Shadows.ShadowMode = JShadowMode.ShadowMap;

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

			// We create and sprite to show the shadowmap and shadowmask RT
			sprite = new JSprite();

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

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

			return true;
		}

		/// <summary>
		/// Creates the room of the scene
		/// </summary>
		void CreateRoom()
		{
			JMesh cubeMesh = JMesh.CreateCube(14, 10, 25);

			cubeMesh.IndexBuffer.InvertFaces();

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

			cube.Mesh = cubeMesh;

			// Material

			JMaterial[] material = new JMaterial[1];

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

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

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

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

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

			JMesh mesh = JMesh.CreateCube(1f, 1f, 8f);

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

			meshObject.Mesh = mesh;

			meshObject.Transform.RotateRadiansX(Geometry.DegreeToRadian(-45));

			// Material

			material = new JMaterial[1];

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

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

			layer.DiffuseMap.Color = new JColor(1, 1, 0);

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

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

			// Clone the object, using the same materials
			meshObject = meshObject.Clone("2");

			meshObject.Transform.RotateRadiansX(Geometry.DegreeToRadian(-25));

			meshObject.Transform.Position = new Vector3(-2, -3, 0);
		}

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

			if (Jad.Input.Keyboard[Key.P].Pressed)
				Jad.Scene.Shadows.ShadowMap.UsePCF = !Jad.Scene.Shadows.ShadowMap.UsePCF;

			if (Jad.Input.Keyboard[Key.O].Pressed)
				Jad.Scene.Shadows.SoftShadows = !Jad.Scene.Shadows.SoftShadows;

			float inc = Jad.At1Second(1.0f);

			// Everything was right, continue
			return true;
		}

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

			backbuffer.Render();

			int y = Jad.Video.Height - 100;

			sprite.SetPositionSize(0, y, 100, 100);
			sprite.Effect = Jad.Video.Effects.InHouse.ShowFloat;
			sprite.Texture = Jad.Video.Views[0].RenderTargets.ShadowMap.Texture;
			sprite.Render();

			sprite.SetPositionSize(100, y, 100, 100);
			sprite.Effect = Jad.Video.Effects.InHouse.PosColorTex1;
			sprite.Texture = Jad.Video.Views[0].RenderTargets.ShadowMask.Texture;
			sprite.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, yellow color
				font.RenderLine(Jad.Video.Views.CurrentView.Stats.Fps + " FPS", 0, JColor.White);

				font.RenderLine("PCF (P):" + Jad.Scene.Shadows.ShadowMap.UsePCF.ToString(), 0, JColor.White);

				font.RenderLine("Soft Shadows (o):" + Jad.Scene.Shadows.SoftShadows.ToString(), 0, JColor.White);

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

			// End the scene
			//Jad.Scene.End();

			backbuffer.Present();
		}

		#endregion
	}
}

Last edited Mar 8, 2007 at 2:20 PM by Vicente, version 3

Comments

No comments yet.