Sending a simple heightmap as texture to vertex shader

Started by
2 comments, last by wtherapy 11 years, 5 months ago
Hello,

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 );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
}

glEnable(GL_TEXTURE_2D );
glActiveTexture(GL_TEXTURE0);
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.
Advertisement
Try to use texture nearest filtering instead of linear
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 http://www.opengl.org/wiki/Common_Mistakes#Creating_a_complete_texture – 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).
hello,

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:


#define OPENTG_HEIGHTMAPRES 256
float xn = X / OPENTG_SUBCELLSIZE;
float yn = Y / OPENTG_SUBCELLSIZE;

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

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

if( OPENTG_HEIGHTMAPRES == i )
{
i --;
v = 1;
}
if( OPENTG_HEIGHTMAPRES == j )
{
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.

This topic is closed to new replies.

Advertisement