Sign in to follow this  
enkei

Heightmap - Vertex Shader

Recommended Posts

I want to use a heightmap within a vertex shader to set the z-value of the incoming vertex, but I've got some trouble with this. It works for me fine, if I write: uniform sampler2D heightMap; uniform vec2 dimension; const float height = 10.0; void main() { vec4 vertex = vec4(gl_Vertex); vec2 texCoord = vec2(vertex.x/dimension.y, vertex.y/dimension.x); float offset = height * texture2D(heightMap, texCoord).r; if (texCoord.x <= 1.0 && texCoord.x >= 0.0 && texCoord.y <= 1.0 && texCoord.y >= 0.0) { vertex.z += offset; } gl_FrontColor = gl_Color; gl_Position = gl_ModelViewProjectionMatrix * vertex; } BUT if I eliminate the if, it doesn't work anymore: uniform sampler2D heightMap; uniform vec2 dimension; const float height = 10.0; void main() { vec4 vertex = vec4(gl_Vertex); vec2 texCoord = vec2(vertex.x/dimension.y, vertex.y/dimension.x); float offset = height * texture2D(heightMap, texCoord).r; vertex.z += offset; gl_FrontColor = gl_Color; gl_Position = gl_ModelViewProjectionMatrix * vertex; } I'm confuesed. Does anybody know why the second doesn't work?

Share this post


Link to post
Share on other sites
I'm sure the problem is your texture. Check to see if the texture shows correctly on screen.

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