A minimal OpenGL framework for C# using OpenTK that simplifies shader management, vertex buffer handling, and provides a ready-to-use window with fullscreen support.
- Fullscreen / Windowed Mode – Select a monitor and run in fullscreen or adjust to windowed mode.
- Shader Management
- Load shaders from embedded resources (
.vert,.fragfiles). - Inline shader support for quick prototyping.
- Automatic compilation, error logging, and uniform inspection.
- Load shaders from embedded resources (
- VAO / VBO Abstraction
MGL2DandMGL3Dprovider classes that set up vertex array objects and buffers for 2D or 3D rendering.
- Debug Tools
- FPS counter displayed in the debug console (when
DEBUGis defined). - Compilation errors and uniform list are printed to the console.
- FPS counter displayed in the debug console (when
- Modern OpenGL – Uses OpenGL 3.3 core profile via OpenTK.
- .NET 7.0 SDK
- OpenTK 4.8.0 (included via NuGet)
git clone https://github.com/MdRezaV/MOpenGL.git
cd MOpenGLdotnet runThe application will launch in fullscreen mode on the second monitor (index 1). If you have only one monitor, change the index in Program.cs:
CurrentMonitor = Monitors.GetMonitors()[0].Handle // Use the primary monitor| File | Description |
|---|---|
Program.cs |
Entry point – creates and runs the MGLWindow. |
MGLWindow.cs |
Main window class: handles OpenGL context, rendering loop, shader loading, and provider management. |
MOpenGL.csproj |
Project file with .NET 7.0 and OpenTK reference. |
-
MGLWindow
Derives fromGameWindow. Initializes shaders, providers, and handles rendering. TheOnRenderFramemethod currently draws a hardcoded quad using a triangle fan (demonstration only – replace with your own rendering logic). -
IProvider/MGL2D/MGL3D
Manage OpenGL vertex array objects (VAOs) and vertex buffer objects (VBOs).MGL2Dsets up a 2D vertex attribute (2 floats per vertex).MGL3Dsets up a 3D vertex attribute (3 floats per vertex).
-
IShaderImplementationsInlineShader– Contains hardcoded vertex/fragment shaders (useful for quick testing).ManualShader– A shader program without attached source (used as a base byShaderLoader).ShaderLoader– Loads shaders from embedded resources, compiles them, links the program, and logs errors/uniforms.
-
ShaderFile
Simple DTO that pairs a file name (e.g.,"default.vert") with aShaderType.
-
Add a new shader file (e.g.,
my.vert,my.frag) to the project. -
Set its Build Action to Embedded resource.
-
Load it in
MGLWindow.OnLoad:myShader = ShaderLoader.Load( new ShaderFile("my.vert", ShaderType.VertexShader), new ShaderFile("my.frag", ShaderType.FragmentShader));
-
Use
myShader.Use()before drawing.
The default rendering code in OnRenderFrame uploads a hardcoded quad and draws it. Replace that with your own vertex data and drawing calls.
- Bind a provider (
MGL2DorMGL3D) to set up the VAO/VBO. - Upload vertex data using
GL.BufferData. - Call
GL.DrawArrays(orGL.DrawElements) with your preferred primitive type.
This project is licensed under the MIT License. See the LICENSE file for details.