├── .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 | [](https://nimgl.dev/docs) 2 | 3 | # Modern OpenGL Bindings for Nim [](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 |