gr_tut13_screenshot.jpg

JTutorial13.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 Microsoft.DirectX.DirectInput;
	
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;
	
#endregion
	
namespace Tutorial
{
	/// <summary>
	/// This example will show you the Depth of Field effect and
	/// all the options associated with it.
	/// </summary>
	public class JTutorial : JApplication
	{
		#region Fields
	
		/// <summary>
		/// Indicates if the help must be shown in the screen or not
		/// </summary>
		bool showHelp = 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 Creation
	
			CreateRoom();
	
			CreateSpheres();
	
			Jad.Scene.DepthOfField.Enabled = true;
			Jad.Scene.DepthOfField.Debug = true;
	
			#endregion
	
			#region Light
	
			// Create an omni light
			JOmniLight light = (JOmniLight) Jad.Scene.Lights.Create("light", JLightType.Omni);
	
			// Set its position
			light.Position = new Vector3(0, 0, -2f);
	
			// Intensity
			light.Multiplier = 1f;
	
			// Attenuation
			light.AttenuationType = JLightAttenuationType.DualRadius;
	
			// Distance to start attenuating the light
			light.FarAttenuationStart = 1f;
	
			// Distance where the light is completely attenuated
			light.FarAttenuationEnd = 2f;
	
			// 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
	
			// 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 room of the scene
		/// </summary>
		void CreateRoom()
		{
			JMesh cubeMesh = JMesh.CreateCube(3, 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
			JMaterialLayer 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 JMeshObject
			cube.Material = material;
		}
	
		/// <summary>
		/// Creates the spheres
		/// </summary>
		void CreateSpheres()
		{
			JMesh sphereMesh = JMesh.CreateSphere(0.25f, 20, 20);
	
			JMeshObject sphere = Jad.Scene.MeshObjects.Create("sphere", false);
	
			sphere.Mesh = sphereMesh;
	
			sphere.Position = new Vector3(0, 0, 1.49f);
	
			// Material
	
			JMaterial[] material = new JMaterial[1];
	
			material[0] = Jad.Scene.Materials.Create();
	
			// Create the layer
			JMaterialLayer layer = new JMaterialLayer();
	
			// Add it to the material
			material[0].AddLayer(layer);
	
			layer.DiffuseMap.Texture = Jad.Video.Textures.Create2D("projector", true);
	
			// Set the material we have just created to our JMeshObject
			sphere.Material = material;
	
			// Now we will clone the spheres
	
			float x, y, z;
	
			x = -1.35f;
	
			y = -1.35f;
	
			z = 1.35f;
	
			for (int n = 0; n < 4; n++)
			{
				JMeshObject newSphere = sphere.Clone("Sphere" + n.ToString());
	
				x += 0.6f;
	
				y += 0.5f;
	
				z -= 0.5f;
	
				newSphere.Position = new Vector3(x, y, z);
			}
		}
	
		/// <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 Action()
		{
			// Exit the app?
			if (Jad.Input.Keyboard.KeyPressed(Key.Escape)) return false;
	
			if (Jad.Input.Keyboard.KeyTouch(Key.F1)) showHelp = !showHelp;
	
			if (Jad.Input.Keyboard.KeyTouch(Key.G))
				Jad.Scene.ShowGizmos = !Jad.Scene.ShowGizmos;
	
			if (Jad.Input.Keyboard.KeyTouch(Key.F))
				Jad.Scene.DepthOfField.Enabled = !Jad.Scene.DepthOfField.Enabled;
	
			float inc = Jad.At1Second(1.0f);
	
			if (Jad.Input.Keyboard.KeyPressed(Key.NumPad9))
				Jad.Scene.DepthOfField.NearBlurDepth += inc;
	
			if (Jad.Input.Keyboard.KeyPressed(Key.NumPad3))
				Jad.Scene.DepthOfField.NearBlurDepth -= inc;
	
			if (Jad.Input.Keyboard.KeyPressed(Key.NumPad8))
				Jad.Scene.DepthOfField.NearFocalPlaneDepth += inc;
	
			if (Jad.Input.Keyboard.KeyPressed(Key.NumPad2))
				Jad.Scene.DepthOfField.NearFocalPlaneDepth -= inc;
	
			if (Jad.Input.Keyboard.KeyPressed(Key.NumPad7))
				Jad.Scene.DepthOfField.FarFocalPlaneDepth += inc;
	
			if (Jad.Input.Keyboard.KeyPressed(Key.NumPad1))
				Jad.Scene.DepthOfField.FarFocalPlaneDepth -= inc;
	
			if (Jad.Input.Keyboard.KeyPressed(Key.NumPadPlus))
				Jad.Scene.DepthOfField.FarBlurDepth += inc;
	
			if (Jad.Input.Keyboard.KeyPressed(Key.NumPadMinus))
				Jad.Scene.DepthOfField.FarBlurDepth -= inc;
	
			// Everything was right, continue
			return true;
		}
	
		/// <summary>
		/// Render method
		/// </summary>
		public override void Render()
		{
			// Prepare the scene to start the render
			Jad.Scene.Begin();
	
			// Render all the scene elements
			Jad.Scene.Render();
	
			// Do the post-production
			Jad.Scene.ProcessPostProduction();
	
			// Render various things of the scene (gizmos, axes, names,...)
			Jad.Scene.RenderMiscellaneous();
	
			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.Render.Stats.Fps + " FPS", 0, JColor.White);
	
				font.RenderLine("Near blur(9,3):" + Jad.Scene.DepthOfField.NearBlurDepth.ToString("N2"), 0, JColor.White);
				font.RenderLine("Near focal(8,2):" + Jad.Scene.DepthOfField.NearFocalPlaneDepth.ToString("N2"), 0, JColor.White);
				font.RenderLine("Far focal(7,1):" + Jad.Scene.DepthOfField.FarFocalPlaneDepth.ToString("N2"), 0, JColor.White);
				font.RenderLine("Far blur(+,-):" + Jad.Scene.DepthOfField.FarBlurDepth.ToString("N2"), 0, JColor.White);
	
				// End fonts rendering
				Jad.Video.Fonts.End();
			}
	
			// End the scene
			Jad.Scene.End();
		}
	
		#endregion
	}
}

JMain.cs

#region Using directives
	
using System;
using System.IO;
	
#endregion
	
namespace Tutorial
{
	/// <summary>
	/// Class that holds the main method. It´s the application entry point
	/// </summary>
	public class JMain
	{
		#region Methods
	
		/// <summary>
		/// Application entry point
		/// </summary>
		[STAThreadAttribute]
		static void Main()
		{
			// Set the engine base directory (where the configuration.xml file is located)
			JadEngine.Core.JPath.ChangeDirectory();
	
			// Create a new file where we´ll write console output
			StreamWriter sw = new StreamWriter("out.txt");
	
			// Redirect console output to this file
			Console.SetOut(sw);
	
			// Create the application object
			JTutorial main = new JTutorial();
	
			// Start the application
			main.Start();
	
			// Close console output file
			sw.Close();
		}
	
		#endregion
	}
}

Last edited Jul 24, 2006 at 4:47 PM by Vicente, version 2

Comments

No comments yet.