The following piece of code gives me an error and I'm not able to understand it.
GLdouble objX,objY,objZ;
GLdouble modelview1[4][4], projection1[4][4];
GLint viewport1[4];
glGetDoublev( GL_MODELVIEW_MATRIX, *modelview1 );
glGetDoublev( GL_PROJECTION_MATRIX, *projection1 );
glGetIntegerv( GL_VIEWPORT, viewport1 );
gluUnProject(x, y, z, modelview1, projection1, viewport1, objX , objY , objZ);`
The error :
error C2664: 'gluUnProject' : cannot convert parameter 4 from 'GLdouble [4][4]' to 'const GLdouble []'
It seems that variables modelview1 and projection1 have wrong type.
I found following snippet (here http://www.3dkingdoms.com/selection.html ):
// This function will find 2 points in world space that are on the line into the screen defined by screen-space( ie. window-space ) point (x,y)
double mvmatrix[16];
double projmatrix[16];
int viewport[4];
double dX, dY, dZ, dClickY; // glUnProject uses doubles, but I'm using floats for these 3D vectors
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
dClickY = double (g_WindowHeight - y); // OpenGL renders with (0,0) on bottom, mouse reports with (0,0) on top
gluUnProject ((double) x, dClickY, 0.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);
ClickRayP1 = Vector3 ( (float) dX, (float) dY, (float) dZ );
gluUnProject ((double) x, dClickY, 1.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);
ClickRayP2 = Vector3 ( (float) dX, (float) dY, (float) dZ );
Related
I'm trying to move objects with the mouse using the Gluunproject method with openGL 2.1 , but i'm really struggling here; here's the code i wrote :
int viewport[4];
double modelview[16],
projection[16],
X1, Y1, Z1;
double realY;
GLfloat depth[2];
for(_compt=_OjebctScene.begin();_compt!=_OjebctScene.end();_compt++)
{
if ((*_compt)->IsSelected())
{
GLdouble mouseX=event.x;
GLdouble mouseY=event.y;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
realY = viewport[3] - (GLint) mouseY - 1;
glReadPixels(mouseX, realY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
gluUnProject(mouseX, realY, 0, modelview, projection, viewport, &X1, &Y1, &Z1);
(*_compt)->setX(X1);
(*_compt)->setY(Y1);
(*_compt)->setZ(Z1);
}
}
I use a loop to check all the objects on the scene (I've pushed them into a vector) , then when i find the selected object , i try to move it using the mouse.
I then set the coordinates of my object to the position of the mouse in the 3D space (X1 , Y1 , Z1); but this doesn't really work.
I think I've fixed it just by passing the depth[0] to the gluunproject method.
here's what I've done , change this :
gluUnProject(mouseX, realY, 0, modelview, projection, viewport, &X1, &Y1, &Z1);
by this :
gluUnProject(mouseX, realY,depth[0], modelview, projection, viewport, &X1, &Y1, &Z1);
I don't understand what this means though , if someone could explain it to me it would be nice.
i tried almost everything, copied a lot of codes from different forums but nothing works. What the issue.
I have loop where i draw object from vector. i want to create object where user clicked in 3D space. This is code where i'm trying to do this:
if(glfwGetKey( window, GLFW_KEY_F ) == GLFW_PRESS && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)==GLFW_PRESS && check==false) {
double x, y;
glfwGetCursorPos(window, &x, &y);
double mvmatrix[16];
double projmatrix[16];
int viewport[4];
double dX, dY, dZ, dClickY; // glUnProject uses doubles, but I'm using floats for these 3D vectors
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
dClickY = double (1080 - y); // OpenGL renders with (0,0) on bottom, mouse reports with (0,0) on top
gluUnProject ((double) x, dClickY, 0.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);
bricks.push_back(new Cube(0,0,0,1110));
bricks[bricks.size()-1]->translate(dX, dY, dZ);
check=true;
}
After that operation dX, dY and dZ is -1.#IND. What am i doing wrong?
I have a 2D application which is an image viewer.
I have the ability to pan around the image in the window and I have a basic zoom.
Now I'd like to improve the zoom so it zoom to the point under the mouse.
So far I've read everything possible on google and i've come close to something working,
but still can't find something working, here is what I got:
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
// w and h are respectivly the width and height of the window
// newzoom_pos is the point in openGL coordinates where the user requested the zoom
// zoomFactor is between [0.1,10.0] , 0.1 -> 1.0 means downscale, 1.0 -> 10.0 means upscale
// transX and transY are used to pan around the image
float left = (0.f+transX -newzoom_pos.x())/zoomFactor +newzoom_pos.x();
float right = (w+transX -newzoom_pos.x())/zoomFactor +newzoom_pos.x();
float bottom = (h-transY-newzoom_pos.y())/zoomFactor +newzoom_pos.y();
float top = (0.f-transY -newzoom_pos.y())/zoomFactor +newzoom_pos.y();
glOrtho(left, right, top, bottom, -1, 1);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
It almost works, but it is not okay, it doesn't zoom exactly to the requested point and when the image is small and does not fit yet in the viewer entirely ( there's black around), well it is messy and does not work...
btw, here is the code when the user zoom-in, in the mouse wheel event handler:
zoomFactor+=0.1;
QPoint oglpos = openGLpos(new_zoomPoint.x(), new_zoomPoint.y());
this->newzoom_pos = oglpos;
the openGLpos function is the following:
QPoint ViewerGL::openGLpos(int x,int y){
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX=0, winY=0, winZ=0;
GLdouble posX=0, posY=0, posZ=0;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (float)x;
winY = viewport[3]- y;
if(winY == 0) winY =1.f;
glReadPixels( x, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
GLint b=gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
if(b==GLU_FALSE) cout << "failed unprojection" << endl;
return QPoint(posX,posY);
}
Could anybody help me through this?
I am trying to use gluUnProject to get my mouse coordinates into world coordinates, however it seems to not be working, or I am just misunderstanding the functionality of the glUnProject function, here is the code I am working with, my matrices all check out fine and as for the -300 on the mouse x coordinate, I am using a C++ Win32 Dialog and the ScreenToClient is giving me funky results.
int appWidth = CApplication::GetInstance()->GetWidth();
int appHeight = CApplication::GetInstance()->GetHeight();
float fAspect = (float)appWidth / (float)appHeight;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, fAspect, 0.1f, 100000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(m_vecCamera.x, -m_vecCamera.y, m_vecCamera.z);
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
glEnable(GL_DEPTH);
//Retrieve the Model/View, Projection, and Viewport Matrices
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
//Retrieve the Mouse X and the flipped Mouse Y
winX = (float)pInput->msg.param1-300.0f;
winY = (float)viewport[3]-(float)pInput->msg.param2;
glReadPixels( int(winX), int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
This is however giving me coordinates relative to the center of my screen, and I am assuming is relative to my camera, I also tried implementing my own function
Vector2f MouseUnProject(int x, int y)
{
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
glEnable(GL_DEPTH);
//Retrieve the Model/View, Projection, and Viewport Matrices
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
//Retrieve the Mouse X and the flipped Mouse Y
winX = (float)x;
winY = (float)viewport[3]-y;
glReadPixels( int(winX), int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
double projectionX, projectionY;
double viewX, viewY;
double worldX, worldY;
//Convert from Screen Coords to Projection Coords
projectionX = (double)winX / ((double)viewport[2]/2.0) - 1.0;
projectionY = (double)winY / ((double)viewport[3]/2.0) + 1.0;
//Convert from projection Coords to View Coords
viewX = projectionX * modelview[14];
viewY = projectionY * modelview[14];
//Convert from View Coords to World Coords
worldX = viewX + modelview[12];
worldY = viewY - modelview[13];
return Vector2f(worldX, worldY);
}
It works to a certain mount, but when moving the camera, the numbers instantly go off a bit, the conversion from projection to view coords 'seems' to be ok, and the projection coords are definitely good.
I would really prefer to use glUnProject rather then my own function, but I can't get it to work for the life of me and all of the google searches I found don't seem to answer my question. What exactly does the GL documentation mean by 'object space' perhaps my understanding of that is wrong, and if so what do I additionally have to do to get my coordinates in the right space?
was posted a year ago,but anyways....so you are getting coordinates relative the the screen because you made a call to gluPerspective. this call internally calls glfrustum which will generate normalized coordinates in the range {-1, 1}. However if you called glfrustum directly with your near/far values you would have got the result from gluUnproject in that range.
To get back to your map editor coordinates, simply take the result from gluUnproject and manually range convert back to your editor coordinate system, ie{-1,1} => {0, max}
To get started you should test gluUnProject by inputting (0,0), (midX, midY), (maxX, maxY) and the result from gluUnProject should be (-1, -1, depth), (0, 0, depth) and (1, 1, depth). If you setup the projection matrix using glFrustum then the above result will be returned in the near/far range.
I have this view set:
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
and I get a screen position (sx, sy) from a mouse click.
Given a value of z, how can I calculate x and y in 3d-space from sx and sy?
You should use gluUnProject:
First, compute the "unprojection" to the near plane:
GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
GLdouble x, y, z;
gluUnProject(sx, viewport[1] + viewport[3] - sy, 0, modelMatrix, projMatrix, viewport, &x, &y, &z);
and then to the far plane:
// replace the above gluUnProject call with
gluUnProject(sx, viewport[1] + viewport[3] - sy, 1, modelMatrix, projMatrix, viewport, &x, &y, &z);
Now you've got a line in world coordinates that traces out all possible points you could have been clicking on. So now you just need to interpolate: suppose you're given the z-coordinate:
GLfloat nearv[3], farv[3]; // already computed as above
if(nearv[2] == farv[2]) // this means we have no solutions
return;
GLfloat t = (nearv[2] - z) / (nearv[2] - farv[2]);
// so here are the desired (x, y) coordinates
GLfloat x = nearv[0] + (farv[0] - nearv[0]) * t,
y = nearv[1] + (farv[1] - nearv[1]) * t;