gr_tut22_screenshot.Jpg

BillboardingAndHeatHaze.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 create custom effects to use on postprocess objects
	/// (a heat haze). You will create 3D text, trasparency and billboarding.
	/// You will learn how to use the scenegraph better, changing the pivot point for rotations
	/// and you will see the hierarchical boxes.
	/// </summary>
	public class BillboardingAndHeatHaze : JApplication
	{
		#region Fields

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

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

		/// <summary>
		/// The sphere object
		/// </summary>
		JMeshObject sphere;

		/// <summary>
		/// TODO
		/// </summary>
		JMeshObject rectangle;

		/// <summary>
		/// The 3D text object
		/// </summary>
		JMeshObject text3D;

		/// <summary>
		/// TODO
		/// </summary>
		float blueColor = 1;

		/// <summary>
		/// TODO
		/// </summary>
		float blueColorSign = 1;

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

			CreateObjects();

			CreateHazeEffect();

			CreateText3D();

			CreateBillBoardAlpha();

			#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, -2f);

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

			#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 objects of the scene
		/// </summary>
		void CreateObjects()
		{
			JMesh cubeMesh = JMesh.CreateCube(3, 3, 3);

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

			cube.Mesh = cubeMesh;

			cube.DebugRendering.ShowHierarchicalBox = true;

			cube.DebugRendering.ShowHierarchicalSphere = true;

			cube.DebugRendering.ShowPivotPoint = true;

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

			cube.Material = material;

			JMesh sphereMesh = JMesh.CreateSphere(1, 20, 20);

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

			sphere.Mesh = sphereMesh;

			sphere.Material = material;

			sphere.Transform.Position = new Vector3(1.5f + 1f, 0, 0);

			cube.SceneGraph.AddChild(sphere);
		}

		/// <summary>
		/// Creates the Haze effect
		/// </summary>
		void CreateHazeEffect()
		{
			JMesh mesh = JMesh.CreateRectangle(3, 3, 2, 2, JCreateRectangle.XY, false, 1f, 1f);

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

			rectangle.Transform.Position = new Vector3(0, 0, -3f);

			// We indicate this objet to face always the camera (billboarding)

			rectangle.AlwaysFacingCamera = true;

			rectangle.Mesh = mesh;

			// Let's see the axes

			rectangle.DebugRendering.ShowAxes = true;

			JMaterial[] material = new JMaterial[1];

			material[0] = Jad.Scene.Materials.Load("heathaze.material");

			//material[0].Priority = JMaterialPriority.PostProcess;

			rectangle.Material = material;
		}

		/// <summary>
		/// TODO
		/// </summary>
		void CreateBillBoardAlpha()
		{
			JMesh mesh = JMesh.CreateRectangle(1f, 1f, 2, 2, JCreateRectangle.XY, false, 1f, 1f);

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

			billboard.Transform.Position = new Vector3(0, 2f, 0);

			// We tell this objet to face always the camera (billboarding)

			billboard.AlwaysFacingCamera = true;

			billboard.DebugRendering.ShowAxes = true;

			billboard.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("billboardAlpha", true);

			material[0].HasAlphaTest = true;

			layer.RenderStates.AlphaTestEnable = true;
			layer.RenderStates.AlphaFunction = Compare.Greater;
			layer.RenderStates.ReferenceAlpha = 0;

			billboard.Material = material;
		}

		/// <summary>
		/// Creates the 3D Text
		/// </summary>
		void CreateText3D()
		{
			JMesh mesh = JMesh.CreateText3D(new System.Drawing.Font("arial", 30), "HADDD", 0.2f);

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

			text3D.Transform.Position = new Vector3(0, 0, -3f);

			text3D.Mesh = mesh;

			// We need to render the text the last at the post process
			// Because the haze effect gets the backbuffer BEFORE the
			// JMaterialPriority.PostProcess meshobjects has been rendered

			text3D.Priority = 100;

			JMaterial[] material = new JMaterial[1];

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

			material[0].Priority = JMaterialPriority.PostProcess;

			material[0].RenderStates.ZBufferWriteEnable = true;

			JMaterialLayer layer = new JMaterialLayer();

			material[0].AddLayer(layer);

			layer.DiffuseMap.TextureEnabled = false;

			// We want this object to be transparent
			// Setting the blending one-One will ensure that the rendering order
			// will not affect.

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

			layer.RenderStates.AlphaBlendEnable = true;
			layer.RenderStates.SourceBlend = Blend.One;
			layer.RenderStates.DestinationBlend = Blend.One;

			text3D.Material = material;
		}

		/// <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.Up].Down)
				cube.Transform.TranslateStepY(0.01f);

			if (Jad.Input.Keyboard[Key.Down].Down)
				cube.Transform.TranslateStepY(-0.01f);

			if (Jad.Input.Keyboard[Key.Left].Down)
				sphere.Transform.TranslateStepX(-0.01f);

			if (Jad.Input.Keyboard[Key.Right].Down)
				sphere.Transform.TranslateStepX(0.01f);

			Vector3 pivot = cube.Transform.Pivot;

			if (Jad.Input.Keyboard[Key.NumPad6].Down)
			{
				pivot.X += 0.01f;
				cube.Transform.Pivot = pivot;
			}

			if (Jad.Input.Keyboard[Key.NumPad4].Down)
			{
				pivot.X -= 0.01f;
				cube.Transform.Pivot = pivot;
			}

			if (Jad.Input.Keyboard[Key.NumPad9].Down)
			{
				cube.Transform.RotateRadiansStepZ(Geometry.DegreeToRadian(0.1f));
			}

			if (Jad.Input.Keyboard[Key.NumPad7].Down)
			{
				cube.Transform.RotateRadiansStepZ(-Geometry.DegreeToRadian(0.1f));
			}

			// Now, we will change the transparency

			blueColor -= Jad.AtXSecond(3, 1f) * blueColorSign;

			// Let's do the color ping-pong

			if (blueColor < 0)
			{
				blueColor = 0;
				blueColorSign = -blueColorSign;
			}

			if (blueColor > 1)
			{
				blueColor = 1;
				blueColorSign = -blueColorSign;
			}

			text3D.Material[0].Layers[0].DiffuseMap.Color.Blue = blueColor;

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

				font.RenderLine("Rotate cube (7,9). Move cube (Up,Down). Move sphere(Left,Right)", 0, JColor.White);
				font.RenderLine("Pivot (4,6):" + JMath.Vector3ToString(cube.Transform.Pivot), 0, JColor.White);

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

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

		#endregion
	}
}

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

Comments

No comments yet.