require("demo_common.lua")
--require("water_effect.lua")

local displayLighted = true




function computeSpecularity(src)
	if getPixelShaderModel() == "ps_2_0" then
		return 1 - (0.3 * (1 - src)) -- more specular without nasty look for ps2_0		
	else
		return src
	end
end






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

-- test materials


function test1()
	--log("Creating test 1 material");
	local mat = {};
	mat.lighted = false;
	mat.diffuse = tex("fire.tga");
	--log("Returning test 1 result");
	return mat;
end


function test2()
	--log("Creating test 2 material");
	local mat = {};
	mat.lighted = false;
	mat.diffuse = tex("fire.tga", texPannerLinear(0.5, 0) );
	--log("Returning test 2 result");
	return mat;
end

function test3()
	--log("Creating test 3 material");
	local mat = {};
	mat.lighted = false;
	mat.diffuse = mul(tex("fire.tga", texPannerLinear(0.5,  0   )),
                     tex("fire.tga", texPannerLinear(-0.5, 0.2 ))
                    )
 
	return mat;
end

function test4()
	--log("Creating test 4 material");
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("damier.tga");	
	return mat;
end

function simpleWhite()
	--log("Creating whiteMaterial");
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 1, 1) 
	return mat;
end



function whiteMaterial()
	--log("Creating whiteMaterial");
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 1, 1) 
   mat.ambient = color(0.2, 0.2, 0.2)
	return mat;
end


function whiteMaterialBump()
	--log("Creating whiteMaterial");
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 1, 1) 
   mat.ambient = color(0.2, 0.2, 0.2)
	mat.normal = tex_normal("TilesHeight.tga");
	set_compression_flag(mat.normal, false)
	return mat;
end



function whiteMaterial2()
	--log("Creating whiteMaterial");
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 1, 1) 
--	mat.ambient = color(0.1, 0.1, 0.1)
	mat.specular_on = true
	mat.specularity = 0.8
	--	mat.gloss = color(1, 0, 0)
	return mat;
end



function redMat()	
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 0, 0) 
  	mat.ambient = color(0, 0, 0)
	return mat;
end

function greenMat()	
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0, 1, 0) 
  	mat.ambient = color(0, 0, 0)
	return mat;
end

function magentaMat()	
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 0, 1) 
  	mat.ambient = color(0, 0, 0)
	return mat;
end

function blackMaterial()
	--log("Creating blackMaterial");
	local mat = {};
	mat.lighted = displayLighted;
--	mat.diffuse = color(0, 0, 0) 
 --  mat.ambient = color(0, 0, 0)
mat.diffuse = color(0.4, 0.4, 0.4) 
--   mat.ambient = color(0.2, 0.2, 0.2)

	return mat;
end



function transparent()
 local mat = {}
 mat.transparent = true;
 mat.src_blend = Blend.One
 mat.dest_blend = Blend.One
 mat.diffuse = tex("damier.tga");
 return mat;
end




------------------------
--    specular test   --
------------------------

function specular1()
	local mat = {}
	mat.lighted = displayLighted
	mat.specular_on = true
	mat.specularity = 0.99
	mat.diffuse = tex("damier.tga");
   mat.ambient = color(0.2, 0.2, 0.2)
	return mat;
end

function specular2()
	local mat = {}
	mat.lighted = displayLighted
	mat.specular_on = true
	mat.specularity = 0.8
	mat.diffuse = tex("damier.tga");
   mat.ambient = color(0.2, 0.2, 0.2)
	return mat;
end

function specular3()
	local mat = {}
	mat.lighted = displayLighted
	mat.specular_on = true
	mat.specularity = 1.0
	mat.diffuse = tex("damier.tga");
   mat.ambient = color(0.2, 0.2, 0.2)
	return mat;
end




--------------------
--    bump test   --
--------------------

function bump()
	local mat = {}
	mat.lighted = displayLighted
	mat.specular_on = false
	mat.diffuse = tex("Tiles.tga");
	mat.normal = tex_normal("TilesHeight.tga");
   --mat.ambient = color(0.2, 0.2, 0.2)
	return mat;
end

function bump2()
	local mat = {}
	mat.lighted = displayLighted
	mat.specular_on = true
--	mat.diffuse = tex("Tiles.tga");
   mat.normal = tex_normal("TilesHeight.tga");
   mat.ambient = color(0.2, 0.2, 0.2)
	return mat;
end



----------------------------------------------------------------------------------------------------
-- lights

function simpleLight()
	local mat={}
	 mat.attenuation_on = true
   --   mat.attenuation_on = false
	--mat.color_on = false
   --mat.shader = color(1.0, 0.0, 0.0)
	return mat
end



function light1()
	--log('*')
	local mat={}
	 mat.attenuation_on = true
      --mat.attenuation_on = false
	--mat.color_on = false
   --mat.shader = color(1.0, 0.0, 0.0)
	--log('#')
	return mat
end



function light1bis()
	--log("Creating light1 material");
	local mat = {}
	mat.attenuation_on = true
--	mat.attenuation_on = false
	mat.color_on = false
   local noise = biasx2(tex_cube("noise3d.tga", lightSpinnerY(0.2)));
   noise = mul(noise, color(0.3, 0.3, 0.3));    
	noise = add(tex_coord(lightSpinnerY(0)), noise)
	mat.shader = tex_cube("green.tga", noise)
	if getPixelShaderModel() == "ps_1_4" then
		mat.shader = tex_cube("green.tga", lightSpinnerY(0.2))
	end
	--mat.shader = tex_cube("vitrail.tga", noise)
   --mat.shader = noise
	return mat
end


function light1ter()
	local mat = light1bis()
	mat.color_on = true
	return mat
end



function light2()
	local mat = {}
	mat.attenuation_on = true
	--mat.color_on = false
	mat.shader = mul(tex_cube("noise.tga", lightSpinnerY(0.2)),
                    tex_cube("noise.tga", lightSpinnerY(-0.15)))

	return mat
end


function light3()
	local mat = {}
	mat.attenuation_on = true
	mat.color_on = false
	mat.shader = tex_cube("noise.tga", lightSpinnerY(0.2))
	return mat
end

function lightNoAttenuation()
	local mat = {}
	mat.attenuation_on = false
	mat.color_on = true
	
	return mat
end

function spot()
	local mat = {}
	mat.shader = tex_cube({"black_256.tga", "black_256.tga", "black_256.tga", "spot_45.tga", "black_256.tga", "black_256.tga"}, localLightMatrix())
	return mat
end


function lightSinus(freq, min, max, phase)
	if freq == nil then freq = 1 end
	if min == nil then min = 0 end
	if max == nil then max = 1 end
	if phase == nil then phase = 0 end
	local mat = {}
	mat.attenuation_on = true
	mat.color_on = false	
	mat.shader = color(sinusLightColor(freq, min, max, phase))
	return mat
end

function lightPulse(freq, min, max, phase)
	if freq == nil then freq = 1 end
	if min == nil then min = 0 end
	if max == nil then max = 1 end
	if phase == nil then phase = 0 end
	local mat = {}
	mat.attenuation_on = true
	mat.color_on = false	
	mat.shader = color(pulseLightColor(freq, min, max, phase))
	return mat
end






-----------------------------------------------------
--  probe test
function testProbeMat1()
	local mat = {}
	mat.lighted = displayLighted
	mat.specular_on = true
	mat.diffuse = tex("Tiles.tga");
	mat.normal = tex_normal("TilesHeight.tga");
   mat.ambient = color(0.2, 0.2, 0.2)
	mat.specularity = computeSpecularity(0.95)
	return mat;
end

function plastic(diffuse, specular)
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = diffuse
    mat.ambient = color(0.2, 0.2, 0.2)
	mat.specular_on = true
	mat.specularity = specular
	return mat;
end


function testProbeMat2()
	return plastic(color(1, 1, 0), 1)
end

function testProbeMat3()
	return plastic(color(0, 0, 1), 0.5)
end

function testProbeMat4()
	return plastic(color(1, 0.5, 0.1), 0.6)
end

-- vertex reflection
function probeTest1()
    -- material used for probed object
	local result = plastic(color(0.5, 0, 0), 0.2)
	result.reflection_on = true
	result.reflection_opacity = 0.7
	result.reflection_glossmode = ReflRefrBlend.AdditifAlpha
	result.gloss_map = nil
	return result
end


-- vertex reflection with glossmap
function probeTest2()
    -- material used for probed object
    local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("damier.tga")
    mat.ambient = color(0.2, 0.2, 0.2)
	mat.specular_on = true
	mat.specularity = specular
	mat.reflection_on = true
	mat.reflection_opacity = 0.8
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	mat.reflection_glossmap = mat.diffuse
	return mat
end

-- bumpy reflection with glossmap
function probeTest3()
    -- material used for probed object
   local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("damier.tga")
	mat.normal = tex_normal("noise.tga")
    mat.ambient = color(0.2, 0.2, 0.2)
	--mat.specular_on = true
	mat.specularity = computeSpecularity(0.95)
 	mat.reflection_on = true
	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AdditifAlpha
	mat.reflection_glossmap = mat.diffuse
	mat.reflection_bump_factor = 0.5
	return mat
end

-- vertex refraction
function probeTest4()
    -- material used for probed object
	local result = plastic(color(0.5, 0, 0), 0.2)
	result.refraction_on = true
	result.refraction_opacity = 0.7
	result.refraction_glossmode = ReflRefrBlend.AdditifAlpha
	result.refraction_ior = 0.7 -- zoom out
	result.gloss_map = nil
	return result
end


-- vertex refraction with chromatic aberration
function probeTest5()
    -- material used for probed object
	local result = plastic(color(0.5, 0, 0), 0.2)
	result.refraction_on = true
	result.refraction_opacity = 0.7
	result.refraction_glossmode = ReflRefrBlend.AdditifAlpha
	result.refraction_ior = 0.7 -- zoom out	
	result.gloss_map = nil
	result.chromatic_abberation = 0.01
	return result
end

-- bumpy refraction
function probeTest6()
    -- material used for probed object
   local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("damier.tga")
	mat.normal = tex_normal("noise.tga")
    mat.ambient = color(0.2, 0.2, 0.2)
	--mat.specular_on = true
