Sign in to follow this  
CodaKiller

Fixed function materials vs Shaders for a game engine.

Recommended Posts

OK right now my engine uses shaders but it makes the script for the engine kinda sloppy and the rules for this one engine class have to be different from every other class, for instance:
	GameMaterial = material( "Data/Shaders/Default.fx" )
	GameMaterial:set_texture( "txDiffuse", "Data/Textures/Female.dds" )
	GameMaterial:set_texture( "txEnvironment", "Data/Textures/rnl_cross_mip.dds" )
	GameMaterial:set_variable( "Environment_map_intensity", 1, {0.005} )
	GameMaterial:set_variable( "lightPos", 4, {-5.0, 0.0, 0.0, 0.0} )
	GameMaterial:set_variable( "Diffuse", 4, {3.0, 3.0, 3.0, 1.0} )
	Object:set_material_slot( 0, GameMaterial )







This type of scripting is not the same as the clean easy to use classes that have default values which I have for other things, such as this:
	GameWindow = window()
	GameWindow.title = "Ether"
	GameWindow.icon = "Ether.ico"
	GameWindow.width = 1024
	GameWindow.height = 768
	GameWindow.maximize = false
	GameWindow.size = false







I have to set the light position for each object and I have to set the variables for the shader but if I used fixed function materials I could just add lights to the scene and make a clean simple to use class which could be just as advanced as any shader but at the cost of versatility. For instance to use a fixed function material would look something like this:
	GameMaterial = material()
	GameMaterial.diffuse = {3.0, 3.0, 3.0, 1.0}
	GameMaterial.texture = "Data/Textures/Female.dds"
	GameMaterial.environment_map = "Data/Textures/rnl_cross_mip.dds"
	GameMaterial.reflection_intensity = 0.005
	Object:set_material_slot( 0, Material )







If you were using a game engine which method would you prefer, versatility vs ease of use?

Share this post


Link to post
Share on other sites
Quote:
Original post by Matt_D
using FFP, actually uses shaders behind the scenes (on shader capable cards). ;)
may as well just bite the bullet and provide the flexibility people expect


What is FFP and how does it handle lighting?

I have one idea that would work, I could use semantics for some of the variables which the engine needs direct control of like lighting. That is how I remember 3DS Max interfaced with the shaders so I believe that would provide the most flexibility and ease of use over my other options.

What do you think?

Share this post


Link to post
Share on other sites
FFP = fixed function pipeline.
I wouldn't suggest going with the FFP because API's are starting to veer away from it. DX3D10+ no longer supports FFP (maybe emulated? Not sure).

Share this post


Link to post
Share on other sites
Quote:
Original post by CodaKiller
What is FFP and how does it handle lighting?

Fixed Function Pipeline. And it's pretty much dead as far as games are concerned, so going with shaders seem to be really the only choice unless you are making something exceedingly simple.

You can't make a fixed-function path as advanced as any shader. If you can, then your shaders aren't doing anything interesting. Just encapsulate everything shader-related, then it won't look sloppy on the outside.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jroggy
FFP = fixed function pipeline.
I wouldn't suggest going with the FFP because API's are starting to veer away from it. DX3D10+ no longer supports FFP (maybe emulated? Not sure).


I am using DirectX 10 and I was not talking about using the FFP I was talking about using a shader and only exposing key features to the engine.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sik_the_hedgehog
Lemme understand it: you're asking if you should use a single shader for all materials (and use the shader variables for parameters) or a separate shader for each material, right?


Yeah exactly.

Share this post


Link to post
Share on other sites
Wouldn't different materials potentially require very different data streams? It could be difficult to write a one size fits all shader. The best approach to shaders & materials I've seen so far came from Yann L in this thread. Even though the thread is old, there are some cool ideas in there.

Share this post


Link to post
Share on other sites
Seems like you are using Lua, in this case you can use the __newindex meta method to add some syntactic sugar.
Example:

function Material()
mat = {}
-- init mat
-- ...

-- create a metatable
mat.mt = {}
mat.mt.__newindex =
function (t,key,value)
-- use the prefix to find out what function to call
if (string.sub(key,1,2)=="tx") then
print("set_texture(",key,",",value,")")
-- set_texture(key,value)
end
-- and so on
-- ...
end

setmetatable(mat, mat.mt)
return mat;
end



now you can write something like:

material = Material()
material.txDiffuse = "Data/Textures/Female.dds"


the second line will call the __newindex meta method, you can use prefixes or type checks to figure out what method should be called then.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kambiz
Seems like you are using Lua, in this case you can use the __newindex meta method to add some syntactic sugar.
Example:
*** Source Snippet Removed ***

now you can write something like:

material = Material()
material.txDiffuse = "Data/Textures/Female.dds"


the second line will call the __newindex meta method, you can use prefixes or type checks to figure out what method should be called then.


I'm using lua and luabind. Something like that which registers the variables automatically would be awesome and I could even make it smart so that it knows the type of the variable!

I know how to do it, all I would need to do is get the variable using it's index and then I would check what type it was but the problem is that it would register ALL variables in the shader. How could I search for only variables which are in the main body of the shader and not inside functions and stuff?

Share this post


Link to post
Share on other sites
Quote:
Original post by CodaKiller
Quote:
Original post by Sik_the_hedgehog
Lemme understand it: you're asking if you should use a single shader for all materials (and use the shader variables for parameters) or a separate shader for each material, right?


Yeah exactly.


oh right. sorry.
my bad, i see fixed function and go all cross-eyed.

ive mostly seen people have a set of materials using a single shader (eg: all lighting done through one shader, postprocessing another). but having a number of seperate shaders depending on usage.

I cant provide more information than that unfortunately. having a massive ubershader probrably isnt such a good idea unless you have a very low number of actual materials.



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this