gr_tut27_screenshot.jpg

AdvancedStencilShadows.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 example will teach you to use the advanced properties about stencil shadows:
	/// Infinity projection, Depth pass, Slope adjust & Depth Fail, Soft shadows.
	/// </summary>
	public class AdvancedStencilShadows : JApplication
	{
		#region Fields

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

		/// <summary>
		/// The cube
		/// </summary>
		JMeshObject cube;

		/// <summary>
		/// A direct light
		/// </summary>
		JDirectLight directLight;

		/// <summary>
		/// A spot light
		/// </summary>
		JSpotLight spotLight;

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

			CreateScene();

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

			// Set the camera
			Jad.Scene.Camera.Transform.Position = new Vector3(0, 0f, -2f);

			JCameraFirstPerson camera = (JCameraFirstPerson) Jad.Scene.Camera;

			camera.Speed = 0.2f;

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

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

			return true;
		}

		/// <summary>
		/// Creates a directional light
		/// </summary>
		/// <param name="position">Position of the light</param>
		void CreateDirectLight(Vector3 position)
		{
			// Create a directorial light 
			directLight = (JDirectLight) Jad.Scene.Lights.Create("direct01", JLightType.Directional);

			// Direct light has no position, but if you want targeting, you need
			// to indicate the light position
			directLight.Transform.Position = position;

			directLight.Color = JColor.White;
			directLight.Multiplier = 1.0f;
			directLight.AffectSpecular = true;
			directLight.Attenuation.Type = JLightAttenuationType.None;
			directLight.CastShadows = true;

			directLight.Transform.Target = cube;
		}

		/// <summary>
		/// Creates a spot light
		/// </summary>
		/// <param name="position">Position of the light</param>
		/// <param name="lightColor">Color of the light</param>
		void CreateLight(Vector3 position, JColor lightColor)
		{
			spotLight = (JSpotLight) Jad.Scene.Lights.Create("spot", JLightType.Spot);

			spotLight.Transform.Position = position;

			spotLight.Multiplier = 1f;

			spotLight.Attenuation.Type = JLightAttenuationType.DualRadius;

			spotLight.Attenuation.Start = 2f;

			spotLight.Attenuation.End = 3f;

			spotLight.Color = lightColor;

			spotLight.AffectDiffuse = true;

			spotLight.AffectSpecular = true;

			spotLight.CastShadows = true;

			spotLight.HotSpot = 25f;

			spotLight.FallOff = 50f;

			spotLight.Transform.Target = cube;
		}

		/// <summary>
		/// Creates the room
		/// </summary>
		void CreateRoom()
		{
			JMesh cubeMesh = JMesh.CreateCube(2f, 2f, 2f);

			cubeMesh.IndexBuffer.InvertFaces();

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

			cube.Mesh = cubeMesh;

			JMaterial[] material = new JMaterial[1];

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

			JMaterialLayer layer = new JMaterialLayer();

			material[0].AddLayer(layer);

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

			layer.DiffuseMap.UTile = layer.DiffuseMap.VTile = 4f;

			cube.Material = material;
		}

		/// <summary>
		/// Creates a cube
		/// </summary>
		void CreateCube()
		{
			float cubeSize = 0.25f * 2f;

			JMesh mesh = JMesh.CreateCube(cubeSize, cubeSize, cubeSize);

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

			cube.Mesh = mesh;

			JMaterial[] material = new JMaterial[1];

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

			JMaterialLayer layer = new JMaterialLayer();

			material[0].AddLayer(layer);

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

			cube.Material = material;

			cube.Transform.Position = new Vector3(0, -1f + 0.25f, 0);

			cube.CastShadows = true;

			cube.Mesh.ShadowVolume.ExtrusionConstant = 5f;
		}

		/// <summary>
		/// Creates the scene
		/// </summary>
		void CreateScene()
		{
			CreateRoom();

			CreateCube();

			Vector3 position = new Vector3(0f, 0.4f, 0.4f);

			CreateLight(position, JColor.White);

			CreateDirectLight(position); directLight.Hide = true;

			Jad.Scene.Shadows.ShadowMode = JShadowMode.StencilVolume;

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

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

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

			if (Jad.Input.Keyboard[Key.L].Pressed)
				Jad.Scene.Shadows.Stencil.DepthPass = !Jad.Scene.Shadows.Stencil.DepthPass;

			if (Jad.Input.Keyboard[Key.I].Pressed)
				Jad.Scene.Shadows.Stencil.InfiniteFarPlane = !Jad.Scene.Shadows.Stencil.InfiniteFarPlane;

			if (Jad.Input.Keyboard[Key.U].Pressed)
			{
				spotLight.Hide = !spotLight.Hide;

				directLight.Hide = !spotLight.Hide;
			}

			if (Jad.Input.Keyboard[Key.P].Pressed)
			{
				if (Jad.Scene.Shadows.ShadowMode == JShadowMode.StencilVolume)
					Jad.Scene.Shadows.ShadowMode = JShadowMode.None;

				else
					Jad.Scene.Shadows.ShadowMode = JShadowMode.StencilVolume;
			}

			if (Jad.Input.Keyboard[Key.Up].Down)
				Jad.Scene.Shadows.Stencil.SlopeForStencilShadows += 0.001f;

			if (Jad.Input.Keyboard[Key.Down].Down)
				Jad.Scene.Shadows.Stencil.SlopeForStencilShadows -= 0.001f;

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

				font.RenderLine(Jad.Version + ". Press ESC to exit. Press G to Show/Hide Gizmos. F1 show/hide text", 0, JColor.Yellow);

				font.RenderLine(Jad.Video.Views.CurrentView.Stats.Fps + " FPS", 0, JColor.White);

				font.RenderLine("Depth pass(L):" + Jad.Scene.Shadows.Stencil.DepthPass.ToString(), 0, JColor.White);
				font.RenderLine("Infinite (I):" + Jad.Scene.Shadows.Stencil.InfiniteFarPlane.ToString(), 0, JColor.White);
				font.RenderLine("Light type (U):" + ((spotLight.Hide) ? "Direct" : "Spot"), 0, JColor.White);

				if (!Jad.Scene.Shadows.Stencil.DepthPass)
					font.RenderLine("Slope (Up/Down):" + Jad.Scene.Shadows.Stencil.SlopeForStencilShadows.ToString("N5"), 0, JColor.White);

				font.RenderLine("Shadows (P):" + (Jad.Scene.Shadows.ShadowMode == JShadowMode.None ? "false" : " true"), 0, JColor.White);
				font.RenderLine("Debug shadows (O):" + Jad.Scene.Shadows.Stencil.Debug.ToString(), 0, JColor.White);
				font.RenderLine("Soft Shadows (H):" + Jad.Scene.Shadows.SoftShadows.ToString(), 0, JColor.White);

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

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

		#endregion
	}
}

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

Comments

No comments yet.