glReadPixels使用缩放移动坐标

时间:2014-02-22 17:56:26

标签: c++ opengl coordinates glut glreadpixels

我试图通过使用带有C ++代码的glReadPixels选择两个点(相对的角)来读取像素矩形。选择Y轴值时会出现问题。

目前,我设法让glReadPixels工作直到缩放。当您缩放(进或出)时,该工具从其他Y值读取像素(X始终正常)

我做错了什么?

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(使用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(放大)

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;
    }
}

1 个答案:

答案 0 :(得分:0)

我设法找到解决方案。

替换:

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);

使用:

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

瞧瞧。它工作^^