gr_tut25_screenshot.jpg

Water.cs

#region Using directives

using System;
using System.IO;
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 tutorial will show you how to create a water-like surface
	/// and use the render targets.
	/// </summary>
	public class Water : JApplication
	{
		#region Fields

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

		/// <summary>
		/// TODO
		/// </summary>
		JMeshObject plane;

		/// <summary>
		/// TODO
		/// </summary>
		JSprite sp;

		/// <summary>
		/// TODO
		/// </summary>
		List<JMeshObject> sphereBodies = new List<JMeshObject>();

		/// <summary>
		/// TODO
		/// </summary>
		JCameraFirstPerson userCamera;

		/// <summary>
		/// TODO
		/// </summary>
		JCameraSelfDriven pathCamera;

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

			// Activate Newton's physics system
			Jad.Physics.Create();

			Jad.Timer.FixedMode = true;
			Jad.Timer.FixedModeFPS = 1.0f / 24.0f;

			JDirectLight light = (JDirectLight) Jad.Scene.Lights.Create("light", JLightType.Directional);

			light.Transform.Position = new Vector3(3.0f, 3.0f, 3.0f);
			light.Transform.LookAt(Jad.Scene.Root);
			light.Color = new JColor(1.0f, 0.99f, 0.83f);
			light.Multiplier = 1.7f;
			light.Attenuation.Start = 10.0f;
			light.Attenuation.End = 15.0f;
			light.HotSpot = 20.0f;
			light.FallOff = 23.0f;
			light.CastShadows = false;

			// Create the scene skybox
			Jad.Scene.SkyBox = new JSkyBox("water_skybox");

			Jad.Scene.RenderSkyBoxFirst = true;

			CreatePlane();

			JCameraFirstPerson camera = Jad.Scene.Camera as JCameraFirstPerson;
			camera.Transform.Position = new Vector3(0.0f, 1.0f, -2.0f);
			camera.Speed = 2.0f;

			pathCamera = Jad.Scene.Cameras.CreateSelfDriven("water_test.camera", false);

			Jad.Scene.Lights.Ambient = new JColor(0.3f);

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

			Jad.Scene.BloomEffect.Enabled = false;
			Jad.Scene.BloomEffect.HighPass.HighPassValue = 0.3f;
			Jad.Scene.BloomEffect.HighPass.Multiplier = 0.15f;

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

			return true;
		}

		/// <summary>
		/// Creates the scene
		/// </summary>
		void CreatePlane()
		{
			Jad.Import.Load("water_test.haddd");

			plane = Jad.Scene.MeshObjects["water_plane"];

			JParticleEffect pFX = Jad.Scene.Particles.Create("smoke_prt", "candle_fire.pfx");
			pFX.Transform.Angles = new Vector3((float) (-Math.PI / 2), 0.0f, 0.0f);
			pFX.Transform.Position = new Vector3(-1.123f, 1.022f, -0.182f);

			pFX.ParticleSystems[0].MeshObject.Priority = 1;

			JMaterial[] material = new JMaterial[1];

			material[0] = Jad.Scene.Materials.Create();

			material[0].Load("water.material");

			plane.Material = material;

			// In the shader, use the specularmap as the map that recieves the reflection

			JMaterialLayer layer = material[0].Layers[0];

			layer.SpecularMap.Texture = (JTexture) Jad.Video.Views[0].RenderTargets.Reflection.Texture;
			layer.SpecularMap.Enabled = true;

			// Set the specular values

			layer.SpecularMap.Power = 512;
			layer.SpecularMap.Color = new JColor(1.0f, 0.99f, 0.83f);

			// Value of the vector L
			JShaderParams.JVector4 shaderParams = layer.ShaderParams.VectorList[0];
			shaderParams.Vector = new Vector4(20.0f, 10.0f, 26.0f, 0f);
			layer.ShaderParams.VectorList[0] = shaderParams;

			plane.DoNotRenderOnReflection = true;

			sp = new JSprite();
			sp.Texture = material[0].Layers[0].SpecularMap.Texture;
		}

		/// <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.G].Pressed) Jad.Scene.ShowGizmos = !Jad.Scene.ShowGizmos;

			// Start recording?
			if (Jad.Input.Keyboard[Key.F5].Pressed)
			{
				Jad.Video.Views[0].VideoRecorder.Enabled = !Jad.Video.Views[0].VideoRecorder.Enabled;

				// Save the current camera. It'll be restored later.
				userCamera = Jad.Scene.Camera as JCameraFirstPerson;

				// Set our travelling camera as the current camera
				Jad.Scene.Camera = pathCamera;

				if (Jad.Video.Views[0].VideoRecorder.Enabled)
					Jad.Video.Views[0].VideoRecorder.Init();
			}

			// The engine makes the Scene's update after this call. So we need
			// to tell it that the reflection must be enabled to compute
			// the right matrix
			Jad.Scene.Reflection.Enabled = true;
			Jad.Scene.Reflection.Plane = Plane.FromPointNormal(plane.Transform.Position, new Vector3(0, 1, 0));

			// 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 fuente = Jad.Video.Fonts[0];

				// Start fonts rendering
				Jad.Video.Fonts.Begin();

				// Write text in the first line, position x=0, yellow color
				fuente.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
				fuente.RenderLine(Jad.Video.Views.CurrentView.Stats.Fps + " FPS", 0, JColor.White);

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

			// End the scene & Present
			backBuffer.Present();
		}

		#endregion
	}
}

Last edited Mar 8, 2007 at 7:33 PM by Vicente, version 2

Comments

No comments yet.