glReadPixels 移动坐标与缩放
Posted
技术标签:
【中文标题】glReadPixels 移动坐标与缩放【英文标题】:glReadPixels moving coordinates with zoom 【发布时间】:2014-02-22 17:56:26 【问题描述】:我正在尝试通过使用带有 C++ 代码的 glReadPixels 选择两个点(对角)来读取像素矩形。选择 Y 轴值时出现问题。
目前,我设法让 glReadPixels 工作直到缩放。当您放大(放大或缩小)时,该工具会从其他 Y 值读取像素(X 始终可以)
我做错了什么?
荣耀:
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;
鼠标滚轮(缩放)
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】:我设法找到了解决方案。
替换:
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);
然后瞧。有效^^
【讨论】:
以上是关于glReadPixels 移动坐标与缩放的主要内容,如果未能解决你的问题,请参考以下文章
求一个C语言图形变换程序,先进行图形绘制,后能进行图形复制、移动、缩放、旋转,跪求啦。。。