PDA

View Full Version : Terrain 3D position from mouse coordinates



glararan
08-09-2013, 09:46 AM
Hello,

I have big problem, I don't know why gluUnProject returning NaN values, so I trying to build own gluUnProject, but is failing.

Target platform: Crossplatform
Framework: Qt5
Library: QOpenGL, not QGL!
OpenGL ver: 4.0+
Current OS: Windows 8

So I’m trying to get terrain coords from my own camera class with QMatrix44, but it returns incorrets 3D values.
Im using in camera these properties:
viewMatrix, projectionMatrix and viewProjectionMatrix.
I tried to figure it out with this code:



QMatrix4x4 inverted = m_camera->projectionMatrix() * (m_camera->viewMatrix() * m_modelMatrix);
inverted = inverted.inverted();
float posZ;
// m_funcs = QOpenGLFuncions_4_0_Core
m_funcs->glReadPixels(mouse_position.x(), mouse_position.y(), 1, 1,GL_DEPTH_COMPONENT, GL_FLOAT, &posZ);
QVector4D clickedPointOnScreen(mouse_position.x(),mouse_posi tion.y(), posZ, 1.0f);
QVector4D clickedPointIn3DOrgn = inverted * clickedPointOnScreen;
terrain_pos = clickedPointIn3DOrgn.toVector3D();
qDebug() << terrain_pos;


I’m with camera on position: 250.0f, 10.0f, 250.0f
And I’m getting in result: QVector3D(414.76, 105.6, 6.37669) (cursor is in center of QGLWidget screen [935, 475], i calculated coords). It should be QVector3D(250.0f, 10.0f, 260f+-)

To calculated coords: I have fullHD = 1920x1080, but QGLWidget is only 1870x950
To QGLWidget: I'm using it because QtGui cannot have controls which I needed (making editor)

Edit: Camera is perspective mode!
Edit2: posZ is often 0.9xxx, maximum 1

GClements
08-09-2013, 06:17 PM
I have big problem, I don't know why gluUnProject returning NaN values,
It's probably dividing zero by zero, which probably indicates that the arrays being passed are incorrect.

so I trying to build own gluUnProject, but is failing.
You need to concatenate the viewport, projection and model-view transformations (in that order: V*P*M), invert the result, transform the window coordinates by the resulting matrix, then divide by w to convert from homogeneous to Euclidean coordinates.

Note that the Z coordinate needs to be converted from the 0..1 range returned by glReadPixels() to the -1..+1 range, either explicitly or as part of the viewport transformation (gluUnProject treats this as part of the viewport transformation). Also note that OpenGL window coordinates have (0,0) in the lower left while GUI toolkits typically report mouse coordinates with (0,0) at the upper left.

If gluUnProject() isn't working because you're feeding it incorrect data, feeding the same incorrect data to your own version won't work any better.

glararan
08-10-2013, 04:25 AM
Z coordinate now returning 5.67406e-39 That's bad right? Or not? (Changing every time u run app ex to: 1.28372e-38)
Yes viewport lower left is 0, 0

Currently coords at (935, 475) QVector3D(250, 9.99991, 249.8)
But if you will see screen it's still not correct. Link to image (http://www.sdilej.eu/pics/dada2eedeaf986817a83da22a3bba209.png). On screen its alot away! It should be QVector3D(250, 10, 230)


Final code: (Forum remove whitespaces :/ pastebin: http://pastebin.com/rReETsJC )

boolinvert=false;


QMatrix4x4viewMatrix=m_camera->viewMatrix();
QMatrix4x4modelViewMatrix=viewMatrix*m_modelMatrix ;
QMatrix4x4modelViewProject=m_camera->projectionMatrix()*modelViewMatrix;
QMatrix4x4inverted=m_viewportMatrix*modelViewProje ct; // m_viewportMatrix is from resizeGL from calculated glViewport


inverted=inverted.inverted(&invert);


floatposZ;
floatposY=(float)this->width()-mouse_position.y();


m_funcs->glReadPixels(mouse_position.x(),posY,1,1,GL_DEPTH_ COMPONENT,GL_FLOAT,&posZ);


QVector4DclickedPointOnScreen(mouse_position.x(),m ouse_position.y(),posZ,1.0f);
QVector4DclickedPointIn3DOrgn=inverted*clickedPoin tOnScreen;


clickedPointIn3DOrgn=clickedPointIn3DOrgn/clickedPointIn3DOrgn.w();


terrain_pos=clickedPointIn3DOrgn.toVector3D();


EDIT: when I remove in glReadPixels posY to mouse_position.y() it gives right coords when I arrive to position on nearClip, but from farClip it's still wrong :/ I think, here is problem with posZ

glararan
08-11-2013, 11:48 AM
Hi, BUMPING

I have solved why posZ returning wrong value, it was because posY was width() - mouse height!

But I have still trouble with final Vector3D result! Still Vector3D.Z returning wrong value -20f

Current source code: http://pastebin.com/TX4RYmNM

Currently QVector3D on [935, 475] = QVector3D(249.997, 10.0002, 216.095)
Should be QVector3D(250, 10, 230), I don't care about X, Y +- 0.0003

glararan
08-12-2013, 07:59 AM
New discovering...

When I rotate camera in axis X to left by 90 degrees, my cursor Z axis is now correct, but cursor X have incorrect values, same with axis Y, when I rotate camera on axis Y to -90 degrees I get -values.

Any help to this? What's wrong? Some calculation with Matrix? How it can be fixed? Same code as preview post.

This is my last problem, please let comment if you have any idea!

Thanks!