--	mat.specularity = computeSpecularity(0.95)
 	mat.refraction_on = true
	mat.refraction_opacity = 0.9
	mat.refraction_glossmode = ReflRefrBlend.AdditifAlpha
	mat.refraction_ior = 0.7 -- zoom out	
	mat.gloss_map = nil
--	result.chromatic_abberation = 0.01
	return mat

end

-- bumpy refraction with chromatic abberation
function probeTest7()
    -- material used for probed object
   local mat = probeTest6()
	mat.chromatic_abberation = 0.03
	return mat
end

-- bumpy refraction with chromatic abberation and gloss map
function probeTest8()
    -- material used for probed object
   local mat = probeTest7()
	mat.chromatic_abberation = 0.03
	mat.refraction_glossmap = mat.diffuse
	return mat
end

-- bumpy refraction with chromatic abberation and gloss map
-- + bumpy reflection
function probeTest9()
    -- material used for probed object
   local mat = probeTest8()
	mat.refraction_opacity = 0.5
 	mat.reflection_on = true
	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AdditifAlpha
	mat.reflection_glossmap = mat.diffuse
	mat.reflection_bump_factor = 0.5
	return mat
end

-- fresnel test, no normal map
function probeTest10()
    -- material used for probed object
   local mat = {};
	mat.lighted = displayLighted;
	mat.specular_on = true
	mat.specularity = 1
	mat.diffuse = color(0.6, 0.0, 0.0)
	mat.ambient = color(0.3, 0.0, 0.0)
	mat.gloss = color(1, 0, 0)
	mat.reflection_on = true
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend

	
	mat.reflection_glossmap = fresnel(1, 0.85)
	mat.reflection_color = color(1, 1, 1)
	return mat
end

-- fresnel term with normal map
function probeTest11()
	 -- material used for probed object
   local mat = {};
	mat.lighted = false;
	mat.diffuse = color(0.3, 0.1, 0.8)
	mat.normal = tex_normal("noise.tga")
   mat.ambient = color(0.2, 0.2, 0.2)
	--mat.specular_on = true
	--mat.specularity = 0.95
 	mat.reflection_on = true
	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	mat.reflection_bump_factor = 0.6
	mat.reflection_glossmap = fresnel(1, 0.9, mat.normal, mat.reflection_bump_factor)
	return mat
end



function testProbeMat5()
	return probeTest10()
end

-- planar probe tests

function probeMountain()
local mat = {}
	mat.attenuation_on = true
	mat.diffuse = tex("stone1.tga")
	mat.ambient = color(0.1, 0.1, 0.1)
	return mat
end


function testPlanarMirror1()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0, 1, 0.3) 
  	mat.ambient = color(0.1, 0.1, 0.1)
	mat.reflection_opacity = 0.5
 	mat.reflection_on = true
	--mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	return mat;	
end


function testPlanarMirror2()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("damier.tga", uniformTexScaler(4, 0))
	mat.ambient = color(0.1, 0.1, 0.1)	
 	mat.reflection_on = true
	mat.reflection_bump_factor = 0
	mat.reflection_opacity = 0.9
--	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AdditifAlpha
	mat.reflection_glossmap = mat.diffuse
	return mat;	
end


-- bug dans driver catalyst sur 9000 mobility ? : quant on utilise le materiau suivant 
-- si mat.reflection_opacity vaut 1 alors affichage cassé ... -> fonctionne correctement 
-- avec le reference rasterizer
-- ça la fait une fois sur 2 si on lance depuis max!!!
function testPlanarMirror2QuiBuggue()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("damier.tga", uniformTexScaler(4, 0))
	mat.ambient = color(0.1, 0.1, 0.1)	
 	mat.reflection_on = true
	mat.reflection_bump_factor = 0
--	mat.reflection_opacity = 0.9
	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AdditifAlpha
--	mat.reflection_glossmap = mat.diffuse
	return mat;	
end



function testPlanarMirror3()
	local mat = {};
	mat.lighted = false;
--	mat.ambient = color(0.1, 0.1, 0.1)	
 	mat.reflection_on = true
	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	mat.dudv = add(tex_dudv("noise.tga", texPannerLinear(0.04, 0.06, 0)),
                  tex_dudv("noise.tga", texPannerLinear(-0.02, 0.06, 0)))
	return mat;	
end

function testPlanarMirror4()
	local mat = {};
	mat.lighted = false;
--	mat.ambient = color(0.1, 0.1, 0.1)	
 	mat.refraction_on = true
	mat.refraction_opacity = 1
	mat.refraction_ior = 0.1 -- todo : change this to 'bump_factor'
	mat.refraction_glossmode = ReflRefrBlend.AlphaBlend
	mat.dudv = add(tex_dudv("noise.tga", texPannerLinear(0.043, 0.0612, 0)),
                  tex_dudv("noise.tga", texPannerLinear(-0.01567, 0.0567, 0)))
	return mat;	
end


function testPlanarMirror5()
	local mat = {};
	mat.lighted = false;
--	mat.ambient = color(0.1, 0.1, 0.1)	
 	mat.refraction_on = true
	mat.refraction_opacity = 1
	mat.refraction_ior = 0.1 -- todo : change this to 'bump_factor'
	mat.refraction_glossmode = ReflRefrBlend.AlphaBlend
	mat.dudv = add(tex_dudv("noise.tga", texPannerLinear(0.043, 0.0612, 0)),
                  tex_dudv("noise.tga", texPannerLinear(-0.01567, 0.0567, 0)))
 	mat.reflection_on = true
	mat.reflection_opacity = 0.5
	mat.reflection_bump_factor = 0.1
	mat.reflection_glossmode = ReflRefrBlend.AdditifAlpha
	mat.dudv = add(tex_dudv("noise.tga", texPannerLinear(0.04, 0.06, 0)),
                  tex_dudv("noise.tga", texPannerLinear(-0.02, 0.06, 0)))
	return mat;	
end



function doubleBump(noise1, noise2, panU1, panV1, panU2, panV2, amount1, amount2)
	--return tex_dudv(noise1, add(tex_dudv(noise2, texPannerLinear(panU1, panV1, 0)),
	--			  		 tex_coord(texPannerLinear(panU2, panV2, 0))))

	local b0 = add(mul(tex_dudv(noise2, texPannerLinearScaled(panU1, panV1, 4, 4, 0)), color(amount1, 						amount1, 0)), tex_coord(texPannerLinear(panU2, panV2, 0)))
	return tex_dudv(noise1, b0)
end

function unbias(node)
	return add(mul(node, color(0.5, 0.5, 0.5)), color(0.5, 0.5, 0.5))
end

-- 2 successive dependent read (ps2_0)
function testPlanarMirror6()
	local mat = {};
	mat.lighted = false;
--	mat.ambient = color(0.1, 0.1, 0.1)	
 	mat.refraction_on = true
	mat.refraction_opacity = 1
	mat.refraction_ior = 0.1 -- todo : change this to 'bump_factor'
	mat.refraction_glossmode = ReflRefrBlend.AlphaBlend
	mat.dudv = doubleBump("noise.tga", "noise.tga", -0.01567, 0.0567, 0.043, 0.0612, 0.1, 0.1)
 	mat.reflection_on = true
	mat.reflection_opacity = 0.5
	mat.reflection_bump_factor = 0.1
	mat.reflection_glossmode = ReflRefrBlend.AdditifAlpha
	mat.dudv = doubleBump("noise.tga", "noise.tga", 0.04, 0.06, -0.02, 0.06, 0.1, 0.1)
	return mat;	
end

-----------------------------------------------------------------------------------------
-- 2 successive dependent reads (ps2_0) -> bumpmap test
function testPlanarMirror7()
	local mat = {};
	mat.lighted = false;
	--mat.diffuse = unbias(doubleBump("noise.tga", "noise.tga", -0.01567, 0.0567, 0.043, 0.0612,
   --                     0.1, 0.1))
	mat.diffuse = unbias(doubleBump("noise.tga", "noise.tga", -0.01567, 0.0567, 0, 0,
                        0.3, 0.1))
	return mat;	
end

-----------------------------------------------------------------------------------------
-- custom envmap shader
-- colored fresnel factor bewteen double bumped reflect & refract
function testPlanarMirror8()
	local bm = CBitmap()
	if not bm:loadFromFile("gradient1.tga") then 
		log("gradient texture not found")
		return nil 
	end
	local width = bm:getWidth(0)
	local height = bm:getHeight(0)
	--log("width = " .. tostring(width) .. ", height = " .. tostring(height))
	local col = CRGBA()
	local scale = 8 * (width - 1)
	local bias = -0.25
	local function f(x, y, z)
		local length = math.sqrt(x * x + y * y + z * z)
		local px = scale * ((math.acos(z / length) / 3.14) + bias)
		bm:getRGBA(px, 0, col)
		return col.R, col.G, col.B, col.A
		--return math.random() * 255, math.random() * 255, math.random() * 255, math.random() * 255
	end	
	local cube = tex_lua_cube(64, f, "colored_water_fresnel_cubemap")
	local mat = {}
	mat.reflection_on = true
	mat.reflection_bump_factor = 0.1
	mat.refraction_on = true
	mat.refraction_ior = 0
	local fresnel = tex(cube, { mode = TexCoordGen.LocalVVector	})

	local dudv = 	add(tex_dudv("noise.tga", texPannerLinear(0.04, 0.06, 0)),
                  	 tex_dudv("noise.tga", texPannerLinear(-0.02, 0.06, 0)))

	-- local refl = screen_project_tex(tex_reflect_2d(), probe_projection_scale())
	dudv = mul(dudv, color(mat.reflection_bump_factor, mat.reflection_bump_factor, mat.reflection_bump_factor))
	local uv = add(dudv, screen_project_tex_coord(probe_projection_scale()))
	local refl = tex(tex_reflect_2d(), uv)
	local refr = tex(tex_refract_2d(), uv)
	mat.custom_envmap = lerp(refl, refr, fresnel)
	--mat.custom_envmap = refl
	mat.custom_envmap_gloss_mode = ReflRefrBlend.AlphaBlend
	return mat;	
