2007-06-15 18:50:18 +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 System.Drawing;
|
|
|
|
using System.ComponentModel;
|
2008-01-05 18:11:29 +00:00
|
|
|
using SlimDX;
|
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
|
|
|
using SlimDX.Direct3D9;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
2008-11-19 16:18:36 +00:00
|
|
|
using CodeImp.DoomBuilder.VisualModes;
|
2008-11-30 11:23:20 +00:00
|
|
|
using CodeImp.DoomBuilder.Map;
|
2007-06-15 18:50:18 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Rendering
|
|
|
|
{
|
2008-01-02 21:49:43 +00:00
|
|
|
internal sealed class Renderer3D : Renderer, IRenderer3D
|
2007-06-15 18:50:18 +00:00
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
2008-11-30 22:04:30 +00:00
|
|
|
private const int RENDER_PASSES = 4;
|
2008-01-18 19:52:19 +00:00
|
|
|
private const float PROJ_NEAR_PLANE = 1f;
|
2008-12-04 17:59:34 +00:00
|
|
|
private const float CROSSHAIR_SCALE = 0.06f;
|
2008-12-04 22:35:45 +00:00
|
|
|
private const float FOG_RANGE = 0.9f;
|
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// Matrices
|
|
|
|
private Matrix projection;
|
2008-12-04 17:59:34 +00:00
|
|
|
private Matrix view3d;
|
2008-01-05 18:11:29 +00:00
|
|
|
private Matrix billboard;
|
2008-12-10 16:00:34 +00:00
|
|
|
private Matrix worldviewproj;
|
2008-12-04 17:59:34 +00:00
|
|
|
private Matrix view2d;
|
2008-12-10 16:00:34 +00:00
|
|
|
private Matrix world;
|
2008-12-14 23:24:40 +00:00
|
|
|
private Vector3D cameraposition;
|
2008-12-04 17:59:34 +00:00
|
|
|
|
|
|
|
// Window size
|
|
|
|
private Size windowsize;
|
2008-01-05 18:11:29 +00:00
|
|
|
|
2008-11-30 02:17:19 +00:00
|
|
|
// Frustum
|
|
|
|
private ProjectedFrustum2D frustum;
|
|
|
|
|
2008-12-14 23:24:40 +00:00
|
|
|
// Thing cage
|
|
|
|
private VertexBuffer thingcage;
|
2008-12-15 22:13:24 +00:00
|
|
|
private bool renderthingcages;
|
2008-12-14 23:24:40 +00:00
|
|
|
|
2008-12-03 07:04:57 +00:00
|
|
|
// Crosshair
|
|
|
|
private FlatVertex[] crosshairverts;
|
2008-12-06 13:20:47 +00:00
|
|
|
private bool crosshairbusy;
|
2008-12-03 07:04:57 +00:00
|
|
|
|
2008-11-30 22:04:30 +00:00
|
|
|
// Geometry to be rendered.
|
|
|
|
// Each Dictionary in the array is a render pass.
|
|
|
|
// Each BinaryHeap in the Dictionary contains all geometry that needs
|
|
|
|
// to be rendered with the associated ImageData.
|
|
|
|
// The BinaryHeap sorts the geometry by sector to minimize stream switchs.
|
|
|
|
private Dictionary<ImageData, BinaryHeap<VisualGeometry>>[] geometry;
|
2008-12-09 22:51:27 +00:00
|
|
|
|
|
|
|
// Things to be rendered.
|
|
|
|
// Each Dictionary in the array is a render pass.
|
|
|
|
// Each VisualThing is inserted in the Dictionary by their texture image.
|
|
|
|
private Dictionary<ImageData, List<VisualThing>>[] things;
|
|
|
|
|
2008-12-14 23:24:40 +00:00
|
|
|
// Things to be rendered, sorted by distance from camera
|
|
|
|
private BinaryHeap<VisualThing> thingsbydistance;
|
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
2008-11-30 02:17:19 +00:00
|
|
|
public ProjectedFrustum2D Frustum2D { get { return frustum; } }
|
2008-12-15 22:13:24 +00:00
|
|
|
public bool DrawThingCages { get { return renderthingcages; } set { renderthingcages = value; } }
|
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
2008-01-02 21:49:43 +00:00
|
|
|
internal Renderer3D(D3DDevice graphics) : base(graphics)
|
2007-06-15 18:50:18 +00:00
|
|
|
{
|
|
|
|
// Initialize
|
2008-01-05 18:11:29 +00:00
|
|
|
CreateProjection();
|
2008-12-04 17:59:34 +00:00
|
|
|
CreateMatrices2D();
|
2008-12-14 23:24:40 +00:00
|
|
|
SetupThingCage();
|
2008-12-15 22:13:24 +00:00
|
|
|
renderthingcages = true;
|
2008-12-04 17:59:34 +00:00
|
|
|
|
2008-11-30 02:17:19 +00:00
|
|
|
// Dummy frustum
|
|
|
|
frustum = new ProjectedFrustum2D(new Vector2D(), 0.0f, 0.0f, PROJ_NEAR_PLANE,
|
|
|
|
General.Settings.ViewDistance, Angle2D.DegToRad((float)General.Settings.VisualFOV));
|
2008-12-04 17:59:34 +00:00
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
2008-01-02 21:49:43 +00:00
|
|
|
// Disposer
|
|
|
|
internal override void Dispose()
|
2007-06-15 18:50:18 +00:00
|
|
|
{
|
|
|
|
// Not already disposed?
|
|
|
|
if(!isdisposed)
|
|
|
|
{
|
|
|
|
// Clean up
|
2008-12-14 23:24:40 +00:00
|
|
|
if(thingcage != null) thingcage.Dispose();
|
|
|
|
thingcage = null;
|
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
// Done
|
2007-09-24 19:54:47 +00:00
|
|
|
base.Dispose();
|
2007-06-15 18:50:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2008-12-03 07:04:57 +00:00
|
|
|
#region ================== Management
|
2007-06-15 18:50:18 +00:00
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// This is called before a device is reset
|
|
|
|
// (when resized or display adapter was changed)
|
|
|
|
public override void UnloadResource()
|
|
|
|
{
|
2008-12-03 07:04:57 +00:00
|
|
|
crosshairverts = null;
|
2008-12-14 23:24:40 +00:00
|
|
|
if(thingcage != null) thingcage.Dispose();
|
|
|
|
thingcage = null;
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is called resets when the device is reset
|
|
|
|
// (when resized or display adapter was changed)
|
|
|
|
public override void ReloadResource()
|
|
|
|
{
|
2008-12-04 17:59:34 +00:00
|
|
|
CreateMatrices2D();
|
2008-12-14 23:24:40 +00:00
|
|
|
SetupThingCage();
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 07:04:57 +00:00
|
|
|
// This makes screen vertices for display
|
|
|
|
private void CreateCrosshairVerts(Size texturesize)
|
|
|
|
{
|
|
|
|
// Determine coordinates
|
2008-12-04 17:59:34 +00:00
|
|
|
float width = (float)windowsize.Width;
|
|
|
|
float height = (float)windowsize.Height;
|
2008-12-03 07:04:57 +00:00
|
|
|
float size = (float)height * CROSSHAIR_SCALE;
|
|
|
|
RectangleF rect = new RectangleF((width - size) / 2, (height - size) / 2, size, size);
|
|
|
|
|
|
|
|
// Make vertices
|
|
|
|
crosshairverts = new FlatVertex[4];
|
|
|
|
crosshairverts[0].x = rect.Left;
|
|
|
|
crosshairverts[0].y = rect.Top;
|
|
|
|
crosshairverts[0].c = -1;
|
|
|
|
crosshairverts[0].u = 1f / texturesize.Width;
|
|
|
|
crosshairverts[0].v = 1f / texturesize.Height;
|
|
|
|
crosshairverts[1].x = rect.Right;
|
|
|
|
crosshairverts[1].y = rect.Top;
|
|
|
|
crosshairverts[1].c = -1;
|
|
|
|
crosshairverts[1].u = 1f - 1f / texturesize.Width;
|
|
|
|
crosshairverts[1].v = 1f / texturesize.Height;
|
|
|
|
crosshairverts[2].x = rect.Left;
|
|
|
|
crosshairverts[2].y = rect.Bottom;
|
|
|
|
crosshairverts[2].c = -1;
|
|
|
|
crosshairverts[2].u = 1f / texturesize.Width;
|
|
|
|
crosshairverts[2].v = 1f - 1f / texturesize.Height;
|
|
|
|
crosshairverts[3].x = rect.Right;
|
|
|
|
crosshairverts[3].y = rect.Bottom;
|
|
|
|
crosshairverts[3].c = -1;
|
|
|
|
crosshairverts[3].u = 1f - 1f / texturesize.Width;
|
|
|
|
crosshairverts[3].v = 1f - 1f / texturesize.Height;
|
|
|
|
}
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
#endregion
|
|
|
|
|
2008-12-14 23:24:40 +00:00
|
|
|
#region ================== Thing Cage
|
|
|
|
|
|
|
|
// This sets up the thing cage
|
|
|
|
private void SetupThingCage()
|
|
|
|
{
|
|
|
|
const int totalvertices = 36;
|
|
|
|
WorldVertex[] tv = new WorldVertex[totalvertices];
|
|
|
|
float x0 = -1.0f;
|
|
|
|
float x1 = 1.0f;
|
|
|
|
float y0 = -1.0f;
|
|
|
|
float y1 = 1.0f;
|
|
|
|
float z0 = 0.0f;
|
|
|
|
float z1 = 1.0f;
|
|
|
|
float u0 = 0.0f;
|
|
|
|
float u1 = 1.0f;
|
|
|
|
float v0 = 0.0f;
|
|
|
|
float v1 = 1.0f;
|
|
|
|
int c = -1;
|
|
|
|
|
|
|
|
// Front
|
|
|
|
tv[0] = new WorldVertex(x0, y0, z0, c, u0, v0);
|
|
|
|
tv[1] = new WorldVertex(x0, y0, z1, c, u0, v1);
|
|
|
|
tv[2] = new WorldVertex(x1, y0, z0, c, u1, v0);
|
|
|
|
tv[3] = new WorldVertex(x1, y0, z0, c, u1, v0);
|
|
|
|
tv[4] = new WorldVertex(x0, y0, z1, c, u0, v1);
|
|
|
|
tv[5] = new WorldVertex(x1, y0, z1, c, u1, v1);
|
|
|
|
|
|
|
|
// Right
|
|
|
|
tv[6] = new WorldVertex(x1, y0, z0, c, u0, v0);
|
|
|
|
tv[7] = new WorldVertex(x1, y0, z1, c, u0, v1);
|
|
|
|
tv[8] = new WorldVertex(x1, y1, z0, c, u1, v0);
|
|
|
|
tv[9] = new WorldVertex(x1, y1, z0, c, u1, v0);
|
|
|
|
tv[10] = new WorldVertex(x1, y0, z1, c, u0, v1);
|
|
|
|
tv[11] = new WorldVertex(x1, y1, z1, c, u1, v1);
|
|
|
|
|
|
|
|
// Back
|
|
|
|
tv[12] = new WorldVertex(x1, y1, z0, c, u0, v0);
|
|
|
|
tv[13] = new WorldVertex(x1, y1, z1, c, u0, v1);
|
|
|
|
tv[14] = new WorldVertex(x0, y1, z0, c, u1, v0);
|
|
|
|
tv[15] = new WorldVertex(x0, y1, z0, c, u1, v0);
|
|
|
|
tv[16] = new WorldVertex(x1, y1, z1, c, u0, v1);
|
|
|
|
tv[17] = new WorldVertex(x0, y1, z1, c, u1, v1);
|
|
|
|
|
|
|
|
// Left
|
|
|
|
tv[18] = new WorldVertex(x0, y1, z0, c, u0, v1);
|
|
|
|
tv[19] = new WorldVertex(x0, y1, z1, c, u0, v0);
|
|
|
|
tv[20] = new WorldVertex(x0, y0, z1, c, u1, v0);
|
|
|
|
tv[21] = new WorldVertex(x0, y1, z0, c, u1, v0);
|
|
|
|
tv[22] = new WorldVertex(x0, y0, z1, c, u0, v1);
|
|
|
|
tv[23] = new WorldVertex(x0, y0, z0, c, u1, v1);
|
|
|
|
|
|
|
|
// Top
|
|
|
|
tv[24] = new WorldVertex(x0, y0, z1, c, u0, v0);
|
|
|
|
tv[25] = new WorldVertex(x0, y1, z1, c, u0, v1);
|
|
|
|
tv[26] = new WorldVertex(x1, y0, z1, c, u1, v0);
|
|
|
|
tv[27] = new WorldVertex(x1, y0, z1, c, u1, v0);
|
|
|
|
tv[28] = new WorldVertex(x0, y1, z1, c, u0, v1);
|
|
|
|
tv[29] = new WorldVertex(x1, y1, z1, c, u1, v1);
|
|
|
|
|
|
|
|
// Bottom
|
|
|
|
tv[30] = new WorldVertex(x1, y0, z0, c, u1, v0);
|
|
|
|
tv[31] = new WorldVertex(x0, y1, z0, c, u0, v1);
|
|
|
|
tv[32] = new WorldVertex(x0, y0, z0, c, u0, v0);
|
|
|
|
tv[33] = new WorldVertex(x1, y0, z0, c, u1, v0);
|
|
|
|
tv[34] = new WorldVertex(x1, y1, z0, c, u1, v1);
|
|
|
|
tv[35] = new WorldVertex(x0, y1, z0, c, u0, v1);
|
|
|
|
|
|
|
|
// Create vertexbuffer
|
|
|
|
thingcage = new VertexBuffer(General.Map.Graphics.Device, WorldVertex.Stride * totalvertices,
|
|
|
|
Usage.WriteOnly | Usage.Dynamic, VertexFormat.None, Pool.Default);
|
|
|
|
DataStream bufferstream = thingcage.Lock(0, WorldVertex.Stride * totalvertices, LockFlags.Discard);
|
|
|
|
bufferstream.WriteRange<WorldVertex>(tv);
|
|
|
|
thingcage.Unlock();
|
|
|
|
bufferstream.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2008-12-03 07:04:57 +00:00
|
|
|
#region ================== Presentation
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
// This creates the projection
|
|
|
|
internal void CreateProjection()
|
|
|
|
{
|
|
|
|
// Calculate aspect
|
|
|
|
float aspect = (float)General.Map.Graphics.RenderTarget.ClientSize.Width /
|
|
|
|
(float)General.Map.Graphics.RenderTarget.ClientSize.Height;
|
2008-11-30 02:17:19 +00:00
|
|
|
|
|
|
|
// The DirectX PerspectiveFovRH matrix method calculates the scaling in X and Y as follows:
|
|
|
|
// yscale = 1 / tan(fovY / 2)
|
|
|
|
// xscale = yscale / aspect
|
|
|
|
// The fov specified in the method is the FOV over Y, but we want the user to specify the FOV
|
|
|
|
// over X, so calculate what it would be over Y first;
|
|
|
|
float fov = Angle2D.DegToRad((float)General.Settings.VisualFOV);
|
|
|
|
float reversefov = 1.0f / (float)Math.Tan(fov / 2.0f);
|
|
|
|
float reversefovy = reversefov * aspect;
|
|
|
|
float fovy = (float)Math.Atan(1.0f / reversefovy) * 2.0f;
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
// Make the projection matrix
|
2008-11-30 02:17:19 +00:00
|
|
|
projection = Matrix.PerspectiveFovRH(fovy, aspect, PROJ_NEAR_PLANE, General.Settings.ViewDistance);
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
// Apply matrices
|
2008-12-04 17:59:34 +00:00
|
|
|
ApplyMatrices3D();
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This creates matrices for a camera view
|
|
|
|
public void PositionAndLookAt(Vector3D pos, Vector3D lookat)
|
|
|
|
{
|
|
|
|
Vector3D delta;
|
|
|
|
float anglexy, anglez;
|
|
|
|
|
|
|
|
// Calculate delta vector
|
2008-12-14 23:24:40 +00:00
|
|
|
cameraposition = pos;
|
2008-01-05 18:11:29 +00:00
|
|
|
delta = lookat - pos;
|
|
|
|
anglexy = delta.GetAngleXY();
|
|
|
|
anglez = delta.GetAngleZ();
|
2008-11-30 02:17:19 +00:00
|
|
|
|
|
|
|
// Create frustum
|
|
|
|
frustum = new ProjectedFrustum2D(pos, anglexy, anglez, PROJ_NEAR_PLANE,
|
|
|
|
General.Settings.ViewDistance, Angle2D.DegToRad((float)General.Settings.VisualFOV));
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
// Make the view matrix
|
2008-12-04 17:59:34 +00:00
|
|
|
view3d = Matrix.LookAtRH(D3DDevice.V3(pos), D3DDevice.V3(lookat), new Vector3(0f, 0f, 1f));
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// Make the billboard matrix
|
2008-12-10 16:00:34 +00:00
|
|
|
billboard = Matrix.RotationZ(anglexy + Angle2D.PI);
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
2008-12-09 22:51:27 +00:00
|
|
|
|
2008-12-04 17:59:34 +00:00
|
|
|
// This creates 2D view matrix
|
|
|
|
private void CreateMatrices2D()
|
|
|
|
{
|
|
|
|
windowsize = graphics.RenderTarget.ClientSize;
|
|
|
|
Matrix scaling = Matrix.Scaling((1f / (float)windowsize.Width) * 2f, (1f / (float)windowsize.Height) * -2f, 1f);
|
|
|
|
Matrix translate = Matrix.Translation(-(float)windowsize.Width * 0.5f, -(float)windowsize.Height * 0.5f, 0f);
|
|
|
|
view2d = Matrix.Multiply(translate, scaling);
|
|
|
|
}
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
// This applies the matrices
|
2008-12-04 17:59:34 +00:00
|
|
|
private void ApplyMatrices3D()
|
2008-01-05 18:11:29 +00:00
|
|
|
{
|
2008-12-10 16:00:34 +00:00
|
|
|
worldviewproj = world * view3d * projection;
|
|
|
|
graphics.Shaders.World3D.WorldViewProj = worldviewproj;
|
|
|
|
graphics.Device.SetTransform(TransformState.World, world);
|
2008-01-05 18:11:29 +00:00
|
|
|
graphics.Device.SetTransform(TransformState.Projection, projection);
|
2008-12-04 17:59:34 +00:00
|
|
|
graphics.Device.SetTransform(TransformState.View, view3d);
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 17:59:34 +00:00
|
|
|
// This sets the appropriate view matrix
|
|
|
|
public void ApplyMatrices2D()
|
|
|
|
{
|
2008-12-10 16:00:34 +00:00
|
|
|
graphics.Device.SetTransform(TransformState.World, world);
|
2008-12-04 17:59:34 +00:00
|
|
|
graphics.Device.SetTransform(TransformState.Projection, Matrix.Identity);
|
|
|
|
graphics.Device.SetTransform(TransformState.View, view2d);
|
|
|
|
}
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
#endregion
|
|
|
|
|
2008-12-03 07:04:57 +00:00
|
|
|
#region ================== Start / Finish
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
// This starts rendering
|
|
|
|
public bool Start()
|
|
|
|
{
|
2008-12-14 23:24:40 +00:00
|
|
|
// Create thing box texture if needed
|
|
|
|
if(General.Map.Data.ThingBox.Texture == null)
|
|
|
|
General.Map.Data.ThingBox.CreateTexture();
|
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// Start drawing
|
2008-05-11 00:42:34 +00:00
|
|
|
if(graphics.StartRendering(true, General.Colors.Background.ToColorValue(), graphics.BackBuffer, graphics.DepthBuffer))
|
2008-01-05 18:11:29 +00:00
|
|
|
{
|
|
|
|
// Beginning renderstates
|
|
|
|
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
|
|
|
|
graphics.Device.SetRenderState(RenderState.ZEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
|
2008-09-13 13:24:06 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
|
2008-12-04 22:35:45 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.FogEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.FogDensity, 1.0f);
|
|
|
|
graphics.Device.SetRenderState(RenderState.FogColor, General.Colors.Background.ToInt());
|
|
|
|
graphics.Device.SetRenderState(RenderState.FogStart, General.Settings.ViewDistance * FOG_RANGE);
|
|
|
|
graphics.Device.SetRenderState(RenderState.FogEnd, General.Settings.ViewDistance);
|
|
|
|
graphics.Device.SetRenderState(RenderState.FogTableMode, FogMode.Linear);
|
|
|
|
graphics.Device.SetRenderState(RenderState.RangeFogEnable, false);
|
2008-12-14 23:24:40 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
|
|
|
|
graphics.Shaders.World3D.SetModulateColor(-1);
|
2008-09-28 21:20:56 +00:00
|
|
|
|
|
|
|
// Matrices
|
2008-12-10 16:00:34 +00:00
|
|
|
world = Matrix.Identity;
|
2008-12-04 17:59:34 +00:00
|
|
|
ApplyMatrices3D();
|
2008-12-03 07:04:57 +00:00
|
|
|
|
|
|
|
// Create crosshair vertices
|
|
|
|
if(crosshairverts == null)
|
|
|
|
CreateCrosshairVerts(new Size(General.Map.Data.Crosshair3D.Width, General.Map.Data.Crosshair3D.Height));
|
2008-01-05 18:11:29 +00:00
|
|
|
|
|
|
|
// Ready
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Can't render now
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This begins rendering world geometry
|
|
|
|
public void StartGeometry()
|
|
|
|
{
|
2008-11-30 22:04:30 +00:00
|
|
|
// Make collection
|
|
|
|
geometry = new Dictionary<ImageData, BinaryHeap<VisualGeometry>>[RENDER_PASSES];
|
2008-12-09 22:51:27 +00:00
|
|
|
things = new Dictionary<ImageData, List<VisualThing>>[RENDER_PASSES];
|
2008-12-14 23:24:40 +00:00
|
|
|
thingsbydistance = new BinaryHeap<VisualThing>();
|
2008-12-09 22:51:27 +00:00
|
|
|
for(int i = 0; i < RENDER_PASSES; i++)
|
|
|
|
{
|
|
|
|
geometry[i] = new Dictionary<ImageData, BinaryHeap<VisualGeometry>>();
|
|
|
|
things[i] = new Dictionary<ImageData, List<VisualThing>>();
|
|
|
|
}
|
2008-11-30 22:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This ends rendering world geometry
|
|
|
|
public void FinishGeometry()
|
|
|
|
{
|
|
|
|
// Initial renderstates
|
2008-05-11 00:42:34 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.CullMode, Cull.Counterclockwise);
|
2008-01-13 21:23:59 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.ZEnable, true);
|
|
|
|
graphics.Device.SetRenderState(RenderState.ZWriteEnable, true);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
|
2008-11-30 22:04:30 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
|
2008-02-24 21:52:18 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
|
2008-01-05 18:11:29 +00:00
|
|
|
graphics.Shaders.World3D.Begin();
|
2008-12-03 07:04:57 +00:00
|
|
|
|
|
|
|
// SOLID PASS
|
2008-12-10 16:00:34 +00:00
|
|
|
world = Matrix.Identity;
|
|
|
|
ApplyMatrices3D();
|
2008-01-05 18:11:29 +00:00
|
|
|
graphics.Shaders.World3D.BeginPass(0);
|
2008-11-30 22:04:30 +00:00
|
|
|
RenderSinglePass((int)RenderPass.Solid);
|
|
|
|
graphics.Shaders.World3D.EndPass();
|
2008-11-30 02:17:19 +00:00
|
|
|
|
2008-11-30 22:04:30 +00:00
|
|
|
// MASK PASS
|
2008-12-10 16:00:34 +00:00
|
|
|
world = Matrix.Identity;
|
|
|
|
ApplyMatrices3D();
|
2008-11-30 22:04:30 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, true);
|
|
|
|
graphics.Shaders.World3D.BeginPass(0);
|
|
|
|
RenderSinglePass((int)RenderPass.Mask);
|
|
|
|
graphics.Shaders.World3D.EndPass();
|
|
|
|
|
|
|
|
// ALPHA PASS
|
2008-12-10 16:00:34 +00:00
|
|
|
world = Matrix.Identity;
|
|
|
|
ApplyMatrices3D();
|
2008-11-30 22:04:30 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.ZWriteEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
|
|
|
|
graphics.Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
|
|
|
|
graphics.Shaders.World3D.BeginPass(0);
|
|
|
|
RenderSinglePass((int)RenderPass.Alpha);
|
|
|
|
graphics.Shaders.World3D.EndPass();
|
|
|
|
|
2008-12-14 23:24:40 +00:00
|
|
|
// THINGS
|
2008-12-15 22:13:24 +00:00
|
|
|
if(renderthingcages) RenderThingCages();
|
2008-12-14 23:24:40 +00:00
|
|
|
|
2008-11-30 22:04:30 +00:00
|
|
|
// ADDITIVE PASS
|
2008-12-10 16:00:34 +00:00
|
|
|
world = Matrix.Identity;
|
|
|
|
ApplyMatrices3D();
|
2008-11-30 22:04:30 +00:00
|
|
|
graphics.Device.SetRenderState(RenderState.DestinationBlend, Blend.One);
|
|
|
|
graphics.Shaders.World3D.BeginPass(0);
|
|
|
|
RenderSinglePass((int)RenderPass.Additive);
|
|
|
|
graphics.Shaders.World3D.EndPass();
|
|
|
|
|
|
|
|
// Remove references
|
|
|
|
graphics.Shaders.World3D.Texture1 = null;
|
|
|
|
|
|
|
|
// Done
|
2008-12-03 07:04:57 +00:00
|
|
|
graphics.Shaders.World3D.End();
|
2008-11-30 22:04:30 +00:00
|
|
|
geometry = null;
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2008-12-14 23:24:40 +00:00
|
|
|
// This renders all thing cages
|
|
|
|
private void RenderThingCages()
|
|
|
|
{
|
|
|
|
// Set renderstates
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.ZWriteEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
|
|
|
|
graphics.Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
|
|
|
|
graphics.Device.SetStreamSource(0, thingcage, 0, WorldVertex.Stride);
|
|
|
|
graphics.Device.SetTexture(0, General.Map.Data.ThingBox.Texture);
|
|
|
|
graphics.Shaders.World3D.Texture1 = General.Map.Data.ThingBox.Texture;
|
|
|
|
|
|
|
|
graphics.Shaders.World3D.BeginPass(0);
|
|
|
|
foreach(VisualThing t in thingsbydistance)
|
|
|
|
{
|
|
|
|
// Setup matrix
|
|
|
|
world = Matrix.Multiply(t.CageScales, t.Position);
|
|
|
|
ApplyMatrices3D();
|
|
|
|
|
|
|
|
// Setup color
|
|
|
|
graphics.Shaders.World3D.SetModulateColor(t.CageColor);
|
|
|
|
graphics.Device.SetRenderState(RenderState.TextureFactor, t.CageColor);
|
|
|
|
|
|
|
|
// Render!
|
|
|
|
graphics.Shaders.World3D.ApplySettings();
|
|
|
|
graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done
|
|
|
|
graphics.Shaders.World3D.EndPass();
|
|
|
|
graphics.Shaders.World3D.SetModulateColor(-1);
|
|
|
|
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
|
|
|
|
}
|
|
|
|
|
2008-11-30 22:04:30 +00:00
|
|
|
// This performs a single render pass
|
|
|
|
private void RenderSinglePass(int pass)
|
2008-01-05 18:11:29 +00:00
|
|
|
{
|
2008-11-30 22:04:30 +00:00
|
|
|
// Get geometry for this pass
|
2008-12-09 22:51:27 +00:00
|
|
|
Dictionary<ImageData, BinaryHeap<VisualGeometry>> geopass = geometry[pass];
|
2008-11-30 22:04:30 +00:00
|
|
|
|
|
|
|
// Render the geometry collected
|
2008-12-09 22:51:27 +00:00
|
|
|
foreach(KeyValuePair<ImageData, BinaryHeap<VisualGeometry>> group in geopass)
|
2008-11-30 02:17:19 +00:00
|
|
|
{
|
|
|
|
ImageData curtexture;
|
|
|
|
|
|
|
|
// What texture to use?
|
|
|
|
if((group.Key != null) && group.Key.IsImageLoaded && !group.Key.IsDisposed)
|
|
|
|
curtexture = group.Key;
|
|
|
|
else
|
|
|
|
curtexture = General.Map.Data.Hourglass3D;
|
|
|
|
|
|
|
|
// Create Direct3D texture if still needed
|
|
|
|
if((curtexture.Texture == null) || curtexture.Texture.Disposed)
|
|
|
|
curtexture.CreateTexture();
|
|
|
|
|
|
|
|
// Apply texture
|
|
|
|
graphics.Device.SetTexture(0, curtexture.Texture);
|
|
|
|
graphics.Shaders.World3D.Texture1 = curtexture.Texture;
|
|
|
|
graphics.Shaders.World3D.ApplySettings();
|
2008-11-30 22:04:30 +00:00
|
|
|
|
2008-11-30 02:17:19 +00:00
|
|
|
// Go for all geometry that uses this texture
|
2008-11-30 11:23:20 +00:00
|
|
|
VisualSector sector = null;
|
2008-11-30 02:17:19 +00:00
|
|
|
foreach(VisualGeometry g in group.Value)
|
|
|
|
{
|
2008-11-30 11:23:20 +00:00
|
|
|
// Changing sector?
|
|
|
|
if(!object.ReferenceEquals(g.Sector, sector))
|
2008-11-30 02:17:19 +00:00
|
|
|
{
|
2008-11-30 11:23:20 +00:00
|
|
|
// Update the sector if needed
|
|
|
|
if(g.Sector.NeedsUpdateGeo) g.Sector.Update();
|
|
|
|
|
|
|
|
// Only do this sector when a vertexbuffer is created
|
|
|
|
if(g.Sector.GeometryBuffer != null)
|
|
|
|
{
|
|
|
|
// Change current sector
|
|
|
|
sector = g.Sector;
|
|
|
|
|
|
|
|
// Set stream source
|
|
|
|
graphics.Device.SetStreamSource(0, sector.GeometryBuffer, 0, WorldVertex.Stride);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sector = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render!
|
2008-11-30 22:04:30 +00:00
|
|
|
if(sector != null)
|
|
|
|
{
|
|
|
|
graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, g.VertexOffset, g.Triangles);
|
|
|
|
}
|
2008-11-30 02:17:19 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-09 22:51:27 +00:00
|
|
|
|
|
|
|
// Get things for this pass
|
|
|
|
Dictionary<ImageData, List<VisualThing>> thingspass = things[pass];
|
|
|
|
|
|
|
|
// Render things collected
|
|
|
|
foreach(KeyValuePair<ImageData, List<VisualThing>> group in thingspass)
|
|
|
|
{
|
|
|
|
ImageData curtexture;
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 22:51:27 +00:00
|
|
|
// What texture to use?
|
|
|
|
if((group.Key != null) && group.Key.IsImageLoaded && !group.Key.IsDisposed)
|
|
|
|
curtexture = group.Key;
|
|
|
|
else
|
|
|
|
curtexture = General.Map.Data.Hourglass3D;
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 22:51:27 +00:00
|
|
|
// Create Direct3D texture if still needed
|
|
|
|
if((curtexture.Texture == null) || curtexture.Texture.Disposed)
|
|
|
|
curtexture.CreateTexture();
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 22:51:27 +00:00
|
|
|
// Apply texture
|
|
|
|
graphics.Device.SetTexture(0, curtexture.Texture);
|
|
|
|
graphics.Shaders.World3D.Texture1 = curtexture.Texture;
|
|
|
|
graphics.Shaders.World3D.ApplySettings();
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 22:51:27 +00:00
|
|
|
// Render all things with this texture
|
|
|
|
foreach(VisualThing t in group.Value)
|
|
|
|
{
|
|
|
|
// Update buffer if needed
|
2008-12-10 22:58:58 +00:00
|
|
|
t.Update();
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 22:51:27 +00:00
|
|
|
// Only do this sector when a vertexbuffer is created
|
|
|
|
if(t.GeometryBuffer != null)
|
|
|
|
{
|
|
|
|
// Create the matrix for positioning / rotation
|
2008-12-10 16:00:34 +00:00
|
|
|
world = t.Orientation;
|
|
|
|
if(t.Billboard) world = Matrix.Multiply(world, billboard);
|
|
|
|
world = Matrix.Multiply(world, t.Position);
|
|
|
|
ApplyMatrices3D();
|
2008-12-09 22:51:27 +00:00
|
|
|
graphics.Shaders.World3D.ApplySettings();
|
|
|
|
|
|
|
|
// Apply buffer
|
|
|
|
graphics.Device.SetStreamSource(0, t.GeometryBuffer, 0, WorldVertex.Stride);
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 22:51:27 +00:00
|
|
|
// Render!
|
|
|
|
graphics.Device.DrawPrimitives(PrimitiveType.TriangleList, 0, t.Triangles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
2008-11-30 22:04:30 +00:00
|
|
|
|
2008-01-05 18:11:29 +00:00
|
|
|
// This finishes rendering
|
|
|
|
public void Finish()
|
|
|
|
{
|
|
|
|
// Done
|
|
|
|
graphics.FinishRendering();
|
|
|
|
graphics.Present();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2008-12-03 07:04:57 +00:00
|
|
|
#region ================== Rendering
|
2008-01-05 18:11:29 +00:00
|
|
|
|
2008-11-30 22:04:30 +00:00
|
|
|
// This collects a visual sector's geometry for rendering
|
2008-12-09 22:51:27 +00:00
|
|
|
public void AddSectorGeometry(VisualGeometry g)
|
2008-01-05 18:11:29 +00:00
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
// Must have a texture!
|
|
|
|
if(g.Texture != null)
|
2008-01-05 18:11:29 +00:00
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
// Texture group not yet collected?
|
2008-11-30 22:04:30 +00:00
|
|
|
if(!geometry[g.RenderPassInt].ContainsKey(g.Texture))
|
2008-01-05 18:11:29 +00:00
|
|
|
{
|
2008-11-30 02:17:19 +00:00
|
|
|
// Create texture group
|
2008-11-30 22:04:30 +00:00
|
|
|
geometry[g.RenderPassInt].Add(g.Texture, new BinaryHeap<VisualGeometry>());
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
2008-12-09 22:51:27 +00:00
|
|
|
|
2008-11-30 02:17:19 +00:00
|
|
|
// Add geometry to texture group
|
2008-11-30 22:04:30 +00:00
|
|
|
geometry[g.RenderPassInt][g.Texture].Add(g);
|
2008-01-05 18:11:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-09 22:51:27 +00:00
|
|
|
// This collects a visual sector's geometry for rendering
|
|
|
|
public void AddThingGeometry(VisualThing t)
|
|
|
|
{
|
|
|
|
// Must have a texture!
|
|
|
|
if(t.Texture != null)
|
|
|
|
{
|
2008-12-14 23:24:40 +00:00
|
|
|
// Make sure the distance to camera is calculated
|
|
|
|
t.CalculateCameraDistance(cameraposition);
|
|
|
|
thingsbydistance.Add(t);
|
|
|
|
|
2008-12-09 22:51:27 +00:00
|
|
|
// Texture group not yet collected?
|
|
|
|
if(!things[t.RenderPassInt].ContainsKey(t.Texture))
|
|
|
|
{
|
|
|
|
// Create texture group
|
|
|
|
things[t.RenderPassInt].Add(t.Texture, new List<VisualThing>());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add geometry to texture group
|
|
|
|
things[t.RenderPassInt][t.Texture].Add(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 07:04:57 +00:00
|
|
|
// This renders the crosshair
|
|
|
|
public void RenderCrosshair()
|
|
|
|
{
|
|
|
|
// Set renderstates
|
|
|
|
graphics.Device.SetRenderState(RenderState.CullMode, Cull.None);
|
|
|
|
graphics.Device.SetRenderState(RenderState.ZEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
|
|
|
|
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
|
|
|
|
graphics.Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
|
|
|
|
graphics.Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
|
|
|
|
graphics.Device.SetRenderState(RenderState.TextureFactor, -1);
|
|
|
|
graphics.Device.SetTransform(TransformState.World, Matrix.Identity);
|
|
|
|
graphics.Device.SetTransform(TransformState.Projection, Matrix.Identity);
|
2008-12-04 17:59:34 +00:00
|
|
|
ApplyMatrices2D();
|
2008-12-03 07:04:57 +00:00
|
|
|
|
2008-12-06 13:20:47 +00:00
|
|
|
// Texture
|
|
|
|
if(crosshairbusy)
|
|
|
|
{
|
|
|
|
if(General.Map.Data.CrosshairBusy3D.Texture == null) General.Map.Data.CrosshairBusy3D.CreateTexture();
|
|
|
|
graphics.Device.SetTexture(0, General.Map.Data.CrosshairBusy3D.Texture);
|
|
|
|
graphics.Shaders.Display2D.Texture1 = General.Map.Data.CrosshairBusy3D.Texture;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(General.Map.Data.Crosshair3D.Texture == null) General.Map.Data.Crosshair3D.CreateTexture();
|
|
|
|
graphics.Device.SetTexture(0, General.Map.Data.Crosshair3D.Texture);
|
|
|
|
graphics.Shaders.Display2D.Texture1 = General.Map.Data.Crosshair3D.Texture;
|
|
|
|
}
|
|
|
|
|
2008-12-03 07:04:57 +00:00
|
|
|
// Draw
|
|
|
|
graphics.Shaders.Display2D.Begin();
|
|
|
|
graphics.Shaders.Display2D.SetSettings(1.0f, 1.0f, 0.0f, 1.0f, true);
|
|
|
|
graphics.Shaders.Display2D.BeginPass(1);
|
|
|
|
graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, crosshairverts);
|
|
|
|
graphics.Shaders.Display2D.EndPass();
|
|
|
|
graphics.Shaders.Display2D.End();
|
|
|
|
}
|
2008-12-04 22:35:45 +00:00
|
|
|
|
|
|
|
// This switches fog on and off
|
|
|
|
public void SetFogMode(bool usefog)
|
|
|
|
{
|
|
|
|
graphics.Device.SetRenderState(RenderState.FogEnable, usefog);
|
|
|
|
}
|
2008-12-06 13:20:47 +00:00
|
|
|
|
|
|
|
// This siwtches crosshair busy icon on and off
|
|
|
|
public void SetCrosshairBusy(bool busy)
|
|
|
|
{
|
|
|
|
crosshairbusy = busy;
|
|
|
|
}
|
2008-12-03 07:04:57 +00:00
|
|
|
|
2007-06-15 18:50:18 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|