gpt4 book ai didi

c++ - glReadPixels 和 gluUnProject 无法正常工作

转载 作者:行者123 更新时间:2023-11-28 04:33:03 24 4
gpt4 key购买 nike

我正在尝试使用 glReadPixelsgluUnProject 获取真实坐标,但它不起作用。

void GLWidget::resizeGL(int Width, int Height)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0, 100.0, -100.0, 100.0, -100.0, 100.0);
glViewport(0, 0, (GLint)Width, (GLint)Height);

currentWidth = Width;
currentHeight = Height;
}

...

void GLWidget::plotAxis()
{
GLdouble xc,yc,zc;

glPointSize(7.0);
// Test points
glColor3f(0, 0, 0);
glBegin(GL_POINTS);
glVertex3f(30, 10, 0);
glVertex3f(40, 10, 10);
glVertex3f(50, 10, -10);
glEnd();

}
...
void GLWidget::pickGeometry()
{

makeCurrent();
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX = 0.0, winY = 0.0, winZ = 0.0;
GLdouble posX = 0.0, posY = 0.0, posZ = 0.0;

glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);

this->cursorX=QCursor::pos().x();
this->cursorY=QCursor::pos().y();

winX = cursorX;
winY = viewport[3] - cursorY;

// obtain Z position
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

// obtain global coordinates
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

qDebug() << "win XYZ" << winX << winY << winZ;
qDebug() << "pos XYZ" << posX << posY << posZ;
}

void GLWidget::mousePressEvent(QMouseEvent *event)
{
pressPosition = event->pos();
if(event->button()==Qt::LeftButton)
{
pickGeometry();

}
}

点击点 30, 10, 0 给出

win XYZ 1190 53 0
pos XYZ 137.762 -82.3627 100

点击点 40, 10, 10 给出

win XYZ 1239 51 0
pos XYZ 147.552 -83.0283 100.

你能指出我错在哪里吗?

最佳答案

QCursor::pos()提供的坐标是屏幕坐标。
参见 QPoint QCursor::pos() :

Returns the position of the cursor (hot spot) of the primary screen in global screen coordinates.

但是,QMouseEvent::pos() 提供的坐标是相对于小部件的坐标。
参见 QPoint QMouseEvent::pos() const :

Returns the position of the mouse cursor, relative to the widget that received the event.

这意味着您必须使用鼠标事件接收到的位置而不是 QCursor::pos() 来解决您的问题:

void GLWidget::mousePressEvent(QMouseEvent *event)
{
pressPosition = event->pos();

.....
}

void OGLWidget::pickGeometry()
{
makeCurrent();

GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);

cursorX = pressPosition.rx(); // <-- pressPosition instead of QCursor::pos()
cursorY = pressPosition.ry();

GLfloat winX = 0.0, winY = 0.0, winZ = 0.0;
winX = (GLfloat)cursorX;
winY = (GLfloat)(viewport[3] - cursorY);

// obtain Z position
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

// obtain global coordinates
GLdouble posX = 0.0, posY = 0.0, posZ = 0.0;
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

qDebug() << "win XYZ" << winX << winY << winZ;
qDebug() << "pos XYZ" << posX << posY << posZ;
}

关于c++ - glReadPixels 和 gluUnProject 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52365624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com