Lua Open GL: Scaling Units up and down

Lua Open GL: Scaling Units up and down

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Lua Open GL: Scaling Units up and down

Post by PicassoCT »

Yep, its the bitcher from moddev. Problem is this: I want the swiftspear when he pops out after beeing born to be small, and scale the modell then up to normal size. Thus.
Enter: The Gadget

Code: Select all

-- Copyright (C) 2010,2011.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function gadget:GetInfo()
return {
name = "gltest ",
desc = "test123",
author = "PicassoCT",
date = "7 b.Creation",
license = "GNU GPL, v2 its goes in all fields",
layer = 0,
enabled = true -- loaded by default?
}
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

-- synced only
if (gadgetHandler:IsSyncedCode()) then
local startScale=0.1
swiftSpearTable={}
--registrates the units created
  function gadget:UnitCreated(unitID, unitDefID, unitTeam)
            if unitDefID== UnitDefNames["jswiftspear"].id then
			
			swiftSpearTable[#swiftSpearTable+1]={}
			swiftSpearTable[#swiftSpearTable][1]={}
			swiftSpearTable[#swiftSpearTable][1]=unitID
			swiftSpearTable[#swiftSpearTable][2]={}
			swiftSpearTable[#swiftSpearTable][2]=startScale
			end
			
   end

 local function raiseGrowthEvent(unitID, size)
      _G.laserEventArgs = {}
      _G.laserEventArgs[1] = {}
      _G.laserEventArgs[1] = unitID
      _G.laserEventArgs[2] = {}
      _G.laserEventArgs[2]  = size
      SendToUnsynced("growthEvent")
      _G.laserEventArgs = nil
    end

local UPDATE_FREQUNECY=100

	  function gadget:GameFrame(f)
			   if f%UPDATE_FREQUNECY== 0 then
				   for i=1,table.getn(swiftSpearTable),1 do
						if Spring.ValidUnitID(swiftSpearTable[i][1]) == true and swiftSpearTable[i][2] <=1 then
						swiftSpearTable[i][2]=(swiftSpearTable[i][2]+0.1)
						  raiseGrowthEvent{swiftSpearTable[i][1], swiftSpearTable[i][2]}
							else
							table.remove(swiftSpearTable,i)
							end
				   
				   end
			   
			--call a unsyced function   
			   end
			   
		end


	


else

local unsycedSwiftTable={}

    function gadget:Initialize()
      gadgetHandler:AddSyncAction("growthEvent", HandleGrowthEvent)
    end
	
	    function HandleGrowthEvent()
     
         	unsycedSwiftTable[#unsycedSwiftTable+1]={}
			unsycedSwiftTable[#unsycedSwiftTable][1]={}
			unsycedSwiftTable[#unsycedSwiftTable][1]=SYNCED.laserEventArgs[1]
		
			unsycedSwiftTable[#unsycedSwiftTable][2]={}
			unsycedSwiftTable[#unsycedSwiftTable][2]=SYNCED.laserEventArgs[2]
	  
	  
	                                        
	  
	  
        end
     


   function gadget:DrawWorld()
		   if unsycedSwiftTable~=nil and table.getn(unsycedSwiftTable)~=0 then
				for i=1,table.getn(unsycedSwiftTable),1 do
				scale=unsycedSwiftTable[i][2]
														gl.PushMatrix()
														gl.Unit(unsycedSwiftTable[i][1])
														gl.Scale(scale,scale,scale)
														gl.PopMatrix()	
				
				
				if unsycedSwiftTable[i][2] ==1 then
				table.remove(unsycedSwiftTable,i)
				end
				
				end
			end
	
   
   
  
    
	end




	
end


the problem is, that the resulting unit looks like this.
Image
Making MC Escher happy, but me a sad pandering being.
Positiv: I screw around with some graphics, obviously messing up the depthtest, or order in which they are draw.
Negativ: This is not what i came for.

Help would be very much appreciated. Even made a thread at the right place, with a accurate problem description, so reuse is given. Thanks for any help and goodnight.
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Lua Open GL: Scaling Units up and down

Post by Beherith »

I can't tell from the picture and text what is going wrong exactly.
Also, you might want to use the swiftspeartable a little bit differently: use the unitID as a key, and the growthmod as a value. Then you can use: for k,v in ipairs(swftspeartsble).
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: Lua Open GL: Scaling Units up and down

Post by PicassoCT »

I want to scale a unit. On the picture its clearly not scaled.
But the unit is affected. Thus i asssume, in OpenGl im doing it wrong. Now i assume that i handled the scaling wrong. And did some additonal dephtest wrong.
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Lua Open GL: Scaling Units up and down

Post by Beherith »

I always get mixed up with the order of OpenGL commands, maybe draw and scale are mixed up?

gl.DepthTest(true)
gl.DepthMask(true)
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: Lua Open GL: Scaling Units up and down

Post by PicassoCT »

Thanks Added that..
PS: Do those numbers needed to scale have specific numeric conventions?

It seems one of my values is nil..

So i read that i must hand over a scaling matrix.
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Lua Open GL: Scaling Units up and down

Post by Beherith »

Pposting from phone; isn't scale a vector where 1 is unity scale?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Lua Open GL: Scaling Units up and down

Post by zwzsg »

I'm pretty sure gl.Scale should come before gl.Unit. That doesn't explain your problem though. Echo the scale to check it's positive?

At some point you'll need some code to disable the unit being drawn by the engine so only your OpenGL drawing remains. Have a look at kp's set_alpha_threshold.lua gadget to see how to replace the engine drawing of a unit by a Lua callin. As opposed to kp's shoot_n_run.lua which draw the unit in Lua in addition to the engine drawing.
Post Reply

Return to “Lua Scripts”