Sign in to follow this  

Sending a simple heightmap as texture to vertex shader

Recommended Posts


I am developing a fractal terrain generation based on an original 256x256 heightmap.
I need to use a texture to represent this heightmap, together with other gradients in each point, that is, 3 floats per 'texel'.

//////////////////////////////////////////////////////////////////////////////in the vertex shader, I am doing:

uniform sampler2D TexHeightMap;

vec4 GetVertCellParameters( uint i, uint j )
return texture( TexHeightMap, vec2( i, j ) ) * 2500.0f;

the * 2500.0f multiplication was used just to make sure the values are not somewhere between 0-1; in the final code,
the function will just return texture( TexHeightMap, vec2( i, j ) );

//////////////////////////////////////////////////////////////////////////////in the CPU, I am using:

void GLTexture::CreateHTExture( const void* pvBytes, unsigned int unW, unsigned int unH )
glEnable( GL_TEXTURE_2D );
glGenTextures( 1, &m_uglID );
glBindTexture( GL_TEXTURE_2D , m_uglID );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB32F, unW + 1, unH + 1, 0, GL_RGB, GL_FLOAT, pvBytes );

glEnable(GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D , pTexture->GetID());
glUniform1i(m_ugl_HeightMapTexture, 0);

pTexture is of type GLTexture;

m_ugl_HeightMapTexture = glGetUniformLocation(m_uglProgram, "TexHeightMap");

for some reason, I am only getting zeros in the vertex shader ( when calling GetVertCellParameters ).
I checked the parameters pvBytes with memory view and they look fine.
I just need to get the float values, unmodified, just as I send them, in the vertex shader, and access them via texture as vec4.

Thank you.

Share this post

Link to post
Share on other sites
You should use glGetError() when having problems (be warned that it automatically sets the error value to 0 after returning the value). Your problem is probably described at OpenGL Wiki – you HAVE to set GL_TEXTURE_MIN_FILTER to GL_NEAREST (returns pixelated texture) or GL_LINEAR (returns average of the four texels). That makes your texture incomplete to OpenGL. You want GL_LINEAR for your heightmap (so the slopes are nice).

Share this post

Link to post
Share on other sites

I don't need 'nice slopes', since my grid of 256x256 is much smaller than my 3d world. I am using interpolation functions to create the smooth surface. I just want every texel to contain 3 floats and I want o access them as memory data. Here is a snippet from my shader:


unsigned int i = (uint)floor( yn );
unsigned int j = (uint)floor( xn );

float u = fract(xn);
float v = fract(yn);

i --;
v = 1;
j --;
u = 1;
vec4 vH00 = GetVertCellParameters( i, j );
vec4 vH10 = GetVertCellParameters( i + 1u, j );
vec4 vH01 = GetVertCellParameters( i, j + 1u );
vec4 vH11 = GetVertCellParameters( i + 1u, j + 1u );
/*do something with the vec4's
they contain infos of height values in i, j cell and gradient information

it seems there are problems if some of those values are negative - the rendering looks somehow like it renders part of the scene and then stops rendering anything else in a certain point.

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