Hi I’m new to OpenTK but have som good knowledge and experience with C#.
I’m about to make an open source project and want to do it simple by using C#.
I know OpenGL and hav try it out with C++.
I’m was about to make the setup in visual Studio and got so fra that I was able to make a window with a title but when I try to setup the basics of the renderer, I got stuck.
I was following this video step by step and add som stuff by my self. Link to the video that i found
Here is the source code:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicsLibraryFramework;
using OpenTK.Mathematics;
namespace BlockyBuild {
public interface Script {
/// <summary>
/// Runs once on the first frame
/// </summary>
public void OnStart();
/// <summary>
/// Runs once on each frame update
/// </summary>
/// <param name="delta"></param>
public void OnUpdate(float delta);
/// <summary>
/// Runs once on each input event
/// </summary>
/// <param name="inputEvent"></param>
public void OnInput(string inputEvent);
}
public abstract class GameObject {
public abstract string UUID { get; set; }
public abstract Vector3i Position { get; set; }
public abstract Script Script { get; set; }
public abstract List<GameObject> Children { get; set; }
}
public struct Shader {
public int id;
}
public struct ShaderProgram {
public int id;
}
public static class Game {
private static List<GameObject> GameObjects = new List<GameObject>();
// Add a game object to the game object list
public static void AddGameObject(GameObject gameObject) {
GameObjects.Add(gameObject);
}
// Remove a game object from the game object list
public static void RemoveGameObject(GameObject gameObject) {
GameObjects.Remove(gameObject);
}
// Load shader from path
private static Shader LoadShader(string path, ShaderType type) {
int ID = GL.CreateShader(type);
try {
GL.ShaderSource(ID, File.ReadAllText(path));
GL.CompileShader(ID);
string infoLog = GL.GetShaderInfoLog(ID);
if (!string.IsNullOrEmpty(infoLog))
throw new Exception(infoLog);
} catch {
throw new Exception($"Error: No shader file found on path {path}.");
}
return new Shader() { id = ID };
}
// Load shader program from string paths
private static ShaderProgram LoadShaderProgram(string vertexShaderLocation, string fragmentShaderLocation) {
int id = GL.CreateProgram();
Shader vertexShader = LoadShader(vertexShaderLocation, ShaderType.VertexShader);
Shader fragmentShader = LoadShader(fragmentShaderLocation, ShaderType.FragmentShader);
GL.AttachShader(id, vertexShader.id);
GL.AttachShader(id, fragmentShader.id);
GL.LinkProgram(id);
GL.DetachShader(id, vertexShader.id);
GL.DetachShader(id, fragmentShader.id);
GL.DeleteShader(vertexShader.id);
GL.DeleteShader(fragmentShader.id);
string infoLog = GL.GetProgramInfoLog(id);
if (!string.IsNullOrEmpty(infoLog))
throw new Exception(infoLog);
return new ShaderProgram() { id = id };
}
static void Main(string[] args) {
// Setup game window
// set window settings
GameWindowSettings gws = GameWindowSettings.Default;
NativeWindowSettings nws = NativeWindowSettings.Default;
// Set max FPS (0.0 means no limit)
gws.UpdateFrequency = 0.0;
// Setup API verson
nws.APIVersion = Version.Parse("4.1.0");
nws.ClientSize = new Vector2i(1280, 720);
nws.Title = "Blocky Build";
GameWindow window = new GameWindow(gws, nws);
// Window update frame function
window.UpdateFrame += (args) => {
};
// Load function
ShaderProgram shaderProgram = new ShaderProgram() { id = 0 };
window.Load += () => {
string rootFolder = "../../../Shaders/";
shaderProgram = LoadShaderProgram(rootFolder + "vertex.glsl", rootFolder + "fragment.glsl");
};
// Render frame function
window.RenderFrame += (args) => {
GL.UseProgram(shaderProgram.id);
GL.ClearColor(1.0f, 0, 0, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
window.SwapBuffers();
};
window.Run();
}
}
}
I’m hoping that some of you can help me and tell me what I’m doing wrong. Thx.
I was trying to make a window with a read backgrund by using OpenTK 4.8.2 to test if i have set it up right.
I got a window with a white screen and a title.
I’m not sure if it is the setup that is wrong or me doing something stupit.
I’m using Visual Studio 2022 and have setup OpenTK by using NuGet.