gr_tut29_screenshot.jpg

ParallaxOcclusionMapping.cs

#region Using directives

using System;
using System.Collections.Generic;
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 show you the Parallax Occlusion Mapping effect.
	/// </summary>
	public class ParallaxOcclusionMapping : JApplication
	{
		#region Fields

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

		/// <summary>
		/// The layer of the material that contains the maps
		/// </summary>
		JMaterialLayer layer;

		/// <summary>
		/// Light of the scene
		/// </summary>
		JBaseLight light;

		#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 plane mesh on the XY world plane
			JMesh rectangleMesh = JMesh.CreateRectangle(4.0f, 4.0f, 2, 2, JCreateRectangle.XY, false, 1, 1);

			// Create the mesh object
			JMeshObject rectangle = Jad.Scene.MeshObjects.Create("wall", false);

			// Assign the plane mesh to the mesh object mesh
			rectangle.Mesh = rectangleMesh;

			#endregion

			#region Material

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

			JMaterial[] material = new JMaterial[1];

			// Create the material
			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
			layer = new JMaterialLayer();

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

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

			// ---------------------------------------------------------------------------
			// DIFFUSE
			// ---------------------------------------------------------------------------

			// Set the diffuse texture
			layer.DiffuseMap.Texture = Jad.Video.Textures.Create2D("Pared_Difusse", true);

			// ---------------------------------------------------------------------------
			// NORMAL
			// ---------------------------------------------------------------------------

			// Create the normal map texture
			layer.NormalMap.Texture = Jad.Video.Textures.Create2D("Pared_Normal", true);

			// Activate the map. This is not necessary in the DiffuseMap, but it´s necessary
			// in the rest types of maps
			layer.NormalMap.Enabled = true;

			// Indicate a value of "bumpy quantity". Reasonable values range from 0 to 1.
			layer.NormalMap.Bumpiness = 0.5f;

			// We indicate that we want to use the Normal Mapping shader
			layer.NormalType = JNormalType.Mapping;

			// ---------------------------------------------------------------------------
			// SPECULAR
			// ---------------------------------------------------------------------------

			// Create the specular map texture
			layer.SpecularMap.Texture = Jad.Video.Textures.Create2D("Pared_Spec", true);

			// Activate the map. This is not necessary in the DiffuseMap, but it´s necessary
			// in the rest types of maps
			layer.SpecularMap.Enabled = true;

			// Specular power
			layer.SpecularMap.Power = 64;

			// Specular color
			layer.SpecularMap.Color = new JColor(0.4f);

			// ---------------------------------------------------------------------------
			// HEIGHT MAP (PARALLAX)
			// ---------------------------------------------------------------------------

			// The Height map must be on tha alpha channel of the Normal map

			layer.HeightMap.Enabled = true;

			// Parallax amount
			layer.HeightMap.ParallaxAmount = 0.08f;
			// Perspective bias
			layer.HeightMap.PerspectiveBias = 0.85f;
			// Shadow opacity
			layer.HeightMap.ShadowOpacity = 0.15f;
			// Enable occlusion parallax mapping
			layer.HeightMap.OcclusionMapping = true;

			#endregion

			#region Light

			// Create a spot light
			light = (JSpotLight) Jad.Scene.Lights.Create("light", JLightType.Spot);

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

			// Intensity
			light.Multiplier = 1.0f;

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

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

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

			// Color
			light.Color = JColor.White;

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

			// Falloff & hotspot
			((JSpotLight) light).FallOff = 28.0f;
			((JSpotLight) light).HotSpot = 25.0f;

			// Light will always look at the scene root node
			light.Transform.LookAt(Jad.Scene.Root);

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

			#endregion

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

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

			// Handle material parameters input

			if (Jad.Input.Keyboard[Key.NumPadPlus].Pressed)
				layer.NormalMap.Bumpiness += 0.1f;
			if (Jad.Input.Keyboard[Key.NumPadMinus].Pressed)
				layer.NormalMap.Bumpiness -= 0.1f;

			if (Jad.Input.Keyboard[Key.PageUp].Pressed)
				layer.HeightMap.ParallaxAmount += 0.01f;
			if (Jad.Input.Keyboard[Key.PageDown].Pressed)
				layer.HeightMap.ParallaxAmount -= 0.01f;

			if (Jad.Input.Keyboard[Key.Home].Pressed)
				layer.HeightMap.ShadowOpacity += 0.01f;
			if (Jad.Input.Keyboard[Key.End].Pressed)
				layer.HeightMap.ShadowOpacity -= 0.01f;

			if (Jad.Input.Keyboard[Key.F3].Pressed)
				layer.HeightMap.PerspectiveBias += 0.01f;
			if (Jad.Input.Keyboard[Key.F4].Pressed)
				layer.HeightMap.PerspectiveBias -= 0.01f;

			if (Jad.Input.Keyboard[Key.D1].Pressed)
				layer.NormalMap.Enabled = !layer.NormalMap.Enabled;

			if (Jad.Input.Keyboard[Key.D2].Pressed)
				layer.SpecularMap.Enabled = !layer.SpecularMap.Enabled;

			if (Jad.Input.Keyboard[Key.D3].Pressed)
				layer.HeightMap.Enabled = !layer.HeightMap.Enabled;

			if (Jad.Input.Keyboard[Key.D4].Pressed)
				layer.HeightMap.OcclusionMapping = !layer.HeightMap.OcclusionMapping;

			// Translate spot light describing a circle

			float radius = 2.0f;
			light.Transform.TranslateX(radius * (float) Math.Cos(Jad.Timer.Time));
			light.Transform.TranslateY(radius * (float) Math.Sin(Jad.Timer.Time));

			// Everything was right, continue
			return true;
		}

		/// <summary>
		/// Render method
		/// </summary>
		public override void Render()
		{
			JView backbuffer = Jad.Video.Views[0];

			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, yellow color
				font.RenderLine("Parallax / bump / normal / lit demo", 0, JColor.Yellow);
				font.RenderLine(Jad.Video.Views.CurrentView.Stats.Fps + " FPS", 0, JColor.White);
				font.RenderLine("W-A-S-D Mouse (LButton) (RButton)", 0, JColor.White);
				font.RenderLine("Bumpiness ( Numpad +/- to change ): " + layer.NormalMap.Bumpiness.ToString("N2"), 0, JColor.White);
				font.RenderLine("Parallax ( PgUp/PgDown to change ): " + layer.HeightMap.ParallaxAmount.ToString("N2"), 0, JColor.White);
				font.RenderLine("Perspective Bias ( F3/F4 to change ): " + layer.HeightMap.PerspectiveBias.ToString("N2"), 0, JColor.White);
				font.RenderLine("Shadow Opacity ( Home/End to change ): " + layer.HeightMap.ShadowOpacity.ToString("N2"), 0, JColor.White);
				font.RenderLine("Normal     (1):" + layer.NormalMap.Enabled.ToString(), 0, JColor.White);
				font.RenderLine("Specular   (2):" + layer.SpecularMap.Enabled.ToString(), 0, JColor.White);
				font.RenderLine("Height     (3):" + layer.HeightMap.Enabled.ToString(), 0, JColor.White);
				font.RenderLine("Par. Occl. (4):" + layer.HeightMap.OcclusionMapping.ToString(), 0, JColor.White);

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

			backbuffer.Present();
		}

		#endregion
	}
}

Last edited Mar 8, 2007 at 8:15 PM by Vicente, version 4

Comments

No comments yet.