Problems with gluUnProject

Started by
6 comments, last by ciscox83 14 years, 8 months ago
Hello everybody, I've a grid of Points and I can select it with the mouse. Now I want to change the y vale of the selected point; I'm trying to use glUnProject but the the value along the y axis is fake.. This is a piece of my code: <code> case WM_MOUSEMOVE: { [...] else if(test == MK_LBUTTON) { // arrays to hold matrix information glGetIntegerv(GL_VIEWPORT, viewport_pick); glGetDoublev(GL_PROJECTION_MATRIX, projection_pick); glGetDoublev(GL_MODELVIEW_MATRIX, model_view_pick); // get 3D coordinates based on window coordinates glReadPixels( mouse_x, viewport_pick[3]-mouse_y, 5, 5, GL_DEPTH_COMPONENT, GL_FLOAT, &mouse_z); gluUnProject( mouse_x, viewport_pick[3]-mouse_y, mouse_z, model_view_pick, projection_pick, viewport_pick, &pos3D_x, &pos3D_y, &pos3D_z); if(SOME_SELECTED) { for(int i = 0; i < 4; i++) { if(indexes != -1) { setGridVertexHeight(indexes, pos3D_y); } } } } </code> Someone could suggest me a solution? The y value result too small (e.g. 0.013). Could be a problem in the glReadPixels? Thanks anyway! Chri
Advertisement
I think you should separate the selecting/moving code from the windproc callback function. Maybe the matrices are reseted at that state.
And an other thing, you read 5x5 pixels, but maybe your mouse_z value is just a single float, so you overwrite memory, where you shouldn't, read 1x1, that's legal.

and <br><br>edit: and I don't see, where you convert the world x,y,z coordinates to index of the array? Or using openGl selecting mechanism?<br><br><!--EDIT--><span class=editedby><!--/EDIT-->[Edited by - szecs on August 17, 2009 2:41:00 AM]<!--EDIT--></span><!--/EDIT-->
First of all thanks for the answer and the suggestions :)

I call the selection function when left mouse button is pressed:

case WM_LBUTTONDOWN:{  mouse_x = LOWORD(lParam);            mouse_y = HIWORD(lParam);  Selection();}return 0;


This is a part of the selection()

void Selection() {	GLfloat fAspect;		// Space for selection buffer	static GLuint selectBuff[BUFFER_LENGTH];		// Hit counter and viewport storage	GLint hits, viewport[4];	// Set up selection buffer	glSelectBuffer(BUFFER_LENGTH, selectBuff);	// Get the viewport	glGetIntegerv(GL_VIEWPORT, viewport);	// Switch to projection and save the matrix	glMatrixMode(GL_PROJECTION);	glPushMatrix();	// Change render mode	glRenderMode(GL_SELECT);	// Establish new clipping volume to be unit cube around	// mouse cursor point (xPos, yPos) and extending two pixels	// in the vertical and horizontal direction	glLoadIdentity();	gluPickMatrix(mouse_x, viewport[3] - mouse_y + viewport[1], 3,3, viewport);	// Apply perspective matrix	fAspect = (float)viewport[2] / (float)viewport[3];	gluPerspective(45.0f, fAspect, 1.0, 425.0);	//Draw the scene	RenderScene();		// Collect the hits	hits = glRenderMode(GL_RENDER);	if (hits &gt; 1) {[...]	// Restore the projection matrix	glMatrixMode(GL_PROJECTION);	glPopMatrix();	// Go back to modelview for normal rendering	glMatrixMode(GL_MODELVIEW);}


Maybe you can see something of wrong?

Chri
I think you could easily debug the selection part by outputting the selected vertex index. It was long, since I used selecting, but I miss some things in your code, for exaple:
glInitNames();glPushName(0);//in selection function...glLoadName(i); // in render scene (maybe you have this) i: index of vertex

It was long ago, so try to get more info about using this.
And the 5x5 depth values loaded into a single float is still an issue I think
Hi,

I've corrected the 5x5 issue but my problem persist.

This is my render scene:

int RenderScene(void) {	// Clear the window with current clearing color	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Save the matrix state and do the rotations	glMatrixMode(GL_MODELVIEW);	glPushMatrix();[...]	DrawGrid();	// Initialize the names stack	glInitNames();	glPushName(0);[...]	// Restore the matrix state	glPopMatrix(); // Modelview matrix	return TRUE;}


As you can see

glInitNames();
glPushName(0);

are there.

In DrawGrid() i do the glLoadName().

The selection works fine, the problem is the glUnProject() that doesn't work. It give me value too small for the y axis.
Do you update the mouse's coordinates in WM_MOUSEMOVE while l_button is pressed?
If yes, than I have no idea.
glReadPixels could still be an issue depending on where the cursor read position is set. Make sure to at least use setCursorPos before reading pixels. Also you might need to adjust some read settings in GL.

Then I would recommend to create a test scenario with a rectangle and use glUnProject to create a ray from the mouse click and do an intersection test with the rectangle (google for ray-plane intersection test). Then you would get the actual intersection point in 3d which you can then gluProject to get the Z value you should actually also get from glReadPixels.

Then you can come back with the results which might point to what is going wrong.
------------------------------------I always enjoy being rated up by you ...
Quote:
Do you update the mouse's coordinates in WM_MOUSEMOVE while l_button is pressed?
If yes, than I have no idea.


Yes the coord are updated..I have no idea too

Quote:
I would recommend to create a test scenario


Ok I'll try..this is my last hope

Thanks guys

This topic is closed to new replies.

Advertisement