1

I am trying to display a heatmap with OpenGL using shaders.

Here is my vertex shader:

# version 130 
void main (void) 
{       
    vec4 vertex = gl_Vertex;
    gl_Position = gl_ModelViewProjectionMatrix * vertex; 
    gl_TexCoord[0] = gl_MultiTexCoord0; 
}

And here is my fragment shader:

# version 130 
uniform sampler2D heatmap;
uniform sampler1D colormap; 
void main (void) 
{   
    float temp = texture2D(heatmap, gl_TexCoord[1].st).r; // [0 - 50] degrees celcius
    float r = temp/50.0f;
    r = clamp(r, 0.0f, 1.0f); 
    gl_FragColor = texture1D(colormap, r);
}

Here is the code I call once to send the textures to GPU memory:

glGenTextures(2, textures);
GLenum errc = glGetError();
if (errc != GL_NO_ERROR)
{
    const char* errmsg = (const char*)gluErrorString(errc);
    std::cerr << errmsg;
}

...

glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_1D);
glBindTexture(GL_TEXTURE_2D, textures[0]); // makes the texture with id texture active

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 100, 100, 0, GL_RED, GL_FLOAT, &data[0]);


glBindTexture(GL_TEXTURE_1D, textures[1]); // makes the texture with id texture active
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage1D(GL_TEXTURE_1D, 0, 3, 256, 0, GL_RGB, GL_FLOAT, &rgb[0]); 

Here data is a std::vector of 100x100 floats and rgb is a std::vector of 3x256 floats.

Here is my drawing code:

glBegin(GL_QUADS);                      // Draw A Quad
        glTexCoord2f(0.0, 1.0); 
        glVertex3f(0.0, 1.0, 0.0);
        glTexCoord2f(1.0, 1.0); 
        glVertex3f(1.0, 1.0, 0.0)
        glTexCoord2f(1.0, 0.0); 
        glVertex3f(1.0, 0.0, 0.0);
        glTexCoord2f(0.0, 0.0);
        glVertex3f(0.0, 0.0, 0.0);
glEnd();

Do I need to call glTexCoord1f() for each vertex? These values are not used.

I am using Qt and QGLWidget in particular.

I am not seing anything. What could be wrong?

Some observations: If instead set gl_FragColor = texture2D(heatmap, gl_TexCoord[1].st); inside the fragment shader I see the red component correctly.

In the code above glGenTextures fails, but I still can see the red component as described above. If I move this call to just before glBindTexture it does not fail, but then I do not see anything!?

Andy
  • 3,251
  • 4
  • 32
  • 53
  • THere is a lot of code missing: your whole shader and uniform setup code. Also, it seems like you try to bind a 1D and a 2D texture to the same texture unit simultaneously - that is not goint ot work. – derhass Sep 27 '14 at 16:06
  • textures is declared as unsigned int[2]. The 2D texture is using textures[0] and the 1D texture is using textures[1]. It must be possible to bind a 2D texture and another 1D texture at the same time? – Andy Sep 27 '14 at 16:22
  • 1
    yes, but not on the same texture unit. – derhass Sep 27 '14 at 16:29
  • Sorry I do not understand. What is a texture unit? Is it the GLInts I get from glGenTextures? Do not a glBindTexture call unbind the last bound texture? If not how am I supposed to do this? – Andy Sep 27 '14 at 16:49
  • Having googled a bit it seems I am missing a call to glActiveTexture(GL_TEXTURE1); before glBindTexture(GL_TEXTURE_1D, textures[1]); Is that it? – Andy Sep 28 '14 at 08:09
  • 1
    well. that might be part of it. YOu also need to set your unifrom samplers accordingly. – derhass Sep 28 '14 at 12:24
  • Like this ? layout (binding = 0) uniform sampler2D heatmap; layout (binding = 1) uniform sampler1D colormap; – Andy Sep 28 '14 at 13:58
  • with quite recent GLSL, yes. But not with `#version 130` as you use currently. You will have to set the uniform value from the client side. – derhass Sep 28 '14 at 15:20
  • By passing the texture names textures[0] and textures[1] as uniform variables named heatmap and colormap? – Andy Sep 28 '14 at 17:01
  • 1
    no, you don't pass in the texture names to sampler varibales, by the texture unit index you want to sample from – derhass Sep 28 '14 at 17:13
  • You will get no speedup by using shaders only to display the heatmap. You need to push calculations there to get a speedup. Example: http://stackoverflow.com/a/39839788/895245 – Ciro Santilli OurBigBook.com Oct 03 '16 at 20:47

0 Answers0