Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
-Player moving around on server
Browse files Browse the repository at this point in the history
-Basics of debug renderer
-kill me please
  • Loading branch information
Cassunshine committed Dec 8, 2023
1 parent 9164e91 commit a95579b
Show file tree
Hide file tree
Showing 51 changed files with 444 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<RollForward>Major</RollForward>
<PublishReadyToRun>false</PublishReadyToRun>
<RootNamespace>Voxel</RootNamespace>
<RootNamespace>Voxel.Client</RootNamespace>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
9 changes: 9 additions & 0 deletions Client/Content/shaders/debug.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 450

layout(location = 1) in vec4 fsin_Color;

layout(location = 0) out vec4 fsout_Color;

void main() {
fsout_Color = fsin_Color;
}
19 changes: 19 additions & 0 deletions Client/Content/shaders/debug.vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 450

layout(location = 0) in vec3 Position;
layout(location = 1) in vec4 Color;
layout(location = 2) in vec2 UV;


layout(location = 1) out vec4 fsin_Color;

layout (set = 0, binding = 0) uniform CameraData {
mat4 VPMatrix;
};

void main() {
vec4 pos = vec4(Position, 1) * VPMatrix;
gl_Position = pos;

fsin_Color = Color;
}
2 changes: 1 addition & 1 deletion Client/Keybinding/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System;
using System.Collections.Generic;
using GlmSharp;
using RenderSurface.Input.Gamepad;
using Veldrid;
using Voxel.Core.Input.Gamepad;
using VMouseButton = Veldrid.MouseButton;

namespace Voxel.Client.Keybinding;
Expand Down
4 changes: 2 additions & 2 deletions Client/Keybinding/Keybinds.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using RenderSurface.Input.Gamepad;
using Veldrid;
using Voxel.Core.Input.Gamepad;
using VMouseButton = Veldrid.MouseButton;

namespace Voxel.Client.Keybinding;
Expand Down Expand Up @@ -50,7 +50,7 @@ public static class Keybinds {
KeyButton.Get(Key.ShiftLeft),
ControllerButton.Get(GamepadButton.RightStick)
);

public static readonly Keybind Look = new(
"camera.full",
ControllerJoystickButton.Get(ControllerJoystickButton.GamepadJoystick.Right)
Expand Down
8 changes: 8 additions & 0 deletions Client/Network/ClientConnectionContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using Common.Network.Packets;
using Common.Network.Packets.C2S;
using Common.Network.Packets.C2S.Handshake;
using Common.Network.Packets.S2C;
using Common.Network.Packets.S2C.Gameplay;
using Common.Network.Packets.S2C.Handshake;
using Common.Network.Packets.Utils;
using Voxel.Client.World;
using Voxel.Common.Network.Packets;

namespace Voxel.Client.Network;

Expand Down Expand Up @@ -59,5 +62,10 @@ private void HandleChunkData(ChunkData packet) {
packet.Apply(chunk);
}

public void SendPacket(C2SPacket packet) {
Connection.DeliverPacket(packet);
PacketPool.Return(packet);
}

public void Close() => Connection.Close();
}
2 changes: 1 addition & 1 deletion Client/Rendering/CameraStateManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using GlmSharp;
using RenderSurface.Rendering;
using Veldrid;
using Voxel.Client.Rendering.Utils;
using Voxel.Common.Util;
using Voxel.Core.Rendering;

namespace Voxel.Client.Rendering;

Expand Down
97 changes: 97 additions & 0 deletions Client/Rendering/Debug/DebugRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Runtime.InteropServices;
using GlmSharp;
using Veldrid;
using Voxel.Client.Rendering.VertexTypes;

namespace Voxel.Client.Rendering.Debug;

