From bee207138ca59426729420e43a589a2177d998e1 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 2 Aug 2017 20:06:01 +1000 Subject: [PATCH] Only pick a block if the intersection point is exactly within reach distance. Should address #461. --- ClassicalSharp/GraphicsAPI/Direct3D9Api.cs | 9 ++----- ClassicalSharp/GraphicsAPI/IGraphicsApi.cs | 2 -- ClassicalSharp/GraphicsAPI/OpenGLApi.cs | 16 ------------ ClassicalSharp/Math/Picking.cs | 23 +++++++++++------ ClassicalSharp/Rendering/MapRenderer.cs | 29 +++------------------- src/Client/D3D9Api.c | 10 ++------ src/Client/GraphicsAPI.h | 3 --- src/Client/MapRenderer.c | 25 +++---------------- 8 files changed, 25 insertions(+), 92 deletions(-) diff --git a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs index 559db6361..ce4bc58c4 100644 --- a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs +++ b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs @@ -299,13 +299,8 @@ namespace ClassicalSharp.GraphicsAPI { } internal override void DrawIndexedVb_TrisT2fC4b(int indicesCount, int startIndex) { - device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, startIndex / 6 * 4, - indicesCount / 6 * 4, startIndex, indicesCount / 3); - } - - internal override void DrawIndexedVb_TrisT2fC4b(int indicesCount, int startVertex, int startIndex) { - device.DrawIndexedPrimitives(PrimitiveType.TriangleList, startVertex, 0, - indicesCount / 6 * 4, startIndex, indicesCount / 3); + device.DrawIndexedPrimitives(PrimitiveType.TriangleList, startIndex / 6 * 4, 0, + indicesCount / 6 * 4, 0, indicesCount / 3); } #endregion diff --git a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs index c19ef153e..bd6c7e5bb 100644 --- a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs +++ b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs @@ -180,8 +180,6 @@ namespace ClassicalSharp.GraphicsAPI { public abstract void DrawVb_IndexedTris(int indicesCount); /// Optimised version of DrawIndexedVb for VertexFormat.Pos3fTex2fCol4b - internal abstract void DrawIndexedVb_TrisT2fC4b(int indicesCount, int offsetVertex, int startIndex); - internal abstract void DrawIndexedVb_TrisT2fC4b(int indicesCount, int startIndex); protected static int[] strideSizes = { 16, 24 }; diff --git a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs index dfe90d5f8..0200dd4b1 100644 --- a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs +++ b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs @@ -395,22 +395,6 @@ namespace ClassicalSharp.GraphicsAPI { GL.DrawElements(BeginMode.Triangles, indicesCount, indexType, IntPtr.Zero); } - internal override void DrawIndexedVb_TrisT2fC4b(int indicesCount, int startVertex, int startIndex) { - // TODO: This renders the whole map, bad performance!! FIX FIX - if (glLists) { - if (activeList != lastPartialList) { - GL.CallList(activeList); lastPartialList = activeList; - } - return; - } - - int offset = startVertex * VertexP3fT2fC4b.Size; - GL.VertexPointer(3, PointerType.Float, VertexP3fT2fC4b.Size, new IntPtr(offset)); - GL.ColorPointer(4, PointerType.UnsignedByte, VertexP3fT2fC4b.Size, new IntPtr(offset + 12)); - GL.TexCoordPointer(2, PointerType.Float, VertexP3fT2fC4b.Size, new IntPtr(offset + 16)); - GL.DrawElements(BeginMode.Triangles, indicesCount, indexType, IntPtr.Zero); - } - IntPtr zero = new IntPtr(0), twelve = new IntPtr(12), sixteen = new IntPtr(16); void SetupVbPos3fCol4b() { diff --git a/ClassicalSharp/Math/Picking.cs b/ClassicalSharp/Math/Picking.cs index 9b1aa0f23..4e03402bc 100644 --- a/ClassicalSharp/Math/Picking.cs +++ b/ClassicalSharp/Math/Picking.cs @@ -11,7 +11,7 @@ using BlockID = System.Byte; #endif namespace ClassicalSharp { - public static class Picking { + public static class Picking { static RayTracer t = new RayTracer(); @@ -34,7 +34,14 @@ namespace ClassicalSharp { if (!Intersection.RayIntersectsBox(t.Origin, t.Dir, t.Min, t.Max, out t0, out t1)) return false; Vector3 I = t.Origin + t.Dir * t0; - pos.SetAsValid(t.X, t.Y, t.Z, t.Min, t.Max, t.Block, I); + + float lenSq = (I - t.Origin).LengthSquared; + float reach = game.LocalPlayer.ReachDistance; + if (lenSq <= reach * reach) { + pos.SetAsValid(t.X, t.Y, t.Z, t.Min, t.Max, t.Block, I); + } else { + pos.SetAsInvalid(); + } return true; } @@ -60,12 +67,12 @@ namespace ClassicalSharp { pos.SetAsValid(t.X, t.Y, t.Z, t.Min, t.Max, t.Block, I); switch (pos.Face) { - case BlockFace.XMin: pos.Intersect.X -= adjust; break; - case BlockFace.XMax: pos.Intersect.X += adjust; break; - case BlockFace.YMin: pos.Intersect.Y -= adjust; break; - case BlockFace.YMax: pos.Intersect.Y += adjust; break; - case BlockFace.ZMin: pos.Intersect.Z -= adjust; break; - case BlockFace.ZMax: pos.Intersect.Z += adjust; break; + case BlockFace.XMin: pos.Intersect.X -= adjust; break; + case BlockFace.XMax: pos.Intersect.X += adjust; break; + case BlockFace.YMin: pos.Intersect.Y -= adjust; break; + case BlockFace.YMax: pos.Intersect.Y += adjust; break; + case BlockFace.ZMin: pos.Intersect.Z -= adjust; break; + case BlockFace.ZMax: pos.Intersect.Z += adjust; break; } return true; } diff --git a/ClassicalSharp/Rendering/MapRenderer.cs b/ClassicalSharp/Rendering/MapRenderer.cs index cfd33668b..10a708a4a 100644 --- a/ClassicalSharp/Rendering/MapRenderer.cs +++ b/ClassicalSharp/Rendering/MapRenderer.cs @@ -197,8 +197,6 @@ namespace ClassicalSharp.Renderers { gfx.AlphaBlending = false; } - const int maxVertex = 65536; - const int maxIndices = maxVertex / 4 * 6; void RenderNormalBatch(int batch) { for (int i = 0; i < renderCount; i++) { ChunkInfo info = renderChunks[i]; @@ -242,37 +240,16 @@ namespace ClassicalSharp.Renderers { game.Vertices += part.BackCount; } - // Special handling for top and bottom face, as these can go over 65536 vertices and we need to adjust the indices in this case. if (drawBottom && drawTop) { gfx.FaceCulling = true; - if (part.IndicesCount > maxIndices) { - int part1Count = maxIndices - part.BottomIndex; - gfx.DrawIndexedVb_TrisT2fC4b(part1Count, part.BottomIndex); - gfx.DrawIndexedVb_TrisT2fC4b(part.BottomCount + part.TopCount - part1Count, maxVertex, 0); - } else { - gfx.DrawIndexedVb_TrisT2fC4b(part.BottomCount + part.TopCount, part.BottomIndex); - } + gfx.DrawIndexedVb_TrisT2fC4b(part.BottomCount + part.TopCount, part.BottomIndex); gfx.FaceCulling = false; game.Vertices += part.TopCount + part.BottomCount; } else if (drawBottom) { - int part1Count; - if (part.IndicesCount > maxIndices && - (part1Count = maxIndices - part.BottomIndex) < part.BottomCount) { - gfx.DrawIndexedVb_TrisT2fC4b(part1Count, part.BottomIndex); - gfx.DrawIndexedVb_TrisT2fC4b(part.BottomCount - part1Count, maxVertex, 0); - } else { - gfx.DrawIndexedVb_TrisT2fC4b(part.BottomCount, part.BottomIndex); - } + gfx.DrawIndexedVb_TrisT2fC4b(part.BottomCount, part.BottomIndex); game.Vertices += part.BottomCount; } else if (drawTop) { - int part1Count; - if (part.IndicesCount > maxIndices && - (part1Count = maxIndices - part.TopIndex) < part.TopCount) { - gfx.DrawIndexedVb_TrisT2fC4b(part1Count, part.TopIndex); - gfx.DrawIndexedVb_TrisT2fC4b(part.TopCount - part1Count, maxVertex, 0); - } else { - gfx.DrawIndexedVb_TrisT2fC4b(part.TopCount, part.TopIndex); - } + gfx.DrawIndexedVb_TrisT2fC4b(part.TopCount, part.TopIndex); game.Vertices += part.TopCount; } diff --git a/src/Client/D3D9Api.c b/src/Client/D3D9Api.c index d62c2fbdd..474cc1213 100644 --- a/src/Client/D3D9Api.c +++ b/src/Client/D3D9Api.c @@ -374,15 +374,9 @@ void Gfx_DrawVb_IndexedTris_Range(Int32 indicesCount, Int32 startIndex) { ErrorHandler_CheckOrFail(hresult, "D3D9_DrawVb_IndexedTris"); } -void Gfx_DrawIndexedVb_TrisT2fC4b_Range(Int32 indicesCount, Int32 offsetVertex, Int32 startIndex) { - ReturnCode hresult = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, offsetVertex, - 0, VCOUNT(indicesCount), startIndex, indicesCount / 3); - ErrorHandler_CheckOrFail(hresult, "D3D9_DrawIndexedVb_TrisT2fC4b_Range"); -} - void Gfx_DrawIndexedVb_TrisT2fC4b(Int32 indicesCount, Int32 startIndex) { - ReturnCode hresult = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0, - VCOUNT(startIndex), VCOUNT(indicesCount), startIndex, indicesCount / 3); + ReturnCode hresult = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, VCOUNT(startIndex), + 0, VCOUNT(indicesCount), 0, indicesCount / 3); ErrorHandler_CheckOrFail(hresult, "D3D9_DrawIndexedVb_TrisT2fC4b"); } diff --git a/src/Client/GraphicsAPI.h b/src/Client/GraphicsAPI.h index 701df5eed..d71aaceb2 100644 --- a/src/Client/GraphicsAPI.h +++ b/src/Client/GraphicsAPI.h @@ -167,9 +167,6 @@ void Gfx_DrawVb_IndexedTris_Range(Int32 indicesCount, Int32 startIndex); /* Draws the specified subset of the vertices in the current vertex buffer as triangles. */ void Gfx_DrawVb_IndexedTris(Int32 indicesCount); -/* Optimised version of DrawIndexedVb for VertexFormat_Pos3fTex2fCol4b */ -void Gfx_DrawIndexedVb_TrisT2fC4b_Range(Int32 indicesCount, Int32 offsetVertex, Int32 startIndex); - /* Optimised version of DrawIndexedVb for VertexFormat_Pos3fTex2fCol4b */ void Gfx_DrawIndexedVb_TrisT2fC4b(Int32 indicesCount, Int32 startIndex); diff --git a/src/Client/MapRenderer.c b/src/Client/MapRenderer.c index ac52c3871..2ce38fdfe 100644 --- a/src/Client/MapRenderer.c +++ b/src/Client/MapRenderer.c @@ -164,36 +164,17 @@ void MapRenderer_RenderNormalBatch(Int32 batch) { } offset += part.ZMinCount + part.ZMaxCount; - /* Special handling for top and bottom face, as these can go over 65536 vertices and we need to adjust the indices in this case. */ if (drawYMin && drawYMax) { Gfx_SetFaceCulling(true); - if (part.IndicesCount > Gfx_MaxIndices) { - Int32 part1Count = Gfx_MaxIndices - offset; - Gfx_DrawIndexedVb_TrisT2fC4b(part1Count, offset); - Gfx_DrawIndexedVb_TrisT2fC4b_Range(part.YMinCount + part.YMaxCount - part1Count, Gfx_MaxVertex, 0); - } else { - Gfx_DrawIndexedVb_TrisT2fC4b(part.YMinCount + part.YMaxCount, offset); - } + Gfx_DrawIndexedVb_TrisT2fC4b(part.YMinCount + part.YMaxCount, offset); Gfx_SetFaceCulling(false); Game_Vertices += part.YMaxCount + part.YMinCount; } else if (drawYMin) { - Int32 part1Count; - if (part.IndicesCount > Gfx_MaxIndices && (part1Count = Gfx_MaxIndices - offset) < part.YMinCount) { - Gfx_DrawIndexedVb_TrisT2fC4b(part1Count, offset); - Gfx_DrawIndexedVb_TrisT2fC4b_Range(part.YMinCount - part1Count, Gfx_MaxVertex, 0); - } else { - Gfx_DrawIndexedVb_TrisT2fC4b(part.YMinCount, offset); - } + Gfx_DrawIndexedVb_TrisT2fC4b(part.YMinCount, offset); Game_Vertices += part.YMinCount; } else if (drawYMax) { offset += part.YMinCount; - Int32 part1Count; - if (part.IndicesCount > Gfx_MaxIndices && (part1Count = Gfx_MaxIndices - offset) < part.YMaxCount) { - Gfx_DrawIndexedVb_TrisT2fC4b(part1Count, offset); - Gfx_DrawIndexedVb_TrisT2fC4b_Range(part.YMaxCount - part1Count, Gfx_MaxVertex, 0); - } else { - Gfx_DrawIndexedVb_TrisT2fC4b(part.YMaxCount, offset); - } + Gfx_DrawIndexedVb_TrisT2fC4b(part.YMaxCount, offset); Game_Vertices += part.YMaxCount; }