gr_tut4_screenshot.jpg

MultilayerMaterials.cs

#region Using directives

using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

using JadEngine;
using JadEngine.Core;
using JadEngine.Scene;
using JadEngine.Video;
using JadEngine.Input;

#endregion

namespace Tutorial
{
	/// <summary>
	/// This example will show you how to create objects and create a multilayer material
	/// to represent the detail map effect. This example will show you how the material
	/// would look like applying a variable quantity of detail map (between -1 and 1).
	/// The tutorial uses the default first-person camera of the engine. Use keys WASD
	/// and the mouse to control it.
	/// </summary>
	public class MultilayerMaterials : JApplication
	{
		#region Fields

        /// <summary>
        /// To make the detail map effect do ping pong
        /// </summary>
        bool automatic=true;

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

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

		/// <summary>
		/// Sign of the detail effect
		/// </summary>
		float amountSign = 1f;

		#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 Objets

			// We create a JMesh: the object that has all the information about the vertices
			// and indices. In this example, a rectangle of 1.5x1.5 with 2 vertices (without subdivisions)
			// in the XY plane and the texture coordinates 1x1

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

			JMesh rectangleMesh = JMesh.CreateRectangle(1.5f, 1.5f, 2, 2, JCreateRectangle.XY, false, 1f, 1f);

			// 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 new mesh object, as we have just created it.

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

			// Now, we associate the mesh with the meshobject

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

			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 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("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.3f;

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

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

			// It´s not necessary to set a texture, we can use
			// layer.SpecularMap.Color = new Color(1, 1, 0);

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

			// -------------------------------------------------------------
			// Now we create another layer in the same material
			// -------------------------------------------------------------

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

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

			// The detail map is a system that allows to make multiple combinations
			// of two layers of maps specifiying only the quantity of mixing of 
			// both textures in an alpha channel

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

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

			// Create the normal map texture
			layer.NormalMap.Texture = Jad.Video.Textures.Create2D("mancha_n", 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.3f;

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

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

			// It´s not necessary to set a texture, we can use
			// layer.SpecularMap.Color = new Color(1, 1, 0);

			// Create the specular map texture
			layer.SpecularMap.Texture = Jad.Video.Textures.Create2D("manchaSpec", 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 = 32;

			// ---------------------------------------------------------------------------
			// ACTIVATE THE DETAIL MAP IN THE 2nd LAYER
			// ---------------------------------------------------------------------------
			layer.DetailMap.Enabled = true;

			// We set the textura that will be the detail map
			layer.DetailMap.MaskTexture = Jad.Video.Textures.Create2D("detailAlpha", true);

			// Start indicating that the first layer is completely shown and the second layer
			// can´t be seen at all
			layer.DetailMap.Amount = -1f;


			// As it´s a second layer, we must set the blending type we want to use

			layer.RenderStates.AlphaBlendEnable = true;
			layer.RenderStates.SourceBlend = Blend.SourceAlpha;
			layer.RenderStates.DestinationBlend = Blend.InvSourceAlpha;

			// Set the material we have just created to our JMeshObject
			rectangle.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, -0.5f);

			// Intensity
			light.Multiplier = 0.5f;

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

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

			#endregion

			// Move the camera a little behind of the plane
			Jad.Scene.Camera.Transform.Position = new Vector3(0, 0, -1.5f);

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

			if (Jad.Input.Keyboard[Key.G].Pressed)
				Jad.Scene.ShowGizmos = !Jad.Scene.ShowGizmos;

			// If keys + - (from the numeric keyboard) were pressed, we modify the general bumpiness value

			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.P].Pressed)
                automatic = true;

			if (Jad.Input.Keyboard[Key.NumPad4].Pressed)
            {
                layer.DetailMap.Amount -= 0.1f;
                automatic = false;
            }

			if (Jad.Input.Keyboard[Key.NumPad6].Pressed)
            {
                layer.DetailMap.Amount += 0.1f;
                automatic = false;
            }

            if (automatic)
            {
                // Modify the values so they bounce between -1 and 1
                layer.DetailMap.Amount += Jad.AtXSecond(3, 1f) * amountSign;
            }
            

			if (layer.DetailMap.Amount >= 1f)
			{
				layer.DetailMap.Amount = 1f;

				amountSign = -1f;
			}

			else
				if (layer.DetailMap.Amount <= -1f)
				{
					layer.DetailMap.Amount = -1f;

					amountSign = 1f;
				}

			// 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("Bumpiness ( Numpad + - to change ): " + layer.NormalMap.Bumpiness.ToString("N2"), 0, JColor.White);

				font.RenderLine("Detail amount(4,6,P=auto):" + layer.DetailMap.Amount.ToString("N2"), 0, JColor.White);

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

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

		#endregion
	}
}

Last edited Mar 8, 2007 at 12:05 PM by Vicente, version 7

Comments

No comments yet.