GLSL, is it possible to offsetting vertices based on height map colour?

Posted by Rob on Game Development See other posts from Game Development or by Rob
Published on 2013-11-03T23:33:43Z Indexed on 2013/11/04 4:15 UTC
Read the original article Hit count: 358

I am attempting to generate some terrain based upon a heightmap. I have generated a 32 x 32 grid and a corresponding height map ->

enter image description here

In my vertex shader I am trying to offset the position of the Y axis based upon the colour of the heightmap, white vertices being higher than black ones.

//Vertex Shader Code
#version 330

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform sampler2D heightmap;

layout (location=0) in vec4 vertexPos;
layout (location=1) in vec4 vertexColour;
layout (location=3) in vec2 vertexTextureCoord;
layout (location=4) in float offset;

out vec4 fragCol;
out vec4 fragPos;
out vec2 fragTex;

void main()
{
    // Retreive the current pixel's colour 
    vec4 hmColour = texture(heightmap,vertexTextureCoord);

    // Offset the y position by the value of current texel's colour value ?
    vec4 offset = vec4(vertexPos.x , vertexPos.y + hmColour.r, vertexPos.z , 1.0);

    // Final Position
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * offset;

    // Data sent to Fragment Shader. 
    fragCol = vertexColour;
    fragPos = vertexPos;
    fragTex = vertexTextureCoord;
}

However the code I have produced only creates a grid with none of the y vertices higher than any others.

© Game Development or respective owner

Related posts about opengl

Related posts about glsl