Jump to content
  • Advertisement
Sign in to follow this  
the_cyberlord

Drawing at mouse pos. with gluUnproject

This topic is 5456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

http://users.skynet.be/cyber-lord/engine.cpp The unprojecting of the mouse coordinates should come at the place of the /*MOUSE TO WORLD COORDINATES HERE (with w_mouse_x, y, z as world coords)*/ text. I tried to apply the same gluUnproject I previously used ( http://users.skynet.be/cyber-lord/engine2.cpp ), but it doesn't show the right thing when going to the position of the mouse (the triangle shows at an other depth I think) I tried everything I could think of, and nothing worked. Can someone please help me? Really thanks! Adriaan [Edited by - the_cyberlord on October 7, 2004 11:26:01 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:

gluUnProject(x,viewport[3]-y,0,modelview,projection,viewport,&w_mouse_x,&w_mouse_y,&w_mouse_z);


You know, I've seen this mistake many times from people that use this function without knowing how it works. gluUnProject isn't a magical function that takes a 2D coordinate and transforms it into 3D. How on earth could it generate the depth component out of nothing? As the MSDN states, it just "maps window coordinates to object coordinates". This means you have to supply a value for winz, not 0.

gluUnproject can tell you a pixel's position in 3D if you have already rendered an object there. That means that there is a depth value on the zbuffer, which you can read and pass it to gluUnProject().

Share this post


Link to post
Share on other sites
I've read the function explaination many times you know,...
But it didn't help too much, like you see.
OK, what if I don't have a ZBuffer value for that spot? How can you make it so that the w_mouse_z value allways gets to for example -10?
and I forget something: http://users.skynet.be/cyber-lord/engine.cpp is the new one I'm making, and engine2.cpp the old one.

Share this post


Link to post
Share on other sites
You mean always getting a 3D point at the plane that lies in z=-10, right?

I'm not sure,but you could try that:

Use:
gluProject(0,0,-10,modelview,projection,viewport,&wincoord.x,&wincoord.y,&wincoord.z).

That gets the window coordinates of the point (0,0,-10), which lies on that plane(z=-10).

So, call gluUnproject as you do, but pass winz=wincoord.z

Share this post


Link to post
Share on other sites
Now the problem is: when I click with the mouse for the first time in the window, the triangle jumps to the mouse position.
But the times after that, it displays at strange places... but still at the right depth... what could be the problem here? I can't see anything wrong.

// THE MAIN DRAWING FUNCTION
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;


obj::triangle tri;
// THE MAIN DRAWING FUNCTION
GLvoid DrawGLScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the sreen & depth buffer
glLoadIdentity(); // View reset command
glPushMatrix(); // View reset command
//glTranslatef(0,0.0,-10);

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

winX = (float)mouse_x;
winY = (float)viewport[3] - (float)mouse_y;

glReadPixels( mouse_x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &w_mouse_x, &w_mouse_y, &w_mouse_z);
//
tri.draw(); // Draw the triangle
if (mouse[0] == true ) {
tri.jump(w_mouse_x,w_mouse_y,w_mouse_z); }

glutSwapBuffers(); //Swap the display buffers
} //Blabla

Share this post


Link to post
Share on other sites
You probably need to translate your triangle back to (0,0,0) before translating it to the position of the mouse depending on how you implemented it.

Share this post


Link to post
Share on other sites
1)The use of glReadPixels() has no meaning. You just get the value you used to clear the depth buffer.

2)I don't see any glTranslatef() calls which should be use to position the triangle.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!