2009-06-04 20:21:31 +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.Generic;
|
|
|
|
using System.Drawing;
|
2016-04-27 09:13:07 +00:00
|
|
|
using System.IO;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
2009-06-04 20:21:31 +00:00
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Rendering
|
|
|
|
{
|
2019-08-09 21:22:16 +00:00
|
|
|
internal class SurfaceManager : IRenderResource
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
// The true maximum lies at 65535 if I remember correctly, but that
|
|
|
|
// is a scary big number for a vertexbuffer.
|
|
|
|
private const int MAX_VERTICES_PER_BUFFER = 30000;
|
|
|
|
|
2010-08-26 21:47:25 +00:00
|
|
|
// When a sector exceeds this number of vertices, it should split up it's triangles
|
|
|
|
// This number must be a multiple of 3.
|
|
|
|
public const int MAX_VERTICES_PER_SECTOR = 6000;
|
|
|
|
|
2009-06-04 20:21:31 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// Set of buffers for a specific number of vertices per sector
|
|
|
|
private Dictionary<int, SurfaceBufferSet> sets;
|
|
|
|
|
|
|
|
// List of buffers that are locked
|
|
|
|
// This is null when not in the process of updating
|
|
|
|
private List<VertexBuffer> lockedbuffers;
|
|
|
|
|
|
|
|
// Surface to be rendered.
|
|
|
|
// 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.
|
|
|
|
// This is null when not in the process of rendering
|
|
|
|
private Dictionary<ImageData, List<SurfaceEntry>> surfaces;
|
|
|
|
|
|
|
|
// This is 1 to add the number of vertices to the offset
|
|
|
|
// (effectively rendering the ceiling vertices instead of floor vertices)
|
|
|
|
private int surfacevertexoffsetmul;
|
|
|
|
|
|
|
|
// This is set to true when the resources have been unloaded
|
|
|
|
private bool resourcesunloaded;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public SurfaceManager()
|
|
|
|
{
|
|
|
|
sets = new Dictionary<int, SurfaceBufferSet>();
|
|
|
|
lockedbuffers = new List<VertexBuffer>();
|
|
|
|
|
|
|
|
General.Map.Graphics.RegisterResource(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disposer
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if(sets != null)
|
|
|
|
{
|
|
|
|
General.Map.Graphics.UnregisterResource(this);
|
|
|
|
|
|
|
|
// Dispose all sets
|
|
|
|
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
|
|
|
|
{
|
|
|
|
// Dispose vertex buffers
|
2009-06-16 17:29:00 +00:00
|
|
|
for(int i = 0; i < set.Value.buffers.Count; i++)
|
|
|
|
{
|
|
|
|
if(set.Value.buffers[i] != null)
|
|
|
|
{
|
|
|
|
set.Value.buffers[i].Dispose();
|
|
|
|
set.Value.buffers[i] = null;
|
|
|
|
}
|
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sets = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Management
|
|
|
|
|
|
|
|
// Called when all resource must be unloaded
|
|
|
|
public void UnloadResource()
|
|
|
|
{
|
|
|
|
resourcesunloaded = true;
|
|
|
|
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
|
|
|
|
{
|
2009-06-16 17:29:00 +00:00
|
|
|
// Dispose vertex buffers
|
|
|
|
for(int i = 0; i < set.Value.buffers.Count; i++)
|
|
|
|
{
|
|
|
|
if(set.Value.buffers[i] != null)
|
|
|
|
{
|
|
|
|
set.Value.buffers[i].Dispose();
|
|
|
|
set.Value.buffers[i] = null;
|
|
|
|
}
|
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
2009-06-16 17:29:00 +00:00
|
|
|
|
|
|
|
lockedbuffers.Clear();
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when all resource must be reloaded
|
|
|
|
public void ReloadResource()
|
|
|
|
{
|
|
|
|
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
|
|
|
|
{
|
|
|
|
// Rebuild vertex buffers
|
|
|
|
for(int i = 0; i < set.Value.buffersizes.Count; i++)
|
|
|
|
{
|
|
|
|
// Make the new buffer!
|
2019-08-16 09:24:22 +00:00
|
|
|
VertexBuffer b = new VertexBuffer();
|
2019-08-16 11:07:57 +00:00
|
|
|
General.Map.Graphics.SetBufferData(b, set.Value.buffersizes[i], VertexFormat.Flat);
|
2009-06-04 20:21:31 +00:00
|
|
|
|
2019-08-16 09:24:22 +00:00
|
|
|
// Start refilling the buffer with sector geometry
|
|
|
|
foreach (SurfaceEntry e in set.Value.entries)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
|
|
|
if(e.bufferindex == i)
|
|
|
|
{
|
2019-08-16 11:23:25 +00:00
|
|
|
General.Map.Graphics.SetBufferSubdata(b, e.vertexoffset, e.floorvertices);
|
|
|
|
General.Map.Graphics.SetBufferSubdata(b, e.vertexoffset + e.floorvertices.Length, e.ceilvertices);
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to list
|
2009-06-16 17:29:00 +00:00
|
|
|
set.Value.buffers[i] = b;
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resourcesunloaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This resets all buffers and requires all sectors to get new entries
|
|
|
|
public void Reset()
|
|
|
|
{
|
|
|
|
// Clear all items
|
|
|
|
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
|
|
|
|
{
|
|
|
|
foreach(SurfaceEntry entry in set.Value.entries)
|
|
|
|
{
|
|
|
|
entry.numvertices = -1;
|
|
|
|
entry.bufferindex = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(SurfaceEntry entry in set.Value.holes)
|
|
|
|
{
|
|
|
|
entry.numvertices = -1;
|
|
|
|
entry.bufferindex = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(VertexBuffer vb in set.Value.buffers)
|
|
|
|
vb.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
// New dictionary
|
|
|
|
sets = new Dictionary<int, SurfaceBufferSet>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating sector surface geometry should go in this order;
|
|
|
|
// - Triangulate sectors
|
|
|
|
// - Call FreeSurfaces to remove entries that have changed number of vertices
|
|
|
|
// - Call AllocateBuffers
|
|
|
|
// - Call UpdateSurfaces to add/update entries
|
|
|
|
// - Call UnlockBuffers
|
|
|
|
|
|
|
|
// This (re)allocates the buffers based on an analysis of the map
|
|
|
|
// The map must be updated (triangulated) before calling this
|
|
|
|
public void AllocateBuffers()
|
|
|
|
{
|
|
|
|
// Make analysis of sector geometry
|
|
|
|
Dictionary<int, int> sectorverts = new Dictionary<int, int>();
|
|
|
|
foreach(Sector s in General.Map.Map.Sectors)
|
|
|
|
{
|
|
|
|
if(s.Triangles != null)
|
|
|
|
{
|
2010-08-26 21:47:25 +00:00
|
|
|
int numvertices = s.Triangles.Vertices.Count;
|
|
|
|
while(numvertices > 0)
|
|
|
|
{
|
|
|
|
// Determine for how many vertices in this entry
|
|
|
|
int vertsinentry = (numvertices > MAX_VERTICES_PER_SECTOR) ? MAX_VERTICES_PER_SECTOR : numvertices;
|
|
|
|
|
|
|
|
// We count the number of sectors that have specific number of vertices
|
|
|
|
if(!sectorverts.ContainsKey(vertsinentry))
|
|
|
|
sectorverts.Add(vertsinentry, 0);
|
|
|
|
sectorverts[vertsinentry]++;
|
|
|
|
|
|
|
|
numvertices -= vertsinentry;
|
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now (re)allocate the needed buffers
|
|
|
|
foreach(KeyValuePair<int, int> sv in sectorverts)
|
|
|
|
{
|
|
|
|
// Zero vertices can't be drawn
|
|
|
|
if(sv.Key > 0)
|
|
|
|
{
|
|
|
|
SurfaceBufferSet set = GetSet(sv.Key);
|
|
|
|
|
|
|
|
// Calculte how many free entries we need
|
|
|
|
int neededentries = sv.Value;
|
|
|
|
int freeentriesneeded = neededentries - set.entries.Count;
|
|
|
|
|
|
|
|
// Allocate the space needed
|
|
|
|
EnsureFreeBufferSpace(set, freeentriesneeded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This ensures there is enough space for a given number of free entries (also adds new bufers if needed)
|
|
|
|
private void EnsureFreeBufferSpace(SurfaceBufferSet set, int freeentries)
|
|
|
|
{
|
2009-06-16 17:29:00 +00:00
|
|
|
VertexBuffer vb = null;
|
2009-06-04 20:21:31 +00:00
|
|
|
|
|
|
|
// Check if we have to add entries
|
|
|
|
int addentries = freeentries - set.holes.Count;
|
|
|
|
|
|
|
|
// Begin resizing buffers starting with the last in this set
|
|
|
|
int bufferindex = set.buffers.Count - 1;
|
|
|
|
|
|
|
|
// Calculate the maximum number of entries we can put in a new buffer
|
|
|
|
// Note that verticesperentry is the number of vertices multiplied by 2, because
|
|
|
|
// we have to store both the floor and ceiling
|
|
|
|
int verticesperentry = set.numvertices * 2;
|
|
|
|
int maxentriesperbuffer = MAX_VERTICES_PER_BUFFER / verticesperentry;
|
|
|
|
|
2009-06-19 07:53:39 +00:00
|
|
|
// Make a new bufer when the last one is full
|
|
|
|
if((bufferindex > -1) && (set.buffersizes[bufferindex] >= (maxentriesperbuffer * verticesperentry)))
|
|
|
|
bufferindex = -1;
|
|
|
|
|
2009-06-04 20:21:31 +00:00
|
|
|
while(addentries > 0)
|
|
|
|
{
|
|
|
|
// Create a new buffer?
|
|
|
|
if((bufferindex == -1) || (bufferindex > (set.buffers.Count - 1)))
|
|
|
|
{
|
|
|
|
// Determine the number of entries we will be making this buffer for
|
|
|
|
int bufferentries = (addentries > maxentriesperbuffer) ? maxentriesperbuffer : addentries;
|
|
|
|
|
|
|
|
// Calculate the number of vertices that will be
|
|
|
|
int buffernumvertices = bufferentries * verticesperentry;
|
|
|
|
|
2009-06-16 17:29:00 +00:00
|
|
|
if(!resourcesunloaded)
|
|
|
|
{
|
|
|
|
// Make the new buffer!
|
2019-08-16 09:24:22 +00:00
|
|
|
vb = new VertexBuffer();
|
2019-08-16 11:07:57 +00:00
|
|
|
General.Map.Graphics.SetBufferData(vb, buffernumvertices, VertexFormat.Flat);
|
2009-06-04 20:21:31 +00:00
|
|
|
|
2009-06-16 17:29:00 +00:00
|
|
|
// Add it.
|
|
|
|
set.buffers.Add(vb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We can't make a vertexbuffer right now
|
|
|
|
set.buffers.Add(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also add available entries as holes, because they are not used yet.
|
2009-06-04 20:21:31 +00:00
|
|
|
set.buffersizes.Add(buffernumvertices);
|
|
|
|
for(int i = 0; i < bufferentries; i++)
|
|
|
|
set.holes.Add(new SurfaceEntry(set.numvertices, set.buffers.Count - 1, i * verticesperentry));
|
|
|
|
|
|
|
|
// Done
|
|
|
|
addentries -= bufferentries;
|
|
|
|
}
|
|
|
|
// Reallocate a buffer
|
|
|
|
else
|
|
|
|
{
|
2009-06-16 17:29:00 +00:00
|
|
|
if((set.buffers[bufferindex] != null) && !resourcesunloaded)
|
|
|
|
set.buffers[bufferindex].Dispose();
|
2009-06-04 20:21:31 +00:00
|
|
|
|
|
|
|
// Get the entries that are in this buffer only
|
|
|
|
List<SurfaceEntry> theseentries = new List<SurfaceEntry>();
|
|
|
|
foreach(SurfaceEntry e in set.entries)
|
|
|
|
{
|
|
|
|
if(e.bufferindex == bufferindex)
|
|
|
|
theseentries.Add(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the number of entries we will be making this buffer for
|
|
|
|
int bufferentries = ((theseentries.Count + addentries) > maxentriesperbuffer) ? maxentriesperbuffer : (theseentries.Count + addentries);
|
|
|
|
|
|
|
|
// Calculate the number of vertices that will be
|
|
|
|
int buffernumvertices = bufferentries * verticesperentry;
|
|
|
|
|
2009-06-16 17:29:00 +00:00
|
|
|
if(!resourcesunloaded)
|
|
|
|
{
|
|
|
|
// Make the new buffer and lock it
|
2019-08-16 09:24:22 +00:00
|
|
|
vb = new VertexBuffer();
|
2019-08-16 11:07:57 +00:00
|
|
|
General.Map.Graphics.SetBufferData(vb, buffernumvertices, VertexFormat.Flat);
|
2019-08-16 09:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start refilling the buffer with sector geometry
|
|
|
|
int vertexoffset = 0;
|
2009-06-04 20:21:31 +00:00
|
|
|
foreach(SurfaceEntry e in theseentries)
|
|
|
|
{
|
2009-06-16 17:29:00 +00:00
|
|
|
if(!resourcesunloaded)
|
|
|
|
{
|
2019-09-10 04:17:53 +00:00
|
|
|
// Fill buffer
|
|
|
|
General.Map.Graphics.SetBufferSubdata(vb, vertexoffset, e.floorvertices);
|
|
|
|
General.Map.Graphics.SetBufferSubdata(vb, vertexoffset + e.floorvertices.Length, e.ceilvertices);
|
2009-06-16 17:29:00 +00:00
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
|
|
|
|
// Set the new location in the buffer
|
|
|
|
e.vertexoffset = vertexoffset;
|
|
|
|
|
|
|
|
// Move on
|
|
|
|
vertexoffset += verticesperentry;
|
|
|
|
}
|
|
|
|
|
2009-06-16 17:29:00 +00:00
|
|
|
if(!resourcesunloaded)
|
|
|
|
{
|
|
|
|
set.buffers[bufferindex] = vb;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No vertex buffer at this time, sorry
|
|
|
|
set.buffers[bufferindex] = null;
|
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
|
|
|
|
// Set the new buffer and add available entries as holes, because they are not used yet.
|
|
|
|
set.buffersizes[bufferindex] = buffernumvertices;
|
|
|
|
set.holes.Clear();
|
|
|
|
for(int i = 0; i < bufferentries - theseentries.Count; i++)
|
|
|
|
set.holes.Add(new SurfaceEntry(set.numvertices, bufferindex, i * verticesperentry + vertexoffset));
|
|
|
|
|
|
|
|
// Done
|
|
|
|
addentries -= bufferentries;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always continue in next (new) buffer
|
|
|
|
bufferindex = set.buffers.Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This adds or updates sector geometry into a buffer.
|
2010-08-26 21:47:25 +00:00
|
|
|
// Modiies the list of SurfaceEntries with the new surface entry for the stored geometry.
|
|
|
|
public void UpdateSurfaces(SurfaceEntryCollection entries, SurfaceUpdate update)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
2010-08-26 21:47:25 +00:00
|
|
|
// Free entries when number of vertices has changed
|
|
|
|
if((entries.Count > 0) && (entries.totalvertices != update.numvertices))
|
|
|
|
{
|
|
|
|
FreeSurfaces(entries);
|
|
|
|
entries.Clear();
|
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
|
2010-08-26 21:47:25 +00:00
|
|
|
if((entries.Count == 0) && (update.numvertices > 0))
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
2010-08-26 21:47:25 +00:00
|
|
|
#if DEBUG
|
|
|
|
if((update.floorvertices == null) || (update.ceilvertices == null))
|
|
|
|
General.Fail("We need both floor and ceiling vertices when the number of vertices changes!");
|
|
|
|
#endif
|
2009-06-04 20:21:31 +00:00
|
|
|
|
2010-08-26 21:47:25 +00:00
|
|
|
// If we have no entries yet, we have to make them now
|
|
|
|
int vertsremaining = update.numvertices;
|
|
|
|
while(vertsremaining > 0)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
2010-08-26 21:47:25 +00:00
|
|
|
// Determine for how many vertices in this entry
|
|
|
|
int vertsinentry = (vertsremaining > MAX_VERTICES_PER_SECTOR) ? MAX_VERTICES_PER_SECTOR : vertsremaining;
|
|
|
|
|
|
|
|
// Lookup the set that holds entries for this number of vertices
|
|
|
|
SurfaceBufferSet set = GetSet(vertsinentry);
|
|
|
|
|
|
|
|
// Make sure we can get a new entry in this set
|
2009-06-04 20:21:31 +00:00
|
|
|
EnsureFreeBufferSpace(set, 1);
|
2010-08-26 21:47:25 +00:00
|
|
|
|
|
|
|
// Get a new entry in this set
|
|
|
|
SurfaceEntry e = set.holes[set.holes.Count - 1];
|
2009-06-04 20:21:31 +00:00
|
|
|
set.holes.RemoveAt(set.holes.Count - 1);
|
2010-08-26 21:47:25 +00:00
|
|
|
set.entries.Add(e);
|
|
|
|
|
|
|
|
// Fill the entry data
|
|
|
|
e.floorvertices = new FlatVertex[vertsinentry];
|
|
|
|
e.ceilvertices = new FlatVertex[vertsinentry];
|
|
|
|
Array.Copy(update.floorvertices, update.numvertices - vertsremaining, e.floorvertices, 0, vertsinentry);
|
|
|
|
Array.Copy(update.ceilvertices, update.numvertices - vertsremaining, e.ceilvertices, 0, vertsinentry);
|
|
|
|
e.floortexture = update.floortexture;
|
|
|
|
e.ceiltexture = update.ceiltexture;
|
2023-05-01 07:32:09 +00:00
|
|
|
e.hidden = update.hidden;
|
2018-04-10 17:54:57 +00:00
|
|
|
e.desaturation = update.desaturation;
|
2010-08-26 21:47:25 +00:00
|
|
|
|
|
|
|
entries.Add(e);
|
|
|
|
vertsremaining -= vertsinentry;
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
2010-08-26 21:47:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We re-use the same entries, just copy over the updated data
|
|
|
|
int vertsremaining = update.numvertices;
|
|
|
|
foreach(SurfaceEntry e in entries)
|
|
|
|
{
|
|
|
|
if(update.floorvertices != null)
|
|
|
|
{
|
|
|
|
Array.Copy(update.floorvertices, update.numvertices - vertsremaining, e.floorvertices, 0, e.numvertices);
|
|
|
|
e.floortexture = update.floortexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(update.ceilvertices != null)
|
|
|
|
{
|
|
|
|
Array.Copy(update.ceilvertices, update.numvertices - vertsremaining, e.ceilvertices, 0, e.numvertices);
|
|
|
|
e.ceiltexture = update.ceiltexture;
|
|
|
|
}
|
|
|
|
|
2023-05-01 07:32:09 +00:00
|
|
|
e.hidden = update.hidden;
|
2018-04-10 17:54:57 +00:00
|
|
|
e.desaturation = update.desaturation;
|
|
|
|
|
|
|
|
vertsremaining -= e.numvertices;
|
2010-08-26 21:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-16 17:29:00 +00:00
|
|
|
|
2010-08-26 21:47:25 +00:00
|
|
|
entries.totalvertices = update.numvertices;
|
|
|
|
|
|
|
|
// Time to update or create the buffers
|
|
|
|
foreach(SurfaceEntry e in entries)
|
|
|
|
{
|
|
|
|
SurfaceBufferSet set = GetSet(e.numvertices);
|
|
|
|
|
|
|
|
// Update bounding box
|
|
|
|
e.UpdateBBox();
|
|
|
|
|
2009-06-16 17:29:00 +00:00
|
|
|
if(!resourcesunloaded)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
2010-08-26 21:47:25 +00:00
|
|
|
VertexBuffer vb = set.buffers[e.bufferindex];
|
2019-08-16 11:23:25 +00:00
|
|
|
General.Map.Graphics.SetBufferSubdata(vb, e.vertexoffset, e.floorvertices);
|
|
|
|
General.Map.Graphics.SetBufferSubdata(vb, e.vertexoffset + e.floorvertices.Length, e.ceilvertices);
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This frees the given surface entry
|
2010-08-26 21:47:25 +00:00
|
|
|
public void FreeSurfaces(SurfaceEntryCollection entries)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
2010-08-26 21:47:25 +00:00
|
|
|
foreach(SurfaceEntry e in entries)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
2010-08-26 21:47:25 +00:00
|
|
|
if((e.numvertices > 0) && (e.bufferindex > -1))
|
|
|
|
{
|
|
|
|
SurfaceBufferSet set = sets[e.numvertices];
|
|
|
|
set.entries.Remove(e);
|
|
|
|
SurfaceEntry newentry = new SurfaceEntry(e);
|
|
|
|
set.holes.Add(newentry);
|
|
|
|
}
|
|
|
|
e.numvertices = -1;
|
|
|
|
e.bufferindex = -1;
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This unlocks the locked buffers
|
|
|
|
public void UnlockBuffers()
|
|
|
|
{
|
2009-06-16 17:29:00 +00:00
|
|
|
if(!resourcesunloaded)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
2009-06-16 17:29:00 +00:00
|
|
|
// Clear list
|
|
|
|
lockedbuffers = new List<VertexBuffer>();
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This gets or creates a set for a specific number of vertices
|
|
|
|
private SurfaceBufferSet GetSet(int numvertices)
|
|
|
|
{
|
|
|
|
SurfaceBufferSet set;
|
|
|
|
|
|
|
|
// Get or create the set
|
|
|
|
if(!sets.ContainsKey(numvertices))
|
|
|
|
{
|
|
|
|
set = new SurfaceBufferSet();
|
|
|
|
set.numvertices = numvertices;
|
|
|
|
set.buffers = new List<VertexBuffer>();
|
|
|
|
set.buffersizes = new List<int>();
|
|
|
|
set.entries = new List<SurfaceEntry>();
|
|
|
|
set.holes = new List<SurfaceEntry>();
|
|
|
|
sets.Add(numvertices, set);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set = sets[numvertices];
|
|
|
|
}
|
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Rendering
|
|
|
|
|
|
|
|
// This renders all sector floors
|
2023-05-01 07:32:09 +00:00
|
|
|
internal void RenderSectorFloors(RectangleF viewport, bool skipHidden)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
|
|
|
surfaces = new Dictionary<ImageData, List<SurfaceEntry>>();
|
|
|
|
surfacevertexoffsetmul = 0;
|
|
|
|
|
|
|
|
// Go for all surfaces as they are sorted in the buffers, so that
|
|
|
|
// they are automatically already sorted by vertexbuffer
|
|
|
|
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
|
|
|
|
{
|
|
|
|
foreach(SurfaceEntry entry in set.Value.entries)
|
2010-08-14 09:30:54 +00:00
|
|
|
{
|
2023-05-01 07:32:09 +00:00
|
|
|
if(SurfaceEntryIsVisible(entry, viewport, skipHidden))
|
2010-08-14 09:30:54 +00:00
|
|
|
AddSurfaceEntryForRendering(entry, entry.floortexture);
|
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This renders all sector ceilings
|
2023-05-01 07:32:09 +00:00
|
|
|
internal void RenderSectorCeilings(RectangleF viewport, bool skipHidden)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
|
|
|
surfaces = new Dictionary<ImageData, List<SurfaceEntry>>();
|
|
|
|
surfacevertexoffsetmul = 1;
|
|
|
|
|
|
|
|
// Go for all surfaces as they are sorted in the buffers, so that
|
|
|
|
// they are automatically already sorted by vertexbuffer
|
|
|
|
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
|
|
|
|
{
|
|
|
|
foreach(SurfaceEntry entry in set.Value.entries)
|
2010-08-14 09:30:54 +00:00
|
|
|
{
|
2023-05-01 07:32:09 +00:00
|
|
|
if(SurfaceEntryIsVisible(entry, viewport, skipHidden))
|
2010-08-14 09:30:54 +00:00
|
|
|
AddSurfaceEntryForRendering(entry, entry.ceiltexture);
|
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This renders all sector brightness levels
|
2023-05-01 07:32:09 +00:00
|
|
|
internal void RenderSectorBrightness(RectangleF viewport, bool skipHidden)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
|
|
|
surfaces = new Dictionary<ImageData, List<SurfaceEntry>>();
|
|
|
|
surfacevertexoffsetmul = 0;
|
|
|
|
|
|
|
|
// Go for all surfaces as they are sorted in the buffers, so that
|
|
|
|
// they are automatically already sorted by vertexbuffer
|
|
|
|
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
|
|
|
|
{
|
|
|
|
foreach(SurfaceEntry entry in set.Value.entries)
|
2010-08-14 09:30:54 +00:00
|
|
|
{
|
2023-05-01 07:32:09 +00:00
|
|
|
if(SurfaceEntryIsVisible(entry, viewport, skipHidden))
|
2010-08-14 09:30:54 +00:00
|
|
|
AddSurfaceEntryForRendering(entry, 0);
|
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 07:32:09 +00:00
|
|
|
// Checks to see if a particular surface entry is visible in the viewport
|
|
|
|
private bool SurfaceEntryIsVisible(SurfaceEntry entry, RectangleF viewport, bool skipHidden)
|
|
|
|
{
|
|
|
|
if (skipHidden && entry.hidden)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return entry.bbox.IntersectsWith(viewport);
|
|
|
|
}
|
|
|
|
|
2009-06-04 20:21:31 +00:00
|
|
|
// This adds a surface entry to the list of surfaces
|
|
|
|
private void AddSurfaceEntryForRendering(SurfaceEntry entry, long longimagename)
|
|
|
|
{
|
|
|
|
// Determine texture to use
|
|
|
|
ImageData img;
|
|
|
|
if(longimagename == 0)
|
|
|
|
{
|
|
|
|
img = General.Map.Data.WhiteTexture;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-23 21:51:11 +00:00
|
|
|
if(longimagename == MapSet.EmptyLongName)
|
2013-08-26 11:09:37 +00:00
|
|
|
{
|
|
|
|
img = General.Map.Data.MissingTexture3D;
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
2015-01-23 21:51:11 +00:00
|
|
|
else
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
2015-01-23 21:51:11 +00:00
|
|
|
img = General.Map.Data.GetFlatImage(longimagename);
|
|
|
|
|
2015-01-25 23:22:42 +00:00
|
|
|
if(img is UnknownImage)
|
|
|
|
{
|
|
|
|
img = General.Map.Data.UnknownTexture3D;
|
|
|
|
}
|
|
|
|
else
|
2015-01-23 21:51:11 +00:00
|
|
|
{
|
2019-12-29 19:48:11 +00:00
|
|
|
if(!img.IsImageLoaded || img.LoadFailed)
|
2015-01-23 21:51:11 +00:00
|
|
|
{
|
|
|
|
img = General.Map.Data.WhiteTexture;
|
|
|
|
}
|
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store by texture
|
2015-01-23 21:51:11 +00:00
|
|
|
if(!surfaces.ContainsKey(img)) surfaces.Add(img, new List<SurfaceEntry>());
|
2009-06-04 20:21:31 +00:00
|
|
|
surfaces[img].Add(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This renders the sorted sector surfaces
|
2019-08-09 21:22:16 +00:00
|
|
|
internal void RenderSectorSurfaces(RenderDevice graphics)
|
2009-06-04 20:21:31 +00:00
|
|
|
{
|
|
|
|
if(!resourcesunloaded)
|
|
|
|
{
|
2024-01-11 16:45:31 +00:00
|
|
|
ShaderName pass = (Renderer.FullBrightness && General.Map.Renderer2D.ViewMode != ViewMode.Brightness) ? ShaderName.display2d_fullbright : ShaderName.display2d_normal; //mxd
|
2009-06-04 20:21:31 +00:00
|
|
|
foreach(KeyValuePair<ImageData, List<SurfaceEntry>> imgsurfaces in surfaces)
|
|
|
|
{
|
2019-08-13 00:43:01 +00:00
|
|
|
graphics.SetShader(pass);
|
2019-08-17 01:21:11 +00:00
|
|
|
graphics.SetTexture(imgsurfaces.Key.Texture);
|
2009-06-04 20:21:31 +00:00
|
|
|
|
|
|
|
// Go for all surfaces
|
|
|
|
VertexBuffer lastbuffer = null;
|
|
|
|
foreach(SurfaceEntry entry in imgsurfaces.Value)
|
|
|
|
{
|
2020-05-23 08:01:52 +00:00
|
|
|
graphics.SetUniform(UniformName.desaturation, (float)entry.desaturation);
|
2018-04-10 17:54:57 +00:00
|
|
|
|
2009-06-04 20:21:31 +00:00
|
|
|
// Set the vertex buffer
|
|
|
|
SurfaceBufferSet set = sets[entry.numvertices];
|
|
|
|
if(set.buffers[entry.bufferindex] != lastbuffer)
|
|
|
|
{
|
|
|
|
lastbuffer = set.buffers[entry.bufferindex];
|
2019-08-16 11:07:57 +00:00
|
|
|
graphics.SetVertexBuffer(lastbuffer);
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw
|
2019-08-16 05:12:26 +00:00
|
|
|
graphics.Draw(PrimitiveType.TriangleList, entry.vertexoffset + (entry.numvertices * surfacevertexoffsetmul), entry.numvertices / 3);
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-14 10:36:33 +00:00
|
|
|
graphics.SetUniform(UniformName.desaturation, 0.0f);
|
2018-04-10 17:54:57 +00:00
|
|
|
}
|
2009-06-04 20:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|