# LightGL **Repository Path**: unknowall/LightGL ## Basic Information - **Project Name**: LightGL - **Description**: 一个轻量、高性能的 OpenGL C# 库 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-30 - **Last Updated**: 2026-01-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: Opengl, Wrapper-Library ## README # LightGL - A Lightweight OpenGL C# Wrapper Library [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) [![Build Pass](https://img.shields.io/badge/Build-Pass-brightgreen)](https://github.com/unknowall/LightGL) [![NuGet](https://img.shields.io/nuget/v/LightGL)](https://www.nuget.org/packages/LightGL/) [![GitHub NuGet Package](https://img.shields.io/badge/GitHub%20Package-LightGL-blue)](https://github.com/unknowall/LightGL/pkgs/nuget/LightGL) [![zread](https://img.shields.io/badge/Ask_Zread-_.svg?style=flat&color=00b0aa&labelColor=000000&logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHZpZXdCb3g9IjAgMCAxNiAxNiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTQuOTYxNTYgMS42MDAxSDIuMjQxNTZDMS44ODgxIDEuNjAwMSAxLjYwMTU2IDEuODg2NjQgMS42MDE1NiAyLjI0MDFWNC45NjAxQzEuNjAxNTYgNS4zMTM1NiAxLjg4ODEgNS42MDAxIDIuMjQxNTYgNS42MDAxSDQuOTYxNTZDNS4zMTUwMiA1LjYwMDEgNS42MDE1NiA1LjMxMzU2IDUuNjAxNTYgNC45NjAxVjIuMjQwMUM1LjYwMTU2IDEuODg2NjQgNS4zMTUwMiAxLjYwMDEgNC45NjE1NiAxLjYwMDFaIiBmaWxsPSIjZmZmIi8%2BCjxwYXRoIGQ9Ik00Ljk2MTU2IDEwLjM5OTlIMi4yNDE1NkMxLjg4ODEgMTAuMzk5OSAxLjYwMTU2IDEwLjY4NjQgMS42MDE1NiAxMS4wMzk5VjEzLjc1OTlDMS42MDE1NiAxNC4xMTM0IDEuODg4MSAxNC4zOTk5IDIuMjQxNTYgMTQuMzk5OUg0Ljk2MTU2QzUuMzE1MDIgMTQuMzk5OSA1LjYwMTU2IDE0LjExMzQgNS42MDE1NiAxMy43NTk5VjExLjAzOTlDNS42MDE1NiAxMC42ODY0IDUuMzE1MDIgMTAuMzk5OSA0Ljk2MTU2IDEwLjM5OTlaIiBmaWxsPSIjZmZmIi8%2BCjxwYXRoIGQ9Ik0xMy43NTg0IDEuNjAwMUgxMS4wMzg0QzEwLjY4NSAxLjYwMDEgMTAuMzk4NCAxLjg4NjY0IDEwLjM5ODQgMi4yNDAxVjQuOTYwMUMxMC4zOTg0IDUuMzEzNTYgMTAuNjg1IDUuNjAwMSAxMS4wMzg0IDUuNjAwMUgxMy43NTg0QzE0LjExMTkgNS42MDAxIDE0LjM5ODQgNS4zMTM1NiAxNC4zOTg0IDQuOTYwMVYyLjI0MDFDMTQuMzk4NCAxLjg4NjY0IDE0LjExMTkgMS42MDAxIDEzLjc1ODQgMS42MDAxWiIgZmlsbD0iI2ZmZiIvPgo8cGF0aCBkPSJNNCAxMkwxMiA0TDQgMTJaIiBmaWxsPSIjZmZmIi8%2BCjxwYXRoIGQ9Ik00IDEyTDEyIDQiIHN0cm9rZT0iI2ZmZiIgc3Ryb2tlLXdpZHRoPSIxLjUiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIvPgo8L3N2Zz4K&logoColor=ffffff)](https://zread.ai/unknowall/LightGL) LightGL is a **lightweight C# wrapper library for OpenGL**, offering a concise, efficient, and cross-platform graphics rendering solution. LightGL is built specifically for high-performance graphics rendering. Its call efficiency is almost identical to that of native C/C++ implementations, outperforming general-purpose OpenGL wrapper libraries. **file size: LightGL.1.0.2.nupkg - 54kb** #### Wiki(for 1.0.2): https://deepwiki.com/unknowall/LightGL , https://zread.ai/unknowall/LightGL

