Sending raw data as texture to vertex shader

Hello,

I am developing a terrain fractal generator based on an original heightmap of 256*256. Each point in the height map contain the height but also gradient on X and gradient on Y. These are inputs to my program.
I want to send this heightmap in the shader and access it's values in the vertex shader. That is, because I cannot compute the elevation given X and Y ( Z is upwards ) unless I can compute the cell in which the XY is located and then compute the elevation based on the height and gradients.
I have used the following basic code:

Code doesn't work ( cannot send the actual data to the shader ), unless data contain only positive values. I need a way to get the whole heightmap ( together with gradients ) to the shader.

I can follow you up to this point where you are describing your actual problem For example: what do you mean by "can not sent the actual data to the shader"? You do upload the data, that's what the glTexSubImage2D call does. Can you try explaining the problem again, being careful to state what is happening vs. what you'd expect to happen?

As a somewhat random guess: are you setting the minification/magnification filter modes for your texture? If not your problem is probably that the default minification filter is GL_NEAREST_MIPMAP_LINEAR, but you don't specify all necessary mipmap levels.

The normal way to send negative numbers is to displacement the values eg -1 to +1 is sent as 0 - 2 then you subtract the base in the shader. If space is a problem in the texture you may have to loose accuracy by mapping -1 to 1 to the range 0-1.

I can follow you up to this point where you are describing your actual problem For example: what do you mean by "can not sent the actual data to the shader"? You do upload the data, that's what the glTexSubImage2D call does. Can you try explaining the problem again, being careful to state what is happening vs. what you'd expect to happen?

As a somewhat random guess: are you setting the minification/magnification filter modes for your texture? If not your problem is probably that the default minification filter is GL_NEAREST_MIPMAP_LINEAR, but you don't specify all necessary mipmap levels.

I am uploading the data to the shader, but I cannot see the actual data that are read from the shader ( as I cannot set a breakpoint in the shader ).
My actual shader just draws the terrain in blue color and a highroad in green; I have tried to send random data to the shader ( rand() % 1500 ) in each float value and things worked pretty much as expected: draw a green highroad with a blue terrain that was having quite random hieghtvalues which is ok.
When I changed some values to -(rand() % 1500) ( that is, negative values ), the terrain draw partially and the green highroad ( which is drawn last ) is not drawn at all, something like the shader crashed somewhere or I don't know. I suspect the problem comes from the fact that I pass negative values.
Perhaps I need to map all values in 0-1 but that would add a precision and performance penalty. Best would be to just get the original data into the texture and have it available in the shader as I have it in CPU memory.

Perhaps I need to map all values in 0-1 but that would add a precision and performance penalty. Best would be to just get the original data into the texture and have it available in the shader as I have it in CPU memory.

Yes, texture() accepts values in the range 0.0 - 1.0 when using sampler2D. So if you're providing integer values you're likely to just be getting the texture repeated many times (behaving however the wrap mode specifies). If you use GL_TEXTURE_2D you'll need to divide by the width (+remember to fill or disable mipmaps), alternatively you could use a rectangle texture + sampler2DRect.

Yes, texture() accepts values in the range 0.0 - 1.0 when using sampler2D. So if you're providing integer values you're likely to just be getting the texture repeated many times (behaving however the wrap mode specifies). If you use GL_TEXTURE_2D you'll need to divide by the width (+remember to fill or disable mipmaps), alternatively you could use a rectangle texture + sampler2DRect.

Hello,

thanks for the info. I suspect that, when sending integer, the gltext* will just map the highest representable value to 1 and the lowest to 0.
I have tried your suggestion, but the shader fails to compile with the error: