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?