Dominick
Dominick

Reputation: 321

losing this after glReadPixels call

I have stored different informations in textures/images. I want to render them onto a FBO-texture and then read them out with glReadPixels. The whole image is split into different areas, I have 4 rows and 4 columns of information in this image and I want to store them on different Pointers.

std::vector<float*> data;

for (int i = 0; i < _resolution; ++i) {
    int posX = mod((float)i, (float)sqrt(_resolution));
    posX *= _resolution;
    int posY = ((float)i / _resolution);
    posY *= _resolution;

    float pixels[_resolution*_resolution*3];
    glReadPixels(posX, posY, posX + _resolution, posY + _resolution, GL_RGB, GL_FLOAT, pixels);

    for (int j = 0; j < _resolution*_resolution; ++j) {
        cout << "(" << pixels[j*3] << ", " << pixels[j*3+1] << ", " << pixels[j*3+2] << ")";
    }
    cout << endl;

    data.push_back(pixels);

}

The output on the console is just temporary for debugging.

This Functions is in a class A. Now at the second iteration (i = 1, posX = 16, posY = 0) after the call of glReadPixels I am losing the "this" the debugging console (I don't know the name, left bottom in Xcode) says for this, so the class A NULL. And after that the for loop throws an exception bad access error.

What is happening there?

Upvotes: 0

Views: 75

Answers (1)

user4442671
user4442671

Reputation:

glReadPixels(posX, posY, posX + _resolution, posY + _resolution, GL_RGB, GL_FLOAT, pixels);

glReadPixel's second and third arguments are width and height, not the top-right corner coordinate (as your code appears to assume). The consequence of this is that you are allocating a buffer too small for the amount of data returned by glReadPixel, leading to a buffer overrun.

Assuming that the arguments to glReadPixels are correct, you should be allocating a larger buffer:

float pixels[(posX+_resolution)*(posy+_resolution)*3];

or instead correct the call to glReadPixels itself:

glReadPixels(posX, posY, _resolution, _resolution, GL_RGB, GL_FLOAT, pixels);

Upvotes: 3

Related Questions