[updated] An Ogre shader collection ?

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
User avatar
jmonw
Goblin
Posts: 232
Joined: Sat Apr 23, 2005 3:21 am
Location: Atlanta, GA

Post by jmonw »

I think this was a fantastic addition to the Wiki. It's very helpful for starting with shaders and, I imagine, for getting pluggable shaders and new ideas/implementations later on.

Great initiative. Thanks!
User avatar
guilderstein
Halfling
Posts: 66
Joined: Wed Jan 04, 2006 11:48 am
Location: Hungary, Budapest

Post by guilderstein »

Hi!
I do apologize if I had done the perpixel WIKI very dirty, but had really no time then.
I got those .cg and .material files right out my work dir, so compile errors must be with syntax (spaces, {}s, typos).

Tomorrow, I will update it, add comments and simplify where appropritate.
Also modif the pics.

Have fun :)[/code]
Yonaz
Gnoblar
Posts: 9
Joined: Sun Jun 11, 2006 9:05 pm
Location: Germany
Contact:

Post by Yonaz »

Hey guilderstein,

great work so far. But i think it would be good, if you seperated your two shaders in two different subsections for the sake of some more overview (that wiki-entry is quite longish with all the pics an the code).

Keep it up: :)

Greets
User avatar
Wretched_Wyx
Orc
Posts: 498
Joined: Thu Mar 16, 2006 5:27 pm
Contact:

Post by Wretched_Wyx »

I agree with Yonaz. Glad to hear you are planning on keeping your contributions clean! If I had more knowledge (of many, many things) I would contribute more myself. I have submitted one thing though- a new smoke particle effect (on the wiki). I was also the one who created the sections for Per-Pixel Lighting, and Hemispherical Lighting on the wiki, hoping someone would come along and fill those up. And I I do go through and fix spelling and grammatic errors now and again... So I guess you don't necessarily have to know a whole lot to contribute.

Thanks again guilderstein!
User avatar
guilderstein
Halfling
Posts: 66
Joined: Wed Jan 04, 2006 11:48 am
Location: Hungary, Budapest

Post by guilderstein »

Hello people!

WIKI got a lot better :)

Separated things to:
- basic perpixel (no tex, but ambient, diffuse, and specular, and can be multilight with multiple passes)
- more sophisticated perpixel (more FPS due to lesser precision, 3 lights in 1 pass, plus some theory)
- offset (same as above)

AND

- my best thing ever is coming: a bit complex, a bit ugly, but easily tuneable all-in-one shader
(you can decide whether you want textures, just normal-mapping, without offset, or just lighting without normal-mapping, no specular, etc.
all just commenting out/in 1 or 2 lines before the shader code!
- done with preprocessor directives
- can be used to generate shaders on the fly in ogre, just a file access,
and delete/add a "//" in the beginning few lines of the file

I just think this will :twisted: when I get it working 100% :)
User avatar
Wretched_Wyx
Orc
Posts: 498
Joined: Thu Mar 16, 2006 5:27 pm
Contact:

Post by Wretched_Wyx »

Cool, good job. Looks better seperated into those categories. Could still use a little bit of cleaning up to be *perfect*, but definitely nice. The only reason I nit-pick, is because... Well, it's per-pixel lighting shaders! I've been waiting so long (and I'm sure others have been too) for this, so having those pages cleaned up, very neat, and super-functional would be sweet.

Thanks again!
User avatar
IFASS
Gnome
Posts: 387
Joined: Tue Jan 04, 2005 12:07 pm
Location: Netherlands

Post by IFASS »

Sounds great guilderstein, a all-in-one shader would be great, is it Cg?
Image
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

Have any of you checked out the MapEditor Progress Update topic in the PLSM2 Forum recently? :)

Falagard has been asking a whole lot of questions about shaders lately ... :wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
Wretched_Wyx
Orc
Posts: 498
Joined: Thu Mar 16, 2006 5:27 pm
Contact:

Post by Wretched_Wyx »

I'm not using PLSM yet, but am very interested in it and Falagard's MapEditor. Checking that out now...

Couple questions guilderstein- Light attenuation. That would really bring those shaders together nicely. I was driving myself mad tring to get that down. I ended up having to move my light closer/farther from an object to fake the appearance of attenuation, and using Ogre's built in setAttenuation had no effect. Also, I get ugly framerates with this. Like in a small basic scene (see below) my framerates are averaging 15-20. Any reason why? This doesn't seem to be that hungry of a shader, I'm running a GeForce FX 5600 256mb GPU and have had way higher FPS with more complex scenes and shaders.

Image

The (point) light in this scene is only 16 Ogre units above the ground plane. If I change the material to a normal one, the attenuation looks very nice. It'd be nice to have the best of both worlds :wink:.
User avatar
guilderstein
Halfling
Posts: 66
Joined: Wed Jan 04, 2006 11:48 am
Location: Hungary, Budapest

Post by guilderstein »

Hi!

@Wretched_Wyx:

I believe I can only make it better if you spot out some parts. More
comments? I can't think of anything :)
Anyway, does it work? It shall. Methinks the easiest is to recompile the
dot3bump demo with a material name changed to this in the first lines.

@all:

I have the bad habit of posting here I'm done with the WIKI, when I'm in the
middle of writing it :)

Thx for encouraging :)
User avatar
guilderstein
Halfling
Posts: 66
Joined: Wed Jan 04, 2006 11:48 am
Location: Hungary, Budapest

Post by guilderstein »

Well, atten. is really nothing, just pass in a uniform in frag shader of perpixel
named light_attenuation_blah_blah 0 from Ogre, and the float4 will
have x,y,z,w as distance,const,linear,quadratic atten. (at least I think so).
Now in final color line, multiply with that float4 appropriately.
I think there are plenty of stuff out there on this.
For.ex. check lighthouse3d for the proper color equation.
I can only update wiki with that tomorrow.
Hopefully the tuneable monster will work then too.

Shader rock.
User avatar
guilderstein
Halfling
Posts: 66
Joined: Wed Jan 04, 2006 11:48 am
Location: Hungary, Budapest

Post by guilderstein »

Quite off topic :)

@IFASS:

If ya happen to be the guy at OpenFRAG, I'm thinking heavily about joining ya team part-time :). Got some ideas, etc. Your castle sceneloader changed something in me :)
User avatar
Wretched_Wyx
Orc
Posts: 498
Joined: Thu Mar 16, 2006 5:27 pm
Contact:

Post by Wretched_Wyx »

It's just aestetics really- for example, the screenshots make the wiki page stretch, and some of the text follows it off the page as well. This can be solved by taking screenshots in a lower resolution. Of course if you're in a higher resolution than 1024x768 when viewing the wiki article, you probably wouldn't notice. Hmm, I think that was it really. There are some basic grammatical and spelling errors which I would fix on my own, but I'll wait until you're all done. I'm guessing that's only because of language barriers though :wink:.

I'm not trying to be a jerk or anything, just trying to help make this new very welcomed addition the best it can be :).

[edit]
I tried to get attenuation working, to no avail... Here's some snippets from the various files:

perpixelLighting.cg:

Code: Select all

void PerPixelLighting_FP(
	float3 normal		: TEXCOORD0, 
        float4 LightPos		: TEXCOORD1,
	float4 position 	: TEXCOORD2,

	uniform float3 		eyePosition, 
	uniform float4 		lightDiffuse,
	uniform float4 		lightSpecular,
	uniform float 		lightExponent,
	uniform float4 		lightAmbient,
	uniform float4		lightAttenuation,

        out float4 Color 	: COLOR 
) 
{
	float3 N 		= normalize(normal);
	float3 EyeDir 		= normalize(eyePosition - position.xyz);
	float3 LightDir 	= normalize(LightPos.xyz - (position * LightPos.w));
	float3 HalfAngle 	= normalize(LightDir + EyeDir);
	float NdotL 		= dot(LightDir, N);
	float NdotH 		= dot(HalfAngle, N);
	float4 Lit 		= lit(NdotL,NdotH,lightExponent);
	
	Color 		= ((lightDiffuse * Lit.y) + (lightSpecular * Lit.z)) + lightAmbient * lightAttenuation;
}
shader.perpixelLighting.material:

Code: Select all

fragment_program PerPixelLighting_FP cg 
{ 
       source perpixel.lighting.cg

       default_params 
       {
		param_named_auto 	eyePosition camera_position_object_space
              	param_named_auto 	lightDiffuse light_diffuse_colour 0
		param_named_auto 	lightSpecular light_specular_colour 0
		param_named_auto	lightAttenuation light_attenuation 0
		param_named 		lightExponent float 8
       }  

       entry_point PerPixelLighting_FP 
       profiles ps_2_0 arbfp1 
}
And my light setup in Ogre:

Code: Select all

		createPointLight(
			"PointLightBlue",			// Name
			false,						// Shadows false/true
			Vector3(0,32,0),			// Position
			(ColourValue(230,230,255)),	// Diffuse Color
			(ColourValue(0,0,0)),		// Specular Color
			32,							// Attenuation Range
			128,						// Attenuation Constant
			32,							// Attenuation Linear
			32							// Attenuation Quadratic
			);
What am I missing?
Last edited by Wretched_Wyx on Tue Jun 13, 2006 2:06 pm, edited 1 time in total.
User avatar
IFASS
Gnome
Posts: 387
Joined: Tue Jan 04, 2005 12:07 pm
Location: Netherlands

Post by IFASS »

guilderstein wrote:Quite off topic :)

@IFASS:

If ya happen to be the guy at OpenFRAG, I'm thinking heavily about joining ya team part-time :). Got some ideas, etc. Your castle sceneloader changed something in me :)
If you like, just step by at our IRC channel, we're there :wink:
irc.openfrag.org #openfrag

Great work you're doing here! 8)
Image
User avatar
guilderstein
Halfling
Posts: 66
Joined: Wed Jan 04, 2006 11:48 am
Location: Hungary, Budapest

Post by guilderstein »

Hi!

@Wretched_Wyx:
Why are you multiplying ambient(and only amb)? Atten. supposed to fade diffuse and specular.
And I don't see any attenuation factor calculating like 1/(kc +kl*d+kq*d*d).
kc,kl,kq are parts of float4 atten. uniform, check ogre manual.
Multiply with the 1/... the diffuse part at least in color.
That should do.

@IFASS:
No IRC, no net at home, just school/work :)
But I wrote into openfrag forum some thoughts.

I'm leaving for today.
User avatar
Wretched_Wyx
Orc
Posts: 498
Joined: Thu Mar 16, 2006 5:27 pm
Contact:

Post by Wretched_Wyx »

Heh. I figured I was doing that all wrong. Did I forget to mention I have no real shader experience? Well I don't :P. But I think I'm starting to learn finally. I tried looking for what you told me to in the Ogre manual, but didn't find it. I did find a few other interesting things, but not that in particular. Thanks for all your help so far!
User avatar
sphinkie
Greenskin
Posts: 114
Joined: Wed Jul 07, 2004 11:08 am
Location: Paris
x 1
Contact:

Post by sphinkie »

Hi Wretched_Wyx

I dont understand well what the perPixelShader is doing more than the usual Ogre Lights and simple material system ?
User avatar
Wretched_Wyx
Orc
Posts: 498
Joined: Thu Mar 16, 2006 5:27 pm
Contact:

Post by Wretched_Wyx »

In that screenshot I posted above, the plane had the per-pixel lighting shader on it. That plane also happened to only be comprised of 2 triangles (1 subdivision). Make a large plane with only 1 subdivision lit by a regular Ogre light (point light in this case), and you'll see the difference between the two :). Per-pixel lighting apllies lighting... Well, per-pixel. The regular Ogre lights are per-vertex. Someone please correct me here if I'm wrong.

On a side note, I *kind of* have attenuation working. I read a few articles on per-pixel lighting, and in the end the solution came from a post in which Falagard was asking for some help with a shader. It's still not working so nicely though... To really explain it I'd have to post a good amount of code, and I don't want to litter this thread anymore with my own problems :wink:. Guess if I can't solve it I'll start a new thread asking for some help.
big_o
Goblin
Posts: 279
Joined: Sun Feb 19, 2006 1:08 am

Post by big_o »

Tried the all in one shader, but it does not want to compile properly.

The compiler complains about the #ifdef/ #elif preprocessors. I got rid of those errors by changing the #elifs to plain #ifdefs and putting an #endif before the elif.

Now it keeps returning some vague errors about there being something wrong nere the end of file token.

The log looks like this, I am not using bumpmapping or offset mapping.

With LIGHT0, LIGHT1, LIGHT2 uncommented,

