gr_tut24_screenshot.jpg

LightShafts.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>
	/// In this tutorial you'll see all the possibilities offered by the light shafts.
	/// </summary>
	public class LightShafts : JApplication
	{
		#region Fields

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

		/// <summary>
		/// TODO
		/// </summary>
		JSpotLight shaftLight;

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

			// Set the fixed mode, so the engine works at a fixed number of frames per second
			Jad.Timer.FixedMode = true;
			Jad.Timer.FixedModeFPS = 1.0f / 30.0f;

			// Create the scene
			CreateScene();

			shaftLight = (JSpotLight) Jad.Scene.Lights["fspot01"];

			// Number of sampling planes for the light shaft
			shaftLight.Shafts.NumPlanes = 40;
			// Light shaft power
			shaftLight.Shafts.Power = 1.50f;
			// Light shaft intensity
			shaftLight.Shafts.Intensity = 0.6f;
			// Enable the shafts
			shaftLight.Shafts.Enabled = true;

			// Ambient light
			Jad.Scene.Lights.Ambient = new JColor(0.05f, 0.05f, 0.05f);

			// Camera position
			Jad.Scene.Camera.Transform.Position = new Vector3(0.0f, 1.0f, -1.0f);

			// 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 scene
		/// </summary>
		void CreateScene()
		{
			Jad.Import.Load("focus.haddd", 1.0f);
		}

		/// <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 (Jad.Input.Keyboard[Key.B].Pressed)
				Jad.Scene.Lights.Shafts.ApplyBlur = !Jad.Scene.Lights.Shafts.ApplyBlur;

			// Shaft params

			if (Jad.Input.Keyboard[Key.F].Pressed)
				shaftLight.Shafts.Enabled = !shaftLight.Shafts.Enabled;

			if (Jad.Input.Keyboard[Key.X].Pressed)
				shaftLight.Shafts.SoftwareClipping = !shaftLight.Shafts.SoftwareClipping;

			if (Jad.Input.Keyboard[Key.Z].Pressed)
				shaftLight.Shafts.UseCookie = !shaftLight.Shafts.UseCookie;

			if (Jad.Input.Keyboard[Key.Insert].Pressed)
				shaftLight.Shafts.Power += 0.02f;
			else if (Jad.Input.Keyboard[Key.Delete].Pressed)
				shaftLight.Shafts.Power -= 0.02f;

			if (Jad.Input.Keyboard[Key.Home].Pressed)
				shaftLight.Shafts.Intensity += 0.02f;
			else if (Jad.Input.Keyboard[Key.End].Pressed)
				shaftLight.Shafts.Intensity -= 0.02f;

			if (Jad.Input.Keyboard[Key.PageUp].Pressed)
			{
				if (shaftLight.Shafts.NumPlanes < 202)
					shaftLight.Shafts.NumPlanes += 2;
			}
			else if (Jad.Input.Keyboard[Key.PageDown].Pressed)
			{
				if (shaftLight.Shafts.NumPlanes > 22)
					shaftLight.Shafts.NumPlanes -= 2;
			}

			// Everything was right, continue
			return true;
		}

		/// <summary>
		/// Render method
		/// </summary>
		public override void Render()
		{
			// The views are render 'windows'. The engine creates the implicit
			// view, the backbuffer, by default.

			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(string.Format("Shaft enabled (F): {0}  SoftClipping (X): {1}  NumPlanes (PageUp/PageDown): {2}",
						shaftLight.Shafts.Enabled, shaftLight.Shafts.SoftwareClipping, shaftLight.Shafts.NumPlanes), 0, JColor.White);
				font.RenderLine(string.Format("Shaft Power (Insert/Delete): {0}  Intensity (Home/End): {1}", shaftLight.Shafts.Power.ToString("N2"), shaftLight.Shafts.Intensity.ToString("N2")), 0, JColor.White);

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

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

		#endregion
	}
}

Last edited Mar 8, 2007 at 7:32 PM by Vicente, version 2

Comments

No comments yet.