public class DebugRenderer : Renderer {
private const int BatchSize = 2048;
private static DebugRenderer Instance;

public readonly Pipeline DebugPipeline;
private readonly DeviceBuffer vertexBuffer;

private readonly DebugVertex[] DebugVertices = new DebugVertex[BatchSize];
private int vertexIndex = 0;

public DebugRenderer(VoxelClient client) : base(client) {
Instance = this;

if (!client.RenderSystem.ShaderManager.GetShaders("shaders/debug", out var shaders))
throw new("Shaders not present.");

DebugPipeline = ResourceFactory.CreateGraphicsPipeline(new() {
BlendState = BlendStateDescription.SingleOverrideBlend,
DepthStencilState = new() {
DepthComparison = ComparisonKind.LessEqual, DepthTestEnabled = true, DepthWriteEnabled = true,
},
Outputs = RenderSystem.GraphicsDevice.SwapchainFramebuffer.OutputDescription,
PrimitiveTopology = PrimitiveTopology.LineList,
RasterizerState = new() {
CullMode = FaceCullMode.None, DepthClipEnabled = false, FillMode = PolygonFillMode.Wireframe, ScissorTestEnabled = false,
},
ResourceLayouts = new[] {
Client.GameRenderer.CameraStateManager.CameraResourceLayout,
},
ShaderSet = new() {
VertexLayouts = new[] {
DebugVertex.Layout
},
Shaders = shaders
}
});


vertexBuffer = ResourceFactory.CreateBuffer(new BufferDescription {
Usage = BufferUsage.Dynamic | BufferUsage.VertexBuffer, SizeInBytes = (uint)Marshal.SizeOf<DebugVertex>() * BatchSize
});
}
public override void Render(double delta) {

DrawLine(new() {
position = new vec3(0, 0, 0) - (vec3)Client.GameRenderer.MainCamera.position, color = new vec4(1, 1, 1, 1)
},
new() {
position = new vec3(0, 1000, 0) - (vec3)Client.GameRenderer.MainCamera.position, color = new vec4(1, 1, 1, 1)
}
);

Flush();
}

private void Flush() {
if (vertexIndex == 0)
return;

CommandList.UpdateBuffer(vertexBuffer, 0, DebugVertices.AsSpan(0, vertexIndex));
CommandList.SetPipeline(DebugPipeline);

CommandList.SetGraphicsResourceSet(0, Client.GameRenderer.CameraStateManager.CameraResourceSet);

CommandList.SetVertexBuffer(0, vertexBuffer);
CommandList.Draw((uint)vertexIndex);

vertexIndex = 0;
}

public override void Dispose() {
DebugPipeline.Dispose();
}


public static void DrawLine(DebugVertex a, DebugVertex b) {
Instance.DebugVertices[Instance.vertexIndex++] = a;
Instance.DebugVertices[Instance.vertexIndex++] = b;

if (Instance.vertexIndex >= BatchSize) {
Instance.Flush();
}
}

/*public static void DrawCube(DebugVertex min, DebugVertex max) {
DebugVertices.Add(a);
DebugVertices.Add(b);
}*/
}
12 changes: 12 additions & 0 deletions Client/Rendering/GameRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using GlmSharp;
using Voxel.Client.Keybinding;
using Voxel.Client.Rendering.Debug;
using Voxel.Client.Rendering.GUI;
using Voxel.Client.Rendering.World;
using Voxel.Common.Collision;
Expand All @@ -18,6 +19,7 @@ public class GameRenderer : Renderer {
public readonly WorldRenderer WorldRenderer;
public readonly GuiRenderer GuiRenderer;
public readonly CameraStateManager CameraStateManager;
public readonly DebugRenderer DebugRenderer;

public GameRenderer(VoxelClient client) : base(client) {
//Jank but OK
Expand All @@ -28,17 +30,27 @@ public GameRenderer(VoxelClient client) : base(client) {

WorldRenderer = new(client);
GuiRenderer = new(client);


DebugRenderer = new DebugRenderer(client);
}

public override void Render(double delta) {
CameraStateManager.SetToCamera(MainCamera, Client.timeSinceLastTick);

MainCamera.position = Client.PlayerEntity?.position ?? dvec3.Zero;
MainCamera.oldPosition = MainCamera.position;

WorldRenderer.Render(delta);
GuiRenderer.Render(delta);

DebugRenderer.Render(delta);
}

public override void Dispose() {
WorldRenderer.Dispose();
GuiRenderer.Dispose();

DebugRenderer.Dispose();
}
}
2 changes: 1 addition & 1 deletion Client/Rendering/Models/BlockModelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.IO;
using GlmSharp;
using Newtonsoft.Json;
using RenderSurface.Assets;
using Voxel.Client.Rendering.Texture;
using Voxel.Common.Tile;
using Voxel.Core.Assets;
using Voxel.Rendering.Utils;

namespace Voxel.Client.Rendering.Models;
Expand Down
2 changes: 1 addition & 1 deletion Client/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using RenderSurface.Rendering;
using Veldrid;
using Voxel.Core.Rendering;

namespace Voxel.Client.Rendering;

Expand Down
2 changes: 1 addition & 1 deletion Client/Rendering/Texture/Atlas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Drawing;
using System.Runtime.InteropServices;
using GlmSharp;
using RenderSurface.Rendering;
using Veldrid;
using Veldrid.OpenGLBinding;
using Voxel.Client.Rendering.Utils;
using Voxel.Client.Rendering.VertexTypes;
using Voxel.Core.Rendering;

namespace Voxel.Client.Rendering.Texture;

Expand Down
4 changes: 2 additions & 2 deletions Client/Rendering/Texture/AtlasLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.IO;
using GlmSharp;
using Newtonsoft.Json;
using RenderSurface.Assets;
using RenderSurface.Rendering;
using Voxel.Core.Assets;
using Voxel.Core.Rendering;

namespace Voxel.Client.Rendering.Texture;

Expand Down
2 changes: 1 addition & 1 deletion Client/Rendering/Utils/ArrayTypedDeviceBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Runtime.InteropServices;
using RenderSurface.Rendering;
using Veldrid;
using Voxel.Core.Rendering;

namespace Voxel.Client.Rendering.Utils;

Expand Down
2 changes: 1 addition & 1 deletion Client/Rendering/Utils/TypedDeviceBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Runtime.InteropServices;
using RenderSurface.Rendering;
using Veldrid;
using Voxel.Core.Rendering;

namespace Voxel.Client.Rendering.Utils;

Expand Down
17 changes: 17 additions & 0 deletions Client/Rendering/VertexTypes/DebugVertex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using GlmSharp;
using Veldrid;

namespace Voxel.Client.Rendering.VertexTypes;

public struct DebugVertex {

public static readonly VertexLayoutDescription Layout = new(
new VertexElementDescription("Position", VertexElementFormat.Float3, VertexElementSemantic.Position),
new VertexElementDescription("Color", VertexElementFormat.Float4, VertexElementSemantic.Color),
new VertexElementDescription("UV", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate)
);

public vec3 position;
public vec4 color;
public vec2 uv;
}
3 changes: 2 additions & 1 deletion Client/Rendering/World/ChunkMeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Newtonsoft.Json.Serialization;
using Voxel.Client.Rendering.Models;
using Voxel.Client.Rendering.VertexTypes;
using Voxel.Client.World;
using Voxel.Common.Tile;
using Voxel.Common.Util;
using Voxel.Common.World.Storage;
Expand Down Expand Up @@ -220,7 +221,7 @@ public bool Build(ChunkRenderSlot target, ivec3 worldPosition) {
for (int i = 0; i < DiagonalSelfNeighborPositions.Length; i++) {
var pos = DiagonalSelfNeighborPositions[i] + target.targetChunk!.ChunkPosition;

if (!target.targetChunk!.World.IsChunkLoadedRaw(pos))
if (!target.targetChunk!.World.TryGetChunkRaw(pos, out var c) || c is not ClientChunk clientChunk || !clientChunk.isFilled)
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion Client/Rendering/World/ChunkRenderSlot.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Runtime.InteropServices;
using GlmSharp;
using RenderSurface.Rendering;
using Veldrid;
using Voxel.Client.Rendering.Utils;
using Voxel.Client.Rendering.VertexTypes;
using Voxel.Common.Util;
using Voxel.Common.World;
using Voxel.Core.Rendering;

namespace Voxel.Client.Rendering.World;

Expand Down
4 changes: 3 additions & 1 deletion Client/Rendering/World/ChunkRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public void Reload() {
public override void Render(double delta) {
if (renderSlots == null)
return;

SetRenderPosition(Client.GameRenderer.MainCamera.position);

CommandList.SetPipeline(ChunkPipeline);

Expand Down Expand Up @@ -160,7 +162,7 @@ public void SetRenderPosition(dvec3 worldPosition) {
}

//Sort by distance so that closer chunks are rebuilt first.
createdRenderSlots.Sort((a, b) => (a.RealPosition - renderPosition).LengthSqr.CompareTo((b.RealPosition - renderPosition).LengthSqr));
//createdRenderSlots.Sort((a, b) => (a.RealPosition - renderPosition).LengthSqr.CompareTo((b.RealPosition - renderPosition).LengthSqr));
}


Expand Down
Loading

0 comments on commit a95579b

Please sign in to comment.