0

I'm looking up a 1 dimensional texture containing RGB values in a fragment shader. As the implementation should be OpenGL ES 3.0 compatible I cannot use a true 1D texture but try using a 2D texture with a height of 1.

This is how I implemented it:

Initialization of the texture

glPixelStorei   (GL_UNPACK_ALIGNMENT,  1);
glPixelStorei   (GL_UNPACK_ROW_LENGTH, numColours);
glGenTextures   (1, &colourScaleTexture);
glActiveTexture (colourScaleTextureUnit);
glBindTexture   (GL_TEXTURE_2D, colourScaleTexture);
glTexImage2D    (GL_TEXTURE_2D, 0, GL_RGB, numColours, 1, 0, GL_RGB, GL_FLOAT, colourScale);

Render Callback

// Set texture wrapping and interpolation mode
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glActiveTexture (colourScaleTextureUnit);
glBindTexture (GL_TEXTURE_2D, colourScaleTexture);
surfaceShader->uniforms->colourScale->set (1); // Texture unit 1

// Enable a second other texture used in the vertex shader
glActiveTexture (matrixDataTextureUnit);
glBindTexture  (GL_TEXTURE_2D, matrixDataTexture);
surfaceShader->uniforms->matrixToPlot->set (0); // Texture unit 0

glDrawElements (GL_TRIANGLE_STRIP, surfaceVertexIndexBufferSize, GL_UNSIGNED_INT, 0);

Fragment shader

// depending on the platform #version 330 (Desktop) or #version 300 es and a precision specifier (OpenGL ES) will be prepended

in      float     vGraphHeight;
uniform sampler2D uColourScale;
out     vec4      fragColor;

void main (void)
{
    fragColor = vec4 (texture (uColourScale, vec2 (vGraphHeight, 0.0)).rgb, 1.0);
}

What happens: The fragment color is black. However, I accidentally changed the initialization of the texture to this and it suddenly worked on my Desktop machine under non-ES OpenGL 3.2:

glPixelStorei   (GL_UNPACK_ALIGNMENT,  1);
glPixelStorei   (GL_UNPACK_ROW_LENGTH, numColours);
glGenTextures   (1, &colourScaleTexture);
glActiveTexture (colourScaleTextureUnit);
glBindTexture   (GL_TEXTURE_1D, colourScaleTexture); <-- Here
glTexImage2D    (GL_TEXTURE_2D, 0, GL_RGB, numColours, 1, 0, GL_RGB, GL_FLOAT, colourScale);

Of course binding a GL_TEXTURE_1D and then use a GL_TEXTURE_2D is not the way it should be done, however I don't understand why this makes it working? And I would be highly interested in making it work with a 2D texture.

So who finds the error in my code?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
PluginPenguin
  • 1,576
  • 11
  • 25
  • I don't think `GL_FLOAT` is supported by ES2. – Columbo Sep 26 '19 at 15:13
  • @Columbo The question is tagged *opengl-es-3.0*. `GL_FLOAT` is supported by [`glTexImage2D`](https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml) in OpenGL ES 3.0. – Rabbid76 Sep 26 '19 at 16:06
  • Sorry - had misread. I think the combination of `internalFormat`, `format` and `type` in the calls to `glTexImage2D` is unsupported on both GLES3.0 and GL3.2. OP is using `GL_RGB`, `GL_RGB`, `GL_FLOAT`, but that combo doesn't appear in table 1 or 2 of the ref pages for `glTexImage2D`. Possibly `GL_RGB32F`, `GL_RGB`, `GL_FLOAT` was intended? I'd recommend checking for errorsafter every call to an OpenGL function. – Columbo Sep 26 '19 at 18:31

0 Answers0