Skip to content

Latest commit

 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

MOpenGL

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.

Features

  • Fullscreen / Windowed Mode – Select a monitor and run in fullscreen or adjust to windowed mode.
  • Shader Management
    • Load shaders from embedded resources (.vert, .frag files).
    • Inline shader support for quick prototyping.
    • Automatic compilation, error logging, and uniform inspection.
  • VAO / VBO Abstraction
    • MGL2D and MGL3D provider classes that set up vertex array objects and buffers for 2D or 3D rendering.
  • Debug Tools
    • FPS counter displayed in the debug console (when DEBUG is defined).
    • Compilation errors and uniform list are printed to the console.
  • Modern OpenGL – Uses OpenGL 3.3 core profile via OpenTK.

Requirements

Getting Started

Clone the Repository

git clone https://github.com/MdRezaV/MOpenGL.git
cd MOpenGL

Build and Run

dotnet run

The 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

Project Structure

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.

Key Classes

  • MGLWindow
    Derives from GameWindow. Initializes shaders, providers, and handles rendering. The OnRenderFrame method 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).

    • MGL2D sets up a 2D vertex attribute (2 floats per vertex).
    • MGL3D sets up a 3D vertex attribute (3 floats per vertex).
  • IShader Implementations

    • InlineShader – Contains hardcoded vertex/fragment shaders (useful for quick testing).
    • ManualShader – A shader program without attached source (used as a base by ShaderLoader).
    • 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 a ShaderType.

Adding Your Own Shaders

  1. Add a new shader file (e.g., my.vert, my.frag) to the project.

  2. Set its Build Action to Embedded resource.

  3. Load it in MGLWindow.OnLoad:

    myShader = ShaderLoader.Load(
        new ShaderFile("my.vert", ShaderType.VertexShader),
        new ShaderFile("my.frag", ShaderType.FragmentShader));
  4. Use myShader.Use() before drawing.

Rendering Custom Geometry

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 (MGL2D or MGL3D) to set up the VAO/VBO.
  • Upload vertex data using GL.BufferData.
  • Call GL.DrawArrays (or GL.DrawElements) with your preferred primitive type.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

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.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages