1

I got an array with floating point values in 0.0 - 1.0 range (a height-map) but the texture comes back either black or just red I've tried multiple combinations of internal and source formats but nothing seems to work. My current code that just gives me a red texture is:

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

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, w, h, 0, GL_RED, GL_FLOAT, data);

[Edit] Image output: enter image description here Any ideas what is wrong?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
user1031204
  • 701
  • 1
  • 8
  • 30
  • Did you try checking the `glGetError()` codes after each command? – Nadir Sep 27 '18 at 08:13
  • With the current code above, no there are no errors. – user1031204 Sep 27 '18 at 12:34
  • But your the image you posted looks like a heightmap, what is exactly the output you expect? – Nadir Sep 27 '18 at 14:04
  • 1
    What are you doing in the shader code? If you do something like `gl_FragColor = texture_color;`, then the green and blue color channel will always by 0. You have to do something like `gl_FragColor = texture_color.rrra;` Can you show the fragment shader? – Rabbid76 Sep 27 '18 at 15:11

2 Answers2

3

You have to set the texture swizzling parameters, to treat OpenGL to read the green and blue color channel from the red color channel, when the texture is looked up - see glTexParameteri:

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED );

See OpenGL 4.6 API Compatibility Profile Specification; 16.1 Texture Environments and Texture Functions; page 595:

They are derived by computing the base color Cb and Ab from the filtered texture values Rt, Gt, Bt, At, Lt, and It as shown in table 16.1, ...

Texture Base Texture base color Internal Format    Cb              Ab
...
RED                                                (Rt, 0, 0)      1
RG                                                 (Rt, Gt, 0)     1
RGB                                                (Rt, Gt, Bt)    1
RGBA                                               (Rt, Gt, Bt)    At

Table 16.1: Correspondence of filtered texture components to texture base components.

...followed by swizzling the components of Cb, controlled by the values of the texture parameters TEXTURE_SWIZZLE_R, TEXTURE_SWIZZLE_G, TEXTURE_SWIZZLE_B, and TEXTURE_SWIZZLE_A. If the value of TEXTURE_SWIZZLE_R is denoted by swizzler, swizzling computes the first component of Cs according to

if (swizzler == RED)
    Cs[0] = Cb[0];
else if (swizzler == GREEN)
    Cs[0] = Cb[1];
else if (swizzler == BLUE)
    Cs[0] = Cb[2];
else if (swizzler == ALPHA)
    Cs[0] = Ab;
else if (swizzler == ZERO)
    Cs[0] = 0;
else if (swizzler == ONE)
    Cs[0] = 1; // float or int depending on texture component type

If you would use a shader program, it would be possible to do something similar in the fragment shader, by Swizzling:

in vec3 uv;

out vec4 frag_color;

uniform sampler2D u_texture;

void main()
{
   frag_color = texture(u_texture, uv).rrra;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you. Using the swizzling parameters worked great as I did not want to change the shader code since it was a generic texture rendering shader. – user1031204 Sep 28 '18 at 07:10
0

In the 7th arguement of the glTexImage2D function your suppose so set the format of the image. Your setting it to GL_RED. You should be setting it to the format of your picture, likely GL_RGB or GL_RGBA.

Please read the documentation.

If it dosent work with the proper format of the texture, something else is wrong. Although its impossible to tell not seeing the rest of your code.

Are you sure your data loaded properly? Are you sending the proper Texture Coordinates to the fragment shader? Do you remember to set the active texure? Have you set a Sampler2D in you fragment shader to the same value as the active shader?

Behar
  • 35
  • 11
  • I've attached an image to show the output but if I use GL_RGB or GL_RGBA it causes an exception to be thrown. Data seems to be correct, texture coordinates are normalised, active texture is set correctly for rendering and shader 2D sampler is also correct. – user1031204 Sep 27 '18 at 12:39
  • Have you tried using a different texture. JPG images tend to be RGB. Please try putting data for an image you **know** is RGB for sure and see if it fixes it. If it is throwing an "acces violation" then its most likely that your not using the proper format. – Behar Sep 27 '18 at 13:00