Loading a texture by passing RGB floating point data to glTexImage2D

I have a 2D floating point array with values ​​displayed in the range 0.0f to 1.0f. I am uploading it to OpenGL using one ie channel

glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_FLOAT, data);

which works great. However, I am trying to add two more channels to eventually convert them to RGB colors as shown below:

// Get the current grid
Array2D<real_t> currentGrid = m_Continuum->getCurrentGrid();
float R, G, B;

std::vector<GLfloat> image;
image.resize(currentGrid.size()*3, 0.0f);

for (size_t i = 0; i != currentGrid.sizeWd(); ++i)
{
    for (size_t j = 0; j != currentGrid.sizeHt(); ++j)
    {
        // I have to switch i and j in currentGrid due to the way the Array2D type is layed out
        image[( i * currentGrid.sizeWd() + j) + 0] = currentGrid(j, i);
        image[( i * currentGrid.sizeWd() + j) + 1] = currentGrid(j, i);
        image[( i * currentGrid.sizeWd() + j) + 2] = currentGrid(j, i);
    }
}

      

Now when I update and use glTexImage2D:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT,data);

      

the image is distorted as shown in the below comparison: Single Channel Image

RGB Image

My texture loading function is below. I believe I have used the correct pixel and texture mapping settings.

bool Texture::load(const size_t width, const size_t height, const float *data)
{
    glPixelStoref(GL_UNPACK_ALIGNMENT, 1);

    // Allocate a texture name
    //glGenTextures(1, &m_TextureObj); called in another function

    // select current texture
    glBindTexture(GL_TEXTURE_2D, m_TextureObj);

    // select replace to ensure texture retains each texel colour
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    // if texture wraps over at the edges
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT,data);

    glGenerateMipmap(GL_TEXTURE_2D);

    m_Loaded = true;
    return true;
}

      

Any ideas on what I could try? Here's what I looked at:

  • Here is similar to a question on this topic, which I have tried but to no avail.
  • May I suggest that there may be a problem with the composition of the data I am passing in, like question . I've tried this, but it mostly bleeds the single channel image into the left third of the texture image area.

EDIT:

Updated resulting image based on @ Podgorskiy answer by changing

( i * currentGrid.sizeWd() + j) + c

      

to

( 3 * i * currentGrid.sizeWd() + j) + c

      

where c is the channel 0,1,2. The result is below: RGB Image updated result

+3


source to share


1 answer


You are accessing the elements by index, which you calculate as:

( i * currentGrid.sizeWd() + j) + c

      

Where c is a channel, and it takes the values: 0, 1, 2.



However, this expression may not be correct. It follows from this expression that two adjacent texels have an offset of one float, but must have an offset of three floats. Try this instead:

3 * ( i * currentGrid.sizeWd() + j) + c

      

+3


source







All Articles