almost nail down the rendering

This commit is contained in:
bird_egop
2026-06-06 01:24:09 +03:00
parent 0d8e4ba491
commit 776bb9fcb3
4 changed files with 141 additions and 86 deletions
Binary file not shown.
@@ -1,5 +1,4 @@
using System; using System.Numerics;
using System.Collections.Generic;
using Silk.NET.OpenGL; using Silk.NET.OpenGL;
namespace NResUI.Rendering.Viewport.Meshes; namespace NResUI.Rendering.Viewport.Meshes;
@@ -8,50 +7,64 @@ public static unsafe class PrimitiveMeshes
{ {
public static GpuMesh CreateCube(GL gl) public static GpuMesh CreateCube(GL gl)
{ {
// position.xyz, color.rgb var vertices = new List<float>();
float[] vertices = var indices = new List<uint>();
{
// front
-1, -1, 1, 1, 0, 0,
1, -1, 1, 0, 1, 0,
1, 1, 1, 0, 0, 1,
-1, 1, 1, 1, 1, 0,
// back AddCubeFace(
-1, -1, -1, 1, 0, 1, vertices, indices,
1, -1, -1, 0, 1, 1, new Vector3(-1, -1, 1),
1, 1, -1, 1, 1, 1, new Vector3( 1, -1, 1),
-1, 1, -1, 0.5f, 0.5f, 0.5f, new Vector3( 1, 1, 1),
}; new Vector3(-1, 1, 1),
new Vector3(0, 0, 1),
new Vector3(1, 0, 0));
uint[] indices = AddCubeFace(
{ vertices, indices,
// front new Vector3( 1, -1, -1),
0, 1, 2, new Vector3(-1, -1, -1),
2, 3, 0, new Vector3(-1, 1, -1),
new Vector3( 1, 1, -1),
new Vector3(0, 0, -1),
new Vector3(0, 1, 0));
// right AddCubeFace(
1, 5, 6, vertices, indices,
6, 2, 1, new Vector3(-1, -1, -1),
new Vector3(-1, -1, 1),
new Vector3(-1, 1, 1),
new Vector3(-1, 1, -1),
new Vector3(-1, 0, 0),
new Vector3(0, 0, 1));
// back AddCubeFace(
5, 4, 7, vertices, indices,
7, 6, 5, new Vector3(1, -1, 1),
new Vector3(1, -1, -1),
new Vector3(1, 1, -1),
new Vector3(1, 1, 1),
new Vector3(1, 0, 0),
new Vector3(1, 1, 0));
// left AddCubeFace(
4, 0, 3, vertices, indices,
3, 7, 4, new Vector3(-1, 1, 1),
new Vector3( 1, 1, 1),
new Vector3( 1, 1, -1),
new Vector3(-1, 1, -1),
new Vector3(0, 1, 0),
new Vector3(1, 0, 1));
// top AddCubeFace(
3, 2, 6, vertices, indices,
6, 7, 3, new Vector3(-1, -1, -1),
new Vector3( 1, -1, -1),
new Vector3( 1, -1, 1),
new Vector3(-1, -1, 1),
new Vector3(0, -1, 0),
new Vector3(0, 1, 1));
// bottom return CreateColoredIndexedMesh(gl, vertices, indices, PrimitiveType.Triangles);
4, 5, 1,
1, 0, 4,
};
return CreateIndexedMesh(gl, vertices, indices, PrimitiveType.Triangles);
} }
public static GpuMesh CreateWorldGrid( public static GpuMesh CreateWorldGrid(
@@ -72,25 +85,15 @@ public static unsafe class PrimitiveMeshes
var vertices = new List<float>(); var vertices = new List<float>();
var indices = new List<uint>(); var indices = new List<uint>();
void AddVertex(float x, float y, float z, float r, float g, float b)
{
vertices.Add(x);
vertices.Add(y);
vertices.Add(z);
vertices.Add(r);
vertices.Add(g);
vertices.Add(b);
}
void AddLine( void AddLine(
float x0, float y0, float z0, float x0, float y0, float z0,
float x1, float y1, float z1, float x1, float y1, float z1,
float r, float g, float b) float r, float g, float b)
{ {
var startIndex = (uint)(vertices.Count / 6); var startIndex = (uint)(vertices.Count / 9);
AddVertex(x0, y0, z0, r, g, b); AddVertex(vertices, x0, y0, z0, r, g, b, 0, 1, 0);
AddVertex(x1, y1, z1, r, g, b); AddVertex(vertices, x1, y1, z1, r, g, b, 0, 1, 0);
indices.Add(startIndex); indices.Add(startIndex);
indices.Add(startIndex + 1); indices.Add(startIndex + 1);
@@ -132,30 +135,88 @@ public static unsafe class PrimitiveMeshes
return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), PrimitiveType.Lines); return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), PrimitiveType.Lines);
} }
private static void AddCubeFace(
List<float> vertices,
List<uint> indices,
Vector3 a,
Vector3 b,
Vector3 c,
Vector3 d,
Vector3 normal,
Vector3 color)
{
var start = (uint)(vertices.Count / 9);
AddVertex(vertices, a.X, a.Y, a.Z, color.X, color.Y, color.Z, normal.X, normal.Y, normal.Z);
AddVertex(vertices, b.X, b.Y, b.Z, color.X, color.Y, color.Z, normal.X, normal.Y, normal.Z);
AddVertex(vertices, c.X, c.Y, c.Z, color.X, color.Y, color.Z, normal.X, normal.Y, normal.Z);
AddVertex(vertices, d.X, d.Y, d.Z, color.X, color.Y, color.Z, normal.X, normal.Y, normal.Z);
indices.Add(start + 0);
indices.Add(start + 1);
indices.Add(start + 2);
indices.Add(start + 2);
indices.Add(start + 3);
indices.Add(start + 0);
}
public static GpuMesh CreateUnitWireBox(GL gl) public static GpuMesh CreateUnitWireBox(GL gl)
{ {
float[] vertices = var vertices = new List<float>();
var indices = new List<uint>();
var color = new Vector3(1.0f, 0.82f, 0.15f);
Vector3[] corners =
{ {
-1, -1, -1, 1.0f, 0.82f, 0.15f, new(-1, -1, -1),
1, -1, -1, 1.0f, 0.82f, 0.15f, new( 1, -1, -1),
1, 1, -1, 1.0f, 0.82f, 0.15f, new( 1, 1, -1),
-1, 1, -1, 1.0f, 0.82f, 0.15f, new(-1, 1, -1),
-1, -1, 1, 1.0f, 0.82f, 0.15f, new(-1, -1, 1),
1, -1, 1, 1.0f, 0.82f, 0.15f, new( 1, -1, 1),
1, 1, 1, 1.0f, 0.82f, 0.15f, new( 1, 1, 1),
-1, 1, 1, 1.0f, 0.82f, 0.15f, new(-1, 1, 1),
}; };
uint[] indices = foreach (var corner in corners)
{
AddVertex(vertices, corner.X, corner.Y, corner.Z, color.X, color.Y, color.Z);
}
uint[] lineIndices =
{ {
0, 1, 1, 2, 2, 3, 3, 0, 0, 1, 1, 2, 2, 3, 3, 0,
4, 5, 5, 6, 6, 7, 7, 4, 4, 5, 5, 6, 6, 7, 7, 4,
0, 4, 1, 5, 2, 6, 3, 7, 0, 4, 1, 5, 2, 6, 3, 7,
}; };
return CreateIndexedMesh(gl, vertices, indices, PrimitiveType.Lines); indices.AddRange(lineIndices);
return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), PrimitiveType.Lines);
}
private static void AddVertex(
List<float> vertices,
float x, float y, float z,
float r, float g, float b,
float nx = 0.0f,
float ny = 1.0f,
float nz = 0.0f)
{
vertices.Add(x);
vertices.Add(y);
vertices.Add(z);
vertices.Add(r);
vertices.Add(g);
vertices.Add(b);
vertices.Add(nx);
vertices.Add(ny);
vertices.Add(nz);
} }
public static GpuMesh CreateAxes(GL gl, float length = 1.0f) public static GpuMesh CreateAxes(GL gl, float length = 1.0f)
@@ -163,26 +224,25 @@ public static unsafe class PrimitiveMeshes
if (length <= 0.0f) if (length <= 0.0f)
throw new ArgumentOutOfRangeException(nameof(length)); throw new ArgumentOutOfRangeException(nameof(length));
float[] vertices = var vertices = new List<float>();
var indices = new List<uint>();
void AddLine(Vector3 a, Vector3 b, Vector3 color)
{ {
0, 0, 0, 1.0f, 0.25f, 0.25f, var start = (uint)(vertices.Count / 9);
length, 0, 0, 1.0f, 0.25f, 0.25f,
0, 0, 0, 0.25f, 1.0f, 0.25f, AddVertex(vertices, a.X, a.Y, a.Z, color.X, color.Y, color.Z);
0, length, 0, 0.25f, 1.0f, 0.25f, AddVertex(vertices, b.X, b.Y, b.Z, color.X, color.Y, color.Z);
0, 0, 0, 0.25f, 0.45f, 1.0f, indices.Add(start);
0, 0, length, 0.25f, 0.45f, 1.0f, indices.Add(start + 1);
}; }
uint[] indices = AddLine(Vector3.Zero, new Vector3(length, 0, 0), new Vector3(1.0f, 0.25f, 0.25f));
{ AddLine(Vector3.Zero, new Vector3(0, length, 0), new Vector3(0.25f, 1.0f, 0.25f));
0, 1, AddLine(Vector3.Zero, new Vector3(0, 0, length), new Vector3(0.25f, 0.45f, 1.0f));
2, 3,
4, 5,
};
return CreateIndexedMesh(gl, vertices, indices, PrimitiveType.Lines); return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), PrimitiveType.Lines);
} }
public static GpuMesh CreateColoredIndexedMesh( public static GpuMesh CreateColoredIndexedMesh(
@@ -178,7 +178,7 @@ public static class MshViewportLoader
ref Vector3 boundsMax ref Vector3 boundsMax
) )
{ {
var vertexIndex = (uint)(vertices.Count / 6); var vertexIndex = (uint)(vertices.Count / 9);
vertices.Add(position.X); vertices.Add(position.X);
vertices.Add(position.Y); vertices.Add(position.Y);
@@ -93,16 +93,11 @@ public sealed class ViewportRenderer
if (grid == null || !grid.IsVisible) if (grid == null || !grid.IsVisible)
return; return;
var model = grid.LocalTransform * sceneRotation;
var mvp = model * view * projection;
_gl.Disable(EnableCap.StencilTest); _gl.Disable(EnableCap.StencilTest);
_gl.PolygonMode(TriangleFace.FrontAndBack, PolygonMode.Fill); _gl.PolygonMode(TriangleFace.FrontAndBack, PolygonMode.Fill);
_meshShader.Use(); var model = grid.LocalTransform * sceneRotation;
_meshShader.SetMatrix4(_mvpLocation, mvp); DrawMesh(grid.Mesh, model, view, projection);
grid.Mesh.Draw();
} }
private void DrawScene( private void DrawScene(