Hello there,
i would like to draw the grayscale color 255 as red and i would like to do this with a FragementShader, but it dosen’t work.
I define a lookup texture like this:
glGenTextures(1, &m_tex[0]);
glBindTexture(GL_TEXTURE_1D, m_tex[0]);
// setup texture
glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
float colors[256][3];
for (int i = 0; i < 256; i++)
{
if (i == 255)
{
colors[i][0] = i / 255.;
colors[i][1] = 0.;
colors[i][2] = 0.;
}
else
{
colors[i][0] = i / 255.;
colors[i][1] = i / 255.;
colors[i][2] = i / 255.;
}
}
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0 , GL_RGB, GL_FLOAT, colors);
After this i create my 2D image grayscale texture:
// texture erstellen
glGenTextures(1, &m_tex[1]);
glBindTexture(GL_TEXTURE_2D, m_tex[1]);
// setup texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// copy image pixel to texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_gray.width(), m_gray.height(), 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, m_gray.bits());
Then i define the shaders Fragment and Vertex:
const char *vsrc =
"void main(void)
"
"{
"
" gl_Position = ftransform();
"
"gl_TexCoord[0] = gl_MultiTexCoord0;
"
"}
";
const char *fsrc =
"uniform sampler2D sample;
"
"uniform sampler1D lookup;
"
"void main(void)
"
"{
"
" gl_FragColor = texture1D(lookup, texture2D(sample,gl_TexCoord[0].st).r);
"
"}
";
Now i use the Qt QGLWidget to draw the texture. I compile now the
code and bind it to the card.
m_program = new QGLShaderProgram(this);
m_program->addShader(vshader);
m_program->addShader(fshader);
m_program->link();
m_program->setUniformValue("lookup", m_tex[0]);
m_program->setUniformValue("sample", m_tex[1]);
m_program->bind();
And this is my draw function:
paintGL()
{
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
// clockwise
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, 1.0f); // oben links
glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, 1.0f); // oben rechts
glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, -1.0f); // unten rechts
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, -1.0f); // unten links
glEnd();
glDisable(GL_TEXTURE_2D);
}
Can anyone help me what did i wrong? I see only the 1D lookup texture in my Widget and the color which should be red is black.
Best regards,
Treehouse