gr_tut12_screenshot.jpg

GlowAndFog.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.Import;
using JadEngine.Input;
using System.IO;

#endregion

namespace Tutorial
{
	/// <summary>
	/// This example will teach you to use the emissive mapping material to make a glow effect
	/// and the self-ilum possibilites of the engine.
	/// You can see the different fog options of the engine, and change a lot of parameters.
	/// </summary>
	public class GlowAndFog : JApplication
	{
		#region Fields

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

		/// <summary>
		/// The emissive map layer
		/// </summary>
		JMaterialLayer layer;

		/// <summary>
		/// TODO
		/// </summary>
		JMeshObject hebeModel;

		/// <summary>
		/// Indicates if the glow is animated or not
		/// </summary>
		bool glowAnimated;

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

			CreateGlowWall();

			CreateObjects();

			Jad.Scene.Fog.Type = JFogType.Linear;

			// Set the bottom value for the layered fog
			Jad.Scene.Fog.Bottom = -1.5f;
			// Set the top value for the layered fog
			Jad.Scene.Fog.Top = -0.8f;

			#endregion

			#region Light

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

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

			// Intensity
			light.Multiplier = 1f;

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

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

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

			// Color
			light.Color = JColor.Yellow;

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

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

			#endregion

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

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

		/// <summary>
		/// Creates the glowing wall
		/// </summary>
		void CreateGlowWall()
		{
			JMesh cubeMesh = JMesh.CreateRectangle(2, 2, 2, 2, JCreateRectangle.XY, false, 1f, 1f);

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

			cube.Mesh = cubeMesh;

			cube.Transform.Position = new Vector3(0, 0, 1.49f);

			// Material

			JMaterial[] material = new JMaterial[1];

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

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

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

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

			// Enable de Emissive map
			layer.EmissiveMap.Enabled = true;

			// Set a color, when Mode=JEmissiveMode.Color
			layer.EmissiveMap.Color = JColor.Yellow;

			// Set the intensity of the emissivemap.
			layer.EmissiveMap.Intensity = 0.1f;

			// Set the texture for the Mode=JEmissiveMode.Texture
			layer.EmissiveMap.Texture = Jad.Video.Textures.Create2D("projector", true);

			// Set the mode. The diffuse mode will use the DiffuseMap.Texture
			layer.EmissiveMap.Mode = JEmissiveMode.Diffuse;

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

		/// <summary>
		/// Creates the object of the scene
		/// </summary>
		void CreateObjects()
		{
			// Load hebe

			if (Jad.Import.Load("hebe.haddd") == false)
				throw new FileNotFoundException("The model hebe can't be loaded.");

			// We get the first loaded object ( the only one )

			JMeshObject model = Jad.Import.MeshObjects[0];

			model.Transform.Position = new Vector3(-1.5f, -1.5f + model.Mesh.BoundingBox.Height / 2f, 0);

			// The self-ilum objects add this color to the ambient component
			model.Material[0].Layers[0].EmissiveMap.Color = new JColor(0.3f);

			// We tell the engine to render this mesh without lights, only ambient+EmissiveMap.Color
			model.AffectLight = false;

			hebeModel = model;
		}

		/// <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.T].Pressed)
				glowAnimated = !glowAnimated;

			if (Jad.Input.Keyboard[Key.I].Pressed)
				hebeModel.AffectLight = !hebeModel.AffectLight;

			// There are 4 fog modes, so we are going to alternate them

			if (Jad.Input.Keyboard[Key.F].Pressed)
			{
				int m = (int) Jad.Scene.Fog.Type;

				m++;

				if (m > 4) m = 0;

				Jad.Scene.Fog.Type = (JFogType) m;
			}

			if (Jad.Input.Keyboard[Key.NumPad9].Down)
				Jad.Scene.Fog.Top += 0.1f;

			if (Jad.Input.Keyboard[Key.NumPad3].Down)
				Jad.Scene.Fog.Top -= 0.1f;

			if (Jad.Input.Keyboard[Key.NumPadPlus].Down)
				Jad.Scene.Fog.Near += 0.1f;

			if (Jad.Input.Keyboard[Key.NumPadMinus].Down)
				Jad.Scene.Fog.Near -= 0.1f;

			if (Jad.Input.Keyboard[Key.NumPad8].Down)
				Jad.Scene.Fog.Density += 0.1f;

			if (Jad.Input.Keyboard[Key.NumPad2].Down)
				Jad.Scene.Fog.Density -= 0.1f;

			// There are 3 emissive modes, so we are going to alternate them

			if (Jad.Input.Keyboard[Key.E].Pressed)
			{
				int m = (int) layer.EmissiveMap.Mode;

				m++;

				if (m > 3) m = 1;

				layer.EmissiveMap.Mode = (JEmissiveMode) m;
			}

			// Modify the parameters of the postproduction effect asociated with the glow
			if (glowAnimated)
				Jad.Scene.GlowEffect.HorizontalBlur.GlowAttenuation = 1f + (float) Math.Abs(Math.Sin(Jad.Timer.Time));

			else
				Jad.Scene.GlowEffect.HorizontalBlur.GlowAttenuation = 1.2f;

			Jad.Scene.GlowEffect.VerticalBlur.GlowAttenuation = Jad.Scene.GlowEffect.HorizontalBlur.GlowAttenuation;

			// 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("Wall Emissive mode(E):" + layer.EmissiveMap.Mode.ToString(), 0, JColor.White);

				font.RenderLine("Wall Glow Animated(T):" + glowAnimated.ToString(), 0, JColor.White);

				font.RenderLine("Model AffectLight(I):" + hebeModel.AffectLight.ToString(), 0, JColor.White);

				string fogText = "Fog mode(F):" + Jad.Scene.Fog.Type.ToString();

				if (Jad.Scene.Fog.Type != JFogType.None)
				{
					fogText += " Near (+,-):" + Jad.Scene.Fog.Near.ToString("N2");

					if (Jad.Scene.Fog.Type == JFogType.Layered)
						fogText += " Top (9,3):" + Jad.Scene.Fog.Top.ToString("N2");

					if (Jad.Scene.Fog.Type != JFogType.Linear)
						fogText += " Density (8,2):" + Jad.Scene.Fog.Density.ToString("N2");
				}

				font.RenderLine(fogText, 0, JColor.White);

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

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

		#endregion
	}
}

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

Comments

No comments yet.