Question

I'm trying to read a rectangle of pixels by selecting two points (opposite corners) using glReadPixels with C++ code. The problem comes out when selecting the Y-Axis value.

At the moment, I manage to get the glReadPixels work until zooming. When you zoom (in or out), the tool reads pixels from other Y values (the X are always OK)

What am I doing wrong?

GLORTHO:

IZDA = mCameraPosition.x - ((double)(rectangleDim.x/SCRARatio)) * mZoomFactor / 2;
DCHA = mCameraPosition.x + ((double)(rectangleDim.x/SCRARatio)) * mZoomFactor / 2
ABAJO = mCameraPosition.y - ((double)rectangleDim.y) * mZoomFactor / 2;
ARRIBA = mCameraPosition.y + ((double)rectangleDim.y) * mZoomFactor / 2;

glOrtho(IZDA, DCHA, ABAJO, ARRIBA, -1.0f, 1.0f);

GETSCREENCOORDS

realDim.x = dimension.x * (dimension.x * mZoomFactor / 2);
realDim.y = dimension.y * (dimension.y * mZoomFactor / 2);

GSC.x = (dimension.x*(x-mCameraPosition.x)) / realDim.x;
GSC.y = (dimension.y/realDim.y) * ((pow(dimension.y, 2)*mZoomFactor/2)+mCameraPosition.y + y - imgDim.y);

GETWORLDCOORDS

realDim.x = dimension.x * (dimension.x * mZoomFactor / 2);
realDim.y = dimension.y * (dimension.y * mZoomFactor / 2);

GWC.x =
    ((x * realDim.x) / dimension.x) + mCameraPosition.x;

GWC.y =
    ((y * realDim.y) / dimension.y)
    -((dimension.y * (dimension.y * mZoomFactor/2)) + mCameraPosition.y)
    + imgDim.y;

CHECKFILLVALUE (Using glReadPixels)

ComplexData::Point a, b;

// Set points screen coordinates
getScreenCoords(FIP.x, FIP.y);
a.x = GSC.x;
a.y = GSC.y;
getScreenCoords(FEP.x, FEP.y);
b.x = GSC.x;
b.y = GSC.y;

// Set length and width to read (and write)
double RX_Length, RY_Length;
if (b.x < a.x) RX_Length = a.x - b.x;
else RX_Length = b.x - a.x;
if (b.y < a.y) RY_Length = a.y - b.y;
else RY_Length = b.y - a.y;

// Pixel data vector
float *tdata = new float[3*RX_Length*RY_Length];

double desp = (mZoomFactor*dvcamera)/dvzoom;

getScreenCoords(min(FIP.x, FEP.x), gHeight-FIP.y-2*(mCameraPosition.y-desp));
// Reading from the GET SCREEN COORDINATES global values (GSC)
glReadPixels(GSC.x, GSC.y, RX_Length, RY_Length, GL_RGB, GL_FLOAT, tdata);  // <---     Works great until zooming

glRasterPos2d(min(FIP.x, FEP.x), gHeight-max(FIP.y, FEP.y));
glDrawPixels(RX_Length, RY_Length, GL_RGB, GL_FLOAT, tdata);    // <--- Works great
delete[] tdata;

MOUSEWHEEL (Zoom)

Drawing::Point pt = e->Location;
getWorldCoords(pt.X, pt.Y);

Initial_Zoom.X = rectangleDim.x - GWC.x;
Initial_Zoom.Y = GWC.y;

// ZOOM IN
if (e->Delta > 0){
    if (mZoomFactor > float::Epsilon * 1000.0f){
        mZoomFactor /= 1.15f;
    }
}
// ZOOM OUT
else {
    if (mZoomFactor < float::MaxValue / 1000.0f){
        mZoomFactor *= 1.15f;
    }
}
Was it helpful?

Solution

I manage to find the solution.

Replace:

getScreenCoords(min(FIP.x, FEP.x), gHeight-FIP.y-2*(mCameraPosition.y-desp));
glReadPixels(GSC.x, GSC.y, RX_Length, RY_Length, GL_RGB, GL_FLOAT, tdata);

With:

getScreenCoords(std::min(FIP.x, FEP.x), FIP.y);
glReadPixels(GSC.x, rectangleDim.y - GSC.y, RX, RY, GL_RGB, GL_FLOAT, tdata);

And voilá. It works ^^

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top