Texture mapping using 1d texture with OpenGL 4.x

I want to use 1d texture (color frame) for a simple triangle texture.

enter image description here

My fragment shader looks like this:

#version 420

uniform sampler1D           colorRamp;
in float height;

out vec4 FragColor;

void main()
{
   FragColor = texture(colorRamp, height).rgba;
}

      

My vertex shader looks like this:

#version 420

layout(location = 0) in vec3 position;

out float height;

void main()
{
    height = (position.y + 0.75f) / (2 * 0.75f);
    gl_Position = vec4( position, 1.0);
}

      

When drawing the triangle, I continue this path (I removed the error checking form code for better readability):

glUseProgram(program_);

GLuint samplerLocation = glGetUniformLocation(program_, name.toCString());
glUniform1i(samplerLocation, currentTextureUnit_);

glActiveTexture( GL_TEXTURE0 + currentTextureUnit_ );
glBindTexture(GL_TEXTURE_1D, textureId);

      

Unfortunately my triangle is only black. Any ideas how to solve this problem?

I'm pretty sure the assigned texture is filled with the proper data, since I read it with glGetTexImage

and validated it. Do I need to call other functions to bind and activate the texture? I have pretty much the same code for 2d textures that works.

When I change my shader code like this:

#version 420

uniform sampler1D           colorRamp;
in float height;

out vec4 FragColor;

void main()
{
   FragColor = vec4(height, height, height, 1.0); // change!
}

      

I am getting this output: enter image description here

I have prepared some demo source code that reproduces the error. The source can be found here . It generates the following output:

enter image description here

1d texture is created this way - with only red pixels:

static GLuint Create1DTexture()
{
    GLuint textureId_;

    // generate the specified number of texture objects 
    glGenTextures(1, &textureId_);
    assert(glGetError() == GL_NO_ERROR);

    // bind texture
    glBindTexture(GL_TEXTURE_1D, textureId_);
    assert(glGetError() == GL_NO_ERROR);

    // tells OpenGL how the data that is going to be uploaded is aligned
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    assert(glGetError() == GL_NO_ERROR);

    float data[] =
    {
        1.0f, 0.0f, 0.0f, 1.0f,
        1.0f, 0.0f, 0.0f, 1.0f,
        1.0f, 0.0f, 0.0f, 1.0f
    };

    glTexImage1D(
        GL_TEXTURE_1D,      // Specifies the target texture. Must be GL_TEXTURE_1D or GL_PROXY_TEXTURE_1D.
        0,                  // Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap reduction image.
        GL_RGBA32F,
        3*sizeof(float)*4,
        0,                  // border: This value must be 0.
        GL_RGBA,
        GL_FLOAT,
        data
    );
    assert(glGetError() == GL_NO_ERROR);

    // texture sampling/filtering operation.
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    assert(glGetError() == GL_NO_ERROR);
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    assert(glGetError() == GL_NO_ERROR);
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    assert(glGetError() == GL_NO_ERROR);
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    assert(glGetError() == GL_NO_ERROR);

    glBindTexture(GL_TEXTURE_1D, 0);
    assert(glGetError() == GL_NO_ERROR);

    return textureId_;
}

      

Linking a texture is done as follows:

GLuint samplerLocation = glGetUniformLocation(rc.ToonHandle, "ColorRamp");
glUniform1i(samplerLocation, 0);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_1D, rc.texure1d);

      

+3


source to share


1 answer


Found the error:



glTexImage1D(
    GL_TEXTURE_1D,      // Specifies the target texture. Must be GL_TEXTURE_1D or GL_PROXY_TEXTURE_1D.
    0,                  // Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap reduction image.
    GL_RGBA32F,
    3/*3*sizeof(float)*4*/,  // bug fix!!!!
    0,                  // border: This value must be 0.
    GL_RGBA,
    GL_FLOAT,
    data
);

      

+1


source







All Articles