---------------
-- Api enums --
---------------


COLOR_BUFFER = 1
STENCIL_BUFFER = 2
DEPTH_BUFFER = 4



-- enum for RenderTarget et ZBuffers
BufferType = 
{
	DepthStencilBuffer = 0,
	ColorBuffer = 1,		-- for compatibility : same as Format_A8R8G8B8
	ColorBufferFP16 = 2,	-- for compatibility : same as Format_A16B16G16R16F
	PBuffer = 3,
	HardwareShadowMap = 4,	-- D24S8 nvidia hardware shadow map
	--
	Format_R8G8B8 = 5,
	Format_A8R8G8B8 = 6,
	Format_X8R8G8B8 = 7,
	Format_R5G6B5 = 8,
	Format_X1R5G5B5 = 9,
	Format_A1R5G5B5 = 10,
	Format_A4R4G4B4 = 11,
	Format_R3G3B2 = 12,
	Format_A8R3G3B2 = 13,
	Format_X4A8R3G3B2 = 14,
	Format_A2B10G10R10 = 15,
	Format_A8B8G8R8 = 16,
	Format_X8B8G8R8 = 17,
	Format_G16R16 = 18,
	Format_A2R10G10B10 = 19,
	Format_A16B16G16R16 = 20,
	Format_MULTI2_ARGB8 = 21,
	Format_R16F = 22,
	Format_G16R16F = 23,
	Format_A16B16G16R16F = 24,
	Format_R32F = 25,
	Format_G32R32F = 26,
	Format_A32B32G32R32F = 27	
}

-- Wrap modes
WrapMode = 
{
   Clamp = 0, 
   Wrap = 1,
   Mirror = 2,
   MirrorOnce = 3
}

-- Blending modes
Blend =
{
   Zero = 0,
   One = 1,
   SrcColor = 2,
   SrcAlpha = 3,
   InvSrcColor = 4,
   InvSrcAlpha = 5,
   DestColor = 6,
   DestAlpha = 7,
   InvDestColor = 8,
   InvDestAlpha = 9
}

-- enums for reflect / refract blend
ReflRefrBlend = 
{
	AlphaBlend = 0,
   AdditifAlpha = 1,
	Modulate = 2
}

-- enums for tex coord generators
TexCoordGen =
{
		Position = 0,
		XFormPos = 17,
		TexCoord = 1,			            -- Just copy a texture coordinate "texCoordIndex" field gives the tex coord set
		LocalLVectorNormed = 4,		-- Light Vector expressed in tangent space
		LocalLVector = 3,
		LocalHVectorNormed = 4,		-- Half Angle Vector expressed in tangent space
		LocalHVector = 5,
		LVector = 6,
		LVectorNormed = 7,
		LocalNormal = 9,					-- Normal
		LocalTangent = 10,				-- Tangent space vector is used to fetch the texture		
		WorldReflect = 21,
		LocalVVectorNormed = 25,
		LocalPositionNormed = 27,
		LocalVVector = 28,		
}



--------------------------------------------------------------------------------------------
-- tex coord generators
-- They should be used as the second parameter to the "tex" instruction to give 

local result = CMatrix34();

function texPannerLinear(uSpeed, vSpeed, wantedUVSet, doMod)
	if doMod == nil then 
		doMod = true
	end
	-- the following is now C++ code
	--function computeMatrix(renderContext)				
	--result:setIdentity();
	--lambda = renderContext.Time
	--if doMod then      	
	--	result:setTrans(math.mod(lambda * uSpeed, 1),
	--	  	            math.mod(lambda * vSpeed, 1),
    --			     	    1);
	-- else      		
	--	 result:setTrans(lambda * uSpeed,
	--  	 			 lambda * vSpeed,
	--    			 1);		
	-- end
	-- return result;
	-- end
	return 
	{
		mode  = TexCoordGen.TexCoord,
		texCoordIndex = wantedUVSet,
		gen   = texPanner(uSpeed, vSpeed, doMod)
	}
end

