gr_tut11_screenshot.jpg

LightFeatures.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 different effects about lights:
	/// projector, shafts, coronas and volumetric.
	/// </summary>
	public class LightEffects : JApplication
	{
		#region Fields

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

		/// <summary>
		/// TODO
		/// </summary>
		JSpotLight spotLight;

		/// <summary>
		/// TODO
		/// </summary>
		JSpotLight shaftsSpotLight;

		/// <summary>
		/// TODO
		/// </summary>
		JOmniLight omniLight;

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

			// Create the scene
			CreateScene();

			#endregion

			#region Lights

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

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

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

			// Intensity
			spotLight.Multiplier = 1f;

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

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

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

			// 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 don´t generate shadows
			spotLight.CastShadows = false;

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

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

			// Set the projector map
			spotLight.Projector.Map = Jad.Video.Textures.Create2D("projector", true);

			// Activate the projector
			spotLight.Projector.Enabled = true;

			// Create a spot light for shafts
			shaftsSpotLight = (JSpotLight) Jad.Scene.Lights.Create("shafts", JLightType.Spot);

			// Set its position
			shaftsSpotLight.Transform.Position = new Vector3(0, 1.5f, 3.5f);

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

			// Intensity
			shaftsSpotLight.Multiplier = 1f;

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

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

			// Distance where the light is completely attenuated
			// This is used on shafts too, so you must assign an accurate value
			shaftsSpotLight.Attenuation.End = 6f;

			// Color
			shaftsSpotLight.Color = JColor.White;

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

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

			// We don´t generate shadows
			shaftsSpotLight.CastShadows = false;

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

			// Maximum cone aperture
			shaftsSpotLight.FallOff = 30f;

			// Set the projector map
			shaftsSpotLight.Projector.Map = Jad.Video.Textures.Create2D("shafts", true);

			// Activate the projector
			shaftsSpotLight.Projector.Enabled = true;

			// Set the shaft texture
			shaftsSpotLight.Shafts.CookieTexture = Jad.Video.Textures.Create2D("shafts", true);

			// Enable the shafts
			shaftsSpotLight.Shafts.Enabled = true;

			// Create an omni light
			omniLight = (JOmniLight) Jad.Scene.Lights.Create("omni", JLightType.Omni);

			// Set its position
			omniLight.Transform.Position = new Vector3(2.0f, 3.0f, 0.0f);

			// Intensity
			omniLight.Multiplier = 1f;

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

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

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

			// Color
			omniLight.Color = JColor.RedColor;

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

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

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

			#endregion

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

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

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

			// Invert the cube faces, as we want to be inside the room
			cubeMesh.IndexBuffer.InvertFaces();

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

			// Associate the mesh with the meshobject
			cube.Mesh = cubeMesh;

			// Create a materials array of 1 element
			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);

			// Modify the material tiling so it repeats more times the texture
			layer.DiffuseMap.UTile = layer.DiffuseMap.VTile = 4f;

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

		}

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

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

			// The spot light

			if (Jad.Input.Keyboard[Key.P].Pressed)
				spotLight.Projector.Enabled = !spotLight.Projector.Enabled;

			if (Jad.Input.Keyboard[Key.NumPad8].Pressed)
				spotLight.FallOff += 1f;

			if (Jad.Input.Keyboard[Key.NumPad2].Pressed)
				spotLight.FallOff -= 1f;

			if (Jad.Input.Keyboard[Key.NumPad9].Pressed)
				spotLight.HotSpot += 1f;

			if (Jad.Input.Keyboard[Key.NumPad3].Pressed)
				spotLight.HotSpot -= 1f;

			// The volumetric light
			if (Jad.Input.Keyboard[Key.V].Pressed)
				omniLight.Volumetric.Enabled = !omniLight.Volumetric.Enabled;

			if (Jad.Input.Keyboard[Key.C].Pressed)
				shaftsSpotLight.Corona.Enabled = !shaftsSpotLight.Corona.Enabled;

			if (Jad.Input.Keyboard[Key.NumPad7].Down)
				omniLight.Volumetric.PowValue += 0.1f;

			if (Jad.Input.Keyboard[Key.NumPad1].Down)
				omniLight.Volumetric.PowValue -= 0.1f;

			// The volumetric light
			if (Jad.Input.Keyboard[Key.F].Pressed)
				shaftsSpotLight.Shafts.Enabled = !shaftsSpotLight.Shafts.Enabled;

			// 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("Projector(P):" + spotLight.Projector.Enabled.ToString() + " HotSpot(9,3):" + spotLight.HotSpot.ToString("N0") + " FallOff(8,2):" + spotLight.FallOff.ToString("N0"), 0, JColor.White);

				font.RenderLine("Volumetric(V):" + omniLight.Volumetric.Enabled.ToString() + " Pow(7,1):" + omniLight.Volumetric.PowValue.ToString("N2"), 0, JColor.White);

				font.RenderLine("Corona(C):" + shaftsSpotLight.Corona.Enabled.ToString(), 0, JColor.White);

				font.RenderLine("Shafts(F):" + shaftsSpotLight.Shafts.Enabled.ToString(), 0, JColor.White);

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

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

		#endregion
	}
}

Last edited Mar 8, 2007 at 2:04 PM by Vicente, version 4

Comments

No comments yet.