/ chmod777_includes / DrawUnitCustom_GL4.lua
DrawUnitCustom_GL4.lua
  1  -- File: DrawUnitCustom_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  
 19  local luaWidgetDir = 'LuaUI/Widgets/'
 20  local luaIncludeDir = luaWidgetDir..'Include/'
 21  VFS.Include(luaIncludeDir..'instancevboidtable.lua')
 22  
 23  local spEcho = Spring.Echo
 24  local glGetVBO = gl.GetVBO
 25  local GL_UNSIGNED_INT = GL.UNSIGNED_INT
 26  local GL_ARRAY_BUFFER = GL.ARRAY_BUFFER
 27  local GL_ELEMENT_ARRAY_BUFFER = GL.ELEMENT_ARRAY_BUFFER
 28  
 29  local DrawUnitCustomGL4 = {}
 30  function DrawUnitCustomGL4:new()
 31  	local vertexVBO = glGetVBO(GL_ARRAY_BUFFER, false)
 32  	local indexVBO = glGetVBO(GL_ELEMENT_ARRAY_BUFFER, false)
 33  	vertexVBO:ModelsVBO()
 34  	indexVBO:ModelsVBO()
 35  
 36  	local this = {
 37  		VBOTables = {},
 38  		vertexVBO = vertexVBO,
 39  		indexVBO = indexVBO,
 40  		uniqueID = 0,
 41  	}
 42  
 43  	function this:CreateVBOTable(layout, unitDefID)
 44  		local maxElements = #layout
 45  		local unitIDAttributeIndex
 46  		for i=1, #layout do
 47  			local attrib = layout[i]
 48  			if attrib.name == 'instData'
 49  				and attrib.type == GL_UNSIGNED_INT
 50  				and attrib.size == 4
 51  			then
 52  				unitIDAttributeIndex = attrib.id
 53  				break
 54  			end
 55  		end
 56  		if unitIDAttributeIndex == nil then
 57  			return nil
 58  		end
 59  		local instanceTable = makeInstanceVBOTable(layout, maxElements, 'tableName', unitIDAttributeIndex, 'unitID')
 60  		instanceTable.VAO = makeVAOandAttach(this.vertexVBO, instanceTable.instanceVBO, this.indexVBO)
 61  		instanceTable.indexVBO = this.indexVBO
 62  		instanceTable.vertexVBO = this.vertexVBO
 63  		return instanceTable
 64  	end
 65  
 66  	function this:AddUnit(unitID, unitDefID, layout, data, updateID)
 67  		if not updateID then 
 68  			this.uniqueID = this.uniqueID + 1
 69  			updateID = this.uniqueID
 70  		end
 71  	
 72  		local VBOTables = this.VBOTables
 73  		local DrawUnitVBOTable
 74  		if VBOTables[layout] then
 75  			if VBOTables[layout][unitDefID] then
 76  				DrawUnitVBOTable = VBOTables[layout][unitDefID]
 77  			else
 78  				DrawUnitVBOTable = this:CreateVBOTable(layout, unitDefID)
 79  				VBOTables[layout][unitDefID] = DrawUnitVBOTable
 80  			end
 81  		else
 82  			DrawUnitVBOTable = this:CreateVBOTable(layout, unitDefID)
 83  			VBOTables[layout] = {[unitDefID] = DrawUnitVBOTable}
 84  		end
 85  		if DrawUnitVBOTable == nil then
 86  			spEcho('Layout must contain valid instData attribute')
 87  			return nil
 88  		end
 89  	
 90  		local elementID = pushElementInstance(
 91  			DrawUnitVBOTable,
 92  			data,
 93  			updateID,
 94  			true,
 95  			false,
 96  			unitID,
 97  			'unitID')
 98  		return updateID
 99  	end
100  
101  	function this:GetUnitCustomVAO(unitDefID, layout)
102  		local VBOTables = this.VBOTables
103  		if VBOTables[layout] and VBOTables[layout][unitDefID] then
104  			return VBOTables[layout][unitDefID]
105  		end
106  	end
107  	
108  	function this:RemoveUnit(layout, unitDefID, uniqueID)
109  		if this.VBOTables[layout]
110  			and this.VBOTables[layout][unitDefID]
111  			and this.VBOTables[layout][unitDefID].instanceIDtoIndex[uniqueID]
112  		then
113  			popElementInstance(this.VBOTables[layout][unitDefID], uniqueID)
114  		else
115  			spEcho('Unable to remove what you wanted in StopDrawUnitCustomGL4', uniqueID)
116  		end
117  	end
118  
119  	function this:Delete()
120  		for layout,layoutTable in pairs(this.VBOTables) do
121  			for unitDefID,unitTable in pairs(layoutTable) do
122  				if unitTable.VAO then
123  					unitTable.instanceVBO:Delete()
124  					unitTable.VAO:Delete()
125  				end
126  			end
127  		end
128  	
129  		if this.vertexVBO ~= nil then
130  			vertexVBO:Delete()
131  		end
132  		if this.indexVBO ~= nil then
133  			indexVBO:Delete()
134  		end	
135  	end
136  
137  	return this
138  end
139  
140  return DrawUnitCustomGL4