end


-----------------------------------------------------------------------------------------
-- lua custom material
function testLuaMat()
	local bm = CBitmap()
	if not bm:loadFromFile("gradient1.tga") then 
		log("gradient texture not found")
		return nil 
	end
	local width = bm:getWidth(0)
	local height = bm:getHeight(0)
--	log("width = " .. tostring(width) .. ", height = " .. tostring(height))
	local col = CRGBA()
	local function f(x, y)
		local dist = math.sqrt((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5))
		dist = math.mod(4 * dist, 1)
		bm:getRGBA((width - 1) * dist, 0, col)
		return col.R, col.G, col.B, col.A
	end
	local mat = {}
	mat.diffuse = tex(tex_lua_2d(128, 128, f, "lua_concentric_tex1"))
	mat.lighted = false
	return mat
end


function probeMirror()
	--return testPlanarMirror5()
	return testPlanarMirror8()
end


local reverseMat = CMatrix34()
reverseMat.t = CVector(0, 0, 0)
--reverseMat:setScale(CVector(1, 1, 1));
reverseMat.m = xRotation3(1.57)		



local texGenWorldPos = 
{
	mode = TexCoordGen.Position,
	gen = function(renderContext) return reverseMat end
}


---------------------------------------------------------------------------------------
function mountainSkyBox()
	local mat = {}
	mat.diffuse = tex_cube({"sky_right.jpg", "sky_left.jpg", 
						  "sky_bottom.jpg", "sky_top.jpg",
						  "sky_front.jpg", "sky_back.jpg" 
                   },
						texGenWorldPos						
						)
	mat.lighted = false
	return mat
end

function crystal1()
    local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0.8, 0.2, 0.2)
    mat.ambient = color(0.2, 0.2, 0.2)
	mat.specular_on = true
	mat.specularity = specular
	mat.reflection_on = true
	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	mat.reflection_glossmap = tex("crystal_alpha.tga")
	return mat

end

function explodeSphereMat()
 local mat = {}
 mat.transparent = true;
 mat.src_blend = Blend.One
 mat.dest_blend = Blend.One
 mat.diffuse = color(0.09, 0.05, 0.05)
 return mat;
end

function floor1()
	local mat = {};
	mat.lighted = false
--	mat.ambient = color(0.1, 0.1, 0.1)	
 	mat.refraction_on = false
	mat.refraction_opacity = 1
	mat.refraction_ior = 0.1 -- todo : change this to 'bump_factor'
	mat.refraction_glossmode = ReflRefrBlend.AlphaBlend
	mat.dudv = add(tex_dudv("noise.tga", texPannerLinear(0.043, 0.0612, 0)),
                  tex_dudv("noise.tga", texPannerLinear(-0.01567, 0.0567, 0)))
 	mat.reflection_on = true
	mat.reflection_opacity = 0.5
	mat.reflection_bump_factor = 0.1
	mat.reflection_glossmode = ReflRefrBlend.AdditifAlpha
	mat.dudv = add(tex_dudv("noise.tga", texPannerLinear(0.04, 0.06, 0)),
                  tex_dudv("noise.tga", texPannerLinear(-0.02, 0.06, 0)))
	return mat;	
end



local coreColor = CRGBA(255 * 0.7, 255 * 0.3, 0, 255)
local black = CRGBA(0, 0, 0, 255)
local coreFadeDelta = 0.3


function plasma(filename, ws, hs)
	local mat = {};
 	mat.transparent = true;
	 mat.src_blend = Blend.One
	 mat.dest_blend = Blend.One
	mat.lighted = false
	local ampl = 0.08
	local scale = 0.9
	local dudv0 = mul(color(ampl, ampl, ampl), tex_dudv("noise.tga", texPannerLinearScaled(0.143, 0.1612, scale * ws, scale * hs, 0)))
	ampl = 0.35
	scale = 0.05
	local dudv1 = mul(color(ampl, ampl, ampl), tex_dudv("noise.tga", texPannerLinearScaled(-0.013, 0.032, scale * ws, scale * hs, 0)))
	local base = add(tex_coord(0), add(dudv0, dudv1))
	mat.diffuse = mul(color(blenderInOut(black, coreColor, coreFadeDelta, coreFadeDelta)), add(mul(color(0.5, 0.5, 0.5), tex(filename)), tex(filename, base)))
	return mat
end

function coredumped()
	return plasma("coredumped.tga", 3,1)	
end

function coredumped2()
	local mat = {};
 	mat.transparent = true;
	 mat.src_blend = Blend.One
	 mat.dest_blend = Blend.One
	mat.lighted = false
	mat.diffuse = tex("coredumped.tga")
	return mat	
end


function beam()
	local mat = {};
 	mat.transparent = true;
   mat.src_blend = Blend.One
	mat.dest_blend = Blend.One	
	mat.diffuse = mul(color(blenderInOut(black, coreColor, coreFadeDelta, coreFadeDelta)), tex("beam.tga"))
	return mat
end

function beam2()
	local mat = {};
 	mat.transparent = true;
   mat.src_blend = Blend.One
	mat.dest_blend = Blend.One	
	mat.diffuse = tex("beam.tga")
	return mat
end


function shinySphere()
    -- material used for probed object
   local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("damier.tga")
	mat.normal = tex_normal("noise.tga")
    mat.ambient = color(0.2, 0.2, 0.2)
	--mat.specular_on = true
	mat.specularity = computeSpecularity(0.95)
 	mat.reflection_on = true
	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AdditifAlpha
	mat.reflection_glossmap = mat.diffuse
	mat.reflection_bump_factor = 0.5
	return mat
end


s3amb = true
ambAmount = 0.15

function metal1()
	local mat = {};
	mat.lighted = displayLighted;
	--mat.diffuse = tex("metal_alpha.tga")
	--mat.diffuse = tex("metal_water_drop_128.tga")	
	mat.diffuse = color(1, 1, 1)
	mat.normal = tex_normal("metal_hm2.tga")	
	--if getPixelShaderModel() ~= "ps_1_4" then	
	-- mat.specular_on = true
	-- mat.specularity = 0.95
	-- mat.gloss = color(0.7, 0.7, 0.7) -- metallic look
	-- end
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end

function metal2()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0.5, 0.5, 0.6)
	mat.normal = tex_normal("pipe_hm.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.92)
	mat.gloss = color(0.7, 0.7, 0.9) -- metallic look
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end

function metal3()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0.4, 0.4, 0.4)	
	mat.normal = tex_normal("metal_hm3.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.85)
	mat.gloss = alpha(color(1, 1, 1, 0.3))
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end



function chamfer1()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 1, 1)
	mat.normal = tex_normal("chamfer1.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.85)
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end

function chamfer2()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 1, 1)
	mat.normal = tex_normal("chamfer2.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.85)
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end

function chamfer3()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 1, 1)
	mat.normal = tex_normal("chamfer3.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.85)
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end


function chamfer4()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(1, 1, 1)
	mat.normal = tex_normal("chamfer4.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.85)
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end

-- vertex reflection with glossmap
function brushed()
    -- material used for probed object
   local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0.2, 0.2, 0.2)	
	mat.specular_on = true
	mat.specularity = specular
	mat.reflection_on = true
	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	mat.reflection_glossmap = alpha(tex("brushed.tga"))
	return mat
end

function metal4()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0.6, 0.6, 0.8)	
	mat.normal = tex_normal("metal_hm4.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.85)
	mat.gloss = alpha(color(1, 1, 1, 0.7))
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end


function rustyMetal()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("rusty.tga")
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end

function yellowPlastic()
	local mat = plastic(color(1, 0.8, 0), 0.8)
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end

function cellularFX()
	local mat = {}
	mat.lighted = false
	mat.diffuse = max(tex("cellular.tga", texPannerLinear(0.1, 0.21, 0)), 
							tex("cellular.tga", texPannerLinear(-0.147, -0.123, 0)))
	mat.diffuse = max(mat.diffuse,
							tex("cellular.tga", texPannerLinear(-0.127, 0.082, 0)))
	mat.diffuse = max(mat.diffuse,
							tex("cellular.tga", texPannerLinear(0.0953, -0.105, 0)))
	mat.diffuse = mul(mat.diffuse, color(0.4, 0.1, 0.1))
	return mat
end

function metal5()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0.4, 0.4, 0.4)	
	mat.normal = tex_normal("metal_hm5.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.85)
	mat.gloss = alpha(color(1, 1, 1, 0.3))
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end


function metal6()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("metal_diffuse6.tga")
	mat.normal = tex_normal("metal_hm6.tga")
	if getPixelShaderModel() ~= "ps_1_4" then	
		mat.specular_on = true
		mat.specularity = computeSpecularity(0.85)
	end
	mat.gloss = alpha(tex("metal_hm6.tga"))
	if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return mat
end



function yellowStripe()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("yellow_stripe.tga")
	return mat
end

function laser()
	local filename="laser.tga"
	local ws = 1
	local hs = 1
	local mat = {};
 	mat.transparent = true;
   mat.src_blend = Blend.One
	mat.dest_blend = Blend.One
	mat.lighted = false
	local ampl = 0.2
	local scale = 0.5
	local dudv0 = mul(color(ampl, ampl, ampl), tex_dudv("noise.tga", texPannerLinearScaled(0.143, 0.1612, scale * ws, scale * hs, 0)))
	ampl = 2.0
	scale = 0.05
	local dudv1 = mul(color(ampl, ampl, ampl), tex_dudv("noise.tga", texPannerLinearScaled(-0.013, 0.0832, scale * ws, scale * hs, 0)))
	local base = add(tex_coord(0), add(dudv0, dudv1))
	mat.diffuse = mul(color(1,0.5,0), tex(filename, base))
	mat.double_sided = true
	return mat
end

function blank()
	local mat = {};
	mat.lighted = false;
	mat.diffuse = color(1, 1, 1, 1)
	return mat;
end

function cyan()
	local mat = {};
	mat.lighted = false;
	mat.diffuse = color(0, 1, 1, 1)
	return mat;
end

function scrollMotif1()
	local mat = {};
	mat.lighted = false;
	mat.diffuse = mul(color(1, 0, 0), tex("scroll_motif1.tga", texPannerLinear(-1.5, 0, 0)))
	return mat;
end

function scrollMotif2()
	local mat = {};
	mat.lighted = false;
	mat.diffuse = mul(color(1, 0.5, 0), tex("scroll_motif2.tga", texPannerLinear(-1.8, 0, 0)))
	mat.diffuse = mul(mat.diffuse, tex("scroll_motif2.tga", texPannerLinearScaled(0.4, 0, 0.10, 1, 0)))
	return mat;
end


function metalAlpha()
    -- material used for probed object
   local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("metal_alpha.tga")
	mat.normal = tex_normal("noise.tga")
	mat.reflection_bump_factor = 0.2
	mat.reflection_on = true
	mat.reflection_opacity = 1
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	mat.reflection_glossmap = alpha(tex("metal_alpha.tga"))
	return mat
end

function metaballMat()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0.7, 0, 0)
	mat.ambient = color(0.3, 0.3, 0.3)
	mat.gloss = color(0.7, 0.1, 0.1) -- metallic look
