/ chmod777_includes / utilities_GL4.lua
utilities_GL4.lua
1 -- File: utilites_GL4.lua 2 3 --[[ 4 Copyright (C) 2024 chmod777 5 6 This program is free software: you can redistribute it and/or modify it under 7 the terms of the GNU Affero General Public License version 3 as published by the 8 Free Software Foundation. 9 10 This program is distributed in the hope that it will be useful, but WITHOUT ANY 11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 13 14 You should have received a copy of the GNU Affero General Public License along 15 with this program. If not, see <https://www.gnu.org/licenses/>. 16 ]] 17 18 local GL_RGBAF32 = 0x8815 19 local GL_RGB16F = 0x881B 20 local GL_RGBA8 = 0x8058 21 22 local GL_COLOR_ATTACHMENT0_EXT = 0x8CE0 23 local GL_COLOR_ATTACHMENT1_EXT = 0x8CE1 24 local GL_COLOR_ATTACHMENT2_EXT = 0x8CE2 25 26 local GL_DEPTH_COMPONENT = 0x1902 27 local GL_DEPTH_COMPONENT16 = 0x81A5 28 local GL_DEPTH_COMPONENT24 = 0x81A6 29 local GL_DEPTH_COMPONENT32 = 0x81A7 30 31 local glGetVBO = gl.GetVBO 32 local glGetVAO = gl.GetVAO 33 local glCreateFBO = gl.CreateFBO 34 local glDeleteFBO = gl.DeleteFBO 35 local glRawBindFBO = gl.RawBindFBO 36 local glCreateTexture = gl.CreateTexture 37 local glDeleteTexture = gl.DeleteTexture 38 39 local GL_ARRAY_BUFFER = GL.ARRAY_BUFFER 40 local GL_ELEMENT_ARRAY_BUFFER = GL.ELEMENT_ARRAY_BUFFER 41 local GL_TRIANGLES = GL.TRIANGLES 42 local GL_NEAREST = GL.NEAREST 43 local GL_NEAREST = GL.NEAREST 44 local GL_CLAMP_TO_EDGE = GL.CLAMP_TO_EDGE 45 local GL_CLAMP_TO_EDGE = GL.CLAMP_TO_EDGE 46 47 local Quad = {} 48 --- num, num, num, num, bool, bool 49 function Quad:new(minX, minY, maxX, maxY, flipY, flipX) 50 if minX == nil then minX = -1 end 51 if minY == nil then minY = -1 end 52 if maxX == nil then maxX = 1 end 53 if maxY == nil then maxY = 1 end 54 if minX > maxX then minX,maxX = maxX,minX end 55 if minY > maxY then minY,maxY = maxY,minY end 56 57 local startU,endU = 0,1 58 local startV,endV = 0,1 59 if flipX then 60 startU,endU = endU,startU 61 end 62 if flipY then 63 startV,endV = endV,startV 64 end 65 66 local vertices = { 67 minX,minY, startU,startV, --bottom left 68 minX,maxY, startU,endV, --bottom right 69 maxX,maxY, endU,endV, --top right 70 maxX,minY, endU,startV, --top left 71 } 72 local vertexCount = 4 73 local vertexVBOLayout = { 74 {id = 0, name = 'coords', size = 2}, 75 {id = 1, name = 'uv', size = 2}, 76 } 77 78 local indices = { 79 2, 1, 0, 80 3, 2, 0, 81 } 82 local indexCount = #indices 83 84 local VBO = glGetVBO(GL_ARRAY_BUFFER, false) 85 VBO:Define(vertexCount, vertexVBOLayout) 86 VBO:Upload(vertices) 87 88 local indexVBO = glGetVBO(GL_ELEMENT_ARRAY_BUFFER, false) 89 indexVBO:Define(#indices) 90 indexVBO:Upload(indices) 91 92 local VAO = glGetVAO() 93 VAO:AttachVertexBuffer(VBO) 94 VAO:AttachIndexBuffer(indexVBO) 95 96 local this = { 97 vertexVBO = VBO, 98 indexVBO = VBO, 99 VAO = VAO, 100 vertices = vertices, 101 vertexCount = vertexCount, 102 indices = indices, 103 indexCount = indexCount, 104 } 105 106 function this:draw() 107 this.VAO:DrawElements(GL_TRIANGLES, indexCount, 0, 0, 0, 0) 108 end 109 110 function this:Delete() 111 if this.indexVBO ~= nil then 112 this.indexVBO:Delete() 113 end 114 if this.vertexVBO ~= nil then 115 this.vertexVBO:Delete() 116 end 117 if this.VAO ~= nil then 118 this.VAO:Delete() 119 end 120 end 121 122 return this 123 end 124 125 local FBO = {} 126 --- num num bool 127 function FBO:new(sizeX, sizeY, withDepth) 128 if sizeX == nil then sizeX = 256 end 129 if sizeY == nil then sizeY = 256 end 130 if withDepth == nil then withDepth = false end 131 132 local tex = glCreateTexture(sizeX, sizeY, { 133 format = GL_RGBA8, 134 border = false, 135 target = GL_TEXTURE_2D, 136 min_filter = GL_NEAREST, 137 mag_filter = GL_NEAREST, 138 wrap_s = GL_CLAMP_TO_EDGE, 139 wrap_t = GL_CLAMP_TO_EDGE, 140 fbo = true, 141 }) 142 143 local config 144 local depth 145 if withDepth then 146 depth = glCreateTexture(sizeX, sizeY, { 147 format = GL_DEPTH_COMPONENT24, 148 border = false, 149 min_filter = GL_NEAREST, 150 mag_filter = GL_NEAREST, 151 }) 152 end 153 config = { 154 color0 = tex, 155 depth = depth, 156 drawbuffers = {GL_COLOR_ATTACHMENT0_EXT}, 157 } 158 fbo = glCreateFBO(config) 159 160 local this = { 161 fbo = fbo, 162 tex = tex, 163 depth = depth 164 } 165 166 function this:Delete() 167 if glDeleteTexture then 168 if this.tex ~= nil then 169 glDeleteTexture(this.tex) 170 end 171 if this.depth ~= nil then 172 glDeleteTexture(this.depth) 173 end 174 end 175 if this.fbo ~= nil and glDeleteFBO then 176 glDeleteFBO(this.fbo) 177 end 178 end 179 180 function this:bind() 181 this.prevFBO = glRawBindFBO(this.fbo) 182 end 183 184 function this:unbind() 185 glRawBindFBO(nil, nil, this.prevFBO) 186 this.prevFBO = nil 187 end 188 189 return this 190 end 191 192 return Quad, FBO