gr_tut28_screenshot.jpg

AdvancedParticleEffects.cs

#region Using directives

using System;
using System.IO;
using System.Windows.Forms;

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 some advanced particle effects.
	/// </summary>
	public class AdvancedParticleEffects : JApplication
	{
		#region Fields

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

		/// <summary>
		/// The particle effect
		/// </summary>
		JParticleEffect pFX;

		/// <summary>
		/// TODO
		/// </summary>
		JMaterialLayer layer;

		/// <summary>
		/// The hebe model
		/// </summary>
		JMeshObject hebeModel;

		/// <summary>
		/// TODO
		/// </summary>
		int state = -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();

			Jad.Timer.FixedMode = true;
			Jad.Timer.FixedModeFPS = 1.0f / 30.0f;

			CreateObjects();

			pFX = Jad.Scene.Particles.Create("smoke_prt", "smoke_prt.pfx");
			pFX.Transform.Angles = new Vector3((float) (-Math.PI / 2), 0.0f, 0.0f);
			pFX.Transform.Position = new Vector3(0.0f, 0.05f, 0.0f);
			pFX.ParticleSystems[0].MeshObject.Material[0].Priority = JMaterialPriority.Transparency;

			#region Omni 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, -0f);

			// Intensity
			light.Multiplier = 1f;

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

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

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

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

			// Attach the light to the camera
			//light.AddChild(Jad.Scene.Camera);

			#endregion

			#region Spot Light

			// Create a spot light that will be associated with the lamp,
			// so when the lamp moves, the light will move with it

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

			spotLight.Hide = true;

			// Set its position, in this example as the light is associated with the lamp,
			// the vector is a relative position to the lamp
			spotLight.Transform.Position = new Vector3(0, 1f, -0.4f);

			// By default, it is created looking at the Z axis. We want that light
			// goes down, so we rotate it in the X axis
			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 = false;

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

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

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

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

			spotLight.Projector.Enabled = true;

			// Attach the light to the camera
			//spotLight.Attach(Jad.Scene.Camera);

			#endregion

			Jad.Scene.Lights.Ambient = new JColor(0.0f);

			JCameraFirstPerson camera = Jad.Scene.Camera as JCameraFirstPerson;
			camera.Transform.Position = new Vector3(0.0f, 0.0f, -2.0f);
			camera.Speed = 2.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 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 HMeshObject
			cube.Material = material;
		}

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

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

			// Get the first loaded object (the only one)

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

			model.Transform.Position = new Vector3(-0.25f, -0.37f, 0.2f);

			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.V].Pressed)
				pFX.ParticleSystems[0].MeshObject.Material[0].SphericalMapping = !pFX.ParticleSystems[0].MeshObject.Material[0].SphericalMapping;

			if (Jad.Input.Keyboard[Key.P].Pressed)
				pFX.Pause();

			if (Jad.Input.Keyboard[Key.O].Pressed)
				pFX.Play();

			if (Jad.Input.Keyboard[Key.M].Pressed)
			{
				if (pFX.ParticleSystems[0].MeshObject.Material[0].Priority == JMaterialPriority.Transparency)
					pFX.ParticleSystems[0].MeshObject.Material[0].Priority = JMaterialPriority.PostProcess;

				else
					pFX.ParticleSystems[0].MeshObject.Material[0].Priority = JMaterialPriority.Transparency;
			}

			float inc = Jad.At1Second(1f);
			Vector3 pos = hebeModel.Transform.Position;

			if (Jad.Input.Keyboard[Key.Left].Down) pos.X -= inc;
			if (Jad.Input.Keyboard[Key.Right].Down) pos.X += inc;
			if (Jad.Input.Keyboard[Key.Up].Down) pos.Y += inc;
			if (Jad.Input.Keyboard[Key.Down].Down) pos.Y -= inc;
			if (Jad.Input.Keyboard[Key.Home].Down) pos.Z += inc;
			if (Jad.Input.Keyboard[Key.End].Down) pos.Z -= inc;

			hebeModel.Transform.Position = pos;

			// Start recording?
			if (Jad.Input.Keyboard[Key.F5].Pressed)
			{
				Jad.Video.Views[0].VideoRecorder.Enabled = !Jad.Video.Views[0].VideoRecorder.Enabled;

				if (Jad.Video.Views[0].VideoRecorder.Enabled)
					Jad.Video.Views[0].VideoRecorder.Init();
			}

			if (Jad.Video.Views[0].VideoRecorder.Enabled)
			{
				if (state == -1)
				{
					state = 0;
					pFX.ParticleSystems[0].MeshObject.Material[0].Priority = JMaterialPriority.PostProcess;
				}
				if (state == 0 && Jad.Timer.Time > 5)
				{
					state = 1;

					pFX.ParticleSystems[0].MeshObject.Material[0].Priority = JMaterialPriority.Transparency;
				}
				if (state == 1 && Jad.Timer.Time > 10)
				{
					state = 2;

					pFX.ParticleSystems[0].MeshObject.Material[0].Priority = JMaterialPriority.PostProcess;
					pFX.ParticleSystems[0].MeshObject.Material[0].SphericalMapping = true;
				}
				if (state == 2 && Jad.Timer.Time > 15)
				{
					return false;
				}
			}
			// Everything was right, continue
			return true;
		}

		/// <summary>
		/// Render method
		/// </summary>
		public override void Render()
		{
			string text;

			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", 0, JColor.Yellow);

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

				font.RenderLine("Mode (M):" + pFX.ParticleSystems[0].MeshObject.Material[0].Priority.ToString(), 0, JColor.White);

				font.RenderLine("Volumetric (V):" + pFX.ParticleSystems[0].MeshObject.Material[0].SphericalMapping.ToString(), 0, JColor.White);

				if (pFX.ParticleSystems[0].MeshObject.Material[0].Priority == JMaterialPriority.Transparency)
					text = "Particles are now affected by the light";

				else
					text = "Standard Particles";

				if (pFX.ParticleSystems[0].MeshObject.Material[0].SphericalMapping)
					text = "Volumetric particles ENABLED";

				font.RenderCentered(text, Jad.Video.Height - 25, JColor.White);

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

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

		#endregion
	}
}

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

Comments

No comments yet.