🌐 中文版说明

LightGL是一个针对**OpenGL的C#轻量封装库**,提供简洁、高效、跨平台的图形渲染解决方案。 LightGL专为高性能图形渲染打造,相比通用 OpenGL 封装库,调用效率和原生 C/C++ 调用几乎无差。 **包大小:LightGL.1.0.2.nupkg - 54kb** #### Wiki: https://zread.ai/unknowall/LightGL , https://deepwiki.com/unknowall/LightGL ## 核心特性 ### 🚀 轻量无依赖 - 纯C#实现的OpenGL封装,**无任何第三方依赖**,仅依赖系统原生OpenGL驱动和.NET运行时 - 精简代码结构,核心逻辑聚焦渲染本质,易于理解和扩展 - 生成库文件体积小巧,部署便捷,无额外依赖负担 ### 🌍 多平台支持 - 统一接口设计,适配Windows、Linux、macOS等主流操作系统 - 基于平台抽象层实现OpenGL上下文管理,无需修改代码即可跨平台运行 - 兼容OpenGL 3.2+,适配主流显卡驱动 - 统一的 IGlContext 接口为不同操作系统提供了专门的实现: - Windows 平台:使用 WGL 实现,支持 OpenGL 3.2 + - Linux 平台:使用 GLX 实现,支持 X11 窗口系统,提供了完整的 Linux 平台 OpenGL 上下文管理 - macOS 平台:使用 Cocoa 的 NSOpenGLContext 实现,支持 macOS 的原生 OpenGL 框架 - Android 平台:使用 EGL(Embedded System Graphics Library)实现 ### 📦 完整功能封装 - 核心功能覆盖:着色器管理、缓冲区对象、纹理处理、矩阵/向量运算等 - 提供简洁API,降低OpenGL原生接口的使用复杂度 - 包含可直接运行的Demo项目,演示完整渲染流程 ### 🎯 实际项目验证 - 已在真实项目中落地使用: - PS1模拟器 ScePSX https://github.com/unknowall/ScePSX - PSP模拟器 ScePSP https://github.com/unknowall/ScePSP - Demo项目提供完整示例代码,快速上手无门槛 ## 环境要求 - **开发环境**:Visual Studio 2022+ 或支持.NET 6.0+的IDE - **运行环境**:.NET 6.0+ 运行时 - **硬件要求**:支持OpenGL 3.2+的显卡及对应驱动 ## 快速开始 ### 1. 获取项目 git: ```bash git clone https://github.com/unknowall/LightGL.git ``` nuget: ```bash git clone https://github.com/unknowall/LightGL.git ``` nuget: ```bash dotnet add package LightGL --version 1.0.4 ``` ### 2. 编译项目 - 打开解决方案 `LightGL.sln` - 选择目标平台(Windows/Linux/macOS) - 编译生成 ### 3. 集成使用 在你的C#项目中引用 LightGL,参考以下示例代码快速实现基础渲染: ```csharp // 1. 创建OpenGL上下文(以窗口句柄为例) IntPtr windowHandle = YourWindow.GetHandle(); // 替换为你的窗口句柄 IGlContext context = GlContextFactory.CreateFromWindowHandle(windowHandle, 3, 2, GlProfile.Core); context.MakeCurrent(); // 2. 初始化着色器 var vertexShaderCode = @" attribute vec4 position; attribute vec2 texCoord; varying vec2 v_texCoord; void main() { gl_Position = position; v_texCoord = texCoord; }"; var fragmentShaderCode = @" uniform sampler2D u_texture; varying vec2 v_texCoord; void main() { gl_FragColor = texture2D(u_texture, v_texCoord); }"; GLShader shader = new GLShader(vertexShaderCode, fragmentShaderCode); // 3. 创建顶点缓冲区 float[] vertices = { -1.0f, -1.0f, 0.0f, 0.0f, // 左下 1.0f, -1.0f, 1.0f, 0.0f, // 右下 1.0f, 1.0f, 1.0f, 1.0f, // 右上 -1.0f, 1.0f, 0.0f, 1.0f // 左上 }; GLBuffer vertexBuffer = GLBuffer.Create(); vertexBuffer.SetData(vertices); // 4. 创建纹理 GLTexture2D texture = GLTexture2D.Create() .SetFormat(TextureFormat.RGBA) .SetSize(2, 2) .SetData(new uint[] { 0xFF0000FF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFFFF }); // 5. 渲染循环 bool running = true; while (running) { // 清除缓冲区 GL.ClearColor(0.1f, 0.1f, 0.1f, 1.0f); GL.Clear(GL.GL_COLOR_BUFFER_BIT); // 绑定资源并绘制 shader.Use(); shader.SetUniform("u_texture", texture); vertexBuffer.Bind(); GL.DrawArrays(GL.GL_TRIANGLE_FAN, 0, 4); // 交换缓冲区 context.SwapBuffers(); // 处理窗口事件(根据你的窗口框架实现) HandleWindowEvents(ref running); } // 资源释放 shader.Dispose(); vertexBuffer.Dispose(); texture.Dispose(); context.Dispose(); ``` ## 项目结构 ``` LightGL/ ├── Demo/ # 演示项目(完整可运行) ├── GLControl/ # 可视化渲染控件(Windows Forms控件) ├── LightGL/ # 核心库源码 │ ├── Platform/ # 平台适配层(Windows/Linux/macOS) │ ├── Utils/ # 工具类(矩阵、向量、纹理等) │ ├── GL.cs # OpenGL核心封装 │ ├── IGlContext.cs # 跨平台上下文接口 │ └── GlContextFactory.cs # 上下文创建工厂 ├── LightGL.sln # 解决方案文件 ├── LICENSE.txt # 开源许可证 └── README.md # 项目说明文档 ``` ## 运行Demo 1. 编译解决方案后,启动 `Demo` 项目 2. Demo将展示:窗口创建、上下文初始化、着色器编译、纹理渲染等核心流程 3. 可直接参考Demo源码,快速迁移到你的项目中 ## 贡献指南 1. **提交Issue**:报告Bug、提出功能建议或疑问 2. **提交PR**: - Fork本仓库 - 创建特性分支(`feature/xxx`)或修复分支(`fix/xxx`) - 提交代码并确保编译通过 - 发起Pull Request,描述修改内容 3. 代码风格:遵循C#编码规范,保持代码简洁可读 ## 许可证 本项目采用 **MIT License** 开源协议,允许: - 商业使用、修改、分发 - 私人使用 - 无需公开修改后的源码(但需保留原版权声明) ## 联系与反馈 - 如有问题或建议,欢迎提交Issue或Pull Request ------
## Core Features ### 🚀 Lightweight & Dependency-Free - Pure C# implementation of OpenGL wrapper with **zero third-party dependencies**, relying solely on system-native OpenGL drivers and the .NET runtime - Streamlined code structure with core logic focused on the essence of rendering, facilitating easy understanding and extension - Compact generated library size for hassle-free deployment with no extra dependency burden ### 🌍 Cross-Platform Support - Unified interface design compatible with mainstream operating systems including Windows, Linux, and macOS - OpenGL context management implemented via a platform abstraction layer, enabling cross-platform operation without code modification - Compatible with OpenGL 3.2+ and mainstream graphics card drivers - The unified `IGlContext` interface provides dedicated implementations for different operating systems: - **Windows**: Built on WGL, supporting OpenGL 3.2+ - **Linux**: Built on GLX, supporting the X11 window system with complete OpenGL context management for Linux platforms - **macOS**: Built on Cocoa’s `NSOpenGLContext`, supporting macOS’s native OpenGL framework - **Android**: Built on EGL (Embedded System Graphics Library) interface ### 📦 Comprehensive Feature Wrapper - Core functionality coverage: shader management, buffer objects, texture processing, matrix/vector operations, etc. - Offers a simplified API that reduces the complexity of using native OpenGL interfaces - Includes out-of-the-box demo projects demonstrating complete rendering workflows ### 🎯 Battle-Tested in Real-World Projects - Proven in production-grade applications: - PS1 Emulator ScePSX: https://github.com/unknowall/ScePSX - PSP Emulator ScePSP: https://github.com/unknowall/ScePSP - Demo projects provide complete sample code for quick and barrier-free onboarding ## Environment Requirements - **Development Environment**: Visual Studio 2022+ or any IDE supporting .NET 6.0+ - **Runtime Environment**: .NET 6.0+ runtime - **Hardware Requirements**: Graphics card supporting OpenGL 3.2+ with corresponding drivers installed ## Quick Start ### 1. Get the Project git: ```bash git clone https://github.com/unknowall/LightGL.git ``` nuget: ```bash dotnet add package LightGL --version 1.0.4 ``` ### 2. Build the Project - Open the solution `LightGL.sln` - Select the target platform (Windows/Linux/macOS) - Build the project ### 3. Integration & Usage Reference `LightGL` in your C# project, and use the following sample code to quickly implement basic rendering: ```csharp // 1. Create OpenGL context (window handle example) IntPtr windowHandle = YourWindow.GetHandle(); // Replace with your actual window handle IGlContext context = GlContextFactory.CreateFromWindowHandle(windowHandle, 3, 2, GlProfile.Core); context.MakeCurrent(); // 2. Initialize shaders var vertexShaderCode = @" attribute vec4 position; attribute vec2 texCoord; varying vec2 v_texCoord; void main() { gl_Position = position; v_texCoord = texCoord; }"; var fragmentShaderCode = @" uniform sampler2D u_texture; varying vec2 v_texCoord; void main() { gl_FragColor = texture2D(u_texture, v_texCoord); }"; GLShader shader = new GLShader(vertexShaderCode, fragmentShaderCode); // 3. Create vertex buffer float[] vertices = { -1.0f, -1.0f, 0.0f, 0.0f, // Bottom-left 1.0f, -1.0f, 1.0f, 0.0f, // Bottom-right 1.0f, 1.0f, 1.0f, 1.0f, // Top-right -1.0f, 1.0f, 0.0f, 1.0f // Top-left }; GLBuffer vertexBuffer = GLBuffer.Create(); vertexBuffer.SetData(vertices); // 4. Create texture GLTexture2D texture = GLTexture2D.Create() .SetFormat(TextureFormat.RGBA) .SetSize(2, 2) .SetData(new uint[] { 0xFF0000FF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFFFF }); // 5. Render loop bool running = true; while (running) { // Clear buffers GL.ClearColor(0.1f, 0.1f, 0.1f, 1.0f); GL.Clear(GL.GL_COLOR_BUFFER_BIT); // Bind resources and draw shader.Use(); shader.SetUniform("u_texture", texture); vertexBuffer.Bind(); GL.DrawArrays(GL.GL_TRIANGLE_FAN, 0, 4); // Swap buffers context.SwapBuffers(); // Handle window events (implement based on your windowing framework) HandleWindowEvents(ref running); } // Release resources shader.Dispose(); vertexBuffer.Dispose(); texture.Dispose(); context.Dispose(); ``` ## Project Structure ``` LightGL/ ├── Demo/ # Demo projects (fully runnable) ├── GLControl/ # Visual rendering control (Windows Forms control) ├── LightGL/ # Core library source code │ ├── Platform/ # Platform abstraction layer (Windows/Linux/macOS) │ ├── Utils/ # Utility classes (matrix, vector, texture, etc.) │ ├── GL.cs # Core OpenGL wrapper │ ├── IGlContext.cs # Cross-platform context interface │ └── GlContextFactory.cs # Context creation factory ├── LightGL.sln # Solution file ├── LICENSE.txt # Open source license └── README.md # Project documentation ``` ## Run the Demo 1. After building the solution, launch the `Demo` project 2. The demo showcases core workflows: window creation, context initialization, shader compilation, texture rendering, etc. 3. Demo source code can be directly referenced for quick migration to your own projects ## Contribution Guidelines 1. **Submit Issues**: Report bugs, suggest features, or ask questions 2. **Submit Pull Requests (PRs)**: - Fork this repository - Create a feature branch (`feature/xxx`) or a bugfix branch (`fix/xxx`) - Commit your code and ensure successful compilation - Open a Pull Request with a clear description of your changes 3. **Code Style**: Follow C# coding standards to maintain concise and readable code ## License This project is licensed under the **MIT License**, which permits: - Commercial use, modification, and distribution - Private use - No requirement to disclose modified source code (original copyright notice must be retained) ## Contact & Feedback - For questions or suggestions, feel free to submit Issues or Pull Requests