mirror of
https://mirror.ghproxy.com/https://github.com/dexyfex/CodeWalker
synced 2026-05-18 09:02:05 +08:00
R26_dev8 - First public commit
This commit is contained in:
@@ -0,0 +1,372 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SharpDX.DXGI;
|
||||
using Device = SharpDX.Direct3D11.Device;
|
||||
using Buffer = SharpDX.Direct3D11.Buffer;
|
||||
using MapFlags = SharpDX.Direct3D11.MapFlags;
|
||||
using SharpDX.Direct3D11;
|
||||
using SharpDX;
|
||||
using SharpDX.Direct3D;
|
||||
|
||||
namespace CodeWalker.Rendering
|
||||
{
|
||||
public class GpuVarsBuffer<T> where T:struct //shader vars buffer helper!
|
||||
{
|
||||
public int Size;
|
||||
public Buffer Buffer;
|
||||
public T Vars;
|
||||
public bool Flag;//for external use
|
||||
public GpuVarsBuffer(Device device)
|
||||
{
|
||||
Size = System.Runtime.InteropServices.Marshal.SizeOf<T>();// (sizeof(T));
|
||||
Buffer = new Buffer(device, Size, ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0);// //DXUtility::CreateShaderVarsBuffer(Size, name);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (Buffer != null)
|
||||
{
|
||||
Buffer.Dispose();
|
||||
Buffer = null;
|
||||
}
|
||||
}
|
||||
public void Update(DeviceContext context)
|
||||
{
|
||||
var dataBox = context.MapSubresource(Buffer, 0, MapMode.WriteDiscard, MapFlags.None);
|
||||
Utilities.Write(dataBox.DataPointer, ref Vars);
|
||||
context.UnmapSubresource(Buffer, 0);
|
||||
}
|
||||
public void SetVSCBuffer(DeviceContext context, int slot)
|
||||
{
|
||||
context.VertexShader.SetConstantBuffer(slot, Buffer);
|
||||
}
|
||||
public void SetPSCBuffer(DeviceContext context, int slot)
|
||||
{
|
||||
context.PixelShader.SetConstantBuffer(slot, Buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public class GpuSBuffer<T> where T : struct //for static struct data as resource view
|
||||
{
|
||||
public int StructSize;
|
||||
public int StructCount;
|
||||
public int BufferSize;
|
||||
public Buffer Buffer;
|
||||
public ShaderResourceView SRV;
|
||||
public GpuSBuffer(Device device, T[] data)
|
||||
{
|
||||
StructCount = data.Length;
|
||||
StructSize = System.Runtime.InteropServices.Marshal.SizeOf<T>();// (sizeof(T));
|
||||
BufferSize = StructCount * StructSize;
|
||||
//var ds = new DataStream(new DataPointer()
|
||||
Buffer = Buffer.Create<T>(device, BindFlags.ShaderResource, data, BufferSize, ResourceUsage.Default, CpuAccessFlags.None, ResourceOptionFlags.BufferStructured, StructSize);
|
||||
//Buffer = new Buffer(device, BufferSize, ResourceUsage.Default, BindFlags.ShaderResource, CpuAccessFlags.None, ResourceOptionFlags.BufferStructured, StructSize);
|
||||
SRV = DXUtility.CreateShaderResourceView(device, Buffer, SharpDX.DXGI.Format.Unknown, SharpDX.Direct3D.ShaderResourceViewDimension.Buffer, 0, 0, StructCount, 0);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (SRV != null)
|
||||
{
|
||||
SRV.Dispose();
|
||||
SRV = null;
|
||||
}
|
||||
if (Buffer != null)
|
||||
{
|
||||
Buffer.Dispose();
|
||||
Buffer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class GpuCBuffer<T> where T : struct //Dynamic GPU buffer of items updated by CPU
|
||||
{
|
||||
public int StructSize;
|
||||
public int StructCount;
|
||||
public int BufferSize;
|
||||
public int CurrentCount;
|
||||
public Buffer Buffer;
|
||||
public ShaderResourceView SRV;
|
||||
public List<T> Data;
|
||||
public T[] DataArray;
|
||||
|
||||
public GpuCBuffer(Device device, int count)
|
||||
{
|
||||
StructCount = count;
|
||||
StructSize = System.Runtime.InteropServices.Marshal.SizeOf<T>();// (sizeof(T));
|
||||
BufferSize = StructCount * StructSize;
|
||||
//Buffer = Buffer.Create<T>(device, BindFlags.ShaderResource, null, BufferSize, ResourceUsage.Dynamic, CpuAccessFlags.Write, ResourceOptionFlags.BufferStructured, StructSize);
|
||||
Buffer = new Buffer(device, BufferSize, ResourceUsage.Dynamic, BindFlags.ShaderResource, CpuAccessFlags.Write, ResourceOptionFlags.BufferStructured, StructSize);
|
||||
SRV = DXUtility.CreateShaderResourceView(device, Buffer, SharpDX.DXGI.Format.Unknown, SharpDX.Direct3D.ShaderResourceViewDimension.Buffer, 0, 0, StructCount, 0);
|
||||
Data = new List<T>(count);
|
||||
DataArray = new T[count];
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (SRV != null)
|
||||
{
|
||||
SRV.Dispose();
|
||||
SRV = null;
|
||||
}
|
||||
if (Buffer != null)
|
||||
{
|
||||
Buffer.Dispose();
|
||||
Buffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Data.Clear();
|
||||
CurrentCount = 0;
|
||||
}
|
||||
public bool Add(T item)
|
||||
{
|
||||
if (CurrentCount < StructCount)
|
||||
{
|
||||
Data.Add(item);
|
||||
CurrentCount++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Update(DeviceContext context)
|
||||
{
|
||||
for (int i = 0; i < CurrentCount; i++)
|
||||
{
|
||||
DataArray[i] = Data[i];
|
||||
}
|
||||
var dataBox = context.MapSubresource(Buffer, 0, MapMode.WriteDiscard, MapFlags.None);
|
||||
Utilities.Write(dataBox.DataPointer, DataArray, 0, CurrentCount);
|
||||
context.UnmapSubresource(Buffer, 0);
|
||||
}
|
||||
|
||||
public void SetVSResource(DeviceContext context, int slot)
|
||||
{
|
||||
context.VertexShader.SetShaderResource(slot, SRV);
|
||||
}
|
||||
public void SetPSResource(DeviceContext context, int slot)
|
||||
{
|
||||
context.PixelShader.SetShaderResource(slot, SRV);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public class GpuBuffer<T> where T : struct //Dynamic GPU buffer of items updated by compute shader
|
||||
{
|
||||
public int StructSize;
|
||||
public int StructCount;
|
||||
public int ItemTotalSize;
|
||||
public int ItemCount;
|
||||
public int Size;
|
||||
public Buffer Buffer;
|
||||
public ShaderResourceView SRV;
|
||||
public UnorderedAccessView UAV;
|
||||
|
||||
public GpuBuffer(Device device, int itemSize, int itemCount)
|
||||
{
|
||||
StructSize = System.Runtime.InteropServices.Marshal.SizeOf<T>();// sizeof(T)),
|
||||
StructCount = itemCount * itemSize;
|
||||
ItemTotalSize = itemSize * StructSize;
|
||||
ItemCount = itemCount;
|
||||
Size = StructSize * itemSize * itemCount;
|
||||
Buffer = DXUtility.CreateBuffer(device, Size, ResourceUsage.Default, BindFlags.ShaderResource | BindFlags.UnorderedAccess, 0, ResourceOptionFlags.BufferStructured, StructSize);
|
||||
SRV = DXUtility.CreateShaderResourceView(device, Buffer, Format.Unknown, ShaderResourceViewDimension.Buffer, 0, 0, StructCount, 0);
|
||||
UAV = DXUtility.CreateUnorderedAccessView(device, Buffer, Format.Unknown, UnorderedAccessViewDimension.Buffer, 0, StructCount, 0, 0);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (UAV != null)
|
||||
{
|
||||
UAV.Dispose();
|
||||
UAV = null;
|
||||
}
|
||||
if (SRV != null)
|
||||
{
|
||||
SRV.Dispose();
|
||||
SRV = null;
|
||||
}
|
||||
if (Buffer != null)
|
||||
{
|
||||
Buffer.Dispose();
|
||||
Buffer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class GpuTexture //texture and render targets (depth, MS).
|
||||
{
|
||||
public Texture2D Texture;
|
||||
public Texture2D TextureMS;
|
||||
public Texture2D Depth;
|
||||
public Texture2D DepthMS;
|
||||
public RenderTargetView RTV;
|
||||
public DepthStencilView DSV;
|
||||
public RenderTargetView MSRTV;
|
||||
public DepthStencilView MSDSV;
|
||||
public ShaderResourceView SRV;
|
||||
public int VramUsage;
|
||||
public bool Multisampled;
|
||||
public bool UseDepth;
|
||||
|
||||
public void Init(Device device, int w, int h, Format f, int sc, int sq, bool depth, Format df)
|
||||
{
|
||||
VramUsage = 0;
|
||||
Multisampled = (sc > 1);
|
||||
UseDepth = depth;
|
||||
ResourceUsage u = ResourceUsage.Default;
|
||||
BindFlags b = BindFlags.RenderTarget | BindFlags.ShaderResource;
|
||||
RenderTargetViewDimension rtvd = RenderTargetViewDimension.Texture2D;
|
||||
ShaderResourceViewDimension srvd = ShaderResourceViewDimension.Texture2D;// D3D11_SRV_DIMENSION_TEXTURE2D;
|
||||
int fs = DXUtility.ElementSize(f);
|
||||
int wh = w * h;
|
||||
BindFlags db = BindFlags.DepthStencil;// D3D11_BIND_DEPTH_STENCIL;
|
||||
DepthStencilViewDimension dsvd = DepthStencilViewDimension.Texture2D;
|
||||
|
||||
Texture = DXUtility.CreateTexture2D(device, w, h, 1, 1, f, 1, 0, u, b, 0, 0);
|
||||
RTV = DXUtility.CreateRenderTargetView(device, Texture, f, rtvd, 0, 0, 0);
|
||||
SRV = DXUtility.CreateShaderResourceView(device, Texture, f, srvd, 1, 0, 0, 0);
|
||||
VramUsage += (wh * fs);
|
||||
|
||||
if (Multisampled)
|
||||
{
|
||||
b = BindFlags.RenderTarget;
|
||||
rtvd = RenderTargetViewDimension.Texture2DMultisampled;
|
||||
dsvd = DepthStencilViewDimension.Texture2DMultisampled;
|
||||
|
||||
TextureMS = DXUtility.CreateTexture2D(device, w, h, 1, 1, f, sc, sq, u, b, 0, 0);
|
||||
MSRTV = DXUtility.CreateRenderTargetView(device, TextureMS, f, rtvd, 0, 0, 0);
|
||||
VramUsage += (wh * fs);
|
||||
|
||||
if (depth)
|
||||
{
|
||||
DepthMS = DXUtility.CreateTexture2D(device, w, h, 1, 1, df, sc, sq, u, db, 0, 0);
|
||||
MSDSV = DXUtility.CreateDepthStencilView(device, DepthMS, df, dsvd);
|
||||
VramUsage += (wh * DXUtility.ElementSize(df));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (depth)
|
||||
{
|
||||
Depth = DXUtility.CreateTexture2D(device, w, h, 1, 1, df, sc, sq, u, db, 0, 0);
|
||||
DSV = DXUtility.CreateDepthStencilView(device, Depth, df, dsvd);
|
||||
VramUsage += (wh * DXUtility.ElementSize(df));
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (SRV != null)
|
||||
{
|
||||
SRV.Dispose();
|
||||
SRV = null;
|
||||
}
|
||||
if (MSDSV != null)
|
||||
{
|
||||
MSDSV.Dispose();
|
||||
MSDSV = null;
|
||||
}
|
||||
if (MSRTV != null)
|
||||
{
|
||||
MSRTV.Dispose();
|
||||
MSRTV = null;
|
||||
}
|
||||
if (DSV != null)
|
||||
{
|
||||
DSV.Dispose();
|
||||
DSV = null;
|
||||
}
|
||||
if (RTV != null)
|
||||
{
|
||||
RTV.Dispose();
|
||||
RTV = null;
|
||||
}
|
||||
if (DepthMS != null)
|
||||
{
|
||||
DepthMS.Dispose();
|
||||
DepthMS = null;
|
||||
}
|
||||
if (Depth != null)
|
||||
{
|
||||
Depth.Dispose();
|
||||
Depth = null;
|
||||
}
|
||||
if (TextureMS != null)
|
||||
{
|
||||
TextureMS.Dispose();
|
||||
TextureMS = null;
|
||||
}
|
||||
if (Texture != null)
|
||||
{
|
||||
Texture.Dispose();
|
||||
Texture = null;
|
||||
}
|
||||
}
|
||||
public GpuTexture(Device device, int w, int h, Format f, int sc, int sq, bool depth, Format df)
|
||||
{
|
||||
Init(device, w, h, f, sc, sq, depth, df);
|
||||
}
|
||||
public GpuTexture(Device device, int w, int h, Format f, int sc, int sq)
|
||||
{
|
||||
Init(device, w, h, f, sc, sq, false, Format.Unknown);
|
||||
}
|
||||
public GpuTexture(Device device, int w, int h, Format f)
|
||||
{
|
||||
Init(device, w, h, f, 1, 0, false, Format.Unknown);
|
||||
}
|
||||
|
||||
public void Clear(DeviceContext context, Color4 colour)
|
||||
{
|
||||
if (Multisampled)
|
||||
{
|
||||
context.ClearRenderTargetView(MSRTV, colour);
|
||||
if (UseDepth)
|
||||
{
|
||||
context.ClearDepthStencilView(MSDSV, DepthStencilClearFlags.Depth, 1.0f, 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
context.ClearRenderTargetView(RTV, colour);
|
||||
if (UseDepth)
|
||||
{
|
||||
context.ClearDepthStencilView(DSV, DepthStencilClearFlags.Depth, 1.0f, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearDepth(DeviceContext context)
|
||||
{
|
||||
if (!UseDepth) return;
|
||||
if (Multisampled)
|
||||
{
|
||||
context.ClearDepthStencilView(MSDSV, DepthStencilClearFlags.Depth, 1.0f, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.ClearDepthStencilView(DSV, DepthStencilClearFlags.Depth, 1.0f, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetRenderTarget(DeviceContext context)
|
||||
{
|
||||
if (Multisampled)
|
||||
{
|
||||
context.OutputMerger.SetRenderTargets(UseDepth ? MSDSV : null, MSRTV);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.OutputMerger.SetRenderTargets(UseDepth ? DSV : null, RTV);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using SharpDX;
|
||||
using SharpDX.Direct3D11;
|
||||
using SharpDX.Mathematics.Interop;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CodeWalker.Rendering
|
||||
{
|
||||
public class RenderTargetSwitch
|
||||
{
|
||||
|
||||
RenderTargetView[] OrigRenderTargetViewArr;
|
||||
DepthStencilView OrigDepthStencilView;
|
||||
RasterizerState OrigRasterizerState;
|
||||
RawViewportF[] OrigViewports;
|
||||
bool IsReset = true;
|
||||
//bool ResetOnDestroy = false;
|
||||
int RenderTargetCount = 1;
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (OrigRenderTargetViewArr != null)
|
||||
{
|
||||
for (int i = 0; i < RenderTargetCount; i++)
|
||||
{
|
||||
if (OrigRenderTargetViewArr[i] != null)
|
||||
{
|
||||
OrigRenderTargetViewArr[i].Dispose();
|
||||
}
|
||||
}
|
||||
OrigRenderTargetViewArr = null;
|
||||
}
|
||||
if (OrigDepthStencilView != null)
|
||||
{
|
||||
OrigDepthStencilView.Dispose();
|
||||
OrigDepthStencilView = null;
|
||||
}
|
||||
if (OrigRasterizerState != null)
|
||||
{
|
||||
OrigRasterizerState.Dispose();
|
||||
OrigRasterizerState = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Set(DeviceContext context)
|
||||
{
|
||||
Dispose();
|
||||
|
||||
OrigRenderTargetViewArr = context.OutputMerger.GetRenderTargets(RenderTargetCount, out OrigDepthStencilView);
|
||||
OrigViewports = context.Rasterizer.GetViewports<RawViewportF>();
|
||||
OrigRasterizerState = context.Rasterizer.State;
|
||||
|
||||
//OrigRenderTargetViewArr = new RenderTargetView[RenderTargetCount];
|
||||
//uint origNumViewports = 1;
|
||||
//dc->OMGetRenderTargets(RenderTargetCount, OrigRenderTargetViewArr, &OrigDepthStencilView);
|
||||
//dc->RSGetViewports(&origNumViewports, &OrigViewport);
|
||||
//dc->RSGetState(&OrigRasterizerState);
|
||||
|
||||
IsReset = false;
|
||||
}
|
||||
public void Reset(DeviceContext context)
|
||||
{
|
||||
if (IsReset) return;
|
||||
|
||||
context.OutputMerger.SetRenderTargets(OrigDepthStencilView, OrigRenderTargetViewArr);
|
||||
context.Rasterizer.State = OrigRasterizerState;
|
||||
context.Rasterizer.SetViewports(OrigViewports);
|
||||
|
||||
//auto dc = DXManager::GetDeviceContext();
|
||||
//dc->OMSetRenderTargets(RenderTargetCount, OrigRenderTargetViewArr, OrigDepthStencilView);
|
||||
//dc->RSSetState(OrigRasterizerState);
|
||||
//dc->RSSetViewports(1, &OrigViewport);
|
||||
|
||||
IsReset = true;
|
||||
|
||||
Dispose();
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,268 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SharpDX;
|
||||
using SharpDX.Direct3D;
|
||||
using SharpDX.Direct3D11;
|
||||
using Device = SharpDX.Direct3D11.Device;
|
||||
using Buffer = SharpDX.Direct3D11.Buffer;
|
||||
using SharpDX.DXGI;
|
||||
|
||||
namespace CodeWalker.Rendering
|
||||
{
|
||||
public class UnitCapsule
|
||||
{
|
||||
private Buffer VertexBuffer { get; set; }
|
||||
private Buffer IndexBuffer { get; set; }
|
||||
private InputLayout InputLayout { get; set; }
|
||||
private VertexBufferBinding vbbinding;
|
||||
private int indexcount;
|
||||
|
||||
private struct SphTri
|
||||
{
|
||||
public int v1;
|
||||
public int v2;
|
||||
public int v3;
|
||||
public SphTri(int i1, int i2, int i3)
|
||||
{
|
||||
v1 = i1;
|
||||
v2 = i2;
|
||||
v3 = i3;
|
||||
}
|
||||
}
|
||||
|
||||
public UnitCapsule(Device device, byte[] vsbytes, int detail)
|
||||
{
|
||||
|
||||
InputLayout = new InputLayout(device, vsbytes, new[]
|
||||
{
|
||||
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
|
||||
//new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0),
|
||||
});
|
||||
|
||||
|
||||
|
||||
List<Vector4> verts = new List<Vector4>();
|
||||
Dictionary<Vector4, int> vdict = new Dictionary<Vector4, int>();
|
||||
List<SphTri> curtris = new List<SphTri>();
|
||||
//List<SphTri> nxttris = new List<SphTri>();
|
||||
|
||||
verts.Add(new Vector4(0.0f, -1.0f, 0.0f, 0.0f));//top end
|
||||
verts.Add(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));//bottom end
|
||||
|
||||
//detail = nlats each hemisphere
|
||||
//nlons = detail*4
|
||||
|
||||
int nlats = detail;
|
||||
int nlons = detail * 4;
|
||||
int firstlat = 1 - nlats;
|
||||
int lastlat = nlats-1;
|
||||
int lastlon = nlons - 1;
|
||||
int vertsperlon = 2 + (nlats - 1) * 2;
|
||||
float latrng = 1.0f / (detail);
|
||||
float lonrng = 1.0f / (nlons);
|
||||
float halfpi = (float)(0.5 * Math.PI);
|
||||
float twopi = (float)(2.0 * Math.PI);
|
||||
|
||||
for (int lon = 0; lon < nlons; lon++)
|
||||
{
|
||||
float tlon = lon * lonrng;
|
||||
float rlon = tlon * twopi;
|
||||
float lonx = (float)Math.Sin(rlon);
|
||||
float lonz = (float)Math.Cos(rlon);
|
||||
for (int lat = firstlat; lat < nlats; lat++)
|
||||
{
|
||||
float tlat = lat * latrng;
|
||||
float rlat = tlat * halfpi;
|
||||
float laty = (float)Math.Sin(rlat);
|
||||
float latxz = (float)Math.Cos(rlat);
|
||||
float hemi = (lat > 0) ? 1.0f : 0.0f;
|
||||
|
||||
verts.Add(new Vector4(lonx * latxz, laty, lonz * latxz, hemi));
|
||||
|
||||
if (lat == 0)
|
||||
{
|
||||
verts.Add(new Vector4(lonx * latxz, laty, lonz * latxz, 1.0f)); //split at the "equator"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int lon = 0; lon < nlons; lon++)
|
||||
{
|
||||
int i0 = 2 + lon * vertsperlon;//top row
|
||||
int i1 = i0 + vertsperlon;
|
||||
int i2 = i1 - 1;//bottom row
|
||||
int i3 = i2 + vertsperlon;
|
||||
|
||||
if (lon == lastlon)
|
||||
{
|
||||
i1 = 2;
|
||||
i3 = 1 + vertsperlon;
|
||||
}
|
||||
|
||||
curtris.Add(new SphTri(0, i1, i0)); //top cap triangles
|
||||
|
||||
for (int lat = firstlat; lat <= lastlat; lat++)
|
||||
{
|
||||
int offs = lat - firstlat;
|
||||
int f1 = i0 + offs;
|
||||
int f2 = f1 + vertsperlon;
|
||||
int f3 = f1 + 1;
|
||||
if (lon == lastlon)
|
||||
{
|
||||
f2 = 2 + offs;
|
||||
}
|
||||
int f4 = f2 + 1;
|
||||
curtris.Add(new SphTri(f1, f2, f3));
|
||||
curtris.Add(new SphTri(f3, f2, f4)); //fill the rest
|
||||
}
|
||||
|
||||
curtris.Add(new SphTri(1, i2, i3)); //bottom cap triangles
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#region cube version (unfinished)
|
||||
/* cube version
|
||||
|
||||
verts.Add(new Vector4(-1.0f, 0.0f, 0.0f, 0.0f));
|
||||
verts.Add(new Vector4(1.0f, 0.0f, 0.0f, 0.0f));
|
||||
verts.Add(new Vector4(0.0f, -1.0f, 0.0f, 0.0f));
|
||||
verts.Add(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));//bottom end
|
||||
verts.Add(new Vector4(0.0f, 0.0f, -1.0f, 0.0f));
|
||||
verts.Add(new Vector4(0.0f, 0.0f, 1.0f, 0.0f));
|
||||
|
||||
verts.Add(new Vector4(-1.0f, 0.0f, 0.0f, 1.0f));//0==6 - bottom equator split
|
||||
verts.Add(new Vector4(1.0f, 0.0f, 0.0f, 1.0f));//1==7
|
||||
verts.Add(new Vector4(0.0f, 0.0f, -1.0f, 1.0f));//4==8
|
||||
verts.Add(new Vector4(0.0f, 0.0f, 1.0f, 1.0f));//5==9
|
||||
|
||||
curtris.Add(new SphTri(0, 4, 2));
|
||||
curtris.Add(new SphTri(4, 1, 2));
|
||||
curtris.Add(new SphTri(1, 5, 2));
|
||||
curtris.Add(new SphTri(5, 0, 2));
|
||||
|
||||
curtris.Add(new SphTri(8, 6, 3));//split halves - Y axis
|
||||
curtris.Add(new SphTri(7, 8, 3));
|
||||
curtris.Add(new SphTri(9, 7, 3));
|
||||
curtris.Add(new SphTri(6, 9, 3));
|
||||
|
||||
for (int i = 0; i < verts.Count; i++)
|
||||
{
|
||||
vdict[verts[i]] = i;
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < detail; i++)
|
||||
{
|
||||
nxttris.Clear();
|
||||
foreach (var tri in curtris)
|
||||
{
|
||||
Vector4 v1 = verts[tri.v1];
|
||||
Vector4 v2 = verts[tri.v2];
|
||||
Vector4 v3 = verts[tri.v3];
|
||||
Vector4 s1 = new Vector4(Vector3.Normalize((v1 + v2).XYZ()), v1.W);
|
||||
Vector4 s2 = new Vector4(Vector3.Normalize((v2 + v3).XYZ()), v1.W);
|
||||
Vector4 s3 = new Vector4(Vector3.Normalize((v3 + v1).XYZ()), v1.W);
|
||||
int i1, i2, i3;
|
||||
if (!vdict.TryGetValue(s1, out i1))
|
||||
{
|
||||
i1 = verts.Count;
|
||||
verts.Add(s1);
|
||||
vdict[s1] = i1;
|
||||
}
|
||||
if (!vdict.TryGetValue(s2, out i2))
|
||||
{
|
||||
i2 = verts.Count;
|
||||
verts.Add(s2);
|
||||
vdict[s2] = i2;
|
||||
}
|
||||
if (!vdict.TryGetValue(s3, out i3))
|
||||
{
|
||||
i3 = verts.Count;
|
||||
verts.Add(s3);
|
||||
vdict[s3] = i3;
|
||||
}
|
||||
nxttris.Add(new SphTri(tri.v1, i1, i3));
|
||||
nxttris.Add(new SphTri(tri.v2, i2, i1));
|
||||
nxttris.Add(new SphTri(tri.v3, i3, i2));
|
||||
nxttris.Add(new SphTri(i1, i2, i3));
|
||||
}
|
||||
var cur = curtris;
|
||||
curtris = nxttris;
|
||||
nxttris = cur;
|
||||
}
|
||||
*/
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
List<uint> idata = new List<uint>();
|
||||
foreach (var tri in curtris)
|
||||
{
|
||||
idata.Add((uint)tri.v1);
|
||||
idata.Add((uint)tri.v2);
|
||||
idata.Add((uint)tri.v3);
|
||||
}
|
||||
|
||||
|
||||
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, verts.ToArray());
|
||||
vbbinding = new VertexBufferBinding(VertexBuffer, 16, 0);
|
||||
|
||||
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, idata.ToArray());
|
||||
indexcount = idata.Count;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void Draw(DeviceContext context)
|
||||
{
|
||||
context.InputAssembler.InputLayout = InputLayout;
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
|
||||
context.DrawIndexed(indexcount, 0, 0);
|
||||
}
|
||||
|
||||
public void DrawInstanced(DeviceContext context, int count)
|
||||
{
|
||||
context.InputAssembler.InputLayout = InputLayout;
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
|
||||
context.DrawIndexedInstanced(indexcount, count, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (VertexBuffer != null)
|
||||
{
|
||||
VertexBuffer.Dispose();
|
||||
VertexBuffer = null;
|
||||
}
|
||||
if (IndexBuffer != null)
|
||||
{
|
||||
IndexBuffer.Dispose();
|
||||
IndexBuffer = null;
|
||||
}
|
||||
if (InputLayout != null)
|
||||
{
|
||||
InputLayout.Dispose();
|
||||
InputLayout = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SharpDX;
|
||||
using SharpDX.Direct3D;
|
||||
using SharpDX.Direct3D11;
|
||||
using Device = SharpDX.Direct3D11.Device;
|
||||
using Buffer = SharpDX.Direct3D11.Buffer;
|
||||
using SharpDX.DXGI;
|
||||
|
||||
namespace CodeWalker.Rendering
|
||||
{
|
||||
public class UnitCube
|
||||
{
|
||||
private Buffer VertexBuffer { get; set; }
|
||||
private Buffer IndexBuffer { get; set; }
|
||||
private InputLayout InputLayout { get; set; }
|
||||
private VertexBufferBinding vbbinding;
|
||||
private bool issigned;
|
||||
private bool islines;
|
||||
private bool isnormals;
|
||||
private int indexcount;
|
||||
|
||||
public UnitCube(Device device, byte[] vsbytes, bool signed, bool lines, bool normals)
|
||||
{
|
||||
issigned = signed;
|
||||
islines = lines;
|
||||
isnormals = normals;
|
||||
|
||||
if (normals)
|
||||
{
|
||||
InputLayout = new InputLayout(device, vsbytes, new[]
|
||||
{
|
||||
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
|
||||
new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0),
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
InputLayout = new InputLayout(device, vsbytes, new[]
|
||||
{
|
||||
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (signed)
|
||||
{
|
||||
if (normals)
|
||||
{
|
||||
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, new[]
|
||||
{
|
||||
//position (x4), normal (x3)
|
||||
//-Z face
|
||||
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f,0.0f,-1.0f,0.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f,0.0f,-1.0f,0.0f,
|
||||
-1.0f, 1.0f, -1.0f, 1.0f, 0.0f,0.0f,-1.0f,0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f,0.0f,-1.0f,0.0f,
|
||||
//+Z face
|
||||
-1.0f, -1.0f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f,
|
||||
-1.0f, 1.0f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f,
|
||||
//-Y face
|
||||
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f,-1.0f,0.0f,0.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f,-1.0f,0.0f,0.0f,
|
||||
-1.0f, -1.0f, 1.0f, 1.0f, 0.0f,-1.0f,0.0f,0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f,-1.0f,0.0f,0.0f,
|
||||
//+Y face
|
||||
-1.0f, 1.0f, -1.0f, 1.0f, 0.0f,1.0f,0.0f,0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f,1.0f,0.0f,0.0f,
|
||||
-1.0f, 1.0f, 1.0f, 1.0f, 0.0f,1.0f,0.0f,0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,1.0f,0.0f,0.0f,
|
||||
//-X face
|
||||
-1.0f, -1.0f, -1.0f, 1.0f, -1.0f,0.0f,0.0f,0.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f,0.0f,0.0f,0.0f,
|
||||
-1.0f, -1.0f, 1.0f, 1.0f, -1.0f,0.0f,0.0f,0.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f,0.0f,0.0f,0.0f,
|
||||
//+X face
|
||||
1.0f, -1.0f, -1.0f, 1.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,0.0f,0.0f,0.0f,
|
||||
1.0f, -1.0f, 1.0f, 1.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,0.0f,0.0f,0.0f,
|
||||
});
|
||||
vbbinding = new VertexBufferBinding(VertexBuffer, 32, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, new[]
|
||||
{
|
||||
//position (x4)
|
||||
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
|
||||
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f,
|
||||
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
|
||||
});
|
||||
vbbinding = new VertexBufferBinding(VertexBuffer, 16, 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (normals)
|
||||
{
|
||||
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, new[]
|
||||
{
|
||||
//position (x4), normal (x3)
|
||||
//-Z face
|
||||
0.0f, 0.0f, 0.0f, 1.0f, 0.0f,0.0f,-1.0f,0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,0.0f,-1.0f,0.0f,
|
||||
0.0f, 1.0f, 0.0f, 1.0f, 0.0f,0.0f,-1.0f,0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,0.0f,-1.0f,0.0f,
|
||||
//+Z face
|
||||
0.0f, 0.0f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f,
|
||||
0.0f, 1.0f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f,
|
||||
//-Y face
|
||||
0.0f, 0.0f, 0.0f, 1.0f, 0.0f,-1.0f,0.0f,0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,-1.0f,0.0f,0.0f,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, 0.0f,-1.0f,0.0f,0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,-1.0f,0.0f,0.0f,
|
||||
//+Y face
|
||||
0.0f, 1.0f, 0.0f, 1.0f, 0.0f,1.0f,0.0f,0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,1.0f,0.0f,0.0f,
|
||||
0.0f, 1.0f, 1.0f, 1.0f, 0.0f,1.0f,0.0f,0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,1.0f,0.0f,0.0f,
|
||||
//-X face
|
||||
0.0f, 0.0f, 0.0f, 1.0f, -1.0f,0.0f,0.0f,0.0f, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f,0.0f,0.0f,0.0f,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, -1.0f,0.0f,0.0f,0.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f,0.0f,0.0f,0.0f,
|
||||
//+X face
|
||||
1.0f, 0.0f, 0.0f, 1.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,0.0f,0.0f,0.0f,
|
||||
1.0f, 0.0f, 1.0f, 1.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,0.0f,0.0f,0.0f,
|
||||
});
|
||||
vbbinding = new VertexBufferBinding(VertexBuffer, 32, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, new[]
|
||||
{
|
||||
//position (x4)
|
||||
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
|
||||
0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
|
||||
0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
|
||||
});
|
||||
vbbinding = new VertexBufferBinding(VertexBuffer, 16, 0);
|
||||
}
|
||||
}
|
||||
if (lines)
|
||||
{
|
||||
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, new uint[]
|
||||
{
|
||||
0,1,1,3,3,2,2,0,
|
||||
4,5,5,7,7,6,6,4,
|
||||
0,4,1,5,3,7,2,6,
|
||||
});
|
||||
indexcount = 24;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (normals)
|
||||
{
|
||||
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, new uint[]
|
||||
{
|
||||
0,2,1,1,2,3,
|
||||
4,5,6,5,7,6,
|
||||
8,9,10,9,11,10,
|
||||
12,14,13,13,14,15,
|
||||
16,18,17,17,18,19,
|
||||
20,21,22,21,23,22
|
||||
});
|
||||
indexcount = 36;
|
||||
}
|
||||
else
|
||||
{
|
||||
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, new uint[]
|
||||
{
|
||||
0,2,1,1,2,3,
|
||||
4,5,6,5,7,6,
|
||||
//todo: other faces
|
||||
});
|
||||
indexcount = 12; //36..
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void Draw(DeviceContext context)
|
||||
{
|
||||
context.InputAssembler.InputLayout = InputLayout;
|
||||
|
||||
if (islines)
|
||||
{
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
|
||||
}
|
||||
else
|
||||
{
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
}
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, SharpDX.DXGI.Format.R32_UInt, 0);
|
||||
|
||||
context.DrawIndexed(indexcount, 0, 0);
|
||||
}
|
||||
|
||||
public void DrawInstanced(DeviceContext context, int count)
|
||||
{
|
||||
context.InputAssembler.InputLayout = InputLayout;
|
||||
|
||||
if (islines)
|
||||
{
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
|
||||
}
|
||||
else
|
||||
{
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
}
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, SharpDX.DXGI.Format.R32_UInt, 0);
|
||||
|
||||
context.DrawIndexedInstanced(indexcount, count, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (VertexBuffer != null)
|
||||
{
|
||||
VertexBuffer.Dispose();
|
||||
VertexBuffer = null;
|
||||
}
|
||||
if (IndexBuffer != null)
|
||||
{
|
||||
IndexBuffer.Dispose();
|
||||
IndexBuffer = null;
|
||||
}
|
||||
if (InputLayout != null)
|
||||
{
|
||||
InputLayout.Dispose();
|
||||
InputLayout = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SharpDX;
|
||||
using SharpDX.Direct3D;
|
||||
using SharpDX.Direct3D11;
|
||||
using Device = SharpDX.Direct3D11.Device;
|
||||
using Buffer = SharpDX.Direct3D11.Buffer;
|
||||
using SharpDX.DXGI;
|
||||
|
||||
namespace CodeWalker.Rendering
|
||||
{
|
||||
public class UnitCylinder
|
||||
{
|
||||
private Buffer VertexBuffer { get; set; }
|
||||
private Buffer IndexBuffer { get; set; }
|
||||
private InputLayout InputLayout { get; set; }
|
||||
private VertexBufferBinding vbbinding;
|
||||
private int indexcount;
|
||||
|
||||
private struct SphTri
|
||||
{
|
||||
public int v1;
|
||||
public int v2;
|
||||
public int v3;
|
||||
public SphTri(int i1, int i2, int i3)
|
||||
{
|
||||
v1 = i1;
|
||||
v2 = i2;
|
||||
v3 = i3;
|
||||
}
|
||||
}
|
||||
|
||||
public UnitCylinder(Device device, byte[] vsbytes, int detail)
|
||||
{
|
||||
|
||||
InputLayout = new InputLayout(device, vsbytes, new[]
|
||||
{
|
||||
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
|
||||
new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0),
|
||||
});
|
||||
|
||||
|
||||
|
||||
List<Vector4> verts = new List<Vector4>();
|
||||
Dictionary<Vector4, int> vdict = new Dictionary<Vector4, int>();
|
||||
List<SphTri> curtris = new List<SphTri>();
|
||||
//List<SphTri> nxttris = new List<SphTri>();
|
||||
|
||||
verts.Add(new Vector4(0.0f, 0.0f, 0.0f, 0.0f));//top end (translated by VS!)
|
||||
verts.Add(new Vector4(0.0f, -1.0f, 0.0f, 0.0f));//top normal
|
||||
verts.Add(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));//bottom end
|
||||
verts.Add(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));//bottom normal
|
||||
|
||||
int nlons = detail * 4;
|
||||
int lastlon = nlons - 1;
|
||||
float latrng = 1.0f / (detail);
|
||||
float lonrng = 1.0f / (nlons);
|
||||
float twopi = (float)(2.0 * Math.PI);
|
||||
|
||||
for (int lon = 0; lon < nlons; lon++)
|
||||
{
|
||||
float tlon = lon * lonrng;
|
||||
float rlon = tlon * twopi;
|
||||
float lonx = (float)Math.Sin(rlon);
|
||||
float lonz = (float)Math.Cos(rlon);
|
||||
|
||||
verts.Add(new Vector4(lonx, 0.0f, lonz, 0.0f));//0
|
||||
verts.Add(new Vector4(0.0f, -1.0f, 0.0f, 0.0f));//top normal
|
||||
verts.Add(new Vector4(lonx, 0.0f, lonz, 0.0f));//1
|
||||
verts.Add(new Vector4(lonx, 0.0f, lonz, 0.0f));//side normal
|
||||
verts.Add(new Vector4(lonx, 0.0f, lonz, 1.0f));//2
|
||||
verts.Add(new Vector4(lonx, 0.0f, lonz, 0.0f));//side normal
|
||||
verts.Add(new Vector4(lonx, 0.0f, lonz, 1.0f));//3
|
||||
verts.Add(new Vector4(0.0f, 1.0f, 0.0f, 0.0f));//bottom normal
|
||||
}
|
||||
|
||||
for (int lon = 0; lon < nlons; lon++)
|
||||
{
|
||||
int i0 = 2 + lon * 4;// vertsperlon;//top row
|
||||
int i1 = i0 + 4;// vertsperlon;
|
||||
int i2 = i0 + 3;//bottom row
|
||||
int i3 = i2 + 4;// vertsperlon;
|
||||
int f1 = i0 + 1;
|
||||
int f2 = f1 + 4;// vertsperlon;
|
||||
|
||||
if (lon == lastlon)
|
||||
{
|
||||
i1 = 2;
|
||||
i3 = 5;// 1 + vertsperlon;
|
||||
f2 = 3;// + offs;
|
||||
}
|
||||
|
||||
|
||||
curtris.Add(new SphTri(0, i1, i0)); //top cap triangles
|
||||
|
||||
curtris.Add(new SphTri(f1, f2, f1+1));
|
||||
curtris.Add(new SphTri(f1+1, f2, f2+1)); //fill the rest
|
||||
|
||||
curtris.Add(new SphTri(1, i2, i3)); //bottom cap triangles
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
List<uint> idata = new List<uint>();
|
||||
foreach (var tri in curtris)
|
||||
{
|
||||
idata.Add((uint)tri.v1);
|
||||
idata.Add((uint)tri.v2);
|
||||
idata.Add((uint)tri.v3);
|
||||
}
|
||||
|
||||
|
||||
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, verts.ToArray());
|
||||
vbbinding = new VertexBufferBinding(VertexBuffer, 32, 0);
|
||||
|
||||
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, idata.ToArray());
|
||||
indexcount = idata.Count;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void Draw(DeviceContext context)
|
||||
{
|
||||
context.InputAssembler.InputLayout = InputLayout;
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
|
||||
context.DrawIndexed(indexcount, 0, 0);
|
||||
}
|
||||
|
||||
public void DrawInstanced(DeviceContext context, int count)
|
||||
{
|
||||
context.InputAssembler.InputLayout = InputLayout;
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
|
||||
context.DrawIndexedInstanced(indexcount, count, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (VertexBuffer != null)
|
||||
{
|
||||
VertexBuffer.Dispose();
|
||||
VertexBuffer = null;
|
||||
}
|
||||
if (IndexBuffer != null)
|
||||
{
|
||||
IndexBuffer.Dispose();
|
||||
IndexBuffer = null;
|
||||
}
|
||||
if (InputLayout != null)
|
||||
{
|
||||
InputLayout.Dispose();
|
||||
InputLayout = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SharpDX;
|
||||
using SharpDX.Direct3D;
|
||||
using SharpDX.Direct3D11;
|
||||
using Device = SharpDX.Direct3D11.Device;
|
||||
using Buffer = SharpDX.Direct3D11.Buffer;
|
||||
using SharpDX.DXGI;
|
||||
|
||||
namespace CodeWalker.Rendering
|
||||
{
|
||||
public class UnitDisc
|
||||
{
|
||||
public int SegmentCount { get; set; }
|
||||
public int IndexCount { get; set; }
|
||||
public Buffer VertexBuffer { get; set; }
|
||||
public Buffer IndexBuffer { get; set; }
|
||||
|
||||
private VertexBufferBinding vbbinding;
|
||||
|
||||
public UnitDisc(Device device, int segmentCount, bool invert = false)
|
||||
{
|
||||
SegmentCount = segmentCount;
|
||||
List<Vector3> verts = new List<Vector3>();
|
||||
List<uint> inds = new List<uint>();
|
||||
verts.Add(Vector3.Zero);
|
||||
float incr = (float)Math.PI * 2.0f / segmentCount;
|
||||
for (int i = 0; i < segmentCount; i++)
|
||||
{
|
||||
float a = incr * i;
|
||||
float px = (float)Math.Sin(a);
|
||||
float py = (float)Math.Cos(a);
|
||||
verts.Add(new Vector3(px, py, 0));
|
||||
}
|
||||
for (int i = 0; i < segmentCount; i++)
|
||||
{
|
||||
uint ci = (uint)((i == 0) ? segmentCount : i);
|
||||
uint ni = (uint)i + 1;
|
||||
inds.Add(0);
|
||||
inds.Add(invert ? ni : ci);
|
||||
inds.Add(invert ? ci : ni);
|
||||
}
|
||||
IndexCount = inds.Count;
|
||||
|
||||
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, verts.ToArray());
|
||||
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, inds.ToArray());
|
||||
vbbinding = new VertexBufferBinding(VertexBuffer, 12, 0);
|
||||
}
|
||||
|
||||
|
||||
public void Draw(DeviceContext context)
|
||||
{
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
context.DrawIndexed(IndexCount, 0, 0);
|
||||
}
|
||||
public void DrawInstanced(DeviceContext context, int instcount)
|
||||
{
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
context.DrawIndexedInstanced(IndexCount, instcount, 0, 0, 0);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (VertexBuffer != null)
|
||||
{
|
||||
VertexBuffer.Dispose();
|
||||
VertexBuffer = null;
|
||||
}
|
||||
if (IndexBuffer != null)
|
||||
{
|
||||
IndexBuffer.Dispose();
|
||||
IndexBuffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
public InputElement[] GetLayout()
|
||||
{
|
||||
return new[]
|
||||
{
|
||||
new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SharpDX;
|
||||
using SharpDX.Direct3D;
|
||||
using SharpDX.Direct3D11;
|
||||
using Device = SharpDX.Direct3D11.Device;
|
||||
using Buffer = SharpDX.Direct3D11.Buffer;
|
||||
using SharpDX.DXGI;
|
||||
|
||||
namespace CodeWalker.Rendering
|
||||
{
|
||||
public class UnitQuad
|
||||
{
|
||||
public Buffer VertexBuffer { get; set; }
|
||||
public Buffer IndexBuffer { get; set; }
|
||||
|
||||
private VertexBufferBinding vbbinding;
|
||||
|
||||
public UnitQuad(Device device, bool invert = false)
|
||||
{
|
||||
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, new[]
|
||||
{
|
||||
//position (x4), texture (x2)
|
||||
-1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
|
||||
//new Vector4(-1.0f, -1.0f, 0.0f, 1.0f), new Vector4(1.0f, -1.0f, 0.0f, 1.0f),
|
||||
//new Vector4(-1.0f, 1.0f, 0.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
|
||||
//new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.5f, -0.5f, 0.5f, 1.0f),
|
||||
//new Vector4(-0.5f, 0.5f, 0.5f, 1.0f), new Vector4(0.5f, 0.5f, 0.5f, 1.0f),
|
||||
});
|
||||
if (invert)
|
||||
{
|
||||
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, new[]
|
||||
{
|
||||
0u,1u,2u,1u,3u,2u
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, new[]
|
||||
{
|
||||
0u,2u,1u,1u,2u,3u
|
||||
});
|
||||
}
|
||||
|
||||
vbbinding = new VertexBufferBinding(VertexBuffer, 24, 0);
|
||||
}
|
||||
|
||||
|
||||
public void Draw(DeviceContext context)
|
||||
{
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
context.DrawIndexed(6, 0, 0);
|
||||
}
|
||||
public void DrawInstanced(DeviceContext context, int instcount)
|
||||
{
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
context.DrawIndexedInstanced(6, instcount, 0, 0, 0);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (VertexBuffer != null)
|
||||
{
|
||||
VertexBuffer.Dispose();
|
||||
VertexBuffer = null;
|
||||
}
|
||||
if (IndexBuffer != null)
|
||||
{
|
||||
IndexBuffer.Dispose();
|
||||
IndexBuffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public InputElement[] GetLayout()
|
||||
{
|
||||
return new[]
|
||||
{
|
||||
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
|
||||
new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SharpDX;
|
||||
using SharpDX.Direct3D;
|
||||
using SharpDX.Direct3D11;
|
||||
using Device = SharpDX.Direct3D11.Device;
|
||||
using Buffer = SharpDX.Direct3D11.Buffer;
|
||||
using SharpDX.DXGI;
|
||||
|
||||
namespace CodeWalker.Rendering
|
||||
{
|
||||
public class UnitSphere
|
||||
{
|
||||
private Buffer VertexBuffer { get; set; }
|
||||
private Buffer IndexBuffer { get; set; }
|
||||
private InputLayout InputLayout { get; set; }
|
||||
private VertexBufferBinding vbbinding;
|
||||
private int indexcount;
|
||||
|
||||
private struct SphTri
|
||||
{
|
||||
public int v1;
|
||||
public int v2;
|
||||
public int v3;
|
||||
public SphTri(int i1,int i2, int i3)
|
||||
{
|
||||
v1 = i1;
|
||||
v2 = i2;
|
||||
v3 = i3;
|
||||
}
|
||||
}
|
||||
|
||||
public UnitSphere(Device device, byte[] vsbytes, int detail)
|
||||
{
|
||||
|
||||
InputLayout = new InputLayout(device, vsbytes, new[]
|
||||
{
|
||||
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
|
||||
//new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0),
|
||||
});
|
||||
|
||||
|
||||
|
||||
List<Vector3> verts = new List<Vector3>();
|
||||
Dictionary<Vector3, int> vdict = new Dictionary<Vector3, int>();
|
||||
List<SphTri> curtris = new List<SphTri>();
|
||||
List<SphTri> nxttris = new List<SphTri>();
|
||||
|
||||
verts.Add(new Vector3(-1.0f, 0.0f, 0.0f));
|
||||
verts.Add(new Vector3(1.0f, 0.0f, 0.0f));
|
||||
verts.Add(new Vector3(0.0f, -1.0f, 0.0f));
|
||||
verts.Add(new Vector3(0.0f, 1.0f, 0.0f));
|
||||
verts.Add(new Vector3(0.0f, 0.0f, -1.0f));
|
||||
verts.Add(new Vector3(0.0f, 0.0f, 1.0f));
|
||||
curtris.Add(new SphTri(0, 4, 2));
|
||||
curtris.Add(new SphTri(4, 1, 2));
|
||||
curtris.Add(new SphTri(1, 5, 2));
|
||||
curtris.Add(new SphTri(5, 0, 2));
|
||||
curtris.Add(new SphTri(4, 0, 3));
|
||||
curtris.Add(new SphTri(1, 4, 3));
|
||||
curtris.Add(new SphTri(5, 1, 3));
|
||||
curtris.Add(new SphTri(0, 5, 3));
|
||||
|
||||
for (int i = 0; i < verts.Count; i++)
|
||||
{
|
||||
vdict[verts[i]] = i;
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < detail; i++)
|
||||
{
|
||||
nxttris.Clear();
|
||||
foreach (var tri in curtris)
|
||||
{
|
||||
Vector3 v1 = verts[tri.v1];
|
||||
Vector3 v2 = verts[tri.v2];
|
||||
Vector3 v3 = verts[tri.v3];
|
||||
Vector3 s1 = Vector3.Normalize(v1 + v2);
|
||||
Vector3 s2 = Vector3.Normalize(v2 + v3);
|
||||
Vector3 s3 = Vector3.Normalize(v3 + v1);
|
||||
int i1, i2, i3;
|
||||
if (!vdict.TryGetValue(s1, out i1))
|
||||
{
|
||||
i1 = verts.Count;
|
||||
verts.Add(s1);
|
||||
vdict[s1] = i1;
|
||||
}
|
||||
if (!vdict.TryGetValue(s2, out i2))
|
||||
{
|
||||
i2 = verts.Count;
|
||||
verts.Add(s2);
|
||||
vdict[s2] = i2;
|
||||
}
|
||||
if (!vdict.TryGetValue(s3, out i3))
|
||||
{
|
||||
i3 = verts.Count;
|
||||
verts.Add(s3);
|
||||
vdict[s3] = i3;
|
||||
}
|
||||
nxttris.Add(new SphTri(tri.v1, i1, i3));
|
||||
nxttris.Add(new SphTri(tri.v2, i2, i1));
|
||||
nxttris.Add(new SphTri(tri.v3, i3, i2));
|
||||
nxttris.Add(new SphTri(i1, i2, i3));
|
||||
}
|
||||
var cur = curtris;
|
||||
curtris = nxttris;
|
||||
nxttris = cur;
|
||||
}
|
||||
|
||||
|
||||
List<Vector4> vdata = new List<Vector4>();
|
||||
foreach (var vert in verts)
|
||||
{
|
||||
vdata.Add(new Vector4(vert, 1.0f));
|
||||
}
|
||||
|
||||
List<uint> idata = new List<uint>();
|
||||
foreach (var tri in curtris)
|
||||
{
|
||||
idata.Add((uint)tri.v1);
|
||||
idata.Add((uint)tri.v2);
|
||||
idata.Add((uint)tri.v3);
|
||||
}
|
||||
|
||||
|
||||
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, vdata.ToArray());
|
||||
vbbinding = new VertexBufferBinding(VertexBuffer, 16, 0);
|
||||
|
||||
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, idata.ToArray());
|
||||
indexcount = idata.Count;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void Draw(DeviceContext context)
|
||||
{
|
||||
context.InputAssembler.InputLayout = InputLayout;
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
|
||||
context.DrawIndexed(indexcount, 0, 0);
|
||||
}
|
||||
|
||||
public void DrawInstanced(DeviceContext context, int count)
|
||||
{
|
||||
context.InputAssembler.InputLayout = InputLayout;
|
||||
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
|
||||
context.InputAssembler.SetVertexBuffers(0, vbbinding);
|
||||
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
|
||||
|
||||
context.DrawIndexedInstanced(indexcount, count, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (VertexBuffer != null)
|
||||
{
|
||||
VertexBuffer.Dispose();
|
||||
VertexBuffer = null;
|
||||
}
|
||||
if (IndexBuffer != null)
|
||||
{
|
||||
IndexBuffer.Dispose();
|
||||
IndexBuffer = null;
|
||||
}
|
||||
if (InputLayout != null)
|
||||
{
|
||||
InputLayout.Dispose();
|
||||
InputLayout = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user