Giter Site home page Giter Site logo

debugst / stgl Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 12.91 MB

STGL is a very lightweight OpenGL library. At present, STGL only transplants the functions in OpenGL to C#, and there is no other additional Class encapsulation. So when using STGL, it feels very similar to using native OpenGL, which is very suitable for learning OpenGL.

License: MIT License

Makefile 0.17% HTML 44.31% Hack 0.96% Python 1.06% XSLT 0.44% Perl 0.02% JavaScript 0.01% PHP 0.43% CSS 0.03% C# 52.58%
csharp glfw netcore netframework opengl

stgl's Introduction

.Net35 .NetCore LICENSE

简体中文

Learn OpenGL

https://learnopengl.com/

https://learnopengl-cn.readthedocs.io/

Buiding

Since STGL's current functions are not yet perfect, the calling library will not be uploaded for the time being. The author will update this source code from time to time. If you are interested in this project, you can download this project and compile it. STGL was created with VS2010 Net3.5, so the syntax is compatible with almost all .Net platforms. The project contains three projects, developers only need to compile STLib.OpenGL.

STGL

STGL is a very lightweight OpenGL library. At present, STGL only transplants the functions in OpenGL to C#, and there is no other additional Class encapsulation. So when using STGL, it feels very similar to using native OpenGL, which is very suitable for learning OpenGL. Even you can directly copy the source code of C language from https://learnopengl.com/ to C# and make some simple modifications to run it directly, such as:

glfwXXX  replace to GLFW.XXX
GLFW_XXX replace to GLFW.XXX
glXXX    replace to GL.XXX
GL_XXX   replace to GL.GL_XXX

Of course, due to pointers, some functions are overloaded, such as: glGenXXX, glCreateXXX, glDeleteXXX, glGetXXX, and some functions that require strings or arrays Overloaded functions are contained in GL.Overload.cs

Code Organization

  • GL.Const.cs
    • All constant values in GL
  • GL.Method.DebugCallback.cs
    • Some debug callbacks in GL
  • GL.Method.Safe.cs
    • Functions that do not contain pointers in GL native functions and can be directly called by C#.
  • GL.Method.Unsafe.cs
    • GL native functions contain pointers and partially converted functions such as: void* is replaced with IntPtr.
  • GL.Method.Overload.cs
    • GL native functions contain pointer functions and are overloaded into C# basic types of functions
  • GLNative.Method.cs
    • All native functions of GL. Part of it is modified by the unsafe keyword and retains the pointer. If the function in GL.Method.XX.cs has conversion problems, you can directly use the function in GLNative.Method.cs. Of course, your code also Must be decorated with unsafe and use pointers.
  • GLNativeDelegate.cs
    • The signatures of all functions in GL, because the GL functions are obtained by GetProcAddress and converted from addresses.
  • GLNativeDeclare.cs
    • All function string names in GL correspond to Delegate, used in GL.InitAll().

All GL.Method.XX call GLNative.Method.

/// <summary>
/// [Overload] - void ShaderSource(uint shader,int count,byte** str,int* length)
/// </summary>
public unsafe static void ShaderSource(uint shader, string str) {
    GL.ShaderSource(shader, new string[] { str }, 1);
}
/// <summary>
/// [Overload] - void ShaderSource(uint shader,int count,byte** str,int* length)
/// </summary>
public unsafe static void ShaderSource(uint shader, string[] strs) {
    GL.ShaderSource(shader, strs, strs.Length);
}
/// <summary>
/// [Overload] - void ShaderSource(uint shader,int count,byte** str,int* length)
/// </summary>
public unsafe static void ShaderSource(uint shader, string[] strs, int count) {
    int[] nLens = new int[strs.Length];
    IntPtr ptr = MarshalExtend.AllocStringArr(strs, nLens, Encoding.UTF8);
    try {
        fixed (int* pNLens = &nLens[0]) {
            GLNative.glShaderSource(shader, strs.Length, (byte**)ptr, pNLens);
        }
    } finally {
        MarshalExtend.FreeStringArr(ptr, strs.Length);
    }
}

All GLNative.Method are obtained through GetProcAddress address and converted.

public unsafe static void glShaderSource(uint shader, int count, byte** str, int* length) {
    var _F = _GetProc<GLNativeDelegate.FNglShaderSource>("glShaderSource");
    _F(shader, count, str, length);
}

private static T _GetProc<T>(string strName) {
    if (!m_dic_proc.ContainsKey(strName)) {
        if (m_getter == null) {
            throw new InvalidOperationException(
                "GL is not initialized, please call [GL.Init()] or [GL.InitAll()] to initialize it first."
            );
        }
        IntPtr ptr = m_getter(strName);
        if (ptr != IntPtr.Zero) {
            var func = Marshal.GetDelegateForFunctionPointer(ptr, typeof(T));
            m_dic_proc.Add(strName, func);
            GLNative.InitedFunctions++;
        }
        // _GetProc will not write null to m_dic_proc when it cannot get Proc, but GL.InitAll will.
    }
    if (m_dic_proc.ContainsKey(strName)) {
        object obj = m_dic_proc[strName];
        if (obj != null) {
            return (T)obj;
        }
    }
    throw new PlatformNotSupportedException(
        "The [" + strName + "] function cannot be initialized. " +
        "Or maybe the current OpenGL version [" + GLNative.Info.Version + "]  does not support this function, " +
        "Of course it is also possible that the stupid author wrote the bug."
    );
}

Dependent project

STGL relies on the GLFW project. The GLFW library of Windows and Mac has been packaged in the STGL library, supporting:

  • Windows_NT_32
  • Windows_NT_64
  • Mac_X86_64
  • Mac_Arm_64

Windows

Mac

Ubuntu

When the program is run, STGL will automatically detect the current system version and application platform target and extract one of them and the LICENSE file into the runtime directory.

If you need to allow it in the Linux system, you can download the source code of GLFW and compile it:

https://www.glfw.org/docs/latest/compile_guide.html#compile_deps_x11

And you need to add -D BUILD_SHARED_LIBS=ON in CMake.

Then copy the compiled so file to the runtime directory and name it glfw3

stgl's People

Contributors

debugst avatar

Stargazers

 avatar  avatar netero avatar nana avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.