How to send non-normalized/unclamped values to GLSL shader using opengl textures?
Asked Answered
R

1

5

I am trying to send an array of float values(height map) to GLSL as a GL_TEXTURE_2D. While I have the samplers set right, the issue comes from the clamping that happens when I upload the texture using glTexImage2D().

  glGenTextures(1, &_idnewTID);
  glBindTexture(GL_TEXTURE_2D, _idnewTID);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, regMesh._cols, regMesh._rows, 0, GL_RED, GL_FLOAT, regMesh._heightMap);

The values that I am sending are in the range 900.0f to 1590.0f. But this gets completely clamped out to 1.0f as it loads into the GL_TEXTURE_2D.

How do I preserve the values from being clamped to [0,1]. How should I modify the glTexImage2D() function to get this achieved?

P.S I know the sampler works fine because when I divide the values by 1500.0f and upload it to GPU and scale it in the shader, I get the proper height map structure.

Regazzi answered 21/5, 2019 at 20:45 Comment(0)
M
7

First, you should always use a sized internal format (that's the third parameter of glTexImage*). GL_RED leaves too much up to the implementation.

Second, you are trying to store and use floating-point data. So you need to choose a floating-point internal format, rather than a normalized integer one. If the internal format's size doesn't end in F, then it's not storing floating-point values. GL_R32F would be appropriate.

Madrigalist answered 21/5, 2019 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.