├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── config.nims ├── opengl.nimble ├── src └── opengl.nim ├── tests ├── config.nims ├── tcomplete.nim ├── test.nim └── web │ └── shell_minimal.html └── tools ├── generator.nim └── utils.nim /.editorconfig: -------------------------------------------------------------------------------- 1 | # find your plugin here: http://editorconfig.org/ 2 | # always follow this rules 3 | 4 | root = true 5 | 6 | [*] 7 | indent_style = space 8 | indent_size = 2 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### C ### 2 | # Prerequisites 3 | *.d 4 | 5 | # Object files 6 | *.o 7 | *.ko 8 | *.obj 9 | *.elf 10 | 11 | # Linker output 12 | *.ilk 13 | *.map 14 | *.exp 15 | 16 | # Precompiled Headers 17 | *.gch 18 | *.pch 19 | 20 | # Executables 21 | *.exe 22 | *.out 23 | *.app 24 | *.i*86 25 | *.x86_64 26 | *.hex 27 | 28 | # Debug files 29 | *.dSYM/ 30 | *.su 31 | *.idb 32 | *.pdb 33 | 34 | # Kernel Module Compile Results 35 | *.mod* 36 | *.cmd 37 | .tmp_versions/ 38 | modules.order 39 | Module.symvers 40 | Mkfile.old 41 | dkms.conf 42 | 43 | ### C++ ### 44 | # Prerequisites 45 | 46 | # Compiled Object files 47 | *.slo 48 | 49 | # Precompiled Headers 50 | 51 | # Compiled Dynamic libraries 52 | 53 | # Fortran module files 54 | *.mod 55 | *.smod 56 | 57 | # Compiled Static libraries 58 | *.lai 59 | 60 | # Executables 61 | 62 | ### Linux ### 63 | *~ 64 | 65 | # temporary files which can be created if a process still has a handle open of a deleted file 66 | .fuse_hidden* 67 | 68 | # KDE directory preferences 69 | .directory 70 | 71 | # Linux trash folder which might appear on any partition or disk 72 | .Trash-* 73 | 74 | # .nfs files are created when an open file is removed but is still being accessed 75 | .nfs* 76 | 77 | ### macOS ### 78 | *.DS_Store 79 | .AppleDouble 80 | .LSOverride 81 | 82 | # Icon must end with two \r 83 | Icon 84 | 85 | # Thumbnails 86 | ._* 87 | 88 | # Files that might appear in the root of a volume 89 | .DocumentRevisions-V100 90 | .fseventsd 91 | .Spotlight-V100 92 | .TemporaryItems 93 | .Trashes 94 | .VolumeIcon.icns 95 | .com.apple.timemachine.donotpresent 96 | 97 | # Directories potentially created on remote AFP share 98 | .AppleDB 99 | .AppleDesktop 100 | Network Trash Folder 101 | Temporary Items 102 | .apdisk 103 | 104 | ### Nim ### 105 | nimcache/ 106 | 107 | ### OSX ### 108 | 109 | # Icon must end with two \r 110 | 111 | # Thumbnails 112 | 113 | # Files that might appear in the root of a volume 114 | 115 | # Directories potentially created on remote AFP share 116 | 117 | ### VisualStudioCode ### 118 | .vscode/* 119 | !.vscode/settings.json 120 | !.vscode/tasks.json 121 | !.vscode/launch.json 122 | !.vscode/extensions.json 123 | .history 124 | 125 | ### Windows ### 126 | # Windows thumbnail cache files 127 | Thumbs.db 128 | ehthumbs.db 129 | ehthumbs_vista.db 130 | 131 | # Folder config file 132 | Desktop.ini 133 | 134 | # Recycle Bin used on file shares 135 | $RECYCLE.BIN/ 136 | 137 | # Binaries 138 | [Bb]in/ 139 | 140 | # Windows Installer files 141 | *.cab 142 | *.msi 143 | *.msm 144 | *.msp 145 | 146 | # Windows shortcuts 147 | *.lnk 148 | 149 | # Dynamic Libraries 150 | *.dll 151 | *.so 152 | *.dylib 153 | 154 | ### NimGL ### 155 | 156 | # Tests 157 | tests/general 158 | tests/tgeneral 159 | tests/tglfw 160 | tests/test 161 | tmp/ 162 | tests/tmath 163 | tests/web/ 164 | tests/topengl 165 | tools/generator 166 | imgui.ini 167 | docs/ 168 | 169 | # End of https://www.gitignore.io/api/c,osx,nim,c++,linux,macos,windows,visualstudiocode 170 | 171 | *.xml 172 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Leonardo Mariscal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![docs](https://img.shields.io/badge/docs-passing-4caf50.svg?style=flat-square)](https://nimgl.dev/docs) 2 | 3 | # Modern OpenGL Bindings for Nim [![Nimble](https://raw.githubusercontent.com/yglukhov/nimble-tag/master/nimble.png)](https://github.com/nim-lang/nimble) 4 | 5 | Separated module from [NimGL](https://nimgl.dev/). In order to mantain small 6 | sized modules to facilitate development and reduce number of required modules. 7 | 8 | Some developers do not require the entire bundle of APIs that 9 | [NimGL](https://nimgl.dev/) offer, and so, prefere to only download the required 10 | modules. 11 | 12 | ## Installation 13 | 14 | ### Nimble download 15 | 16 | You can install this package through the official package manager of Nim. 17 | 18 | ```bash 19 | $ nimble install https://github.com/nimgl/opengl.git 20 | ``` 21 | 22 | In order to respect already existing libraries in the package registry, and 23 | because [NimGL](https://nimgl.dev/) already exists in there, this package is 24 | only able to be accesible by direct git url. 25 | 26 | ### Nimble direct install 27 | 28 | ```bash 29 | $ git clone --recursive -j8 https://github.com/nimgl/opengl.git 30 | $ cd opengl 31 | $ nimble install 32 | ``` 33 | 34 | ### NimGL module 35 | 36 | ```bash 37 | $ nimble install nimgl 38 | ``` 39 | 40 | You can find more information in the [main repo](https://github.com/nimgl/nimgl). 41 | 42 | ### Development 43 | 44 | It is currently being developed and tested on 45 | 46 | * Windows 10 47 | * MacOS Mojave 48 | * Linux Ubuntu 18.10 49 | 50 | ## Contribute 51 | 52 | I'm only one person and I use this library almost daily for school and personal 53 | projects. If you are missing some extension, procedures, bindings or anything 54 | related feel free to PR them or open an Issue with the specification and 55 | if you can some examples to have an idea on how to implement it. 56 | Thank you so much :tada: 57 | 58 | ### This being a separate module behaves slightly diferently. 59 | 60 | Please open all issues in the [main repository](https://github.com/nimgl/nimgl). 61 | The PRs and new feature development will occur in each binding's repo. 62 | 63 | ## Usage 64 | 65 | ```nim 66 | import opengl 67 | 68 | # Create a context (GLFW recommended) 69 | 70 | if glInit(): 71 | echo "OpenGL loaded correctly." 72 | 73 | glClearColor(0.68f, 1f, 0.34f, 1f) 74 | glClear(GL_COLOR_BUFFER_BIT) 75 | 76 | # You can load extensions 77 | 78 | loadGL_ARB_gl_spirv() 79 | if glSpecializeShaderARB == nil: 80 | echo "Extension not available" 81 | ``` 82 | 83 | Check out the references and doc in order to understand OpenGL usage. 84 | 85 | ## License 86 | 87 | [MIT License](https://github.com/nimgl/nimgl/blob/master/LICENSE) 88 | 89 | NimGL is open source and is under the MIT License, we highly encourage every 90 | developer that uses it to make improvements and fork them here. 91 | 92 | ## Contributors 93 | 94 | Thank you to every contributor that has spent their time improving this library. 95 | 96 | [List of all contributors.](https://github.com/nimgl/nimgl/graphs/contributors) 97 | -------------------------------------------------------------------------------- /config.nims: -------------------------------------------------------------------------------- 1 | when defined(emscripten): 2 | # This path will only run if -d:emscripten is passed to nim. 3 | 4 | --nimcache:tmp # Store intermediate files close by in the ./tmp dir. 5 | 6 | --os:linux # Emscripten pretends to be linux. 7 | --cpu:i386 # Emscripten is 32bits. 8 | --cc:clang # Emscripten is very close to clang, so we ill replace it. 9 | when defined(windows): 10 | --clang.exe:emcc.bat # Replace C 11 | --clang.linkerexe:emcc.bat # Replace C linker 12 | --clang.cpp.exe:emcc.bat # Replace C++ 13 | --clang.cpp.linkerexe:emcc.bat # Replace C++ linker. 14 | else: 15 | --clang.exe:emcc # Replace C 16 | --clang.linkerexe:emcc # Replace C linker 17 | --clang.cpp.exe:emcc # Replace C++ 18 | --clang.cpp.linkerexe:emcc # Replace C++ linker. 19 | --listCmd # List what commands we are running so that we can debug them. 20 | 21 | --gc:arc # GC:arc is friendlier with crazy platforms. 22 | --exceptions:goto # Goto exceptions are friendlier with crazy platforms. 23 | 24 | # Pass this to Emscripten linker to generate html file scaffold for us. 25 | switch("passL", "-o tests/web/index.html -s USE_WEBGL2=1 --shell-file tests/web/shell_minimal.html") 26 | -------------------------------------------------------------------------------- /opengl.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "1.0.1" 4 | author = "Leonardo Mariscal" 5 | description = "OpenGL bindings for Nim" 6 | license = "MIT" 7 | srcDir = "src" 8 | skipDirs = @["tests"] 9 | 10 | # Dependencies 11 | 12 | requires "nim >= 1.0.0" 13 | 14 | task gen, "Generate bindings": 15 | exec("nim c -d:ssl -r tools/generator.nim") 16 | 17 | task test, "Build and test bindings": 18 | requires "nimgl@#1.0" # Please https://github.com/nim-lang/nimble/issues/482 19 | exec("nim c -r tests/test.nim") 20 | 21 | task testWeb, "Build and test bindings with emscripten": 22 | # for this to work, you need emsdk on your PATH. 23 | # first, clone https://github.com/emscripten-core/emsdk 24 | # then run: 25 | # ./emsdk install latest 26 | # ./emsdk activate latest 27 | # and then add the directories it prints out to your PATH 28 | exec("nim c -d:emscripten tests/test.nim") 29 | let port = "8000" 30 | echo "Open http://localhost:" & port & "/tests/web/index.html" 31 | let ret = gorgeEx("python3 -m http.server " & port) 32 | echo ret.output 33 | -------------------------------------------------------------------------------- /tests/config.nims: -------------------------------------------------------------------------------- 1 | switch("path", "$projectDir/../src") -------------------------------------------------------------------------------- /tests/tcomplete.nim: -------------------------------------------------------------------------------- 1 | # Copyright 2019, NimGL contributors. 2 | 3 | import nimgl/glfw 4 | import opengl 5 | import glm 6 | import os 7 | 8 | if os.getEnv("CI") != "": 9 | quit() 10 | 11 | proc keyProc(window: GLFWWindow, key: int32, scancode: int32, action: int32, mods: int32): void {.cdecl.} = 12 | if key == GLFWKey.Escape and action == GLFWPress: 13 | window.setWindowShouldClose(true) 14 | if key == GLFWKey.Space: 15 | glPolygonMode(GL_FRONT_AND_BACK, if action != GLFWRelease: GL_LINE else: GL_FILL) 16 | 17 | proc statusShader(shader: uint32) = 18 | var status: int32 19 | glGetShaderiv(shader, GL_COMPILE_STATUS, status.addr); 20 | if status != GL_TRUE.ord: 21 | var 22 | log_length: int32 23 | message = newSeq[char](1024) 24 | glGetShaderInfoLog(shader, 1024, log_length.addr, message[0].addr); 25 | echo message 26 | 27 | proc toRGB(vec: Vec3[float32]): Vec3[float32] = 28 | return vec3(vec.x / 255, vec.y / 255, vec.z / 255) 29 | 30 | proc main = 31 | # GLFW 32 | assert glfwInit() 33 | 34 | glfwWindowHint(GLFWContextVersionMajor, 3) 35 | glfwWindowHint(GLFWContextVersionMinor, 3) 36 | glfwWindowHint(GLFWOpenglForwardCompat, GLFW_TRUE) 37 | glfwWindowHint(GLFWOpenglProfile, GLFW_OPENGL_CORE_PROFILE) 38 | glfwWindowHint(GLFWResizable, GLFW_FALSE) 39 | 40 | let w: GLFWWindow = glfwCreateWindow(800, 600, "NimGL", nil, nil) 41 | assert w != nil 42 | 43 | discard w.setKeyCallback(keyProc) 44 | w.makeContextCurrent 45 | 46 | # Opengl 47 | assert glInit() 48 | 49 | echo $glVersionMajor & "." & $glVersionMinor 50 | 51 | glEnable(GL_BLEND) 52 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 53 | 54 | var 55 | mesh: tuple[vbo, vao, ebo: uint32] 56 | vertex : uint32 57 | fragment: uint32 58 | program : uint32 59 | 60 | var vert = @[ 61 | 0.3f, 0.3f, 62 | 0.3f, -0.3f, 63 | -0.3f, -0.3f, 64 | -0.3f, 0.3f 65 | ] 66 | 67 | var ind = @[ 68 | 0'u32, 1'u32, 3'u32, 69 | 1'u32, 2'u32, 3'u32 70 | ] 71 | 72 | glGenBuffers(1, mesh.vbo.addr) 73 | glGenBuffers(1, mesh.ebo.addr) 74 | glGenVertexArrays(1, mesh.vao.addr) 75 | 76 | glBindVertexArray(mesh.vao) 77 | 78 | glBindBuffer(GL_ARRAY_BUFFER, mesh.vbo) 79 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.ebo) 80 | 81 | glBufferData(GL_ARRAY_BUFFER, cint(cfloat.sizeof * vert.len), vert[0].addr, GL_STATIC_DRAW) 82 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, cint(cuint.sizeof * ind.len), ind[0].addr, GL_STATIC_DRAW) 83 | 84 | glEnableVertexAttribArray(0) 85 | glVertexAttribPointer(0'u32, 2, EGL_FLOAT, false, cfloat.sizeof * 2, nil) 86 | 87 | vertex = glCreateShader(GL_VERTEX_SHADER) 88 | var vsrc: cstring = """ 89 | #version 330 core 90 | layout (location = 0) in vec2 aPos; 91 | 92 | uniform mat4 uMVP; 93 | 94 | void main() { 95 | gl_Position = vec4(aPos, 0.0, 1.0) * uMVP; 96 | } 97 | """ 98 | glShaderSource(vertex, 1'i32, vsrc.addr, nil) 99 | glCompileShader(vertex) 100 | statusShader(vertex) 101 | 102 | fragment = glCreateShader(GL_FRAGMENT_SHADER) 103 | var fsrc: cstring = """ 104 | #version 330 core 105 | out vec4 FragColor; 106 | 107 | uniform vec3 uColor; 108 | 109 | void main() { 110 | FragColor = vec4(uColor, 1.0f); 111 | } 112 | """ 113 | glShaderSource(fragment, 1, fsrc.addr, nil) 114 | glCompileShader(fragment) 115 | statusShader(fragment) 116 | 117 | program = glCreateProgram() 118 | glAttachShader(program, vertex) 119 | glAttachShader(program, fragment) 120 | glLinkProgram(program) 121 | 122 | var 123 | log_length: int32 124 | message = newSeq[char](1024) 125 | pLinked: int32 126 | glGetProgramiv(program, GL_LINK_STATUS, pLinked.addr); 127 | if pLinked != GL_TRUE.ord: 128 | glGetProgramInfoLog(program, 1024, log_length.addr, message[0].addr); 129 | echo message 130 | 131 | let 132 | uColor = glGetUniformLocation(program, "uColor") 133 | uMVP = glGetUniformLocation(program, "uMVP") 134 | var 135 | bg = vec3(33f, 33f, 33f).toRgb() 136 | color = vec3(50f, 205f, 50f).toRgb() 137 | mvp = ortho(-2f, 2f, -1.5f, 1.5f, -1f, 1f) 138 | 139 | while not w.windowShouldClose: 140 | glClearColor(bg.r, bg.g, bg.b, 1f) 141 | glClear(GL_COLOR_BUFFER_BIT) 142 | 143 | glUseProgram(program) 144 | glUniform3fv(uColor, 1, color.caddr) 145 | glUniformMatrix4fv(uMVP, 1, false, mvp.caddr) 146 | 147 | glBindVertexArray(mesh.vao) 148 | glDrawElements(GL_TRIANGLES, ind.len.cint, GL_UNSIGNED_INT, nil) 149 | 150 | w.swapBuffers 151 | glfwPollEvents() 152 | 153 | w.destroyWindow 154 | 155 | glfwTerminate() 156 | 157 | glDeleteVertexArrays(1, mesh.vao.addr) 158 | glDeleteBuffers(1, mesh.vbo.addr) 159 | glDeleteBuffers(1, mesh.ebo.addr) 160 | 161 | main() 162 | -------------------------------------------------------------------------------- /tests/test.nim: -------------------------------------------------------------------------------- 1 | import nimgl/glfw, opengl 2 | 3 | proc keyProc(window: GLFWWindow, key: int32, scancode: int32, 4 | action: int32, mods: int32): void {.cdecl.} = 5 | if key == GLFWKey.ESCAPE and action == GLFWPress: 6 | window.setWindowShouldClose(true) 7 | 8 | when defined(emscripten): 9 | proc emscripten_set_main_loop(f: proc() {.cdecl.}, a: cint, b: bool) {.importc.} 10 | 11 | var window: GLFWWindow 12 | 13 | proc tick() {.cdecl.} = 14 | glfwPollEvents() 15 | glClearColor(0.68f, 1f, 0.34f, 1f) 16 | glClear(GL_COLOR_BUFFER_BIT) 17 | window.swapBuffers() 18 | 19 | proc main() = 20 | assert glfwInit() 21 | 22 | glfwWindowHint(GLFWContextVersionMajor, 3) 23 | glfwWindowHint(GLFWContextVersionMinor, 3) 24 | glfwWindowHint(GLFWOpenglForwardCompat, GLFW_TRUE) # Used for Mac 25 | glfwWindowHint(GLFWOpenglProfile, GLFW_OPENGL_CORE_PROFILE) 26 | glfwWindowHint(GLFWResizable, GLFW_FALSE) 27 | 28 | window = glfwCreateWindow(800, 600, "NimGL") 29 | if window == nil: 30 | quit(-1) 31 | 32 | discard window.setKeyCallback(keyProc) 33 | window.makeContextCurrent() 34 | 35 | assert glInit() 36 | 37 | when defined(emscripten): 38 | emscripten_set_main_loop(tick, 0, true) 39 | else: 40 | while not window.windowShouldClose: 41 | tick() 42 | 43 | window.destroyWindow() 44 | glfwTerminate() 45 | 46 | main() 47 | -------------------------------------------------------------------------------- /tests/web/shell_minimal.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Emscripten-Generated Code 7 | 25 | 26 | 27 | 28 | 29 | 30 | 97 | {{{ SCRIPT }}} 98 | 99 | 100 | -------------------------------------------------------------------------------- /tools/generator.nim: -------------------------------------------------------------------------------- 1 | # Written by Leonardo Mariscal , 2019 2 | 3 | import ./utils, httpClient, strutils, xmlparser, xmltree, os, streams, strformat, sets 4 | 5 | type 6 | GLEnum = object 7 | name: string 8 | value: string 9 | eType: string 10 | comment: string 11 | GLArg = object 12 | name: string 13 | argType: string 14 | GLProc = object 15 | name: string 16 | args: seq[GLArg] 17 | rVal: string 18 | 19 | var glEnums: seq[GLEnum] 20 | var glProcs: seq[GLProc] 21 | 22 | var gl1_0: seq[GLProc] 23 | var gl1_1: seq[GLProc] 24 | var gl1_2: seq[GLProc] 25 | var gl1_3: seq[GLProc] 26 | var gl1_4: seq[GLProc] 27 | var gl1_5: seq[GLProc] 28 | var gl2_0: seq[GLProc] 29 | var gl2_1: seq[GLProc] 30 | var gl3_0: seq[GLProc] 31 | var gl3_1: seq[GLProc] 32 | 33 | var gl3_2: seq[GLProc] 34 | var gl3_3: seq[GLProc] 35 | var gl4_0: seq[GLProc] 36 | var gl4_1: seq[GLProc] 37 | var gl4_2: seq[GLProc] 38 | var gl4_3: seq[GLProc] 39 | var gl4_4: seq[GLProc] 40 | var gl4_5: seq[GLProc] 41 | var gl4_6: seq[GLProc] 42 | 43 | proc translateType(cmd: string): string = 44 | result = cmd 45 | result = result.replace("const ", "") 46 | result = result.replace("const", "") 47 | result = result.replace(" *", "*") 48 | result = result.replace("void*", "pointer") 49 | result = result.replace("GLchar*", "cstring") 50 | result = result.replace("GLeglClientBufferEXT", "pointer") 51 | result = result.replace("GLuintbas", "GLuint") 52 | 53 | if result.contains('*'): 54 | let levels = result.count('*') 55 | result = result.replace("*", "") 56 | for i in 0.., 2019 2 | 3 | const srcHeader* = """ 4 | # Written by Leonardo Mariscal , 2019 5 | 6 | ## Modern OpenGL Bindings 7 | ## ==== 8 | ## WARNING: This is a generated file. Do not edit 9 | ## Any edits will be overwritten by the generator. 10 | ## 11 | ## NimGL is completely unaffiliated with OpenGL and 12 | ## Khronos, each Doc is under individual copyright 13 | ## You can find it in their appropiate file in the 14 | ## official `repo`_ 15 | ## 16 | ## NOTE: This bindings only support modern OpenGL (3.2 >=) 17 | ## so fixed pipelines are not supported. 18 | 19 | import strutils 20 | 21 | var glGetProc: proc(procName: cstring): pointer {.cdecl.} 22 | 23 | when not defined(glCustomLoader) and not defined(emscripten): 24 | import dynlib 25 | 26 | # Thanks to ephja for this loading system 27 | when defined(windows): 28 | const glDLL = "opengl32.dll" 29 | elif defined(macosx): 30 | const glDLL = "/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries/libGL.dylib" 31 | else: 32 | const glDLL = "libGL.so.1" 33 | 34 | let glHandle = loadLib(glDLL) 35 | if isNil(glHandle): 36 | quit("could not load: " & gldll) 37 | 38 | when defined(windows): 39 | var wglGetProcAddress = cast[proc (s: cstring): pointer {.stdcall.}]( 40 | symAddr(glHandle, "wglGetProcAddress")) 41 | elif defined(linux): 42 | var glxGetProcAddress = cast[proc (s: cstring): pointer {.cdecl.}]( 43 | symAddr(glHandle, "glXGetProcAddress")) 44 | var glxGetProcAddressArb = cast[proc (s: cstring): pointer {.cdecl.}]( 45 | symAddr(glHandle, "glXGetProcAddressARB")) 46 | 47 | glGetProc = proc(procName: cstring): pointer {.cdecl.} = 48 | when defined(windows): 49 | result = symAddr(glHandle, procName) 50 | if result != nil: 51 | return 52 | if not isNil(wglGetProcAddress): 53 | result = wglGetProcAddress(procName) 54 | elif defined(linux): 55 | result = symAddr(glHandle, procname) 56 | if result != nil: 57 | return 58 | if not isNil(glxGetProcAddressArb): 59 | result = glxGetProcAddressArb(procName) 60 | if result != nil: 61 | return 62 | if not isNil(glxGetProcAddress): 63 | result = glxGetProcAddress(procName) 64 | else: 65 | result = symAddr(glHandle, procName) 66 | if result == nil: raiseInvalidLibrary(procName) 67 | 68 | proc setGLGetProc*(getProc: proc(procName: cstring): pointer {.cdecl.}) = 69 | glGetProc = getProc 70 | """ 71 | 72 | const srcTypes* = """ 73 | type 74 | GLenum* = distinct uint32 75 | GLboolean* = bool 76 | GLbitfield* = distinct uint32 77 | GLvoid* = pointer 78 | GLbyte* = int8 79 | GLshort* = int16 80 | GLint* = int32 81 | GLclampx* = int32 82 | GLubyte* = uint8 83 | GLushort* = uint16 84 | GLuint* = uint32 85 | GLhandle* = GLuint 86 | GLsizei* = int32 87 | GLfloat* = float32 88 | GLclampf* = float32 89 | GLdouble* = float64 90 | GLclampd* = float64 91 | GLeglImageOES* = distinct pointer 92 | GLchar* = char 93 | GLcharArb* = char 94 | GLfixed* = int32 95 | GLhalfNv* = uint16 96 | GLvdpauSurfaceNv* = uint 97 | GLintptr* = int 98 | GLintptrArb* = int 99 | GLint64EXT* = int64 100 | GLuint64EXT* = uint64 101 | GLint64* = int64 102 | GLsizeiptrArb* = int 103 | GLsizeiptr* = int 104 | GLsync* = distinct pointer 105 | GLuint64* = uint64 106 | ClContext* = distinct pointer 107 | ClEvent* = distinct pointer 108 | GLdebugProc* = proc ( 109 | source: GLenum, 110 | typ: GLenum, 111 | id: GLuint, 112 | severity: GLenum, 113 | length: GLsizei, 114 | message: ptr GLchar, 115 | userParam: pointer) {.stdcall.} 116 | GLdebugProcArb* = proc ( 117 | source: GLenum, 118 | typ: GLenum, 119 | id: GLuint, 120 | severity: GLenum, 121 | len: GLsizei, 122 | message: ptr GLchar, 123 | userParam: pointer) {.stdcall.} 124 | GLdebugProcAmd* = proc ( 125 | id: GLuint, 126 | category: GLenum, 127 | severity: GLenum, 128 | len: GLsizei, 129 | message: ptr GLchar, 130 | userParam: pointer) {.stdcall.} 131 | GLdebugProcKhr* = proc ( 132 | source, typ: GLenum, 133 | id: GLuint, 134 | severity: GLenum, 135 | length: GLsizei, 136 | message: ptr GLchar, 137 | userParam: pointer) {.stdcall.} 138 | GLVULKANPROCNV* = proc(): void {.stdcall.} 139 | 140 | when defined(macosx): 141 | type 142 | GLhandleArb = pointer 143 | else: 144 | type 145 | GLhandleArb = uint32 146 | 147 | var 148 | glVersionMajor*: int 149 | glVersionMinor*: int 150 | 151 | proc `==`*(a, b: GLenum): bool {.borrow.} 152 | proc `==`*(a, b: GLbitfield): bool {.borrow.} 153 | proc `or`*(a, b: GLbitfield): GLbitfield {.borrow.} 154 | proc hash*(x: GLenum): int = x.int 155 | """ 156 | 157 | const glInit* = """ 158 | proc glInit*(): bool = 159 | glGetString = cast[proc(name: GLenum): ptr GLubyte {.stdcall.}](glGetProc("glGetString")) 160 | if glGetString == nil: 161 | return false 162 | 163 | let glVersion = cast[cstring](glGetString(GL_VERSION)) 164 | if glVersion.isNil: 165 | return false 166 | 167 | let prefixes = ["OpenGL ES-CM ", "OpenGL ES-CL ", "OpenGL ES "] 168 | var version: string = $glVersion 169 | for prefix in prefixes: 170 | if version.startsWith(prefix): 171 | version = version.replace(prefix) 172 | break 173 | 174 | let major = ord(glVersion[0]) - ord('0') 175 | let minor = ord(glVersion[2]) - ord('0') 176 | 177 | glVersionMajor = major 178 | glVersionMinor = minor 179 | 180 | if (major == 1 and minor >= 0) or major > 1: glLoad1_0() 181 | if (major == 1 and minor >= 1) or major > 1: glLoad1_1() 182 | if (major == 1 and minor >= 2) or major > 1: glLoad1_2() 183 | if (major == 1 and minor >= 3) or major > 1: glLoad1_3() 184 | if (major == 1 and minor >= 4) or major > 1: glLoad1_4() 185 | if (major == 1 and minor >= 5) or major > 1: glLoad1_5() 186 | if (major == 2 and minor >= 0) or major > 2: glLoad2_0() 187 | if (major == 2 and minor >= 1) or major > 2: glLoad2_1() 188 | if (major == 3 and minor >= 0) or major > 3: glLoad3_0() 189 | if (major == 3 and minor >= 1) or major > 3: glLoad3_1() 190 | if (major == 3 and minor >= 2) or major > 3: glLoad3_2() 191 | if (major == 3 and minor >= 3) or major > 3: glLoad3_3() 192 | if (major == 4 and minor >= 0) or major > 4: glLoad4_0() 193 | if (major == 4 and minor >= 1) or major > 4: glLoad4_1() 194 | if (major == 4 and minor >= 2) or major > 4: glLoad4_2() 195 | if (major == 4 and minor >= 3) or major > 4: glLoad4_3() 196 | if (major == 4 and minor >= 4) or major > 4: glLoad4_4() 197 | if (major == 4 and minor >= 5) or major > 4: glLoad4_5() 198 | if (major == 4 and minor >= 6) or major > 4: glLoad4_6() 199 | return true 200 | """ 201 | 202 | let keywords* = ["addr", "and", "as", "asm", "bind", "block", "break", "case", "cast", "concept", 203 | "const", "continue", "converter", "defer", "discard", "distinct", "div", "do", 204 | "elif", "else", "end", "enum", "except", "export", "finally", "for", "from", "func", 205 | "if", "import", "in", "include", "interface", "is", "isnot", "iterator", "let", 206 | "macro", "method", "mixin", "mod", "nil", "not", "notin", "object", "of", "or", 207 | "out", "proc", "ptr", "raise", "ref", "return", "shl", "shr", "static", "template", 208 | "try", "tuple", "type", "using", "var", "when", "while", "xor", "yield"] 209 | 210 | let renameConstants* = ["GL_BYTE", "GL_SHORT", "GL_INT", "GL_FLOAT", "GL_DOUBLE", "GL_FIXED"] 211 | --------------------------------------------------------------------------------