--	mat.specular_on = true
--	mat.specularity = 0.5

	mat.refraction_on = true
	mat.refraction_ior = 0.15 -- screen space refraction
	mat.refraction_opacity = 0.6
	mat.refraction_glossmode = ReflRefrBlend.AlphaBlend

	--mat.reflection_glossmap = fresnel(1, 0.85)


	mat.reflection_on = true
	mat.reflection_opacity = 0.3
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend

	return mat
end


-- vertex reflection with glossmap
function screenRefract()
    -- material used for probed object
    local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("damier.tga")
   mat.ambient = color(0.2, 0.2, 0.2)
	mat.reflection_on = true
	mat.reflection_opacity = 0.5
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	--
	mat.refraction_on = true
	mat.refraction_ior = 0.1 -- screen space refraction
	mat.refraction_opacity = 0.8
	mat.refraction_glossmode = ReflRefrBlend.AlphaBlend
	return mat
end

function cubicRefract()
    -- material used for probed object
    local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("damier.tga")
   mat.ambient = color(0.2, 0.2, 0.2)
	--mat.reflection_on = true
	mat.reflection_opacity = 0.5
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	--
	mat.refraction_on = true
	mat.refraction_ior = 3.33 -- screen space refraction
	mat.refraction_opacity = 0.8
	mat.refraction_glossmode = ReflRefrBlend.AlphaBlend
	return mat
end


function nicoSpline()
    -- material used for probed object
   local mat = {};
	mat.lighted = false;
	mat.transparent = true;
	mat.diffuse = tex("tracer.tga", texPannerLinear(-40, 0, 0, false))
	set_wrap_u(mat.diffuse, WrapMode.Clamp)
	enable_mipmaps(mat.diffuse, false)
	mat.diffuse = mul(color(1.0, 0.2, 0.1, 1), mat.diffuse)
--	mat.force_z_write = true
	mat.src_blend = Blend.One
   mat.dest_blend = Blend.One
	mat.double_sided = true
	return mat
end


function lightTransition()
	local mat = {}
	mat.attenuation_on = true
	mat.color_on = false			
	if getPixelShaderModel() ~= "ps_1_4" then
		local blendColor = blender(CRGBA(255, 255, 255, 255), CRGBA(0, 0, 0, 255), 1)
		mat.shader = add(tex_cube("caustic.tga", lightSpinnerY(0.2)),
	        tex_cube("caustic.tga", lightSpinnerY(-0.15)))			
		mat.shader = mul(color(1, 0.3, 0.3), mat.shader)	
		mat.shader = lerp(mat.shader, color(1, 1, 1), color(blendColor))
	else
		local blendColor = blender(CRGBA(255, 255, 255, 255), CRGBA(255, 255/3, 255/3, 255), 1)
		mat.shader = color(blendColor)
	end
	return mat
end


function asteroidMat()
   local mat = {};
	mat.lighted = displayLighted;	
	mat.diffuse = color(0.6, 0.6, 0.4)
	mat.normal = tex_normal("asteroid.tga")
   --mat.ambient = color(0.2, 0.2, 0.2)
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)
	mat.gloss = alpha(color(1, 1, 1, 0.2))	
	return mat
end


function stars()
   local mat = {};
	mat.lighted = false;	
	mat.diffuse = tex("stars.tga")
	return mat
end


function sunset()
   local mat = {};
	mat.lighted = false;	
	mat.diffuse = tex("sunset.tga")
	return mat
end


function spaceShip()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0.8, 1, 0.8) 
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)
	mat.gloss = alpha(color(1, 1, 1, 0.7))	
	mat.normal=tex_normal("spaceship1.tga")
	mat.reflection_bump_factor = 0.5
--   mat.ambient = color(0.2, 0.2, 0.2)
--	mat.reflection_on = true
	mat.reflection_opacity = 1
	mat.reflection_glossmap = alpha(tex("spaceship1.tga"))
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	return mat
end

--------------------------
-- space ship materials --
--------------------------

local function ssAmbient(mat)
	local ao = tex("spaceship_ambient_occlusion.tga", 1)
	if mat.diffuse then
		mat.diffuse = mul(mat.diffuse, ao)
	else	
		mat.diffuse = ao
	end	
	return mat
end

function pannel1()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("pannel2.tga") 
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)
	mat.gloss = alpha(color(1, 1, 1, 0.1))	
	mat.normal = tex_normal("pannel2_heightmap.tga")
	return ssAmbient(mat)
end

function pannel3()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = tex("pannel3.tga") 
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)
	if getPixelShaderModel() ~= "ps_1_4" then
		mat.gloss = mul(color(0.3, 0.3, 0.3, 0.3), mat.diffuse)
	else
		mat.gloss = mat.diffuse
	end
	mat.normal = tex_normal("pannel3_heightmap.tga")
	return ssAmbient(mat)
end




function pannel3bis()
	local mat = {};
	mat.lighted = displayLighted;
--	mat.diffuse = tex("pannel3.tga") 
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)
	mat.gloss = color(0.3, 0.3, 0.3, 0.3)
	mat.normal = tex_normal("pannel3_heightmap.tga")
	return ssAmbient(mat)
end



function ssWings()
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("metal2_diffuse.tga")
	mat.normal = tex_normal("metal_hm2.tga")
	--mat.emissive = tex("metal2_emissive.tga")
	--mat.aniso_tex = "aniso_lighting.tga"
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.95)
	mat.gloss = mul(color(0.7, 0.7, 0.7, 0.7), mat.diffuse)
	--if s3amb then 	mat.ambient = color(ambAmount, ambAmount, ambAmount) end
	return ssAmbient(mat)
end

function ssRing()
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = color(0.45, 0.45, 0.5)
	mat.aniso_tex = "aniso_lighting.tga"
	return ssAmbient(mat)
end


function ssReactorRim()
	local mat = {}
	mat.lighted = displayLighted	
	mat.diffuse = color(0.4, 0.4, 0.5)
	mat.normal = tex_normal("metal_hm5.tga")	
	return ssAmbient(mat)
end


-- test for anisotropic lighting
function testAnisoLighting()
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = color(0.8, 0.8, 1.0)
	mat.aniso_tex = "aniso_lighting.tga"
	return mat
end

function testAnisoLightingBumped()
	local mat = {}
	mat.lighted = displayLighted
	mat.normal = tex_normal("TilesHeight.tga")
	--mat.normal = color(0, 0, 1)
	mat.diffuse = color(0.8, 0.8, 1.0)
	mat.aniso_tex = "aniso_lighting.tga"
	return mat
end

function ssGradient()
	local mat = {}
	mat.lighted = false
	mat.double_sided = true
	mat.transparent = true
	mat.src_blend = Blend.One
	mat.dest_blend = Blend.One
	local uv = tex_dudv("noise.tga", texPannerLinearScaled(0.1, 0.000, 0.2, 0.2, 0))
	uv = add(tex_coord(0), mul(uv, color(0.6, 0.6)))
	mat.diffuse = tex("ss_gradient.tga", uv)	
	set_wrap_u(mat.diffuse, WrapMode.Clamp)
	return mat
end


function ssReactor()
	local mat = {}
	mat.lighted = false
	mat.double_sided = true	
	local uv = tex_dudv("noise.tga", texPannerLinearScaled(0.1, 0.000, 0.2, 0.2, 0))
	uv = add(tex_coord(0), mul(uv, color(0.4, 0.4)))
	mat.diffuse = tex("ss_reactor.tga", uv)	
	return mat
end

