gr_tut31_screenshot.jpg

StandardAttenuation.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 use standard attenuation.
	/// </summary>
	public class StandardAttenuation : JApplication
	{
		#region Fields

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

		/// <summary>
		/// TODO
		/// </summary>
		JMeshObject sphere;

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

			// Create the scene
			CreateScene();

			// Set the ambient light
			Jad.Scene.Lights.Ambient = new JColor(0.2f, 0.2f, 0.2f);

			// Set the camera
			Jad.Scene.Camera.Transform.Position = new Vector3(0, 0f, -2f);

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

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

			return true;
		}

		/// <summary>
		/// Creates a spot light
		/// </summary>
		/// <param name="position">Position of the light</param>
		/// <param name="lightColor">Color of the light</param>
		void CreateLight(string name, Vector3 position, JColor lightColor)
		{
			light = (JOmniLight) Jad.Scene.Lights.Create(name, JLightType.Omni);

			light.Transform.Position = position;

			light.Multiplier = 1f;

			light.Attenuation.Type = JLightAttenuationType.Standard;

			light.Attenuation.Constant = 0;
			light.Attenuation.Linear = 0.2f;
			light.Attenuation.Quadratic = 0.4f;

			light.Attenuation.End = 3f;

			light.Color = lightColor;

			light.AffectDiffuse = true;

			light.AffectSpecular = true;

		}

		/// <summary>
		/// Creates the room of the scene
		/// </summary>
		void CreateRoom()
		{
			JMesh cubeMesh = JMesh.CreateCube(2f, 2f, 2f);

			// Invert the cube faces, as we want to be inside the room
			cubeMesh.IndexBuffer.InvertFaces();

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

			cube.Mesh = cubeMesh;

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

			layer.DiffuseMap.UTile = layer.DiffuseMap.VTile = 4f;

			cube.Material = material;
		}

		/// <summary>
		/// Creates a sphere
		/// </summary>
		void CreateSphere()
		{
			float radius = 0.25f;

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

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

			sphere.Mesh = sphereMesh;

			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("mancha", true);

			sphere.Material = material;

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

		}

		/// <summary>
		/// Creates the scene
		/// </summary>
		void CreateScene()
		{
			// Create the room
			CreateRoom();

			// Create the spehre
			CreateSphere();

			CreateLight("White", new Vector3(0f, 0.4f, 0.4f), JColor.White);

		}

		/// <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.NumPad1].Pressed) light.Attenuation.Constant += 0.1f; ;
			if (Jad.Input.Keyboard[Key.NumPad3].Pressed) light.Attenuation.Constant -= 0.1f; ;

			if (Jad.Input.Keyboard[Key.NumPad4].Pressed) light.Attenuation.Linear += 0.1f; ;
			if (Jad.Input.Keyboard[Key.NumPad6].Pressed) light.Attenuation.Linear -= 0.1f; ;

			if (Jad.Input.Keyboard[Key.NumPad7].Pressed) light.Attenuation.Quadratic += 0.1f; ;
			if (Jad.Input.Keyboard[Key.NumPad9].Pressed) light.Attenuation.Quadratic -= 0.1f; ;

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

			// 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, white color
				font.RenderLine(Jad.Video.Views.CurrentView.Stats.Fps + " FPS", 0, JColor.White);

				font.RenderLine("Constant (1,3):" + light.Attenuation.Constant.ToString("N1"), 0, JColor.White);
				font.RenderLine("Linear (4,6):" + light.Attenuation.Linear.ToString("N1"), 0, JColor.White);
				font.RenderLine("Quadratic (7,9):" + light.Attenuation.Quadratic.ToString("N1"), 0, JColor.White);

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

			backbuffer.Present();
		}

		#endregion
	}
}

Last edited Mar 8, 2007 at 10:23 PM by Vicente, version 5

Comments

No comments yet.