Skip to content

Commit f20a02a

Browse files
committed
create basic opengl window for visual tests
1 parent 38a4715 commit f20a02a

File tree

11 files changed

+154
-7
lines changed

11 files changed

+154
-7
lines changed

Diff for: OpenVR.NET.sln

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenVR.NET", "OpenVR.NET\Op
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{B5BFC04A-389E-408C-87F5-9BA8276F127A}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisualTests", "VisualTests\VisualTests.csproj", "{6C12C228-7E1D-4871-8F7E-0A81BB4D5960}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{B5BFC04A-389E-408C-87F5-9BA8276F127A}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{B5BFC04A-389E-408C-87F5-9BA8276F127A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{B5BFC04A-389E-408C-87F5-9BA8276F127A}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{6C12C228-7E1D-4871-8F7E-0A81BB4D5960}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{6C12C228-7E1D-4871-8F7E-0A81BB4D5960}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{6C12C228-7E1D-4871-8F7E-0A81BB4D5960}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{6C12C228-7E1D-4871-8F7E-0A81BB4D5960}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

Diff for: Tests/Actions.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace Tests;
1+
namespace Tests;
82

93
public enum Actions {
104
Boolean,

Diff for: VisualTests/Graphics/Shader.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace VisualTests.Graphics;
2+
3+
public class Shader {
4+
public readonly GlHandle Handle;
5+
6+
public Shader ( string vertPath, string fragPath ) {
7+
var vertSource = File.ReadAllText( vertPath );
8+
var fragSource = File.ReadAllText( fragPath );
9+
10+
GlHandle vert = GL.CreateShader( ShaderType.VertexShader );
11+
GlHandle frag = GL.CreateShader( ShaderType.FragmentShader );
12+
13+
GL.ShaderSource( vert, vertSource );
14+
GL.ShaderSource( frag, fragSource );
15+
16+
Handle = GL.CreateProgram();
17+
GL.AttachShader( Handle, vert );
18+
GL.AttachShader( Handle, frag );
19+
GL.LinkProgram( Handle );
20+
21+
GL.DeleteShader( vert );
22+
GL.DeleteShader( frag );
23+
}
24+
25+
public void Bind () {
26+
GL.UseProgram( Handle );
27+
}
28+
}

Diff for: VisualTests/Program.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using VisualTests;
2+
3+
using var program = new TestWindow( new() {
4+
IsMultiThreaded = false,
5+
RenderFrequency = 120,
6+
UpdateFrequency = 240
7+
}, new() {
8+
9+
} );
10+
11+
program.Run();

Diff for: VisualTests/Resources/Shaders/basic.frag

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#version 330 core
2+
out vec4 FragColor;
3+
4+
void main()
5+
{
6+
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
7+
}

Diff for: VisualTests/Resources/Shaders/basic.vert

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 aPos;
3+
4+
void main()
5+
{
6+
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
7+
}

Diff for: VisualTests/Resources/Textures/susie.png

2.63 KB
Loading

Diff for: VisualTests/TestWindow.cs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using OpenTK.Windowing.Common;
2+
using OpenTK.Windowing.Desktop;
3+
using VisualTests.Graphics;
4+
using VisualTests.Vetrices;
5+
6+
namespace VisualTests;
7+
internal class TestWindow : GameWindow {
8+
public TestWindow ( GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings )
9+
: base( gameWindowSettings, nativeWindowSettings ) {
10+
11+
basicShader = new( "Resources/Shaders/basic.vert", "Resources/Shaders/basic.frag" );
12+
}
13+
14+
Shader basicShader;
15+
16+
Vector3[] triangleData = null!;
17+
GlHandle VBO;
18+
GlHandle VAO;
19+
protected override void OnLoad () {
20+
base.OnLoad();
21+
22+
triangleData = new Vector3[] {
23+
new( -0.5f, -0.5f, 0.0f ),
24+
new( 0.5f, -0.5f, 0.0f ),
25+
new( 0.0f, 0.5f, 0.0f )
26+
};
27+
VBO = GL.GenBuffer();
28+
GL.BindBuffer( BufferTarget.ArrayBuffer, VBO );
29+
PositionVertex.Upload( triangleData, triangleData.Length );
30+
31+
VAO = GL.GenVertexArray();
32+
GL.BindVertexArray( VAO );
33+
PositionVertex.Link( position: 0 );
34+
}
35+
36+
float time;
37+
protected override void OnRenderFrame ( FrameEventArgs args ) {
38+
base.OnRenderFrame( args );
39+
var deltaTime = (float)args.Time;
40+
time += deltaTime;
41+
42+
GL.Viewport( 0, 0, Size.X, Size.Y );
43+
GL.ClearColor( 0.2f, 0.3f, 0.3f, 1.0f );
44+
GL.Clear( ClearBufferMask.ColorBufferBit );
45+
46+
basicShader.Bind();
47+
GL.BindVertexArray( VAO );
48+
GL.DrawArrays( PrimitiveType.Triangles, 0, 3 );
49+
50+
SwapBuffers();
51+
}
52+
}

Diff for: VisualTests/Vetrices/PositionVertex.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace VisualTests.Vetrices;
2+
3+
public static class PositionVertex {
4+
public static readonly int Stride = sizeof( float ) * 3;
5+
public static void Upload ( Vector3[] data, int length, BufferUsageHint usage = BufferUsageHint.StaticDraw ) {
6+
GL.BufferData( BufferTarget.ArrayBuffer, length * Stride, data, usage );
7+
}
8+
9+
public static void Link ( int position = 0 ) {
10+
GL.VertexAttribPointer( position, 3, VertexAttribPointerType.Float, false, Stride, 0 );
11+
GL.EnableVertexAttribArray( position );
12+
}
13+
}

Diff for: VisualTests/VisualTests.csproj

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<None Remove="Resources\Shaders\basic.frag" />
12+
<None Remove="Resources\Shaders\basic.vert" />
13+
<None Remove="Resources\Textures\susie.png" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<Content Include="Resources\**">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</Content>
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="OpenTK" Version="4.7.2" />
24+
</ItemGroup>
25+
26+
</Project>

Diff for: VisualTests/global.cs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global using OpenTK.Graphics.ES30;
2+
global using OpenTK.Mathematics;
3+
global using GlHandle = System.Int32;

0 commit comments

Comments
 (0)