Sign in to follow this  
Dragon_Strike

POM on geoemtry above camera

Recommended Posts

hmm thats weird... i htought i posted it.. anyways here it is.... ive removed some of the irrelevant code.. im using a small hack for terrain to get the TBN.. dunno if its correct though


//VERTEX
#version 110

uniform sampler2D heightmap;

uniform vec3 CamPos;
varying vec3 ViewVec;
varying vec3 vertPos;
varying vec2 ParallaxOffsetVec;

void main()
{
vec3 WorldPos = gl_Vertex.xyz;
ViewVec = CamPos.xyz - WorldPos.xyz;

vec2 ParallaxDirection = normalize(ViewVec.xz);
float L = length(ViewVec);
float ParallaxLength = sqrt(L * L - ViewVec.y * ViewVec.y) / ViewVec.y;
ParallaxOffsetVec = ParallaxDirection * ParallaxLength;
ParallaxOffsetVec *= 0.00018;

gl_Position = gl_ModelViewProjectionMatrix * vec4(WorldPos, 1.0);
vertPos = gl_ModelViewMatrix * vec4(WorldPos, 1.0);
}


//FRAGMENT
#version 110

uniform sampler2D TextureMap;
uniform sampler2D NormalMap;
uniform sampler2D DetailMap;

varying vec2 ParallaxOffsetVec;
varying vec3 ViewVec;
varying vec3 VertPos;

mat3 GetTBN()
{
vec3 Normal = texture2D(NormalMap, gl_TexCoord[0].st).rgb*2.0-1.0;
vec3 Tangent = cross(Normal,vec3(0.0,0.0,1.0));
vec3 Binormal = cross(Normal,vec3(-1.0,0.0,0.0));
return mat3((Tangent), (Binormal), (Normal));
}

vec2 parallax(vec3 N, vec3 V, vec2 TexCoord)
{
int numSteps = (int)mix(20, 8, dot(V,N));

float CurHeight = 0.0;
float PrevHeight = 1.0;
float NextHeight = 0.0;

float StepSize = 1.0 / (float)numSteps;

vec2 TexOffsetPerStep = StepSize*ParallaxOffsetVec;
vec2 TexCurrentOffset = TexCoord;
float CurrentBound = 1.0;

vec2 p1 = 0;
vec2 p2 = 0;

vec2 TexOffset = 0;

for(int i = 0; i < numSteps; i++)
{
TexCurrentOffset -= TexOffsetPerStep;
CurHeight = texture2D(DetailMap, TexCurrentOffset*30).g+0.0000001;

CurrentBound -= StepSize;

if (CurHeight > CurrentBound)
{
p1 = vec2(CurrentBound, CurHeight);
p2 = vec2(CurrentBound+StepSize, PrevHeight);

TexOffset = TexCurrentOffset - TexOffsetPerStep;

i = numSteps + 1;
}

PrevHeight = CurHeight;

}

float Delta2 = p2.x - p2.y;
float Delta1 = p1.x - p1.y;

float ParallaxAmount = (p1.x * Delta2 - p2.x * Delta1) / (Delta2 - Delta1);

vec2 ParallaxOffset = ParallaxOffsetVec * (1.0 - ParallaxAmount);

return TexCoord - ParallaxOffset;

}

void main()
{
vec2 TexCoord = gl_TexCoord[0].st;

mat3 TBN = GetTBN();

vec3 nViewVec = normalize(ViewVec)*TBN;

vec2 NewTexCoord = parallax(vec3(0.0, 1.0,0.0)*TBN,nViewVec, TexCoord);

vec4 Color = texture2D(TextureMap,NewTexCoord)*texture2D(DetailMap,NewTexCoord*30).g;

gl_FragColor = vec4(Color);
}








[Edited by - Dragon_Strike on April 9, 2007 4:26:32 PM]

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