Code: Select all

Function: CgProgram::loadFromSource
Description: Unable to compile Cg program Offset_Vert: CG ERROR : The compile returned an error.
(127) : warning C7011: implicit cast from "float[4]" to "float[3]"
(145) : warning C7011: implicit cast from "float[4]" to "float[3]"
(163) : warning C7011: implicit cast from "float[4]" to "float[3]"
(382) : error C0000: parse error, unexpected '}', expecting ';' or ',' at token "}"
(382) : error C0501: type name expected at token "}"
382 lines, 3 warnings, 2 errors.
It changes to this if only LIGHT0 is uncommented

Code: Select all

Function: CgProgram::loadFromSource
Description: Unable to compile Cg program Offset_Vert: CG ERROR : The compile returned an error.
(127) : warning C7011: implicit cast from "float[4]" to "float[3]"
(0) : error C0000: parse error, unexpected $ at token "<EOF>"
(0) : error C0501: type name expected at token "<EOF>"
382 lines, 1 warnings, 2 errors.
I have already learned a lot of stuff from this shader, and It was a pretty good idea.

One question though, is this shader written to use grayscale bumpmaps, and meshes without tangents, or the multicolored normal maps?
User avatar
guilderstein
Halfling
Posts: 66
Joined: Wed Jan 04, 2006 11:48 am
Location: Hungary, Budapest

Post by guilderstein »

Hi all!

I had no time now to check your shader, but I extended my perpixel to support atten. Few thoughts: in Ogre, use mLight->setattenuation(50000,1, 0.01,0.0001)
or something like this. The first value needs to be BIG, cause it is max. rendering distance the light affects the scene. It applies to shaders too, Ogre won't give the uniform light parameters into your shader if object and ligth is farther each other than that value. Next, 1 is linear atten. Don't change too much. Other too shall be that ~.

Also, WIKI update remains to a later date, but use:

http://rapidshare.de/files/23048131/wonder.zip.html

The tuneable monster is well there :)

Bye
User avatar
Praetor
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 3335
Joined: Tue Jun 21, 2005 8:26 pm
Location: Rochester, New York, US
x 3
Contact:

Post by Praetor »

The first value only needs to be as large as the scene needs it to be. Setting it too high would seem to be a waste. I normally use 1 unit = 1 meter. Right now my demos have their entities go invisible at > 1000. So setting an attenuation max at greater than this wasteful. The value is very scene dependent.
User avatar
Falagard
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 2060
Joined: Thu Feb 26, 2004 12:11 am
Location: Toronto, Canada
x 3
Contact:

Post by Falagard »

I currently have point lights and directional lights in different shaders, basically because I apply attenuation to point lights and don't to directional lights. It would have been possible to set extremely high attenuation values for directional lights and have the same lighting effect as if attenuation wasn't applied, but I thought it makes more sense to separate them and save on instructions in the directional light shader.
big_o
Goblin
Posts: 279
Joined: Sun Feb 19, 2006 1:08 am

Post by big_o »

Much better, the new monster works!!

It looks pretty good to, but for the fact that textures apparently ignore specular and ambient. It looks a little strange, and I have to use two texture units, even if I'm not doing offste or normal mapping.
User avatar
Wretched_Wyx
Orc
Posts: 498
Joined: Thu Mar 16, 2006 5:27 pm
Contact:

Post by Wretched_Wyx »

Okay, here are some more 'thorough' suggestions:

1. Clean up the grammar and spelling.
2. Make the screenshots small enough that they don't cause horizontal overflow.
3. Seperate all the per-pixel shaders on ONE page. This way the list of shaders on the Code Snippets TOC isn't over-run with multiple versions of one shader.

And lastly, I'm guessing these shaders are (hopefully) going to be updated to be more... easier to work with. I'd like the shader to listen to my lights, rather than having to change my lights to confrom to the shader. I'm adressing attenuation in particular here. It seems more logical (to me) to be able to affect the attenuation using mLight->setAttenuation(256, 0.5, 0.0005, 0);.

It also strikes me odd that I can run the Dot3Bump Ogre demo with the specular offset mapping shader on and pull 80-90 fps, and then run the per-pixel shaders from the wiki and only get 25fps max. All that I'm doing is applying the shader to a plane, with a point light for lighting. It's a small plane, 256 x 256 with only 1 subdivision.

