Advertisement

dragging object under a mouse

Started by January 19, 2001 09:04 AM
9 comments, last by ogl 23 years, 9 months ago
Hi I have the following code for dragging object under the mouse - it isn't working at the moment - Edited by - ogl on January 24, 2001 7:17:34 AM
for dragging objects i go
first youve gotta select an object eg with picking.

and then every frame
after everything has been drawn (important or using glReadPixels is gonna give u the wrong depth value of the point under the cursor)
see what the window coords of the cursor are
use readpixels to read that points depth value
use that in gluUnProject(winx,winy,winz,....
u got winz from readpixels

http://members.xoom.com/myBollux
Advertisement



THANKYOU!THANKYOU!THANKYOU!THANKYOU!





That function just saved me alot of time and work!!! Thanks!!!





THANKYOU!THANKYOU!THANKYOU!THANKYOU!





Edited by - smart_idiot on January 22, 2001 11:40:19 AM
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I would have said this sooner but I couldn''t.

the line that reads:

glGetDoublev(GL_MODELVIEW_MATRIX, projMatrix

should read:

glGetDoublev(GL_PROJECTION_MATRIX, projMatrix


Other than that, I still am unsure what to use for the z-value for the unprojection thing. I''m sure reading the z-value from the buffer will work for ogl, but I want say, 20 units into the screen. The z value should be between 0 and 1 I think, to specify a value between zmin and zmax, but I don''t know how to convert it. Could you please help me?

What happens to z during projection?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
using glReadPixels(..) with GL_DEPTH_COMPONENT and a format of GL_FLOAT will return a z value of between 0 and 1

heres some windows code

  POINT pt;	GetCursorPos(&pt);		GLint viewport[4];	GLdouble mvmatrix[16], projmatrix[16];	GLint realy;  //  OpenGL y coordinate position  	GLdouble wx, wy, wz;  //  returned world x, y, z coords  	glGetIntegerv (GL_VIEWPORT, viewport);    glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);    glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);//  note viewport[3] is height of window in pixels  	realy = viewport[3] - (GLint) pt.y - 1;	float depth;	glReadPixels(pt.x,realy,1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);    gluUnProject ((GLdouble) pt.x, (GLdouble) realy, depth, mvmatrix, projmatrix, viewport, &wx, &wy, &wz); 	cursor_world_coords = VECTOR(wx,wy,wz);  


http://members.xoom.com/myBollux
quote: Original post by zedzeek

using glReadPixels(..) with GL_DEPTH_COMPONENT and a format of GL_FLOAT will return a z value of between 0 and 1



would this be for orthogonal view?
Advertisement
ogl:

No, I have it working with perspective.

zedzeek:

I knew how to read the depth from the buffer, but I want to be able to draw points for mesh on the screen in a perspective view that can be rotated around. The camera is translated units behind the origin, and I want to draw on an invisible plane that intersects the origin and is parellel to the screen. I therefore need to figure out what to use with gluUnProject to get z units after gluUnproject undoes the projection matrix but before it undoes the model matrix, so need to know what happens to the z value during projection. How does OpenGL convert z to a number between 0 and 1?

I tried just drawing a polygon to represent my plane, read the depth from that, and clear the buffer, but I think it reads from the frame being displayed because it didn''t work. I don''t know why. I selected the GL_BACK for both drawing and reading.

I am very confused.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
smart_idiot

how did you get the mouse dragging to work - can you explain

zedzeek

what is

cursor_world_coords declared as ?

Edited by - ogl on January 24, 2001 10:06:25 AM
readpixels reads pixels from the screen buffer (its got nothing to do with ortho or perspective viewing matrices)
cursor_world_coords = a VECTOR which is a point with x,y,z real numbers

>>I tried just drawing a polygon to represent my plane, read the depth from that, and clear the buffer, but I think it reads from the frame being displayed because it didn''t work. I don''t know why. I selected the GL_BACK for both drawing and reading.<<

did u try a flush first

also i might add trying to position 3d points on a 2d screen exactly where u want them is practically impossible, try it with a modelling program



http://members.xoom.com/myBollux
I didn''t get dragging to work, just the cords to do it, but I figured a little math could do it, ie object.x = mouse.x - old_mouse.x.

Maybe you problem is that when you try to move the object, it gets moved in front of where it was in the old frame, makeing it fly off behind you. Does the thing you try to move do that? If so, try drawing everything else first, read the buffer, then draw the object. Other than that, I have no idea why it doesn''t work for you.

Sorry I couldn''t be of more help to you. You helped me more actually by introducing my to the gluUnProject function. I was trying to do that my self.

zeedzeek:

I got it to work, but I''m just clearing the depth buffer and tranparently drawing a quad for the plane. It''s not as accurate as I wanted it, but its working. Thanks.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement