Skip to content

Commit

Permalink
basic cube
Browse files Browse the repository at this point in the history
  • Loading branch information
aquagoose committed Jul 22, 2023
1 parent ddd78ac commit e81923e
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/CSharp/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<AssemblyName>Tutorial</AssemblyName>
<LangVersion>9</LangVersion>
<LangVersion>latest</LangVersion>
<WarningsAsErrors>0618</WarningsAsErrors>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Numerics;
using System.Runtime.InteropServices;

namespace SelfContainedTexturedCube;

[StructLayout(LayoutKind.Sequential)]
public ref struct CameraInfo
{
public Matrix4x4 ProjectionMatrix;
public Matrix4x4 ViewMatrix;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 430 core

in vec2 frag_texCoord;

out vec4 out_color;

void main()
{
out_color = vec4(1.0, 0.5, 0.25, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#version 430 core

layout (location = 0) in vec3 aPosition;
layout (location = 1) in vec2 aTexCoord;

out vec2 frag_texCoord;

layout (std140, binding = 0) uniform CameraInfo
{
mat4 ProjectionMatrix;
mat4 ViewMatrix;
};

layout (std140, binding = 1) uniform DrawInfo
{
mat4 ModelMatrix;
};

void main()
{
gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vec4(aPosition, 1.0);
frag_texCoord = aTexCoord;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions examples/CSharp/OpenGL Demos/SelfContainedTexturedCube/Cube.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Drawing;
using System.Numerics;

namespace SelfContainedTexturedCube;

public struct Cube
{
public VertexPositionTexture[] Vertices => new[]
{
new VertexPositionTexture(new Vector3(-0.5f, 0.5f, -0.5f), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector2(1, 0)),
new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector2(1, 1)),
new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector2(0, 1)),
new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector2(1, 0)),

new VertexPositionTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector2(1, 1)),
new VertexPositionTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector2(0, 1)),
new VertexPositionTexture(new Vector3(-0.5f, 0.5f, -0.5f), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector2(1, 0)),
new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector2(1, 1)),
new VertexPositionTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector2(0, 1)),

new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector2(1, 0)),
new VertexPositionTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector2(1, 1)),
new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector2(0, 1)),
new VertexPositionTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3(-0.5f, 0.5f, -0.5f), new Vector2(1, 0)),

new VertexPositionTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector2(1, 1)),
new VertexPositionTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector2(0, 1)),
new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector2(1, 0)),
new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector2(1, 1)),
new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector2(0, 1))
};

public uint[] Indices => new uint[]
{
0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
8, 9, 10, 8, 10, 11,
12, 13, 14, 12, 14, 15,
16, 17, 18, 16, 18, 19,
20, 21, 22, 20, 22, 23
};
}
125 changes: 120 additions & 5 deletions examples/CSharp/OpenGL Demos/SelfContainedTexturedCube/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System.Drawing;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Drawing;
using System.IO;
using System.Numerics;
using Silk.NET.Maths;
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
Expand All @@ -10,12 +16,26 @@ public class Program
private static IWindow _window;
private static GL _gl;

private static uint _vao;
private static uint _vbo;
private static uint _ebo;

private static uint _cameraInfoBuffer;
private static uint _modelMatrixBuffer;

private static uint _program;

private static uint _texture;

private static Quaternion _rotation;

public static void Main(string[] args)
{
WindowOptions options = WindowOptions.Default with
{
Title = "Self Contained Textured Cube Demo",
Size = new Vector2D<int>(1280, 720)
Size = new Vector2D<int>(1280, 720),
API = new GraphicsAPI(ContextAPI.OpenGL, ContextProfile.Core, ContextFlags.Default, new APIVersion(4, 3))
};

_window = Window.Create(options);
Expand All @@ -28,20 +48,115 @@ public static void Main(string[] args)
_window.Dispose();
}

private static void Initialize()
private static unsafe void Initialize()
{
_gl = _window.CreateOpenGL();

_gl.ClearColor(Color.Indigo);

_vao = _gl.GenVertexArray();
_gl.BindVertexArray(_vao);

Cube cube = new Cube();

_vbo = _gl.GenBuffer();
_ebo = _gl.GenBuffer();

_gl.BindBuffer(BufferTargetARB.ArrayBuffer, _vbo);
_gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _ebo);

fixed (VertexPositionTexture* vertices = cube.Vertices)
_gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint) (cube.Vertices.Length * sizeof(VertexPositionTexture)), vertices, BufferUsageARB.StaticDraw);

fixed (uint* indices = cube.Indices)
_gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint) (cube.Indices.Length * sizeof(uint)), indices, BufferUsageARB.StaticDraw);

CameraInfo camera = new CameraInfo()
{
ProjectionMatrix = Matrix4x4.CreatePerspectiveFieldOfView(75 * MathF.PI / 180, _window.Size.X / (float) _window.Size.Y, 0.1f, 100f),
ViewMatrix = Matrix4x4.CreateLookAt(new Vector3(0, -3, 0), Vector3.Zero, Vector3.UnitX)
};

