gr_tut19_screenshot.jpg

JTutorial19.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;
using JadEngine.Import;
		
#endregion
		
namespace Tutorial
{
	/// <summary>
	/// This tutorial will show you how to create a skybox. Also, you'll load a 
	/// mesh object and you'll apply it a material with reflection and refraction.
	/// Finally, you'll enable the Bloom effect to pop up the brighter areas in
	/// the scene.
	/// </summary>
	public class JTutorial : JApplication
	{
		#region Fields
		
		/// <summary>
		/// Indicates if the help must be shown in the screen or not
		/// </summary>
		bool showHelp = true;
		
		/// <summary>
		/// TODO
		/// </summary>
		JMeshObject model;
		
		/// <summary>
		/// TODO
		/// </summary>
		JMaterial material;
		
		/// <summary>
		/// TODO
		/// </summary>
		JCameraFirstPerson userCamera;
		
		#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 skybox
			Jad.Scene.SkyBox = new JSkyBox("grace_cube");
		
			// Create the glass material we'll use for our model
			// This material is loaded from the material script "glass.material"
			// you can find in the "base/materials/tutorials" folder
			material = Jad.Scene.Materials.Load("glass");
		
			#region MeshObject creation
		
			// Load our mesh object
			Jad.Import.Load("TorusKnot");
			// Get a reference of our mesh object
			model = Jad.Import.MeshObjects[0];
			// Assign the glass material to the mesh object
			model.Material[0] = material;
		
			#endregion
		
			#region Camera parameters
		
			// Set some camera parameters
			userCamera = (JCameraFirstPerson)Jad.Scene.Camera;
			userCamera.Position = new Vector3(10.0f, 0.0f, 0.0f);
			userCamera.Angles = new Vector3(0.0f, -(float)(Math.PI / 2), 0.0f);
			userCamera.Speed = 5.0f;
		
			#endregion
		
			// Everything is set, start the engine
			Jad.Begin();
		
			#region Bloom effect
		
			// Important to set this values AFTER Hadd.Begin has been called
			Jad.Scene.BloomEffect.Enabled = true;
			Jad.Scene.BloomEffect.HighPass.HighPassValue = 0.56f;
			Jad.Scene.BloomEffect.HighPass.Multiplier = 0.7f;
		
			#endregion
		
			// 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 Action()
		{
			// Exit the app?
			if (Jad.Input.Keyboard.KeyPressed(Key.Escape)) return false;
		
			if (Jad.Input.Keyboard.KeyTouch(Key.F1)) 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.KeyTouch(Key.G))
				Jad.Scene.ShowGizmos = !Jad.Scene.ShowGizmos;
		
			// Change some material parameters?
		
			// Reflectivity
			if (Jad.Input.Keyboard.KeyTouch(Key.NumPad7))
				material.Layers[0].ReflectionMap.Reflectivity += 0.1f;
			else if (Jad.Input.Keyboard.KeyTouch(Key.NumPad4))
				material.Layers[0].ReflectionMap.Reflectivity -= 0.1f;
		
			// IOR
			if (Jad.Input.Keyboard.KeyTouch(Key.NumPad8))
				material.Layers[0].RefractionMap.IndexOfRefraction += 0.1f;
			else if (Jad.Input.Keyboard.KeyTouch(Key.NumPad5))
				material.Layers[0].RefractionMap.IndexOfRefraction -= 0.1f;
		
			// Transmittance
			if (Jad.Input.Keyboard.KeyTouch(Key.NumPad9))
				material.Layers[0].RefractionMap.Transmittance += 0.1f;
			else if (Jad.Input.Keyboard.KeyTouch(Key.NumPad6))
				material.Layers[0].RefractionMap.Transmittance -= 0.1f;
		
			// Rotate the mesh object
			model.Rotate(Jad.Timer.Time * 0.5f, Jad.Timer.Time * 2.0f, Jad.Timer.Time);
		
			// 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();
		
				// Help text
				font.RenderLine(Jad.Version + ". Press ESC to exit. Press G to Show/Hide Gizmos. F1 show/hide text", 0, JColor.Yellow);
		
				// Show some material parameters
				float reflectivity = material.Layers[0].ReflectionMap.Reflectivity;
				float ior = material.Layers[0].RefractionMap.IndexOfRefraction;
				float transmittance = material.Layers[0].RefractionMap.Transmittance;
				font.RenderLine(string.Format("Reflectivity (7,4): {0} / IOR (8,5): {1} / Transmittance (9,6): {2}", reflectivity, ior, transmittance), 0, JColor.Yellow);
		
				// Show frame rate
				font.RenderLine(Jad.Video.Render.Stats.Fps + " FPS", 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
	}
}

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 3:49 PM by Vicente, version 2

Comments

No comments yet.