Don't get me wrong, this is a cool addition. Just don't go and let all the "thank-you's" and "it works!" go to your head and leave the thing how it is now... Again I say I'm no shader whiz, and if I was I would do these things myself. Instead I'm left with nudging those who can, to do it.
User avatar
guilderstein
Halfling
Posts: 66
Joined: Wed Jan 04, 2006 11:48 am
Location: Hungary, Budapest

Post by guilderstein »

Ok guys, response time.

@everyone:

Use MONSTER instead of any other I wrote. Its generally faster and tuneable.
On Falagard's post, I will extend it further, like this:
since directional light is always light0 as passed to a shader (I suppose you
want only 1 directional, while multiple point lights beside it), I can rewrite
MONSTER not to use attenuation (even if requested) for light0, if
DIRECTIONAL_LIGHT0 (a future #define) is on.

@big_o:

Please ignore any old versions of MONSTER, they were placeholders.
I don't know whether they even worked :)
In the new one, due to proper conditional branching with preprocessor directives, you won't need to use any textures to get perpixel lighting.
Textures won't even get passed to fragment shader.
And with this newest version, specular and ambient is fine on textures,
(just modify ambient in material script to desired color)
and since neither gets modulated by texture color, they are not damped,
like in multipass shaders. If u still get artifacts with new MONSTER, please
post.

@Wretched_Wyx

I understand your concerns about being flawless and grammatically correct, but u have to understand that my point here is to give cool, working, understandable shaders to the community. And since I have to pass an exam on electromagnetic fields next Tuesday, I won't check spelling on WIKI soon if u don't mind :)

I do not really understand what you meant on "changing your lights to conform the shader". If you mean attenuation, it is a tuneable parameter,
like light color, given from Ogre to shader. Values are really depending on
your scene setup, as Praetor just said. The shader is just too stupid to guess what attenuation parameters are right for your scene.
Do some math on att=1/(k1+d*k2+d*d*k3) to get your values right.
For ex. with d=1000, tweak att to be 0, decrementing in linear way,
and set light range to 1000 also. This has to conform with your models general size, and general light and model distance.

On to low FPS: that is because Ogre shaders originally do not do perpixel, just normal perturbation which resembles it. The Simple_Perpixel of the
WIKI does it, does it correctly, but does it VERY SLOW.
I wrote many docs on why. If you move some calc into vertex shader from fragment shader, it gets MUCH faster, but less accurate.
See docs on what I mean on accuracy (WIKI, MONSTER's cg file, etc).



And what I wished to post today :)


TADADADA,

Here she comes, the MONSTER shader, ver1.0 final.

http://rapidshare.de/files/23108338/won ... s.zip.html

Features:

1-pass, lightning fast, with compile time easily tuneable (0/1) options.

Supports:
Perpixel-lighting, basic normal-mapping, offset-mapping, with ambient,
diffuse, and specular components.
Maximum 3 point/directional lights.

OR

Attenuation with all above for 2 lights.

OR

Attenuation with normal-mapping only, and no ambient, for 3 lights.

Note: when used with attenuation, 3rd closest light (if used) has no specular. Technical limit with ps2.0, not bug.

And all in 1 shader, which is faster than all others I wrote, even 1-pass 1-light versions,
somewhere with 10-15 FPS.
I don't know how :)

WIKI updated. (means I'm writing it now)

Expect new versions here
(more features like ambient occlusion, environment mappin', etc. and multipass to go
without restrictions)

Bugfree tuning,
(please test it, everyone)

guilderstein

PS: I used dot3bump demo with ogrehead and knot meshes, and
mLights->setAttenuation(50000,1,0.01,0.0001) worked for me for out-of-box
object sizes and camera position.

PS2: Though I know original shaders of OgreDemos are by far not meant to be optimal,
I couldn't stop myself doing performance tests. (Respect goes there, my work is
greatly based on them.)

So, guess what?

Wonder vs. original (lowest FPS perceived, object filled the screen):
1-light offset: 192 vs 85
3-light normal: 104 vs 85
1-light normal, no specular: 303 vs 85

(and if I started out-of-box dot3bump demo with my shader, on knot mesh:
590 vs 85 :), due to my version is doing perpixel, a knot does not fill the screen
at start
)
Post Reply