Ogre Ocean shader

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
elvman
Posts: 253
Joined: Sun Sep 17, 2006 9:37 am
Location: Riga, Latvia
Contact:

Ogre Ocean shader

Post by elvman »

I want to implement the ogre ocean shader in my Irrlicht application, but I have a few questions.
HLSL pixel shader code:

Code: Select all

struct v2f {
	float4 Position  : POSITION;  // in clip space
	float3 rotMatrix1 : TEXCOORD0; // first row of the 3x3 transform from tangent to obj space
	float3 rotMatrix2 : TEXCOORD1; // second row of the 3x3 transform from tangent to obj space
	float3 rotMatrix3 : TEXCOORD2; // third row of the 3x3 transform from tangent to obj space

	float2 bumpCoord0 : TEXCOORD3;
	float2 bumpCoord1 : TEXCOORD4;
	float2 bumpCoord2 : TEXCOORD5;

	float3 eyeVector  : TEXCOORD6;
};


float4 main(v2f IN,
			uniform sampler2D NormalMap,
			uniform samplerCUBE EnvironmentMap,
			uniform float4 deepColor,
			uniform float4 shallowColor,
			uniform float4 reflectionColor,
			uniform float reflectionAmount,
			uniform float reflectionBlur,
			uniform float waterAmount,
			uniform float fresnelPower,
			uniform float fresnelBias,
			uniform float hdrMultiplier
			) : COLOR
{
	// sum normal maps
	// sample from 3 different points so no texture repetition is noticeable
    float4 t0 = tex2D(NormalMap, IN.bumpCoord0) * 2.0 - 1.0;
    float4 t1 = tex2D(NormalMap, IN.bumpCoord1) * 2.0 - 1.0;
    float4 t2 = tex2D(NormalMap, IN.bumpCoord2) * 2.0 - 1.0;
    float3 N = t0.xyz + t1.xyz + t2.xyz;

    float3x3 m; // tangent to world matrix
    m[0] = IN.rotMatrix1;
    m[1] = IN.rotMatrix2;
    m[2] = IN.rotMatrix3;

    N = normalize( mul( N, m ) );

	// reflection
    float3 E = normalize(IN.eyeVector);
    float4 R;
    R.xyz = reflect(E, N);
    // Ogre conversion for cube map lookup
    R.z = -R.z;
    R.w = reflectionBlur;
    float4 reflection = texCUBEbias(EnvironmentMap, R);
    // cheap hdr effect
    reflection.rgb *= (reflection.r + reflection.g + reflection.b) * hdrMultiplier;

	// fresnel
    float facing = 1.0 - max(dot(-E, N), 0);
    float fresnel = saturate(fresnelBias + pow(facing, fresnelPower));

    float4 waterColor = lerp(shallowColor, deepColor, facing) * waterAmount;

    reflection = lerp(waterColor,  reflection * reflectionColor, fresnel) * reflectionAmount;
    return waterColor + reflection;
}
How can I pass a uniform samplerCUBE constant to shader? How are cubemaps in Irrlicht used.
afecelis
Admin
Posts: 3075
Joined: Sun Feb 22, 2004 10:44 pm
Location: Colombia
Contact:

Post by afecelis »

Not sure if it my help, but check Sio2's examples and water shaders. Some of them got the source included:
http://sio2.g0dsoft.com/modules/wmpdownloads/
elvman
Posts: 253
Joined: Sun Sep 17, 2006 9:37 am
Location: Riga, Latvia
Contact:

Post by elvman »

Already checked out! The only demo which uses cubemapping (Island Demo (Part 1) for the IrrSpintz 3D Engine) is provided without source.
elvman
Posts: 253
Joined: Sun Sep 17, 2006 9:37 am
Location: Riga, Latvia
Contact:

Post by elvman »

Ok, I found out, that Irrlicht doesn't support Cubemaps yet. When will it do? I have to finish my project soon and it would be great if you implemented CubeTexture in Irrlicht (like IrrSpintz does).
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No way within a short term project's time.
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

As I state in my credits, my Island Demo implements a version of the ogre water "ocean" shader which in turn is just a version of an nvidia shader. I had to bugfix it via PIX, though, as the reflections were wrong.

Vanilla Irrlicht does not currently support cubemaps. Why not use a spheremap instead? It's just one texture and you can do the math in the shaders.
elvman
Posts: 253
Joined: Sun Sep 17, 2006 9:37 am
Location: Riga, Latvia
Contact:

Post by elvman »

Ok, I am making my own water shader in render monkey, but I have a few questions. Googled for a tutorial on water for top-view games (I am making a strategy game), but found nothing. I am trying to make something like this (screens from age of empires 3):
Image
Image
I guess it is stupid to use skybox or skydome in a top-view game. Then how is the color of wave calculated? As I am thinking, it has something to do with the bump normals (like, the colour of the pixel is calculated by a direction of the normal e.g. if the normal is pointing to the left, the pixel is white, but if the normal is pointing to the right, it is blue). Or maybe there is used something like a 2D sky texture for the vawe colur calculation?
Ico
Posts: 289
Joined: Tue Aug 22, 2006 11:35 pm

Post by Ico »

Afaik most such games have a sky above the game world you just can't see it in the top down view. Many games allow you to zoom/pan the camera and then you can see the sky.

As another example - Anno 1701 / A.D. 1701:

"top down":
http://www.anno1701.de/screenshots/bedu ... re_002.jpg
"zoomed":
http://www.anno1701.de/screenshots/bedu ... em_002.jpg
elvman
Posts: 253
Joined: Sun Sep 17, 2006 9:37 am
Location: Riga, Latvia
Contact:

Post by elvman »

How long wiil it take for Irrlicht to implement cubemaps? I must implement it in my application. What if I made my own modification to irrlicht and gave it to Irrlicht developers? Will they implement it in Irrlicht? I'd like it to be implemented in Irrlicht because I want my application to be compatible with new versions of irrlicht.
Ico
Posts: 289
Joined: Tue Aug 22, 2006 11:35 pm

Post by Ico »

If it's written well and works - don't think anyone would say "no" as long as it's still in the scope of a graphics engine (no game elements etc.).

As for compatibility: Why don't you just statically link to irrlicht? :) I'd say you'll allways have to make at least small adjustements for new versions anyway.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Cubemaps shouldn't be too complex to implement. A new texture constructor, some adjustments to the members, and a check for the correct render setup should be all.
For inclusion into the core you might have to think a little more about proper integration into the current Irrlicht API. Avoid quick'n'dirty hacks etc. But delivering a small and not over-complicated solution might be enough for a starter. Other can continue this work then.
noreg
Posts: 158
Joined: Fri Jun 23, 2006 6:01 pm
Location: continental europe

Post by noreg »

@elvman: The shader in your pictures uses RTT instead of a cubemap. RTT is superior to Cubemapping as you can easily adjust translucency. Also the reflections with a cubemap look plain fake, whereas with RTT its better. As you can see in the ogre examples the ocean surface is opaque. Don't be sorry, just make a nice RTT water shader and improve it with the layered ripples from the ocean shader. Got RTT, no real need for Cubemapping. Animated normals give that blinking reflections
elvman
Posts: 253
Joined: Sun Sep 17, 2006 9:37 am
Location: Riga, Latvia
Contact:

Post by elvman »

What do you mean with RTT? Do you mean Render To Texture?
noreg
Posts: 158
Joined: Fri Jun 23, 2006 6:01 pm
Location: continental europe

Post by noreg »

Yes
Post Reply