Vexator

02-27-2007, 05:34 AM

i could finally afford a new graphics card (gf7800gt) and so i wanted to test features possible with sm3.0 like advanced parallax mapping. i downloaded the ati sdk which had a nice demo on that, but it won't run on my nv as it does not support texture2dlod.. argh!

then i found this: http://graphics.cs.brown.edu/games/SteepParallax/index.html

i tried to compile the sample fragment shader and after i fixed about a dozen syntax errors i got it running. it does not look like i expected, though. seems that my vertex shader does not provide the correct information.

vertex:

varying vec2 v_Coordinates;

varying vec3 v_ViewDirection;

varying vec3 v_LightDirection;

attribute vec3 a_Normal;

attribute vec3 a_Tangent;

attribute vec3 a_Bitangent;

attribute vec2 a_Coordinates;

uniform float u_LightRadius;

uniform vec3 u_LightPosition;

uniform vec3 u_CameraPosition;

void main()

{

v_Coordinates = a_Coordinates;

vec3 Vertex = vec3(gl_ModelViewMatrix*gl_Vertex);

vec3 ViewDirection = u_CameraPosition-Vertex;

vec3 LightDirection = (1.0/u_LightRadius)*(u_LightPosition-Vertex);

// compute normalized TBN matrix

vec3 Normal = normalize( gl_NormalMatrix*a_Normal );

vec3 Tangent = normalize( gl_NormalMatrix*a_Tangent );

vec3 Bitangent = normalize( gl_NormalMatrix*a_Bitangent );

// compute view direction

v_ViewDirection.x = dot( Tangent, ViewDirection );

v_ViewDirection.y = dot( Bitangent, ViewDirection );

v_ViewDirection.z = dot( Normal, ViewDirection );

// compute light direction

v_LightDirection.x = dot( Tangent, LightDirection.xyz );

v_LightDirection.y = dot(Bitangent, LightDirection.xyz );

v_LightDirection.z = dot( Normal, LightDirection.xyz );

gl_Position = ftransform();

}fragment:

uniform sampler2D u_Texture0;

uniform sampler2D u_Texture1;

float bumpScale = 0.05;

varying vec3 v_LightDirection;

varying vec3 v_ViewDirection;

varying vec2 v_Coordinates;

void main()

{

float Height = 1.0;

float StepCount = 5.0;

vec2 OffsetCoordinates = v_Coordinates.xy;

vec4 NormalHeight = texture2D( u_Texture1, OffsetCoordinates );

StepCount = mix( StepCount*2.0, StepCount, normalize( v_ViewDirection ).z );

float Step = 1.0 / StepCount;

vec2 Delta = vec2( -v_ViewDirection.x, v_ViewDirection.y ) * bumpScale / ( v_ViewDirection.z * StepCount );

while ( NormalHeight.a < Height )

{

Height -= Step;

OffsetCoordinates +=Delta;

NormalHeight = texture2D( u_Texture1, OffsetCoordinates );

}

Height = NormalHeight.a;

vec3 Color = texture2D( u_Texture0, OffsetCoordinates ).rgb;

vec3 LightDirection = normalize( v_LightDirection );

vec3 Normal = NormalHeight.xyz * 2.0 - 1.0;

vec3 tsH = normalize( LightDirection + normalize( v_ViewDirection ) );

float NdotL = max( 0.0, dot( Normal, LightDirection ) );

float NdotH = max( 0.0, dot( Normal, tsH ) );

float spec = NdotH * NdotH;

float Shadow = 0.0;

if ( NdotL > 0.0 )

{

float ShadowStepCount = mix( 60.0, 5.0, LightDirection.z );

Step = 1.0 / ShadowStepCount;

Delta = vec2( LightDirection.x, -LightDirection.y ) * bumpScale / ( ShadowStepCount * LightDirection.z );

Height = NormalHeight.a + Step * 0.1;

while ( ( NormalHeight.a < Height ) && ( Height < 1.0 ) )

{

Height += Step;

OffsetCoordinates +=Delta;

NormalHeight = texture2D( u_Texture1, OffsetCoordinates );

}

Shadow = ( NormalHeight.a < Height ? 1.0 : 0.0 );

}

gl_FragColor.rgb =Color * NdotL * Shadow;

}any ideas? thanks!

