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;
|
2008-05-29 11:54:45 +00:00
|
|
|
using CodeImp.DoomBuilder.Windows;
|
2008-01-04 00:16:58 +00:00
|
|
|
using CodeImp.DoomBuilder.IO;
|
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
2008-05-29 11:34:01 +00:00
|
|
|
using CodeImp.DoomBuilder.Actions;
|
2008-01-04 00:16:58 +00:00
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
|
|
|
using System.Drawing;
|
2008-01-05 18:11:29 +00:00
|
|
|
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
|
|
|
{
|
2008-01-05 18:11:29 +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
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
private const float ANGLE_FROM_MOUSE = 0.0001f;
|
2008-11-30 02:17:19 +00:00
|
|
|
public const float MAX_ANGLEZ_LOW = 100f / Angle2D.PIDEG;
|
|
|
|
public const float MAX_ANGLEZ_HIGH = (360f - 100f) / Angle2D.PIDEG;
|
2008-12-05 06:23:24 +00:00
|
|
|
private const double MOVE_SPEED_MULTIPLIER = 0.001d;
|
2008-01-05 18:11:29 +00:00
|
|
|
|
2008-01-04 00:16:58 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// 3D Mode thing
|
|
|
|
protected Thing modething;
|
|
|
|
|
|
|
|
// Graphics
|
|
|
|
protected IRenderer3D renderer;
|
|
|
|
private Renderer3D renderer3d;
|
|
|
|
|
|
|
|
// Camera
|
|
|
|
private Vector3D campos;
|
|
|
|
private Vector3D camtarget;
|
|
|
|
private float camanglexy, camanglez;
|
2008-11-30 02:17:19 +00:00
|
|
|
private Sector camsector;
|
2008-11-27 06:53:38 +00:00
|
|
|
|
2008-01-13 21:23:59 +00:00
|
|
|
// Input
|
|
|
|
private bool keyforward;
|
|
|
|
private bool keybackward;
|
|
|
|
private bool keyleft;
|
|
|
|
private bool keyright;
|
2008-12-05 06:23:24 +00:00
|
|
|
private bool doublespeed;
|
2008-01-13 21:23:59 +00:00
|
|
|
|
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;
|
2008-11-30 02:17:19 +00:00
|
|
|
protected List<VisualBlockEntry> visibleblocks;
|
2008-11-25 21:54:50 +00:00
|
|
|
protected Dictionary<Sector, VisualSector> visiblesectors;
|
2008-11-30 02:17:19 +00:00
|
|
|
protected List<VisualGeometry> visiblegeometry;
|
2008-11-20 21:32:23 +00:00
|
|
|
|
2008-01-04 00:16:58 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
public Vector3D CameraPosition { get { return campos; } set { campos = value; } }
|
|
|
|
public Vector3D CameraTarget { get { return camtarget; } }
|
2008-11-30 02:17:19 +00:00
|
|
|
public Sector CameraSector { get { return camsector; } }
|
2008-01-05 18:11:29 +00:00
|
|
|
|
2008-01-04 00:16:58 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Provides specialized functionality for a visual (3D) Doom Builder editing mode.
|
|
|
|
/// </summary>
|
2008-01-04 00:16:58 +00:00
|
|
|
public VisualMode()
|
|
|
|
{
|
|
|
|
// Initialize
|
2008-01-05 18:11:29 +00:00
|
|
|
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);
|
2008-11-30 02:17:19 +00:00
|
|
|
this.visibleblocks = new List<VisualBlockEntry>();
|
2008-11-25 21:54:50 +00:00
|
|
|
this.visiblesectors = new Dictionary<Sector, VisualSector>(50);
|
2008-11-30 02:17:19 +00:00
|
|
|
this.visiblegeometry = new List<VisualGeometry>(200);
|
2008-01-04 00:16:58 +00:00
|
|
|
}
|
|
|
|
|
2008-02-19 19:04:19 +00:00
|
|
|
// Disposer
|
2008-01-05 18:11:29 +00:00
|
|
|
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;
|
2008-11-30 02:17:19 +00:00
|
|
|
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
|
2008-01-05 18:11:29 +00:00
|
|
|
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
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// Mode is engaged
|
2008-05-15 08:10:29 +00:00
|
|
|
public override void OnEngage()
|
2008-01-05 18:11:29 +00:00
|
|
|
{
|
2008-05-15 08:10:29 +00:00
|
|
|
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();
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// 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;
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start special input mode
|
|
|
|
General.Interface.SetProcessorState(true);
|
|
|
|
General.Interface.StartExclusiveMouseInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode is disengaged
|
2008-05-15 08:10:29 +00:00
|
|
|
public override void OnDisengage()
|
2008-01-05 18:11:29 +00:00
|
|
|
{
|
2008-05-15 08:10:29 +00:00
|
|
|
base.OnDisengage();
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
// 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);
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// Mouse input
|
2008-05-15 08:10:29 +00:00
|
|
|
public override void OnMouseInput(Vector2D delta)
|
2008-01-05 18:11:29 +00:00
|
|
|
{
|
2008-05-15 08:10:29 +00:00
|
|
|
base.OnMouseInput(delta);
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
// 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-05 18:11:29 +00:00
|
|
|
|
2008-01-13 21:23:59 +00:00
|
|
|
// Normalize angles
|
2008-01-05 18:11:29 +00:00
|
|
|
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;
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
General.MainWindow.UpdateCoordinates(new Vector2D(camanglexy, camanglez));
|
|
|
|
}
|
2008-01-13 21:23:59 +00:00
|
|
|
|
2008-05-26 12:14:48 +00:00
|
|
|
[BeginAction("moveforward", BaseAction = true)]
|
|
|
|
public virtual void BeginMoveForward()
|
2008-01-13 21:23:59 +00:00
|
|
|
{
|
2008-05-26 12:14:48 +00:00
|
|
|
keyforward = true;
|
2008-01-13 21:23:59 +00:00
|
|
|
}
|
|
|
|
|
2008-05-26 12:14:48 +00:00
|
|
|
[EndAction("moveforward", BaseAction = true)]
|
|
|
|
public virtual void EndMoveForward()
|
2008-01-13 21:23:59 +00:00
|
|
|
{
|
2008-05-26 12:14:48 +00:00
|
|
|
keyforward = false;
|
|
|
|
}
|
2008-01-13 21:23:59 +00:00
|
|
|
|
2008-05-26 12:14:48 +00:00
|
|
|
[BeginAction("movebackward", BaseAction = true)]
|
|
|
|
public virtual void BeginMoveBackward()
|
|
|
|
{
|
|
|
|
keybackward = true;
|
|
|
|
}
|
2008-01-13 21:23:59 +00:00
|
|
|
|
2008-05-26 12:14:48 +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-12-05 06:23:24 +00:00
|
|
|
|
|
|
|
[BeginAction("movedoublespeed", BaseAction = true)]
|
|
|
|
public virtual void BeginDoubleSpeed()
|
|
|
|
{
|
|
|
|
doublespeed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
[EndAction("movedoublespeed", BaseAction = true)]
|
|
|
|
public virtual void EndDoubleSpeed()
|
|
|
|
{
|
|
|
|
doublespeed = false;
|
|
|
|
}
|
2008-01-05 18:11:29 +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()
|
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
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;
|
|
|
|
|
2008-11-30 02:17:19 +00:00
|
|
|
// Get the blocks within view range
|
|
|
|
visibleblocks = blockmap.GetFrustumRange(renderer.Frustum2D);
|
2008-11-27 06:53:38 +00:00
|
|
|
|
2008-11-30 02:17:19 +00:00
|
|
|
// Fill visiblity collections
|
2008-11-25 21:54:50 +00:00
|
|
|
visiblesectors = new Dictionary<Sector, VisualSector>(visiblesectors.Count);
|
2008-11-30 02:17:19 +00:00
|
|
|
visiblegeometry = new List<VisualGeometry>(visiblegeometry.Capacity);
|
|
|
|
foreach(VisualBlockEntry block in visibleblocks)
|
2008-11-25 21:54:50 +00:00
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
foreach(Linedef ld in block.Lines)
|
2008-11-25 21:54:50 +00:00
|
|
|
{
|
2008-11-30 22:04:30 +00:00
|
|
|
// Line not already processed?
|
|
|
|
if(!visiblelines.ContainsKey(ld))
|
2008-11-25 21:54:50 +00:00
|
|
|
{
|
2008-11-30 22:04:30 +00:00
|
|
|
// Add line if not added yet
|
|
|
|
visiblelines.Add(ld, ld);
|
|
|
|
|
|
|
|
// Which side of the line is the camera on?
|
|
|
|
if(ld.SideOfLine(campos2d) < 0)
|
|
|
|
{
|
|
|
|
// Do front of line
|
2008-12-02 06:53:24 +00:00
|
|
|
if(ld.Front != null) ProcessSidedefCulling(ld.Front);
|
2008-11-30 22:04:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Do back of line
|
2008-12-02 06:53:24 +00:00
|
|
|
if(ld.Back != null) ProcessSidedefCulling(ld.Back);
|
2008-11-30 22:04:30 +00:00
|
|
|
}
|
2008-11-25 21:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-30 02:17:19 +00:00
|
|
|
}
|
2008-11-25 21:54:50 +00:00
|
|
|
|
2008-11-30 02:17:19 +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
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
camsector = GetCameraSectorFromLinedef(nld);
|
|
|
|
if(camsector != null)
|
2008-11-25 21:54:50 +00:00
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
foreach(Sidedef sd in camsector.Sidedefs)
|
2008-11-25 21:54:50 +00:00
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
float side = sd.Line.SideOfLine(campos2d);
|
|
|
|
if(((side < 0) && sd.IsFront) ||
|
|
|
|
((side > 0) && !sd.IsFront))
|
2008-12-02 06:53:24 +00:00
|
|
|
ProcessSidedefCulling(sd);
|
2008-11-25 21:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-30 02:17:19 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-30 02:17:19 +00:00
|
|
|
|
|
|
|
// This finds and adds visible sectors
|
2008-12-02 06:53:24 +00:00
|
|
|
private void ProcessSidedefCulling(Sidedef sd)
|
2008-11-25 21:54:50 +00:00
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
VisualSector vs;
|
2008-11-25 21:54:50 +00:00
|
|
|
|
2008-11-30 02:17:19 +00:00
|
|
|
// Find the visualsector and make it if needed
|
|
|
|
if(allsectors.ContainsKey(sd.Sector))
|
2008-11-25 21:54:50 +00:00
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
// Take existing visualsector
|
|
|
|
vs = allsectors[sd.Sector];
|
2008-11-25 21:54:50 +00:00
|
|
|
}
|
|
|
|
else
|
2008-11-30 02:17:19 +00:00
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2008-11-30 02:17:19 +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-12-01 06:51:00 +00:00
|
|
|
#region ================== Object Picking
|
|
|
|
|
|
|
|
// This picks an object from the scene
|
|
|
|
public VisualPickResult PickObject(Vector3D from, Vector3D to)
|
|
|
|
{
|
|
|
|
VisualPickResult result = new VisualPickResult();
|
2008-12-02 15:47:06 +00:00
|
|
|
Line2D ray2d = new Line2D(from, to);
|
2008-12-04 21:37:44 +00:00
|
|
|
Vector3D delta = to - from;
|
2008-12-02 15:47:06 +00:00
|
|
|
|
2008-12-02 06:53:24 +00:00
|
|
|
// Setup no result
|
|
|
|
result.geometry = null;
|
|
|
|
result.hitpos = new Vector3D();
|
2008-12-02 15:47:06 +00:00
|
|
|
result.u_ray = 1.0f;
|
2008-12-01 06:51:00 +00:00
|
|
|
|
2008-12-02 06:53:24 +00:00
|
|
|
// Find all blocks we are intersecting
|
2008-12-01 06:51:00 +00:00
|
|
|
List<VisualBlockEntry> blocks = blockmap.GetLineBlocks(from, to);
|
2008-12-02 15:47:06 +00:00
|
|
|
|
|
|
|
// Make collections
|
2008-12-02 06:53:24 +00:00
|
|
|
Dictionary<Linedef, Linedef> lines = new Dictionary<Linedef, Linedef>(blocks.Count * 10);
|
|
|
|
Dictionary<Sector, VisualSector> sectors = new Dictionary<Sector, VisualSector>(blocks.Count * 10);
|
|
|
|
List<VisualGeometry> potentialgeometry = new List<VisualGeometry>(blocks.Count * 10);
|
2008-12-02 15:47:06 +00:00
|
|
|
|
|
|
|
// Add geometry from the camera sector
|
|
|
|
if((camsector != null) && allsectors.ContainsKey(camsector))
|
|
|
|
{
|
|
|
|
VisualSector vs = allsectors[camsector];
|
|
|
|
sectors.Add(camsector, vs);
|
|
|
|
potentialgeometry.AddRange(vs.FixedGeometry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go for all lines to see which ones we intersect
|
|
|
|
// We will collect geometry from the sectors and sidedefs
|
2008-12-02 06:53:24 +00:00
|
|
|
foreach(VisualBlockEntry b in blocks)
|
|
|
|
{
|
|
|
|
foreach(Linedef ld in b.Lines)
|
|
|
|
{
|
|
|
|
// Make sure we don't test a line twice
|
|
|
|
if(!lines.ContainsKey(ld))
|
|
|
|
{
|
|
|
|
lines.Add(ld, ld);
|
2008-12-02 15:47:06 +00:00
|
|
|
|
2008-12-02 06:53:24 +00:00
|
|
|
// Intersecting?
|
|
|
|
float u;
|
|
|
|
if(ld.Line.GetIntersection(ray2d, out u))
|
|
|
|
{
|
|
|
|
// Check on which side we are
|
2008-12-02 15:47:06 +00:00
|
|
|
float side = ld.SideOfLine(ray2d.v1);
|
2008-12-02 06:53:24 +00:00
|
|
|
|
|
|
|
// Calculate intersection point
|
2008-12-04 21:37:44 +00:00
|
|
|
Vector3D intersect = from + delta * u;
|
2008-12-02 06:53:24 +00:00
|
|
|
|
|
|
|
// We must add the sectors of both sides of the line
|
|
|
|
// If we wouldn't, then aiming at a sector that is just within range
|
|
|
|
// could result in an incorrect hit (because the far line of the
|
|
|
|
// sector may not be included in this loop)
|
|
|
|
if(ld.Front != null)
|
|
|
|
{
|
|
|
|
// Find the visualsector
|
|
|
|
if(allsectors.ContainsKey(ld.Front.Sector))
|
|
|
|
{
|
|
|
|
VisualSector vs = allsectors[ld.Front.Sector];
|
|
|
|
|
|
|
|
// Add sector if not already added
|
|
|
|
if(!sectors.ContainsKey(ld.Front.Sector))
|
|
|
|
{
|
|
|
|
sectors.Add(ld.Front.Sector, vs);
|
|
|
|
potentialgeometry.AddRange(vs.FixedGeometry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add sidedef if on the front side
|
|
|
|
if(side < 0.0f)
|
|
|
|
{
|
|
|
|
int previndex = potentialgeometry.Count;
|
|
|
|
potentialgeometry.AddRange(vs.GetSidedefGeometry(ld.Front));
|
|
|
|
for(int i = previndex; i < potentialgeometry.Count; i++)
|
2008-12-02 15:47:06 +00:00
|
|
|
potentialgeometry[i].SetPickResults(intersect, u);
|
2008-12-02 06:53:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add back side also
|
|
|
|
if(ld.Back != null)
|
|
|
|
{
|
|
|
|
// Find the visualsector
|
|
|
|
if(allsectors.ContainsKey(ld.Back.Sector))
|
|
|
|
{
|
|
|
|
VisualSector vs = allsectors[ld.Back.Sector];
|
|
|
|
|
|
|
|
// Add sector if not already added
|
|
|
|
if(!sectors.ContainsKey(ld.Back.Sector))
|
|
|
|
{
|
|
|
|
sectors.Add(ld.Back.Sector, vs);
|
|
|
|
potentialgeometry.AddRange(vs.FixedGeometry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add sidedef if on the front side
|
|
|
|
if(side > 0.0f)
|
|
|
|
{
|
|
|
|
int previndex = potentialgeometry.Count;
|
|
|
|
potentialgeometry.AddRange(vs.GetSidedefGeometry(ld.Back));
|
|
|
|
for(int i = previndex; i < potentialgeometry.Count; i++)
|
2008-12-02 15:47:06 +00:00
|
|
|
potentialgeometry[i].SetPickResults(intersect, u);
|
2008-12-02 06:53:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-02 15:47:06 +00:00
|
|
|
|
2008-12-02 06:53:24 +00:00
|
|
|
// Now we have a list of potential geometry that lies along the trace line.
|
|
|
|
// We still don't know what geometry actually hits, but we ruled out that which doesn't get even close.
|
|
|
|
// This is still too much for accurate intersection testing, so we do a fast reject pass first.
|
|
|
|
Vector3D direction = to - from;
|
|
|
|
direction = direction.GetNormal();
|
|
|
|
List<VisualGeometry> likelygeometry = new List<VisualGeometry>(potentialgeometry.Count);
|
|
|
|
foreach(VisualGeometry g in potentialgeometry)
|
|
|
|
{
|
|
|
|
if(g.PickFastReject(from, to, direction)) likelygeometry.Add(g);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we do an accurate intersection test for all resulting geometry
|
|
|
|
// We keep only the closest hit!
|
|
|
|
foreach(VisualGeometry g in likelygeometry)
|
|
|
|
{
|
|
|
|
float u = result.u_ray;
|
|
|
|
if(g.PickAccurate(from, to, direction, ref u))
|
|
|
|
{
|
|
|
|
// Closer than previous find?
|
2008-12-02 15:47:06 +00:00
|
|
|
if((u > 0.0f) && (u < result.u_ray))
|
2008-12-02 06:53:24 +00:00
|
|
|
{
|
|
|
|
result.u_ray = u;
|
|
|
|
result.geometry = g;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-02 15:47:06 +00:00
|
|
|
|
2008-12-02 06:53:24 +00:00
|
|
|
// Setup final result
|
|
|
|
result.hitpos = from + to * result.u_ray;
|
2008-12-02 15:47:06 +00:00
|
|
|
|
2008-12-01 06:51:00 +00:00
|
|
|
// Done
|
|
|
|
return result;
|
|
|
|
}
|
2008-12-02 06:53:24 +00:00
|
|
|
|
2008-12-01 06:51:00 +00:00
|
|
|
#endregion
|
|
|
|
|
2008-12-05 06:23:24 +00:00
|
|
|
#region ================== Processing
|
|
|
|
|
|
|
|
// This creates a visual sector
|
|
|
|
protected abstract VisualSector CreateVisualSector(Sector s);
|
|
|
|
|
|
|
|
// This fills the blockmap
|
|
|
|
protected virtual void FillBlockMap()
|
|
|
|
{
|
|
|
|
blockmap.AddLinedefsSet(General.Map.Map.Linedefs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Processing
|
|
|
|
public override void OnProcess(double deltatime)
|
|
|
|
{
|
|
|
|
Vector3D camvec;
|
|
|
|
Vector3D camvecstrafe;
|
|
|
|
double multiplier;
|
|
|
|
|
|
|
|
base.OnProcess(deltatime);
|
|
|
|
|
|
|
|
// Calculate camera direction vectors
|
|
|
|
camvec = Vector3D.FromAngleXYZ(camanglexy, camanglez);
|
|
|
|
camvecstrafe = Vector3D.FromAngleXY(camanglexy + Angle2D.PIHALF);
|
|
|
|
|
|
|
|
// Move the camera
|
|
|
|
if(doublespeed) multiplier = MOVE_SPEED_MULTIPLIER * 2.0f; else multiplier = MOVE_SPEED_MULTIPLIER;
|
|
|
|
if(keyforward) campos += camvec * (float)((double)General.Settings.MoveSpeed * multiplier * deltatime);
|
|
|
|
if(keybackward) campos -= camvec * (float)((double)General.Settings.MoveSpeed * multiplier * deltatime);
|
|
|
|
if(keyleft) campos -= camvecstrafe * (float)((double)General.Settings.MoveSpeed * multiplier * deltatime);
|
|
|
|
if(keyright) campos += camvecstrafe * (float)((double)General.Settings.MoveSpeed * multiplier * deltatime);
|
|
|
|
|
|
|
|
// Target the camera
|
|
|
|
camtarget = campos + camvec;
|
|
|
|
|
|
|
|
// Apply new camera matrices
|
|
|
|
renderer.PositionAndLookAt(campos, camtarget);
|
|
|
|
|
|
|
|
// Visibility culling
|
|
|
|
DoCulling();
|
|
|
|
|
|
|
|
// Now redraw
|
|
|
|
General.Interface.RedrawDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2008-11-25 21:54:50 +00:00
|
|
|
#region ================== Rendering
|
|
|
|
|
|
|
|
// Call this to simply render all visible sectors
|
2008-12-01 06:51:00 +00:00
|
|
|
protected virtual void AddGeometry()
|
2008-11-25 21:54:50 +00:00
|
|
|
{
|
|
|
|
// Render all visible sectors
|
2008-11-30 02:17:19 +00:00
|
|
|
foreach(VisualGeometry g in visiblegeometry)
|
2008-12-01 06:51:00 +00:00
|
|
|
renderer.AddGeometry(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();
|
2008-11-30 02:17:19 +00:00
|
|
|
visiblegeometry.Clear();
|
2008-11-25 21:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2008-01-04 00:16:58 +00:00
|
|
|
}
|
|
|
|
}
|