So I want to draw a height field using shaders. I have a VBO of 2D position coordinates and 2D texture coordinates, and a texture containing height values. I have a shader that takes in this texture and displaces vertices based on the value in the texture.
Before I used this approach I just had one big VBO with 3D position coordinates, but I wanted to be able to deform the height field quickly and easily using shaders by making changes to the texture, so I changed to this approach.
So I get this working, but it is really slow (~6fps). The height field is 600x600 and ran at around 50-60 fps when I was using just a VBO.
I am curious if this is just too much data for the GPU to handle, or if I am doing something wrong. So here’s my vertex shader,
uniform sampler2D heights;
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
vec4 position = gl_Vertex;
// Since the texture is black and white RGB, just take the value in red, and scale by 2
position.z += texture2D(heights, gl_MultiTexCoord0.st).r * 2.0;
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * position;
}
And the fragment shader:
uniform sampler2D heights;
void main()
{
gl_FragColor = texture2D(heights, gl_TexCoord[0].st);
}
And here’s a snip of the rendering process just in case:
* textures and buffers are bound, vertex and texcoord pointers created *
* The field is a flat plane rendered as rows of triangle strips, the shader displaces along z *
int stride = numCols * 4;
shader.engage(gl);
// Draw the VBO
for(int i=0; i<(numRows-1)/2; i++) {
gl.glDrawRangeElements(GL.GL_TRIANGLE_STRIP,
0,
endIndex,
stride,
GL.GL_UNSIGNED_INT,
stride*i*BufferUtil.SIZEOF_INT
);
}
shader.disengage(gl);
Thanks!