then i found this: http://graphics.cs.brown.edu/games/SteepParallax/index.html

i tried to compile the sample fragment shader and after i fixed about a dozen syntax errors i got it running. it does not look like i expected, though. seems that my vertex shader does not provide the correct information.

vertex:

varying vec2 v_Coordinates;

varying vec3 v_ViewDirection;

varying vec3 v_LightDirection;

attribute vec3 a_Normal;

attribute vec3 a_Tangent;

attribute vec3 a_Bitangent;

attribute vec2 a_Coordinates;

uniform float u_LightRadius;

uniform vec3 u_LightPosition;

uniform vec3 u_CameraPosition;

void main()

{

v_Coordinates = a_Coordinates;

vec3 Vertex = vec3(gl_ModelViewMatrix*gl_Vertex);

vec3 ViewDirection = u_CameraPosition-Vertex;

vec3 LightDirection = (1.0/u_LightRadius)*(u_LightPosition-Vertex);

// compute normalized TBN matrix

vec3 Normal = normalize( gl_NormalMatrix*a_Normal );

vec3 Tangent = normalize( gl_NormalMatrix*a_Tangent );

vec3 Bitangent = normalize( gl_NormalMatrix*a_Bitangent );

// compute view direction

v_ViewDirection.x = dot( Tangent, ViewDirection );

v_ViewDirection.y = dot( Bitangent, ViewDirection );

v_ViewDirection.z = dot( Normal, ViewDirection );

// compute light direction

v_LightDirection.x = dot( Tangent, LightDirection.xyz );

v_LightDirection.y = dot(Bitangent, LightDirection.xyz );

v_LightDirection.z = dot( Normal, LightDirection.xyz );

gl_Position = ftransform();

}fragment:

uniform sampler2D u_Texture0;

uniform sampler2D u_Texture1;

float bumpScale = 0.05;

varying vec3 v_LightDirection;

varying vec3 v_ViewDirection;

varying vec2 v_Coordinates;

void main()

{

float Height = 1.0;

float StepCount = 5.0;

vec2 OffsetCoordinates = v_Coordinates.xy;

vec4 NormalHeight = texture2D( u_Texture1, OffsetCoordinates );

StepCount = mix( StepCount*2.0, StepCount, normalize( v_ViewDirection ).z );

float Step = 1.0 / StepCount;

vec2 Delta = vec2( -v_ViewDirection.x, v_ViewDirection.y ) * bumpScale / ( v_ViewDirection.z * StepCount );

while ( NormalHeight.a < Height )

{

Height -= Step;

OffsetCoordinates +=Delta;

NormalHeight = texture2D( u_Texture1, OffsetCoordinates );

}

Height = NormalHeight.a;

vec3 Color = texture2D( u_Texture0, OffsetCoordinates ).rgb;

vec3 LightDirection = normalize( v_LightDirection );

vec3 Normal = NormalHeight.xyz * 2.0 - 1.0;

vec3 tsH = normalize( LightDirection + normalize( v_ViewDirection ) );

float NdotL = max( 0.0, dot( Normal, LightDirection ) );

float NdotH = max( 0.0, dot( Normal, tsH ) );

float spec = NdotH * NdotH;

float Shadow = 0.0;

if ( NdotL > 0.0 )

{

float ShadowStepCount = mix( 60.0, 5.0, LightDirection.z );

Step = 1.0 / ShadowStepCount;

Delta = vec2( LightDirection.x, -LightDirection.y ) * bumpScale / ( ShadowStepCount * LightDirection.z );

Height = NormalHeight.a + Step * 0.1;

while ( ( NormalHeight.a < Height ) && ( Height < 1.0 ) )

{

Height += Step;

OffsetCoordinates +=Delta;

NormalHeight = texture2D( u_Texture1, OffsetCoordinates );

}

Shadow = ( NormalHeight.a < Height ? 1.0 : 0.0 );

}

gl_FragColor.rgb =Color * NdotL * Shadow;

}any ideas? thanks!