-- axial pipe
function ssAxialPipe()
	local mat = {};
	mat.lighted = displayLighted;	
	mat.diffuse = color(0.8, 0.9, 1.0)
	mat.normal = tex_normal("metal_hm3.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.85)
	mat.gloss = color(0.2, 0.2, 0.2, 0.2)
	--mat.aniso_tex = "aniso_lighting.tga"
	return ssAmbient(mat)
end


function ssBrushed()
	local mat = {};
	mat.lighted = displayLighted;	
	mat.diffuse = tex("brushed2.tga")	
	mat.specular_on = true
	mat.specularity = 0.85
	mat.gloss = alpha(mat.diffuse)	
	return ssAmbient(mat)
end

-- axial pipe
function ssStripes()
	local mat = {};
	mat.lighted = displayLighted;	
	mat.diffuse = color(0.5, 0.25, 0.25)
	mat.normal = tex_normal("ss_stripes_hm.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.85)
	mat.gloss = color(0.9, 0.0, 0.0, 0.0)	
	return ssAmbient(mat)
end

-- cockpit

function ssCockpit()
	local mat = {}
	mat.lighted = false;
	mat.diffuse = tex_cube("ss_cockpit.jpg", envMapReflect())
	return ssAmbient(mat);
end

function ssAmbientOcclusion()
	local mat = {}	
	mat.lighted = false
	mat.diffuse = tex("spaceship_ambient_occlusion.tga", 1)
	return mat;
end



function testAmbientOcclusion()
	local mat = {}	
	mat.lighted = false
	mat.diffuse = mul(tex("metal_diffuse6.tga"), tex("ambient_occlusion.tga", 1))
	return mat;
end

function ssPlanet()
	local mat = {}
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.SrcAlpha
 	mat.dest_blend = Blend.InvSrcAlpha
 	mat.diffuse = tex("planet.tga");
	return mat
end

function ssPlanet2()
	local mat = {}
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.SrcAlpha
 	mat.dest_blend = Blend.InvSrcAlpha
 	mat.diffuse = tex("planet2.tga");
	return mat
end

function ssPlanet3()
	local mat = {}
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.SrcAlpha
 	mat.dest_blend = Blend.InvSrcAlpha
 	mat.diffuse = tex("planet3.tga");
	return mat
end

function ssPlanet4()
	local mat = {}
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.SrcAlpha
 	mat.dest_blend = Blend.InvSrcAlpha
 	mat.diffuse = tex("planet4.tga");
	return mat
end



function ssNebulus()
	local mat = {}
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.One
 	mat.diffuse = tex("nebulus.tga");
	return mat
end

function ssNebulus2()
	local mat = {}
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.One
 	mat.diffuse = tex("nebulus2.tga", texPannerLinear(0.01, 0));
	return mat
end

function ssAsteroidLayer()
	local mat = {}
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.SrcAlpha
 	mat.dest_blend = Blend.InvSrcAlpha	
 	mat.diffuse = tex("asteroid_layer.tga");
	return mat
end

-----------------
-- ocean scene --
-----------------



function osCloudLayer(cloudColor, speed)
	local mat = {}
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.SrcAlpha
 	mat.dest_blend = Blend.InvSrcAlpha	
 	local texture = tex("cloud_layer2.tga", texPannerLinear(speed, 0)); 	
 	set_wrap_v(texture, WrapMode.Clamp)
 	mat.diffuse = separate_alpha(mul(texture, cloudColor), alpha(texture))
	return mat
end


cloudColor1 = color(0.4, 0.3, 0.3, 1); cloudSpeed1 = 0.02;
cloudColor2 = color(0.35, 0.3, 0.3, 1); cloudSpeed2 = 0.04;
cloudColor3 = color(0.32, 0.35, 0.4, 1); cloudSpeed3 = 0.06;

function osCloudLayer1()
	return osCloudLayer(cloudColor1, cloudSpeed1)
end	

function osCloudLayer2()
	return osCloudLayer(cloudColor2, cloudSpeed2)
end	

function osCloudLayer3()
	return osCloudLayer(cloudColor3, cloudSpeed3)
end	

-------------------
-- lightmap test --
-------------------

function lightmap()
	local mat = {};
	mat.lighted = displayLighted;
	mat.diffuse = color(0.3, 0.3, 0.8) 
	mat.ambient = color(0.1, 0.1, 0.2) 
	mat.specular_on = true
	mat.specularity = 0.5
	return mat;
end



function demoWater()
	local mat = {};
	mat.lighted = false;	
	mat.diffuse = tex("snow.tga")
--	mat.ambient = color(0.1, 0.1, 0.1)	
 	mat.reflection_on = true 	
	mat.reflection_glossmap = alpha(vertex_color0())
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	mat.reflection_bump_factor = 0.1
	mat.dudv = add(tex_dudv("noise.tga", texPannerLinear(0.04, 0.06, 0)),
                  tex_dudv("noise.tga", texPannerLinear(-0.02, 0.06, 0)))
	return mat;	
end

function demoWaterFog()
	local mat = demoWater();
	mat.diffuse = color(72 / 255, 83 / 255, 92 / 255)
	--mat.dudv = mul(mat.dudv, vertex_color1())
	mat.reflection_glossmap = alpha(vertex_color1())
	return mat;	
end

function cloudTransition()	
	local mat = {};
	mat.lighted = false;	
	local texture = tex("cloud_transition.tga")
	set_wrap_v(texture, WrapMode.Clamp)
	mat.diffuse = texture
    mat.transparent = true
 	mat.src_blend = Blend.SrcAlpha
 	mat.dest_blend = Blend.InvSrcAlpha
 	mat.double_sided = true
	return mat;	
end

-- cube map used to select a texture for tri plane texturing
local triPlaneTexCubeMap = nil

local function linCrop(value, min, max)
	if (value < min) then return 0 end
	if (value > max) then return 1 end
	return (value - min) / (max - min)
end

function getTriPlaneTexCubeMap()	
	local sinMin = math.sin(math.pi / 4)
	local sinMax = math.sin(math.pi / 2)
	if triPlaneTexCubeMap == nil  then
		local function f(x, y, z)
			local r, g, b, a
			r = linCrop(y, sinMin, sinMax)
			g = linCrop(-y, sinMin, sinMax)
			b = linCrop(x, sinMin, sinMax) + linCrop(-x, sinMin, sinMax)
			a = linCrop(z, sinMin, sinMax) + linCrop(-z, sinMin, sinMax)
			local w = 255 / (r + g + b + a)			
			return w * r, w * g, w * b, w * a			
		end	
		triPlaneTexCubeMap = tex_lua_cube(64, f, "tri_plane_texturing_cube_map")	
	end
	return  triPlaneTexCubeMap
end


function triTexMatrix(vI, vJ, vK)
	
    local value = CMatrix34();
    value:setIdentity()
    value:setRot(vI, vJ, vK);    	
	return 
	{
		mode  = TexCoordGen.Position,
		gen   = constant_matrix(value) 
	}
end

function triTexTopBottom(scale)
	return triTexMatrix(CVector(scale, 0, 0),
    					CVector(0, 0, 0),
    					CVector(0, scale, 0))	
end

function triTexRightLeft(scale)
	return triTexMatrix(CVector(0, 0, 0),
    					CVector(0, scale, 0),
    					CVector(scale, 0, 0))	
end

function triTexFrontBack(scale)
	return triTexMatrix(CVector(scale, 0, 0),
    					CVector(0, scale, 0),
    					CVector(0, 0, 0))	
end

-- create tri-plane textured material
function triPlaneTex(top, side, bottom, scale)
	local c = tex(getTriPlaneTexCubeMap(), { mode = TexCoordGen.LocalNormal })
	set_wrap_u(c, WrapMode.Clamp)
	set_wrap_v(c, WrapMode.Clamp)
	set_wrap_w(c, WrapMode.Clamp)	
	local result = mul(red(c), tex_normal(top, triTexTopBottom(scale)))
	result = add(result, mul(blue(c), tex_normal(side, triTexRightLeft(scale))))
	result = add(result, mul(alpha(c), tex_normal(side, triTexFrontBack(scale))))
	result = add(result, mul(green(c), tex_normal(bottom, triTexTopBottom(scale))))
	return result
end

function waterRock()
	mat = {}
	mat.lighted = displayLighted
	mat.diffuse = mul(color(0.4, 0.4, 0.4), vertex_color0())	
	mat.emissive = mul(tex("snow.tga"), complement(vertex_color0())) 
	if getPixelShaderModel() ~= "ps_1_4" then
		mat.specular_on = true
		mat.specularity = computeSpecularity(0.95)
		mat.gloss = mul(color(0.8, 0.8, 0.8), vertex_color0())
	end
	mat.normal = triPlaneTex("cellular.tga", "alien_heightmap.tga", "mud_heightmap.tga", 0.01)
	return mat
end

function testVertexColor()
	mat = {}
	mat.lighted = false
	mat.diffuse = vertex_color1()
	return mat
end

function loadingLogo()
	mat = {}
	mat.lighted = false
	mat.diffuse = tex("planet2.tga")
	return mat
end

function precaching()
	mat = {}
	mat.lighted = false
	mat.diffuse = tex("precaching.tga")
	return mat
end

function progressBar()
	mat = {}
	mat.lighted = false
	local pt = tex("progress_transition.tga", texPannerLinear(1, 0));
	set_wrap_u(pt, WrapMode.Clamp)	
	set_wrap_v(pt, WrapMode.Clamp)	
	mat.diffuse = lerp(tex("load_progress_0.tga"), 
					   tex("load_progress_1.tga"),
					   pt
	                  )
	return mat
end

------------------
-- demo scene 7 --
------------------




function lightBulb()
	mat = {}
	mat.lighted = false
	mat.double_sided = true
	mat.transparent = true
	mat.diffuse = tex("light_bulb.tga");	
	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.Zero
	return mat
end

function lightHalo()
	mat = {}
	mat.lighted = false
	mat.double_sided = true
	mat.transparent = true
	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.One
 	local grad= tex("generic_gradient.tga")
 	set_wrap_u(grad, WrapMode.Clamp)
	set_wrap_v(grad, WrapMode.Clamp)	
	local noise = add(tex("noise.tga", texPannerLinear(0.2, 0)),
					  tex("noise.tga", texPannerLinear(-0.08156, 0)))	
	mat.diffuse = mul(color(0.08, 0.08, 0.005), grad)
	mat.diffuse = mul(mat.diffuse, noise)
	return mat
end

function lightMetal()
	mat = {}
	mat.lighted = displayLighted
	mat.diffuse = mul(vertex_color0(), mul(color(0.6, 0.6, 0.8), tex("brushed2.tga"))) 
	mat.ambient = color(0.2, 0.15, 0.15)
	if getPixelShaderModel() ~= "ps_1_4" then			
		mat.specular_on = true
		mat.specularity = computeSpecularity(0.8)	
		mat.normal = tex_normal("simple_heightmap.tga")
		mat.gloss = mul(vertex_color0(), mul(color(0.3, 0.3, 0.5), tex("brushed2.tga")))
	end
	return mat
end



function pipe()
	local mat = {}
	mat.lighted = displayLighted
	mat.specular_on = false
	mat.diffuse = color(0.5, 0.5, 0.8);
	mat.normal = tex_normal("pipe_hm.tga");
--	mat.normal = tex_normal("TilesHeight.TGA")
   mat.ambient = color(0.2, 0.2, 0.2)   	
   	mat.gloss = alpha(tex("pipe_hm.tga"))   	
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.7)
	return mat;
end

function pipe2()
	local mat = {}	
	--mat.specular_on = false
	if getPixelShaderModel() ~= "ps_1_4" then
		mat.diffuse = mul(vertex_color0(), tex("pipe2.tga"))
	else
		mat.diffuse = tex("pipe2.tga")
	end
	if getPixelShaderModel() ~= "ps_1_4" then
		mat.normal = tex_normal("pipe2_hm.tga");
	end
	--mat.ambient = color(0.2, 0.2, 0.2)
   	mat.gloss = mat.diffuse
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)
	mat.lighted = displayLighted
	return mat;