-----------------------------------------
-- VOLUMETRIC LIGHTING DEMO  MATERIALS --
-----------------------------------------
function animatedGobo1()	
	local mat = {}
	mat.lighted = false
	mat.double_sided = true
	mat.diffuse = mul(tex("green.tga", texPannerLinear(0.15, 0.2)), tex("green.tga", texPannerLinear(-0.15, 0.2)))
	mat.diffuse = mul(mat.diffuse, mat.diffuse)
	mat.diffuse = mul(mat.diffuse, tex("spot_45.tga"))
	return mat
end

function animatedGobo2()	
	local mat = {}
	mat.lighted = false
	mat.double_sided = true
	mat.diffuse = mul(tex("perlin.tga", texPannerLinear(0.35, 0.7)), tex("perlin.tga", texPannerLinear(-0.15, 0.2)))
	mat.diffuse = mul(mat.diffuse, mat.diffuse);
	mat.diffuse = add(mat.diffuse, color(0.2, 0.2, 0.2))
	local sweep = mul(color(1.5, 1.5, 1.5), tex("scanner_sweep.tga", texPannerLinear(0.0, -0.6)))

	sweep = mul(sweep, alpha(tex("scanner_base.tga")))
	mat.diffuse = mul(mat.diffuse, add(tex("scanner_base.tga"),  sweep))	
	mat.diffuse = mul(mat.diffuse, tex("scanner_sweep2.tga", texPannerLinear(0.0, 2.0)))
	mat.diffuse = mul(mat.diffuse, tex("scanner_sweep2.tga", texPannerLinear(0.0, 5.12)))
	mat.diffuse = mul(mat.diffuse, color(3, 3, 3))
	return mat
end



function pumpkin()
	local mat = {}
	mat.diffuse = vertex_color0()
	mat.specular_on = true
	mat.specularity = 0.99
	mat.gloss = color(0.3, 0.3, 0.3)
	mat.ambient = color(0.3, 0.3, 0.3)
	return mat
end

function walls()
	local mat = {}
	mat.normal = tex_normal("asteroid.tga")	
	set_compression_flag(mat.normal, false)
	mat.diffuse = color(0.8, 0.8, 1.0)
	mat.specular_on = true
	mat.specularity = 0.99
	mat.gloss = color(0.3, 0.3, 0.3)
	mat.ambient = color(0.2, 0.2, 0.2)
	return mat
end


function torch()
	local mat = {}	
	mat.lighted = false
	mat.diffuse = color(0.8, 0.8, 0.8)
	mat.specular_on = true
	mat.specularity = 0.99
	mat.gloss = color(0.5, 0.5, 0.5)
	mat.ambient = color(0.2, 0.2, 0.2)
	return mat
end

function metalWheel()
	local mat = {}	
	mat.diffuse = mul(color(0.6, 0.6, 0.6), tex("metal_alpha.tga"))
	mat.normal = tex_normal("metal_water_drop_128.tga")
	set_compression_flag(mat.normal, false)
	mat.specular_on = true
	mat.specularity = 0.99
	mat.gloss = mul(color(1.5, 1.5, 1.5), alpha(tex("metal_alpha.tga")))
	mat.ambient = color(0.2, 0.2, 0.2)
	return mat
end

function metalStar()
	local mat = {}	
	mat.diffuse = mul(color(0.8, 0.8, 0.1), tex("metal_alpha.tga"))
	mat.normal = tex_normal("metal_water_drop_128.tga")
	set_compression_flag(mat.normal, false)
	mat.specular_on = true
	mat.specularity = 0.99
	mat.gloss = mul(color(2.5, 2.3, 0.2), alpha(tex("metal_alpha.tga")))
	return mat
end

function redWall()
	local mat = {}
	mat.normal = tex_normal("crippled.tga")	
	set_compression_flag(mat.normal, false)
	mat.diffuse = color(0.4, 0.2, 0.1)
	mat.specular_on = true
	mat.specularity = 0.99
	mat.gloss = color(0.3, 0.3, 0.3)
	mat.ambient = color(0.2, 0.2, 0.2)
	mat.ambient = color(0.2, 0.2, 0.2)
	return mat
end

