gr_tut23_screenshot.jpg

MotionBlur.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.Mathematics.Geometry;
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 motion blur effect.
	/// </summary>
	public class MotionBlur : 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>
		/// Indicates if the cube must rotate or not
		/// </summary>
		bool rotateCube = true;

		#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 a JMesh: the object that has all the information about the vertices
			// and indices.
			// In this example, we create a cube, 3 meters width, height and depth.

			// It´s important to remember that in Jad, the distance measure unit is the METER

			JMesh cubeMesh = JMesh.CreateCube(3, 3, 3);

			// A JMeshObject is an object with the information about the meshes and the
			// materials. It´s the element we´ll use to render the meshes.

			// We create a new JMeshObject in the scene. We set that we don´t want to
			// create a mesh object, as we have just done it.

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

			// Now, we associate the mesh with the meshobject

			cube.Mesh = cubeMesh;

			#endregion

			#region Material

			// Create a material's array of 1 element.
			// An array is needed because meshes can have subsets:
			// information for more than 1 material.

			JMaterial[] material = new JMaterial[1];

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

			// A material is composed by layers, so we can do multilayer effects.
			// In this example we´ll use only one layer.

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

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

			// Set the diffuse texture. The engine detects if the texture doesn´t exists
			// and load it. If it exists, it only references it.
			// You must not indicate the file extension (.tga, .jpg, .bmp,...). The engine
			// we´ll search the different extensions until it finds the texture.

			// To search for textures, the engine will enter in the base/textures
			// directory, and we´ll move through it recursively until it finds the
			// texture. Thanks to this, you can create subfolders to order the textures
			// without needing to change the engine.

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

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

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

			// Intensity
			light.Multiplier = 1f;

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

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

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

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

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

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

			// Everything is set, start the engine
			Jad.Begin();

			// Number of samples for motion blur
			Jad.Scene.MotionBlur.Filter.NumSamples = 12;

			// Motion blur strength
			Jad.Scene.MotionBlur.Filter.PixelBlurConst = 2.7f;

			// Create input class to manage the keyboard and the mouse
			Jad.CreateInput();

			return true;
		}

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

			if (Jad.Input.Keyboard[Key.M].Pressed)
				Jad.Scene.MotionBlur.Enabled = !Jad.Scene.MotionBlur.Enabled;

			if (Jad.Input.Keyboard[Key.NumPad4].Pressed && Jad.Scene.MotionBlur.Filter.PixelBlurConst > 1)
				Jad.Scene.MotionBlur.Filter.PixelBlurConst -= 0.1f;

			else if (Jad.Input.Keyboard[Key.NumPad6].Pressed)
				Jad.Scene.MotionBlur.Filter.PixelBlurConst += 0.1f;

			if (Jad.Input.Keyboard[Key.NumPad1].Pressed && Jad.Scene.MotionBlur.Filter.NumSamples > 4)
				Jad.Scene.MotionBlur.Filter.NumSamples--;

			else if (Jad.Input.Keyboard[Key.NumPad3].Pressed && Jad.Scene.MotionBlur.Filter.NumSamples < 12)
				Jad.Scene.MotionBlur.Filter.NumSamples++;

			if (rotateCube)
			{
				float speed = 3;
				if (Jad.Scene.MotionBlur.Enabled) speed = 3;
				speed = 1.5f;
				cube.Transform.RotateRadians(Jad.Timer.Time * speed, Jad.Timer.Time * speed, Jad.Timer.Time * speed);
			}

			// 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("f", ImageFileFormat.Png, 1.0f / 60.0f, true);
					Jad.Video.Views[0].VideoRecorder.Init();
				}
			}

			if (Jad.Video.Views[0].VideoRecorder.Enabled)
			{
				if (Jad.Timer.Time >= 8) Jad.Scene.MotionBlur.Enabled = true;
				if (Jad.Timer.Time >= 20) return false;
			}

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

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

			if (showHelp)
			{
				// 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("Rotate cube: (R): " + rotateCube.ToString(), 0, JColor.White);
				font.RenderLine("Motion (M): " + Jad.Scene.MotionBlur.Enabled.ToString(), 0, JColor.White);
				font.RenderLine("Blur (4,6): " + Jad.Scene.MotionBlur.Filter.PixelBlurConst.ToString("N2"), 0, JColor.White);
				font.RenderLine("Num Samples (1,3): " + Jad.Scene.MotionBlur.Filter.NumSamples.ToString(), 0, JColor.White);

			}

			else if (Jad.Scene.MotionBlur.Enabled)
				font.RenderCentered("Motion Blur ENABLED", Jad.Video.Height - 25, JColor.White);

			Jad.Video.Fonts.End();

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

		#endregion
	}
}

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

Comments

No comments yet.