Compare commits

..

1 Commits

Author SHA1 Message Date
e45344c189 Render a triangle for testing purposes 2025-01-16 16:45:52 -06:00
7 changed files with 132 additions and 40 deletions

View File

@ -4,7 +4,6 @@
<option name="projectPerEditor">
<map>
<entry key="GLTFViewer/BaseOpenTKControl.axaml" value="GLTFViewer/GLTFViewer.csproj" />
<entry key="GLTFViewer/Engine/Ui/Controls/BaseOpenTKControl.axaml" value="GLTFViewer/GLTFViewer.csproj" />
<entry key="GLTFViewer/Engine/Ui/Controls/BasePropertyViewerControl.axaml" value="GLTFViewer/GLTFViewer.csproj" />
<entry key="GLTFViewer/Engine/Ui/Controls/BaseSceneTreeControl.axaml" value="GLTFViewer/GLTFViewer.csproj" />
<entry key="GLTFViewer/Engine/Ui/Controls/MainUI.axaml" value="GLTFViewer/GLTFViewer.csproj" />

View File

@ -15,7 +15,6 @@ A simple application to load GLTF scenes and render the model(s) in a lit scene
## Development Status
- 01/15/2025 Update 1: Successfully have Avalonia project set up with a BaseOpenTK control rendering the OpenTK context. Also have a Renerer class to seperate out the rendering code from the control, but it's empty for now. There is also some boilerplate UI for the inspector, but its only visual and will need to be filled out.
- 01/15/2025 Update 2: Have some basic UI and Node structure. Now deciding how best to load models, render them, and handling scenes.
## Development Environment
- OS: Linux (NixOS)

View File

@ -6,32 +6,22 @@ namespace GLTFViewer.Engine;
public class Node
{
public string Name { get; set; } = "Node";
public ObservableCollection<Node> Children { get; } = [];
public ObservableCollection<NodeProperty> Properties { get; } = [];
public string Name { get; }
public ObservableCollection<Node> Children { get; }
public ObservableCollection<NodeProperty> Properties { get; }
public virtual void Update()
public Node(string name)
{
foreach (var node in Children)
{
node.Update();
}
Name = name;
Children = new ObservableCollection<Node>();
Properties = new ObservableCollection<NodeProperty>();
}
public virtual void EnterScene()
public Node(string name, ObservableCollection<Node> children)
{
foreach (var node in Children)
{
node.EnterScene();
}
}
public virtual void ExitScene()
{
foreach (var node in Children)
{
node.ExitScene();
}
Name = name;
Children = children;
Properties = new ObservableCollection<NodeProperty>();
}
public void AddChild(Node child)

View File

@ -1,8 +0,0 @@
using System.Collections.ObjectModel;
namespace GLTFViewer.Engine;
public class Mesh : Node
{
}

View File

@ -8,21 +8,123 @@ namespace GLTFViewer.Rendering;
public class Renderer
{
private int _vao;
private int _vbo;
private int _shaderProgram;
public void Initialize(GlInterface gl)
{
GLLoader.LoadBindings(new AvaloniaBindingsContext(gl));
GL.ClearColor(1,1,1,1);
GL.ClearColor(1, 1, 1, 1);
// Vertex data for a triangle
float[] vertices = {
0.0f, 0.5f, 0.0f, // Top vertex
-0.5f, -0.5f, 0.0f, // Bottom left vertex
0.5f, -0.5f, 0.0f // Bottom right vertex
};
// Create and bind the VAO
_vao = GL.GenVertexArray();
GL.BindVertexArray(_vao);
// Create and bind the VBO
_vbo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsage.StaticDraw);
// Vertex shader source code
string vertexShaderSource = @"
#version 330 core
layout(location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos, 1.0);
}";
// Fragment shader source code
string fragmentShaderSource = @"
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.0, 0.0, 1.0, 1.0); // Blue color
}";
// Compile vertex shader
int vertexShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertexShader, vertexShaderSource);
GL.CompileShader(vertexShader);
CheckShaderCompileStatus(vertexShader);
// Compile fragment shader
int fragmentShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragmentShader, fragmentShaderSource);
GL.CompileShader(fragmentShader);
CheckShaderCompileStatus(fragmentShader);
// Link shaders into a program
_shaderProgram = GL.CreateProgram();
GL.AttachShader(_shaderProgram, vertexShader);
GL.AttachShader(_shaderProgram, fragmentShader);
GL.LinkProgram(_shaderProgram);
CheckProgramLinkStatus(_shaderProgram);
// Clean up shaders as they are no longer needed
GL.DeleteShader(vertexShader);
GL.DeleteShader(fragmentShader);
// Specify the layout of the vertex data
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
GL.EnableVertexAttribArray(0);
// Unbind the VAO
GL.BindVertexArray(0);
}
public void Render(GlInterface gl, int width, int height, int fbo)
{
GL.Viewport(0, 0, width, height);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// Use the shader program
GL.UseProgram(_shaderProgram);
// Bind the VAO
GL.BindVertexArray(_vao);
// Draw the triangle
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
// Unbind the VAO
GL.BindVertexArray(0);
}
public void Deinitialize(GlInterface gl)
{
GL.DeleteVertexArray(_vao);
GL.DeleteBuffer(_vbo);
GL.DeleteProgram(_shaderProgram);
}
private void CheckShaderCompileStatus(int shader)
{
GL.GetShaderi(shader, ShaderParameterName.CompileStatus, out int status);
if (status == (int)All.False)
{
GL.GetShaderInfoLog(shader, out var infoLog);
throw new Exception($"Shader compilation failed: {infoLog}");
}
}
private void CheckProgramLinkStatus(int program)
{
GL.GetProgrami(program, ProgramProperty.LinkStatus, out int status);
if (status == (int)All.False)
{
GL.GetProgramInfoLog(program, out var infoLog);
throw new Exception($"Program linking failed: {infoLog}");
}
}
private class AvaloniaBindingsContext : IBindingsContext
@ -39,5 +141,4 @@ public class Renderer
return _gl.GetProcAddress(procName);
}
}
}

View File

@ -6,8 +6,10 @@ namespace GLTFViewer.Engine.Ui;
public class SceneTree
{
public Node Root { get; } = new Node("root");
private TreeView _sceneTreeView;
private ObservableCollection<Node> _nodes = [];
private ObservableCollection<Node> _nodes;
public SceneTree(TreeView sceneTreeView)
{
@ -18,6 +20,15 @@ public class SceneTree
public void Initialize()
{
Root.AddProperty(new NodeProperty<string>("TestPropertyString", "TestValue"));
Root.AddProperty(new NodeProperty<int>("TestPropertyInt", 42));
_nodes = new ObservableCollection<Node>()
{
Root
};
_sceneTreeView.ItemsSource = _nodes;
_sceneTreeView.ItemTemplate = new FuncTreeDataTemplate<Node>(
(node, _) =>