end


function metalPattern0()
	mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("metal_pattern0.tga")
	mat.normal = tex_normal("metal_pattern0_hm.tga")	
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.95)
	mat.gloss = mat.diffuse
	--mat.lighted = false -- tmp
	return mat
end

function metalPattern1()
	mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("metal_pattern1.tga")
	mat.normal = tex_normal("metal_pattern1_hm.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)
	mat.gloss = mat.diffuse
	--mat.lighted = false -- tmp
	return mat
end

function metalPattern2()
	mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("metal_pattern2.tga")
	mat.normal = tex_normal("metal_pattern2_hm.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)
	mat.gloss = mat.diffuse
	--mat.lighted = false -- tmp
	return mat
end

function metalPattern4()
	local mat = {};
	mat.lighted = displayLighted	
	mat.diffuse = tex("brushed2.tga") 
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)	
	--mat.aniso_tex = "aniso_lighting.tga"
	mat.gloss = color(0.2, 0.2, 0.3)
	mat.normal = tex_normal("pannel3_heightmap.tga")
	return mat
end

function metalPattern5()
	local mat = {};
	mat.lighted = displayLighted
	mat.diffuse = tex("metal_diffuse6.tga")
	mat.normal = tex_normal("metal_hm6.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.95)
	mat.gloss = color(0.1, 0.1, 0.15)	
	return mat
end

function metalPattern7()
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("metal_pattern7.tga")
	mat.normal = tex_normal("metal_pattern7_hm.tga")	
	--mat.diffuse = tex_normal("black_256.tga")	
	mat.specular_on = true
	--mat.specularity = 1
	mat.specularity = computeSpecularity(0.95)
	mat.gloss = alpha(mat.diffuse)
	return mat
end

function metalPattern8()
	mat={}
	mat.lighted = displayLighted	
	mat.diffuse = tex("metal_pattern8.tga")
	mat.normal = tex_normal("metal_pattern8_hm.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.97)
	mat.gloss = alpha(mat.diffuse)
	return mat
end

function metalPattern9()
	mat={}
	mat.lighted = displayLighted	
	mat.diffuse = tex("metal_pattern9.tga")
	mat.normal = tex_normal("metal_pattern9_hm.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.97)
	mat.gloss = alpha(mat.diffuse)
	return mat
end

function scrollChannel()
	local mat = {};
	mat.lighted = displayLighted
	mat.diffuse = tex("scroll_channel.tga") 
	--mat.specular_on = true
	--mat.specularity = 0.98	
	--mat.aniso_tex = "aniso_lighting.tga"
	--mat.gloss = color(0.2, 0.2, 0.3)
	mat.normal = tex_normal("scroll_channel_hm.tga")
	mat.emissive = mul(mul(color(1, 0.2, 0), tex("scroll_motif2.tga", texPannerLinear(-2.5, 0, 0))), alpha(tex("scroll_channel.tga")))
	return mat
end

function scrollChannel1Bis()
	local mat = {};
	mat.lighted = displayLighted
	mat.diffuse = tex("scroll_channel.tga") 
	--mat.specular_on = true
	--mat.specularity = 0.98	
	--mat.aniso_tex = "aniso_lighting.tga"
	--mat.gloss = color(0.2, 0.2, 0.3)
	mat.normal = tex_normal("scroll_channel_hm.tga")
	mat.emissive = mul(mul(color(0, 0.4, 0.2), tex("scroll_motif2.tga", texPannerLinear(-1.5, 0, 0))), alpha(tex("scroll_channel.tga")))
	return mat
end

function scrollChannel2()
	local mat = {};
	mat.lighted = displayLighted
	mat.diffuse = tex("scroll_channel.tga") 
	--mat.specular_on = true
	--mat.specularity = 0.98	
	--mat.aniso_tex = "aniso_lighting.tga"
	--mat.gloss = color(0.2, 0.2, 0.3)
	if getPixelShaderModel() ~= "ps_1_4" then	
		mat.normal = tex_normal("scroll_channel_hm.tga")
	end
	local motifr = mul(color(1, 0.2, 0), tex("scroll_motif2.tga", texPannerLinear(-2.5, 0, 0)))
	local motifl = tex("scroll_motif2.tga", texPannerLinear(2.5, 0, 0))
	if getPixelShaderModel() ~= "ps_1_4" then		
		mat.emissive = mul(mul(motifr, motifl), alpha(tex("scroll_channel.tga")))
	else
		mat.emissive = mul(motifr, alpha(tex("scroll_channel.tga")))
	end	
	return mat
end

function scrollChannel3()
	local mat = {};
	mat.lighted = displayLighted
	mat.diffuse = tex("scroll_channel.tga") 
	--mat.specular_on = true
	--mat.specularity = 0.98	
	--mat.aniso_tex = "aniso_lighting.tga"
	--mat.gloss = color(0.2, 0.2, 0.3)
	mat.normal = tex_normal("scroll_channel_hm.tga")
	local noiseScale = 0.8
	local tc0 = tex_dudv("noise.tga", texPannerLinearScaled(-0.1, 0.12, 0.1, 0.1, 0))	
	tc0 =add(tex_coord(0), mul(color(noiseScale, 3 * noiseScale, noiseScale), tc0))	
	local motif 
	if getPixelShaderModel() ~= "ps_1_4" then	
		local tc1 = tex_dudv("noise.tga", texPannerLinearScaled(0.09, 0.13524, 0.1, 0.1, 0))	
		tc1 =add(tex_coord(0), mul(color(noiseScale, 3 * noiseScale, noiseScale), tc1))
		motif = add(tex("filament.tga", tc0), tex("filament.tga", tc1))		
	else		
		motif = tex("filament.tga", tc0)
		--motif = tex("filament.tga", texPannerLinear(2.5, 0.5))
	end
	motif= mul(motif, color(pulseColor(CRGBA(0, 50, 190, 255), CRGBA(10, 124, 250, 255), 3)))		
	mat.emissive = mul(motif, alpha(tex("scroll_channel.tga")))
	return mat
end



function lightBulb2()
	mat = {}
	local detailScale = 1.23434
	mat.diffuse = mul(tex("detail1.tga", texScalerUV(4 * detailScale, detailScale, 0)), tex("light_bulb2.tga"))	
	mat.lighted = false
	return mat
end

function tubeGlass()
	mat = {}
	mat.diffuse = mul(color(0.0, 0.2, 0.2), tex_cube("ss_cockpit.jpg", envMapReflect()))
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.One	
 	return mat
end

function tubeMetal()
	mat = {}
	mat.lighted = displayLighted
	mat.diffuse = mul(color(0.5, 0.5, 0.5), mul(tex("brushed2.tga"), vertex_color0()))
	mat.specular_on = true
	mat.specularity = 0.8
	mat.gloss = tex("brushed2.tga")
 	return mat
end

function lightContainer()
	mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("light_container.tga")
	mat.normal = tex_normal("light_container_hm.tga")
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.95)
	mat.gloss = mat.diffuse
 	return mat
end

function lightContainerBulb()
	mat = {}
	mat.diffuse = tex("light_container.tga")
	mat.lighted = false
	mat.transparent = true
	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.Zero	
 	return mat
end

function barrelText()
	mat = {}
	mat.lighted = false
	mat.diffuse = mul(color(0.3, 1.0, 1.00), tex("barrel_text_small.tga", texPannerLinear(0, -0.3)))
	return mat
end

function barrel()
	mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("barrel.tga")
	mat.specular_on = true
	mat.specularity = 0.8
	mat.gloss = mat.diffuse
	return mat
end

function pannel()
	local mat = {}
	mat.lighted = displayLighted	
	mat.normal = tex_normal("pannel_hm.tga") 
	mat.diffuse = tex("pannel.tga") 
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.98)
	mat.gloss = mat.diffuse
	--mat.normal = tex_normal("pannel2_heightmap.tga")
	return mat
end

function simpleBrushed()
	local mat = {};
	mat.lighted = displayLighted
	mat.diffuse = mul(color(0.5, 0.5, 0.5), tex("brushed2.tga"))
	--mat.specular_on = true
	mat.specularity = 0.98
	mat.gloss = tex("brushed2.tga")	
	return mat
end

function waterDropMetal()
	local mat = {};
	mat.lighted = displayLighted
	mat.diffuse = tex("metal_water_drop_128.tga")
	mat.specular_on = true
	mat.specularity = 0.92
	mat.gloss = alpha(mat.diffuse)
	return mat
end

function vertexColorPlastic()
	local mat = {};
	mat.lighted = displayLighted
	mat.diffuse = vertex_color0()
	mat.specular_on = true
	mat.specularity = 0.90	
	return mat
end

function sunLight()
	local mat={}
	 mat.attenuation_on = true
   --   mat.attenuation_on = false
	mat.color_on = false
    mat.shader = color(1.5, 1.5, 1.5)
	return mat
end


function glass()
	local mat={}
	mat.diffuse = separate_alpha(color(1, 1, 1), red(tex("glass.tga")))	
	mat.transparent = true
	mat.src_blend = Blend.SrcAlpha
 	mat.dest_blend = Blend.InvSrcAlpha
	return mat
end

function stucco()
	local mat={}
	mat.diffuse = mul(vertex_color0(), color(247/255, 241/255, 175/255))
	mat.normal = tex_normal("stucco_hm.tga")
	mat.lighted = displayLighted
	return mat
end

function stucco2()
	local mat={}
	mat.diffuse = color(247/255, 241/255, 175/255)
	mat.normal = tex_normal("stucco_hm.tga")
	mat.lighted = displayLighted
	return mat
end

-- alpha blended fog based
function whiteFog()
	local mat = {}
	local texDiffuse = tex("grad2.tga")
	mat.lighted = false
	mat.transparent = true
	mat.diffuse = separate_alpha(color(1, 1, 1), red(texDiffuse))
	set_wrap_u(texDiffuse, WrapMode.Clamp)
	mat.src_blend = Blend.SrcAlpha
 	mat.dest_blend = Blend.InvSrcAlpha
	return mat
end

-- //////////////////
-- // DEMO SCENE 8 //
-- //////////////////

function lightningBase(speed0, speed1)
	local ampl = 0.9
	local scaleW = 0.05
	local scaleH = 0.35
	local speed = speed0
	local dudv = tex_dudv_normalized("noise_smooth.tga", texPannerLinearScaled(speed * 0.143, speed * 0.1612, scaleW, scaleH, 0))
	local dudv = add(tex_coord(texPannerLinear(0, speed1)), mul(color(ampl, ampl, ampl), dudv))	
	local result = tex("lightning.tga", dudv)
	set_wrap_u(result, WrapMode.Clamp)
	return result
end

function lightning()
	local mat = {}			
	mat.diffuse = lightningBase(0.2, 0.2)
	mat.diffuse = add(mat.diffuse, lightningBase(-0.324, -0.1234))
	--mat.diffuse = add(mat.diffuse, lightningBase(0.364, -0.1739))
	mat.diffuse = mul(mat.diffuse, vertex_color0())
	mat.diffuse = mul(mat.diffuse, tex("lightning_flash.tga", texPannerLinear(0.06, 0, 1)))
	mat.transparent = true
	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.One
 	mat.double_sided = true
	return mat
end

function neutron()
	local mat = {}
	mat.lighted = false
	mat.diffuse = max(tex("cellular.tga", texPannerLinear(0.1, 0.21, 0)), 
							tex("cellular.tga", texPannerLinear(-0.147, -0.123, 0)))
	mat.diffuse = max(mat.diffuse,
							tex("cellular.tga", texPannerLinear(-0.127, 0.082, 0)))
	mat.diffuse = max(mat.diffuse,
							tex("cellular.tga", texPannerLinear(0.0953, -0.105, 0)))
	mat.diffuse = mul(mat.diffuse, color(0.4, 0.1, 0.1))
	mat.diffuse = add(mat.diffuse, mul(color(252 / 255, 177 / 255, 90 / 255), fresnel(1, 0.85)))
		
	return mat
end

-- //////////////////
-- // DEMO SCENE 9 //
-- //////////////////

function energySphere()
	local mat = {}
	mat.lighted = false
	mat.double_sided = false	
	local uv = tex_dudv("noise.tga", texPannerLinearScaled(0.1, 0.000, 0.2, 0.2, 0))
	uv = add(tex_coord(0), mul(uv, color(0.4, 0.4)))
	mat.diffuse = tex("blue_plasma.tga", uv)	
	return mat
end

function energySphereLight()
	local mat = {}
	mat.attenuation_on = true
	mat.color_on = false
	mat.shader = add(tex_cube("caustic.tga", lightSpinnerY(0.2)),
                    tex_cube("caustic.tga", lightSpinnerY(-0.15)))
                    
	--local blendColor = blender(CRGBA(255, 255, 255, 255), CRGBA(0, 0, 0, 255), 1)
	mat.shader = mul(color(83/255, 218/255, 198/255), mat.shader)
	--mat.shader = lerp(mat.shader, color(1, 1, 1), color(blendColor))
	return mat
end

function unlitWhite()
	local mat= {}
	mat.lighted= false
	mat.diffuse = color(1, 1, 1)
	return mat
end

-----------------------
-- scene 9 character --
-----------------------

function charSkin()
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = color(253/255, 170/255, 127/255)
	return mat
end

function charLeather()
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = color(100/255, 100/255, 100/255)
	mat.specular_on = true
	mat.specularity = 0.90
	mat.gloss = color(0.5, 0.5, 0.5)
	return mat
end

function charLeatherBump()
	local mat = {}
	mat.lighted = displayLighted	
	mat.diffuse = color(170/255, 170/255, 170/255)
	mat.specular_on = true
	mat.specularity = computeSpecularity(0.90)
	mat.normal = tex_normal("leather_hm.tga")
	mat.gloss = color(0.5, 0.5, 0.5)
	return mat
end

-- transparent skin (for debug)
function charTranspaSkin()
	local mat = {}
	mat.transparent = true;
 	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.One
 	mat.diffuse = tex("damier.tga");
	return mat
end

function debugSpotMat()
	local mat = {}	
	mat.lighted = displayLighted
	mat.diffuse = tex("metal_pattern7.tga")
	--mat.normal = tex_normal("metal_pattern7_hm.tga")	
	--mat.diffuse = tex_normal("black_256.tga")	
	--mat.specular_on = true
	--mat.specularity = 1
	--mat.specularity = 0.95
	--mat.gloss = alpha(mat.diffuse)
	return mat
end


--------------
-- scene 10 --
--------------

function metalPatternMirror()
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("metal_pattern7.tga")
	mat.normal = tex_normal("metal_pattern7_hm.tga")	
	--mat.diffuse = tex_normal("black_256.tga")	
	mat.specular_on = true
	--mat.specularity = 1
	mat.specularity = 0.95
	mat.gloss = alpha(mat.diffuse)
	
	mat.reflection_on = true
	mat.reflection_opacity = 0.7
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	--mat.reflection_glossmap = mat.diffuse
	mat.reflection_bump_factor = 0.05
	
	return mat
end


function hotSphere()
	local mat = {}
	mat.lighted = false
	mat.double_sided = false	
	local uv = tex_dudv("noise.tga", texPannerLinearScaled(0.1, 0.000, 0.2, 0.2, 0))
	uv = add(tex_coord(0), mul(uv, color(0.4, 0.4)))
	mat.diffuse = tex("red_plasma.tga", uv)	
	return mat
end

function hotSphereLight()
	local mat = {}
	mat.attenuation_on = true
	mat.color_on = false
	mat.shader = add(tex_cube("caustic.tga", lightSpinnerY(0.8)),
                    tex_cube("caustic.tga", lightSpinnerY(-0.6)))
                    
	--local blendColor = blender(CRGBA(255, 255, 255, 255), CRGBA(0, 0, 0, 255), 1)
	mat.shader = mul(color(255/255, 16/255, 0/255), mat.shader)
	--mat.shader = lerp(mat.shader, color(1, 1, 1), color(blendColor))
	return mat
end

------------
-- GREETZ --
------------

function greetz(emissiveFile)
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = mul(color(0.2, 0.2, 0.2), tex("metal_pattern7.tga"))
	mat.normal = tex_normal("metal_pattern7_hm.tga")	
	--mat.diffuse = tex_normal("black_256.tga")	
	if getPixelShaderModel() ~= "ps_1_4" then	
		mat.specular_on = true
	end
	--mat.specularity = 1
	mat.specularity = computeSpecularity(0.95)
	if getPixelShaderModel() ~= "ps_1_4" then	
		local uv = tex_dudv("noise.tga", texPannerLinearScaled(0.1, 0.000, 0.2, 0.2, 0))
		uv = add(tex_coord(0), mul(uv, color(0.2, 0.2)))
		mat.emissive = add(tex(emissiveFile), mul(color(0.8, 0.8, 0.8), tex(emissiveFile, uv)))
	else
		mat.emissive = tex(emissiveFile)	
	end
	mat.emissive = mul(color(1, 0.3, 0.1), mat.emissive)
	mat.gloss = color(0.3, 0.3, 0.3)
	return mat
end

function greetz0()
	return greetz("black_256.tga")
end

function greetz1()
	return greetz("greetz1.tga")
end

function greetz2()
	return greetz("greetz2.tga")
end

function greetz3()
	return greetz("greetz3.tga")
end

function greetz4()
	return greetz("greetz4.tga")
end

function greetz5()
	return greetz("greetz5.tga")
end

function greetz6()
	return greetz("greetz6.tga")
end

function greetz7()
	return greetz("greetz7.tga")
end



--------------
-- scene 11 --
--------------

function gold()
	local mat = {}
	mat.reflection_on = true
	mat.diffuse = color(0.8, 0.5, 0.1)
	mat.normal = tex_normal("metal_pattern7_hm.tga")	
	--mat.diffuse = tex_normal("black_256.tga")	
	mat.specular_on = true
	mat.gloss = color(0.4, 0.2, 0.05)
	--mat.specularity = 1
	mat.specularity = computeSpecularity(0.95)
	mat.reflection_glossmode = ReflRefrBlend.Modulate
	mat.gloss_map = mat.diffuse
	return mat
end

function alienFlesh()
	local mat = {}
	mat.reflection_on = true
	mat.diffuse = color(0.2, 0.2, 0.4)
	mat.gloss = mat.diffuse
	mat.normal = tex_normal("alien_flesh_hm.tga")	
	mat.aniso_tex = "aniso_lighting.tga"	
	return mat
end


function glassSphere()
 	local mat = {};
	mat.lighted = displayLighted;
	--mat.diffuse = tex("damier.tga")
	mat.diffuse = color(0.2, 0.2, 0.2, 1)
	mat.normal = tex_normal("noise.tga")
    --mat.ambient = color(0.2, 0.2, 0.2)
	--mat.specular_on = true
	--mat.specularity = computeSpecularity(0.95)
 	mat.refraction_on = true
	mat.refraction_opacity = 0.7
	mat.refraction_glossmode = ReflRefrBlend.AdditifAlpha
	mat.refraction_ior = 0.7 -- zoom out	
	mat.gloss_map = nil
	
	mat.chromatic_abberation = 0.03
	mat.refraction_glossmap = mat.diffuse	
	mat.refraction_opacity = 0.5
 	mat.reflection_on = true
	mat.reflection_opacity = 0.5
	mat.reflection_glossmode = ReflRefrBlend.AlphaBlend
	mat.reflection_glossmap = mat.diffuse
	mat.reflection_bump_factor = 0.5
	return mat
end

function erodedMetal()
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("eroded_metal.tga")
	mat.specular_on = true
	mat.specularity = 0.8
	--mat.gloss = mul(alpha(mat.diffuse), mat.diffuse)
	mat.gloss = alpha(mat.diffuse)
	return mat
end

function erodedMetal2()
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("eroded_metal_2.tga")
	mat.specular_on = true
	mat.specularity = 0.8
	--mat.gloss = mul(alpha(mat.diffuse), mat.diffuse)
	mat.gloss = alpha(mat.diffuse)
	return mat
end



function crystalOff()
	local mat = {}
	mat.lighted = false
	mat.diffuse = color(0.2, 0.1, 0)
	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.Zero
	return mat
end

function crystalOn()
	local mat = {}
	mat.lighted = false	
	mat.transparent = true
	mat.diffuse = color(0.9, 0.5, 0.3)	
	mat.src_blend = Blend.One
 	mat.dest_blend = Blend.Zero
	return mat
end


function alphaTest()
	local mat = {}
	mat.lighted = displayLighted
	--mat.double_sided = true
	mat.diffuse = tex("alpha_test.tga")
	mat.alpha_test = true
	mat.alpha_test_threshold = 128
	return mat
end

function alphaTest2()
	local mat = {}
	mat.lighted = displayLighted
	--mat.double_sided = true
	--mat.lighted = false
	mat.diffuse = tex("alpha_test2.tga")
	mat.alpha_test = true
	mat.alpha_test_threshold = 128
	return mat
end

function fence()
	local mat = {}
	mat.lighted = displayLighted
	mat.normal = tex_normal("fence_hm.tga")
	enable_mipmaps(mat.normal, false)
	mat.diffuse = tex("metal_water_drop_128.tga", uniformTexScaler(0.1, 0))
	mat.specular_on = true
	mat.specularity = 0.9	
	mat.gloss = alpha(mat.diffuse)
	mat.diffuse = separate_alpha(mat.diffuse, alpha(mat.normal))	
	mat.lightmap_alpha_test = tex("fence_hm.tga") -- special : simple texture wanted for lightmap occluders
	mat.alpha_test = true
	return mat
end

-- vertex color pipe
function vcPipe()	
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("metal_alpha.tga")
	mat.diffuse = separate_alpha(mul(vertex_color0(), mat. diffuse), alpha(mat.diffuse))
	mat.gloss = alpha(mat.diffuse)
	mat.specular_on = true
	mat.specularity = 0.5		
	return mat
end

function sphereMetal()	
	local mat = {}
	mat.lighted = displayLighted
	mat.diffuse = tex("sphere_metal.tga")
	mat.gloss = mul(mat.diffuse, alpha(mat.diffuse))
	mat.specular_on = true
	mat.specularity = 0.5
	return mat
end

function waterWall()
	local mat = {}
	mat.lighted = false;
	mat.diffuse = color(0.1, 0.1, 0.1)	
 	mat.refraction_on = true 	
 	mat.refraction_ior = 0.2
 	local baseDudv = add(tex_dudv("noise.tga", texPannerLinear(0.043, 0.0612, 0)),
    			  tex_dudv("noise.tga", texPannerLinear(-0.01567, 0.0567, 0))) 	    			  
   	dudv = mul(color(mat.refraction_ior, mat.refraction_ior, 0), baseDudv)
    local uv = add(dudv, screen_project_tex_coord(probe_projection_scale()))
	local refr = tex(tex_refract_2d(), uv)	
	refr = add(tex("rabbit_hole.tga", 1), refr)
	local envMap = tex("spec_envmap.tga", add(tex_coord(1), baseDudv))
	set_wrap_u(envMap, WrapMode.Clamp)
	set_wrap_v(envMap, WrapMode.Clamp)
	refr = add(envMap, refr)
	mat.custom_envmap = refr
	mat.custom_envmap_gloss_mode = ReflRefrBlend.AlphaBlend	
	return mat
end

function lightingScroll()
	local mat = {}
	mat.lighted = false
	mat.diffuse = tex("lighting_scroll.tga", texPannerLinear(2, 1, 0))
	mat.diffuse = add(mat.diffuse, tex("lighting_scroll.tga", texPannerLinear(-2, 1.2, 0)))
	mat.diffuse = add(mat.diffuse, tex("lighting_scroll.tga", texPannerLinear(-1.5, 0.5, 0)))
	
	local motif = tex("intricate.tga", texScalerUV(1, 8, 0))
	motif = mul(motif, color(pulseColor(CRGBA(255, 255, 255, 255), CRGBA(127, 127, 127, 255), 2)))
	mat.diffuse = add(mat.diffuse,  motif)
	
	return mat
end

-----------------------------------
-- NORMAL MAPPED RADIOSITY TESTS --
-----------------------------------

--local radNormalMapped = true;
local radNormalMapped = false;
local radNM = "asteroid.TGA"
--local radNM = "blank.TGA"
local nrmDiffuse = 1.00
local nrmSpecular = 0.5



function nrmColor(r, g, b)
	local mat = {}	
	mat.lighted = displayLighted;
	mat.diffuse = color(nrmDiffuse * r, nrmDiffuse * g, nrmDiffuse * b) 
	if radNormalMapped then 
		mat.normal = tex_normal(radNM)
		set_compression_flag(mat.normal, false)
	end
	mat.specular_on = true
	mat.specularity = 0.98
	mat.gloss = color(nrmSpecular * r, nrmSpecular * g, nrmSpecular * b) 
	-- normal mapped radiosity demo
	if static_ambient_level_slider ~= nil then
		mat.precomputed_ambient_factor = static_ambient_level_slider()
		mat.indirect_lighting_factor = dynamic_ambient_level_slider()
		if radNormalMapped then
			--mat.normal = mul(mat.normal, bumpiness_slider())
			mat.normal = lerp(color(0, 0, 1), mat.normal, bumpiness_slider())
		end
		mat.direct_lighting_factor = direct_lighting_level_slider()
	end
	return mat
end

function nmrWhite()
	return nrmColor(0.9, 0.9, 0.9)
end

function nmrRed()
	return nrmColor(1, 0.05, 0.05)
end

function nmrGreen()
	return nrmColor(0.1, 0.9, 0.1)
end


function nmrYellow()
	return nrmColor(1, 0.25, 0.02)
end

function nmrYellow2()
	return nrmColor(0.9, 0.9, 0.1)
end

function nmrMagenta()
	return nrmColor(1, 0.2, 1)
end

function nmrCyan()
	return nrmColor(0.2, 1, 1)
end

function nmrBlack()
	return nrmColor(0, 0, 0)
end

function indirectLightSphere()
	return nrmColor(1, 1, 1)
	--local mat = {}
	--mat.diffuse = color(0, 0, 0)
	--mat.gloss = color(0, 0, 0)
	--mat.normal = tex_normal(radNM)
	--return mat
end


function indirectLightVoxelMat()
	local mat = {}
	mat.lighted = false
	mat.diffuse = vertex_color0()
	mat.double_sided = true
	return mat
end


function debugLM()
	local mat = {}
	mat.lighted = false
	mat.diffuse = add(tex_coord(1), color(0.2, 0.2, 0.2))
	--mat.diffuse = color(1, 0, 0)
	return mat
end

-------------------------
-- misc
function unitBoxMat()
	local mat = {}
	mat.lighted = false
	mat.diffuse = tex("box_outline.tga")
	return mat
end

function unlitRed()
	local mat = {}
	mat.lighted = false
	mat.diffuse = color(1, 0, 0)
	return mat
end

function unlitGreen()
	local mat = {}
	mat.lighted = false
	mat.diffuse = color(0, 1, 0)
	return mat
end

------------------------------
-- VOLUMETRIC LIGHTING DEMO --
------------------------------
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 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


------------------------------------------------------
-- normal mapped ssdo materials

local ssdoAmbient = color(0.25, 0.25, 0.25)

function walls()
	local mat = {}
	mat.normal = tex_normal("stone_floor.tga")	
	set_compression_flag(mat.normal, false)
	mat.diffuse = vertex_color0() --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 = ssdoAmbient
	return mat
end

function lucy()
	local mat = {}
	mat.normal = biasx2(tex("lucylowresnormalsmap.tga"))
	--mat.normal = color(0, 0, 1)
	mat.diffuse = color(0.6, 0.6, 0.6)
	mat.gloss = color(0.6, 0.6, 0.6)
	mat.specular_on = true
	mat.specularity = 0.99
	mat.ambient = ssdoAmbient
	set_compression_flag(mat.normal, false)
	return mat
end

------------------------
--   ENGINE TECH DEMO --
------------------------
function dustyFloor()
	local mat = {}
	mat.diffuse = lerp(tex("damier.TGA"), tex("metal_alpha.tga"), alpha(vertex_color0()))
	return mat;
end

function scrollingArrow()
	local mat = {}
	mat.diffuse = tex("alveole.tga", texScalerUV(6, 3, 0));
	local emi = mul(tex("alveole_arrow.tga", texPannerLinear(-0.6, 0)), 
                    tex("alveole_mask.tga", texPannerLinear(0.25, 0)))
	emi = mul(emi, complement(alpha(mat.diffuse)))
	mat.emissive = mul(emi, color(1, 0.9, 0.5));
	mat.emissive = mul(mat.emissive, color(pulseColor(CRGBA(122, 122, 102, 255), CRGBA(255, 255, 255, 255), 3)))
	return mat;
end


------------------------
--   GLSL CONVERSION  --
------------------------
function glsl1()
	local mat = {}
	mat.lighted = false
	mat.diffuse = color(1, 0.5, 0)
	mat.double_sided = true
	return mat
end

function glsl2()
	local mat = {}
	mat.lighted = false
	mat.diffuse = color(0, 0.5, 1)
	mat.double_sided = true
	return mat
end

function glsl3()
	local mat = {}
	mat.lighted = false
	mat.diffuse = tex("damier.tga")
	mat.double_sided = true
	return mat
end

function glsl5()
	local mat = {}
	mat.lighted = false
--	mat.diffuse = mul(vertex_color0(), tex("damier.tga", texPannerLinear(0.5, 0)))

	mat.diffuse = lerp(tex("damier.tga"), tex("metal_pattern0.tga"), vertex_color0())

	mat.double_sided = true
	return mat
end


function glsl4()
	local mat = metalPattern9()
	return mat;
	--return simpleWhite()
end




