UltimateZoneBuilder/Source/VisualModes/VisualMode.cs

473 lines
12 KiB
C#
Raw Normal View History

2008-01-04 00:16:58 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using CodeImp.DoomBuilder.Windows;
2008-01-04 00:16:58 +00:00
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Actions;
2008-01-04 00:16:58 +00:00
using CodeImp.DoomBuilder.Geometry;
using System.Drawing;
using SlimDX;
2008-11-19 16:18:36 +00:00
using CodeImp.DoomBuilder.Editing;
2008-01-04 00:16:58 +00:00
#endregion
2008-11-19 16:18:36 +00:00
namespace CodeImp.DoomBuilder.VisualModes
2008-01-04 00:16:58 +00:00
{
/// <summary>
/// Provides specialized functionality for a visual (3D) Doom Builder editing mode.
/// </summary>
2008-01-04 00:16:58 +00:00
public abstract class VisualMode : EditMode
{
#region ================== Constants
private const float ANGLE_FROM_MOUSE = 0.0001f;
public const float MAX_ANGLEZ_LOW = 100f / Angle2D.PIDEG;
public const float MAX_ANGLEZ_HIGH = (360f - 100f) / Angle2D.PIDEG;
2008-01-13 21:23:59 +00:00
private const float CAMERA_SPEED = 6f;
2008-01-04 00:16:58 +00:00
#endregion
#region ================== Variables
// 3D Mode thing
protected Thing modething;
// Graphics
protected IRenderer3D renderer;
private Renderer3D renderer3d;
// Camera
private Vector3D campos;
private Vector3D camtarget;
private float camanglexy, camanglez;
private Sector camsector;
2008-01-13 21:23:59 +00:00
// Input
private bool keyforward;
private bool keybackward;
private bool keyleft;
private bool keyright;
2008-11-20 21:32:23 +00:00
// Map
protected VisualBlockMap blockmap;
2008-11-25 21:54:50 +00:00
protected Dictionary<Sector, VisualSector> allsectors;
protected List<VisualBlockEntry> visibleblocks;
2008-11-25 21:54:50 +00:00
protected Dictionary<Sector, VisualSector> visiblesectors;
protected List<VisualGeometry> visiblegeometry;
2008-11-20 21:32:23 +00:00
2008-01-04 00:16:58 +00:00
#endregion
#region ================== Properties
public Vector3D CameraPosition { get { return campos; } set { campos = value; } }
public Vector3D CameraTarget { get { return camtarget; } }
public Sector CameraSector { get { return camsector; } }
2008-01-04 00:16:58 +00:00
#endregion
#region ================== Constructor / Disposer
/// <summary>
/// Provides specialized functionality for a visual (3D) Doom Builder editing mode.
/// </summary>
2008-01-04 00:16:58 +00:00
public VisualMode()
{
// Initialize
this.renderer = General.Map.Renderer3D;
this.renderer3d = (Renderer3D)General.Map.Renderer3D;
2008-01-13 21:23:59 +00:00
this.campos = new Vector3D(0.0f, 0.0f, 96.0f);
this.camanglez = Angle2D.PI;
2008-11-20 21:32:23 +00:00
this.blockmap = new VisualBlockMap();
2008-11-25 21:54:50 +00:00
this.allsectors = new Dictionary<Sector, VisualSector>(General.Map.Map.Sectors.Count);
this.visibleblocks = new List<VisualBlockEntry>();
2008-11-25 21:54:50 +00:00
this.visiblesectors = new Dictionary<Sector, VisualSector>(50);
this.visiblegeometry = new List<VisualGeometry>(200);
2008-01-04 00:16:58 +00:00
}
// Disposer
public override void Dispose()
2008-01-04 00:16:58 +00:00
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
2008-11-25 21:54:50 +00:00
foreach(KeyValuePair<Sector, VisualSector> s in allsectors) s.Value.Dispose();
2008-11-20 21:32:23 +00:00
blockmap.Dispose();
2008-11-25 21:54:50 +00:00
visiblesectors = null;
visiblegeometry = null;
visibleblocks = null;
2008-11-25 21:54:50 +00:00
allsectors = null;
blockmap = null;
2008-11-20 21:32:23 +00:00
2008-01-04 00:16:58 +00:00
// Done
base.Dispose();
2008-01-04 00:16:58 +00:00
}
}
#endregion
2008-01-13 21:23:59 +00:00
#region ================== Start / Stop
2008-01-04 00:16:58 +00:00
// Mode is engaged
public override void OnEngage()
{
base.OnEngage();
2008-11-25 21:54:50 +00:00
// Update the used textures
General.Map.Data.UpdateUsedTextures();
2008-11-20 21:32:23 +00:00
// Fill the blockmap
FillBlockMap();
// Find a 3D Mode thing
foreach(Thing t in General.Map.Map.Things)
if(t.Type == General.Map.Config.Start3DModeThingType) modething = t;
// Found one?
if(modething != null)
{
// Position camera here
modething.DetermineSector();
2008-01-13 21:23:59 +00:00
campos = modething.Position + new Vector3D(0.0f, 0.0f, 96.0f);
camanglexy = modething.Angle + Angle2D.PI;
camanglez = Angle2D.PI;
}
// Start special input mode
General.Interface.SetProcessorState(true);
General.Interface.StartExclusiveMouseInput();
}
// Mode is disengaged
public override void OnDisengage()
{
base.OnDisengage();
// Do we have a 3D Mode thing?
if(modething != null)
{
// Position the thing to match camera
modething.Move((int)campos.x, (int)campos.y, 0);
2008-01-13 21:23:59 +00:00
modething.Rotate(camanglexy - Angle2D.PI);
}
// Stop special input mode
General.Interface.SetProcessorState(false);
General.Interface.StopExclusiveMouseInput();
}
2008-01-13 21:23:59 +00:00
#endregion
2008-11-20 21:32:23 +00:00
#region ================== Events
public override void OnUndoEnd()
{
base.OnUndoEnd();
// Make new blockmap
if(blockmap != null)
{
blockmap.Dispose();
blockmap = new VisualBlockMap();
FillBlockMap();
}
}
#endregion
2008-01-13 21:23:59 +00:00
#region ================== Input
// Mouse input
public override void OnMouseInput(Vector2D delta)
{
base.OnMouseInput(delta);
// Change camera angles with the mouse changes
2008-01-13 21:23:59 +00:00
camanglexy -= delta.x * ANGLE_FROM_MOUSE;
camanglez += delta.y * ANGLE_FROM_MOUSE;
2008-01-13 21:23:59 +00:00
// Normalize angles
camanglexy = Angle2D.Normalized(camanglexy);
2008-01-13 21:23:59 +00:00
camanglez = Angle2D.Normalized(camanglez);
// Limit vertical angle
if(camanglez < MAX_ANGLEZ_LOW) camanglez = MAX_ANGLEZ_LOW;
if(camanglez > MAX_ANGLEZ_HIGH) camanglez = MAX_ANGLEZ_HIGH;
General.MainWindow.UpdateCoordinates(new Vector2D(camanglexy, camanglez));
}
2008-01-13 21:23:59 +00:00
[BeginAction("moveforward", BaseAction = true)]
public virtual void BeginMoveForward()
2008-01-13 21:23:59 +00:00
{
keyforward = true;
2008-01-13 21:23:59 +00:00
}
[EndAction("moveforward", BaseAction = true)]
public virtual void EndMoveForward()
2008-01-13 21:23:59 +00:00
{
keyforward = false;
}
2008-01-13 21:23:59 +00:00
[BeginAction("movebackward", BaseAction = true)]
public virtual void BeginMoveBackward()
{
keybackward = true;
}
2008-01-13 21:23:59 +00:00
[EndAction("movebackward", BaseAction = true)]
public virtual void EndMoveBackward()
{
keybackward = false;
}
[BeginAction("moveleft", BaseAction = true)]
public virtual void BeginMoveLeft()
{
keyleft = true;
}
[EndAction("moveleft", BaseAction = true)]
public virtual void EndMoveLeft()
{
keyleft = false;
}
[BeginAction("moveright", BaseAction = true)]
public virtual void BeginMoveRight()
{
keyright = true;
}
[EndAction("moveright", BaseAction = true)]
public virtual void EndMoveRight()
{
keyright = false;
2008-01-13 21:23:59 +00:00
}
2008-01-13 21:23:59 +00:00
#endregion
2008-11-25 21:54:50 +00:00
#region ================== Visibility Culling
// This preforms visibility culling
private void DoCulling()
{
Dictionary<Linedef, Linedef> visiblelines = new Dictionary<Linedef, Linedef>(200);
2008-11-25 21:54:50 +00:00
Vector2D campos2d = (Vector2D)campos;
float viewdist = General.Settings.ViewDistance;
// Get the blocks within view range
visibleblocks = blockmap.GetFrustumRange(renderer.Frustum2D);
// Fill visiblity collections
2008-11-25 21:54:50 +00:00
visiblesectors = new Dictionary<Sector, VisualSector>(visiblesectors.Count);
visiblegeometry = new List<VisualGeometry>(visiblegeometry.Capacity);
foreach(VisualBlockEntry block in visibleblocks)
2008-11-25 21:54:50 +00:00
{
foreach(Linedef ld in block.Lines)
2008-11-25 21:54:50 +00:00
{
// Add line if not added yet
visiblelines[ld] = ld;
// Which side of the line is the camera on?
if(ld.SideOfLine(campos2d) < 0)
2008-11-25 21:54:50 +00:00
{
// Do front of line
if(ld.Front != null) ProcessSidedef(ld.Front);
}
else
{
// Do back of line
if(ld.Back != null) ProcessSidedef(ld.Back);
2008-11-25 21:54:50 +00:00
}
}
}
2008-11-25 21:54:50 +00:00
// Find camera sector
Linedef nld = MapSet.NearestLinedef(visiblelines.Values, campos2d);
if(nld != null)
{
camsector = GetCameraSectorFromLinedef(nld);
}
else
{
// Exceptional case: no lines found in any nearby blocks!
// This could happen in the middle of an extremely large sector and in this case
// the above code will not have found any sectors/sidedefs for rendering.
// Here we handle this special case with brute-force. Let's find the sector
// the camera is in by searching the entire map and render that sector only.
nld = General.Map.Map.NearestLinedef(campos2d);
if(nld != null)
2008-11-25 21:54:50 +00:00
{
camsector = GetCameraSectorFromLinedef(nld);
if(camsector != null)
2008-11-25 21:54:50 +00:00
{
foreach(Sidedef sd in camsector.Sidedefs)
2008-11-25 21:54:50 +00:00
{
float side = sd.Line.SideOfLine(campos2d);
if(((side < 0) && sd.IsFront) ||
((side > 0) && !sd.IsFront))
ProcessSidedef(sd);
2008-11-25 21:54:50 +00:00
}
}
else
{
// Too far away from the map to see anything
camsector = null;
}
}
else
{
// Map is empty
camsector = null;
2008-11-25 21:54:50 +00:00
}
}
}
// This finds and adds visible sectors
private void ProcessSidedef(Sidedef sd)
2008-11-25 21:54:50 +00:00
{
VisualSector vs;
2008-11-25 21:54:50 +00:00
// Find the visualsector and make it if needed
if(allsectors.ContainsKey(sd.Sector))
2008-11-25 21:54:50 +00:00
{
// Take existing visualsector
vs = allsectors[sd.Sector];
2008-11-25 21:54:50 +00:00
}
else
{
// Make new visualsector
vs = CreateVisualSector(sd.Sector);
allsectors.Add(sd.Sector, vs);
}
// Add to visible sectors if not added yet
if(!visiblesectors.ContainsKey(sd.Sector))
{
visiblesectors.Add(sd.Sector, vs);
visiblegeometry.AddRange(vs.FixedGeometry);
}
// Add sidedef geometry
visiblegeometry.AddRange(vs.GetSidedefGeometry(sd));
2008-11-25 21:54:50 +00:00
}
// This returns the camera sector from linedef
private Sector GetCameraSectorFromLinedef(Linedef ld)
{
if(ld.SideOfLine(campos) < 0)
{
if(ld.Front != null)
return ld.Front.Sector;
else
return null;
}
else
{
if(ld.Back != null)
return ld.Back.Sector;
else
return null;
}
}
2008-11-25 21:54:50 +00:00
#endregion
2008-01-13 21:23:59 +00:00
#region ================== Processing
2008-11-25 21:54:50 +00:00
// This creates a visual sector
protected abstract VisualSector CreateVisualSector(Sector s);
2008-11-20 21:32:23 +00:00
// This fills the blockmap
protected virtual void FillBlockMap()
{
blockmap.AddLinedefsSet(General.Map.Map.Linedefs);
}
// Processing
public override void OnProcess(double deltatime)
{
2008-01-13 21:23:59 +00:00
Vector3D camvec;
Vector3D camvecstrafe;
base.OnProcess(deltatime);
2008-01-13 21:23:59 +00:00
// Calculate camera direction vectors
camvec = Vector3D.FromAngleXYZ(camanglexy, camanglez);
2008-02-24 21:52:18 +00:00
camvecstrafe = Vector3D.FromAngleXY(camanglexy + Angle2D.PIHALF);
2008-01-13 21:23:59 +00:00
// Move the camera
if(keyforward) campos += camvec * CAMERA_SPEED;
if(keybackward) campos -= camvec * CAMERA_SPEED;
if(keyleft) campos -= camvecstrafe * CAMERA_SPEED;
if(keyright) campos += camvecstrafe * CAMERA_SPEED;
// Target the camera
2008-01-13 21:23:59 +00:00
camtarget = campos + camvec;
// Apply new camera matrices
renderer.PositionAndLookAt(campos, camtarget);
2008-11-25 21:54:50 +00:00
// Visibility culling
DoCulling();
// Now redraw
General.Interface.RedrawDisplay();
}
2008-01-04 00:16:58 +00:00
#endregion
2008-11-25 21:54:50 +00:00
#region ================== Rendering
// Call this to simply render all visible sectors
public override void OnRedrawDisplay()
{
// Render all visible sectors
foreach(VisualGeometry g in visiblegeometry)
renderer.RenderGeometry(g);
2008-11-25 21:54:50 +00:00
}
#endregion
#region ================== Actions
[EndAction("reloadresources", BaseAction = true)]
public virtual void ReloadResources()
{
// Trash all visual sectors, because they are no longer valid
foreach(KeyValuePair<Sector, VisualSector> s in allsectors) s.Value.Dispose();
allsectors.Clear();
visiblesectors.Clear();
visiblegeometry.Clear();
2008-11-25 21:54:50 +00:00
}
#endregion
2008-01-04 00:16:58 +00:00
}
}