_cameraInfoBuffer = _gl.GenBuffer();
_gl.BindBuffer(BufferTargetARB.UniformBuffer, _cameraInfoBuffer);
_gl.BufferData(BufferTargetARB.UniformBuffer, (nuint) sizeof(CameraInfo), &camera, BufferUsageARB.StaticDraw);

Matrix4x4 modelMatrix = Matrix4x4.Identity;

_modelMatrixBuffer = _gl.GenBuffer();
_gl.BindBuffer(BufferTargetARB.UniformBuffer, _modelMatrixBuffer);
_gl.BufferData(BufferTargetARB.UniformBuffer, (nuint) sizeof(Matrix4x4), &modelMatrix, BufferUsageARB.DynamicDraw);

_gl.BindBuffer(BufferTargetARB.UniformBuffer, 0);

uint vertexShader = CreateShader(ShaderType.VertexShader, File.ReadAllText("Content/Cube.vert"));
uint fragmentShader = CreateShader(ShaderType.FragmentShader, File.ReadAllText("Content/Cube.frag"));

_program = _gl.CreateProgram();
_gl.AttachShader(_program, vertexShader);
_gl.AttachShader(_program, fragmentShader);
_gl.LinkProgram(_program);

_gl.GetProgram(_program, ProgramPropertyARB.LinkStatus, out int status);
if (status != (int) GLEnum.True)
throw new Exception($"Failed to link program! {_gl.GetProgramInfoLog(_program)}");

_gl.DetachShader(_program, vertexShader);
_gl.DetachShader(_program, fragmentShader);
_gl.DeleteShader(vertexShader);
_gl.DeleteShader(fragmentShader);

_gl.UseProgram(_program);

const uint positionAttribLocation = 0;
_gl.EnableVertexAttribArray(positionAttribLocation);
_gl.VertexAttribPointer(positionAttribLocation, 3, VertexAttribPointerType.Float, false, (uint) sizeof(VertexPositionTexture), (void*) 0);

const uint texCoordAttribLocation = 1;
_gl.EnableVertexAttribArray(texCoordAttribLocation);
_gl.VertexAttribPointer(texCoordAttribLocation, 2, VertexAttribPointerType.Float, false, (uint) sizeof(VertexPositionTexture), (void*) 12);

_gl.BindVertexArray(0);
_gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0);
_gl.BindBuffer(GLEnum.ElementArrayBuffer, 0);

_rotation = Quaternion.Identity;
}

private static void Update(double dt)
{

_rotation *= Quaternion.CreateFromAxisAngle(Vector3.UnitY, 1 * (float) dt) *
Quaternion.CreateFromAxisAngle(Vector3.UnitX, 1f * (float) dt);
}

private static void Draw(double dt)
private static unsafe void Draw(double dt)
{
Matrix4x4 modelMatrix = Matrix4x4.CreateFromQuaternion(_rotation);
_gl.BindBuffer(BufferTargetARB.UniformBuffer, _modelMatrixBuffer);
_gl.BufferSubData(BufferTargetARB.UniformBuffer, 0, (nuint) sizeof(Matrix4x4), &modelMatrix);
_gl.BindBuffer(BufferTargetARB.UniformBuffer, 0);

_gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

_gl.BindVertexArray(_vao);
_gl.UseProgram(_program);

_gl.BindBufferBase(BufferTargetARB.UniformBuffer, 0, _cameraInfoBuffer);
_gl.BindBufferBase(BufferTargetARB.UniformBuffer, 1, _modelMatrixBuffer);

_gl.DrawElements(PrimitiveType.Triangles, 36, DrawElementsType.UnsignedInt, null);
}

private static uint CreateShader(ShaderType type, string source)
{
uint shader = _gl.CreateShader(type);
_gl.ShaderSource(shader, source);
_gl.CompileShader(shader);

_gl.GetShader(shader, ShaderParameterName.CompileStatus, out int status);
if (status != (int) GLEnum.True)
throw new Exception($"Shader of type {type} failed to compile! {_gl.GetShaderInfoLog(shader)}");

return shader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup><!--<ProjectReference Include="..\..\..\..\src\Input\Silk.NET.Input\Silk.NET.Input.csproj" />
Expand All @@ -15,5 +16,11 @@

<PackageReference Include="Silk.NET.Windowing" Version="2.17.1" />
</ItemGroup>

<ItemGroup>
<Content Include="Content/**/*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Numerics;
using System.Runtime.InteropServices;

namespace SelfContainedTexturedCube;

[StructLayout(LayoutKind.Sequential)]
public struct VertexPositionTexture
{
public Vector3 Position;
public Vector2 TexCoord;

public VertexPositionTexture(Vector3 position, Vector2 texCoord)
{
Position = position;
TexCoord = texCoord;
}

// sizeof(Vector3) + sizeof(Vector2)
public const uint SizeInBytes = 20;
}

0 comments on commit e81923e

Please sign in to comment.