Jump to content
  • Advertisement
Sign in to follow this  
Marianne

3D picking

This topic is 4918 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

i've changed the method code to this:


GLdouble x=0;
GLdouble y=0;
GLdouble z=0;
GLdouble model[16]; glGetDoublev(GL_MODELVIEW,model);
GLdouble proj[16]; glGetDoublev(GL_PROJECTION,proj);
GLint viewport[4]; glGetIntegerv(GL_VIEWPORT,viewport);

for(int i=0; i<4; i++) cout << "viewport [" << i << "] = " << viewport << endl;
for(int i=0; i<16; i++) cout << "model [" << i << "] = " << model << endl;
for(int i=0; i<16; i++) cout << "proj [" << i << "] = " << proj << endl;

cout << "# Unprojecting win x: " << winX << " win y: " << winY<< " win z: " << 1 << endl;

if(!gluUnProject(winX, winY, (GLdouble)1.0,
model, proj, viewport,
&x,&y,&z)){
cout << "Unprojection failed. " << endl;
return new ThetaTriplet(0,0,0);
};

cout << "x: " << x << " y: " << y << " z: " << z << endl;




here's the ouput: (i've edited theoutput tu make matrices easier to read but didn't change any value)

viewport
0,0, 640, 480

model
-0.229924, 0,-0.973209 0,
0, 1 0, 0,
0.973209, 0, -0.229924, 0
4.86604, 0, -1.14962, 1

proj
-2.38424e+11, 1.12335e-314, -2.38424e+11, -2.38424e+11
2.18976e-308, 2.85328e-306, 2.30959e-317, -3.01703e-273
2.29895e-307,-1.99692,-1.99692,-3.23797e-232
2.30959e-317, -3.01703e-273, -1.99692, 6.88044e-308

# Unprojecting win x: 290 win y: 157 win z: 1

results:
x: 0.233788 y: 5.02861e+272 z: -4.94477

if i click many times it continues this way, with different matrices depending on where the camera's looking at and different window coords depending on where i clicked, however when at a static location clicking on different window coords prints different win coords but same results.
ouput examples: (all at the same location and with the same matrices)

# Unprojecting win x: 524 win y: 361 win z: 1
x: 1.00004 y: 8.37584e-12 z: -5

# Unprojecting win x: 90 win y: 115 win z: 1
x: 1.00004 y: 8.37584e-12 z: -5

# Unprojecting win x: 501 win y: 145 win z: 1
x: 1.00004 y: 8.37584e-12 z: -5

Share this post


Link to post
Share on other sites
Advertisement
I may not get to it until tomorrow, but I'll look over your results and see if I can find the problem (unless someone else finds it first).

Share this post


Link to post
Share on other sites
Hi Marianne,

There may be other problems, but the first place I'd look is the projection matrix. I used your values to create a projection matrix using both gluPerspective() and my own code, and got the following:
1.81066  0.00000  0.00000  0.00000
0.00000 2.41421 0.00000 0.00000
0.00000 0.00000 -1.01005 -1.00000
0.00000 0.00000 -2.01005 0.00000
At first glance at least, the projection matrix values you posted seem to be more or less random, so maybe something is going wrong when setting or querying the OpenGL projection matrix.

One other minor detail. I printed out the matrix entries in sequential order like you did, but printing them out in the order 0-4-8-12-1-5-9-13-2-6-10-14-3-7-11-15 would better reflect the actual form of the matrix.

Share this post


Link to post
Share on other sites
well i really don't understand what's going on... if i just copy and paste the codee that prints the projection matrix to a method that's called on a keypress and it gives different values if i click and press the keyboard button whitout changing anything else!

it gave me:

on mouse click:
proj [0] = -2.38424e+11
proj [1] = 1.12335e-314
proj [2] = -2.38424e+11
proj [3] = -2.38424e+11
proj [4] = 2.18978e-308
proj [5] = 2.85328e-306
proj [6] = 2.30944e-317
proj [7] = -3.01703e-273
proj [8] = 2.25498e-307
proj [9] = -1.99692
proj [10] = -1.99692
proj [11] = -3.23797e-232
proj [12] = 2.30944e-317
proj [13] = -3.01703e-273
proj [14] = -1.99692
proj [15] = 6.88041e-308

on key press:
proj [0] = -1.99684
proj [1] = 0
proj [2] = 0
proj [3] = 4.24399e-314
proj [4] = 2.36533e-317
proj [5] = 0
proj [6] = 2.30944e-317
proj [7] = -3.01703e-273
proj [8] = 2.25498e-307
proj [9] = 2.11625e-301
proj [10] = -1.99692
proj [11] = 8.34209e-303
proj [12] = 2.36533e-317
proj [13] = 2.70643e-307
proj [14] = -1.99692
proj [15] = -2.1859e-215

i just don't understand how this can happen they both use the same code :

GLdouble projection_matrix_array[16]; glGetDoublev(GL_PROJECTION,projection_matrix_array);
for(int i=0; i<16; i++) cout << "proj [" << i << "] = " << projection_matrix_array << endl;

and i call them one right after the other (open, click then press key and collect 2 diffrent outputs!) ... really not getting it :S

Share this post


Link to post
Share on other sites
I think this will make you happy :) I can't guarantee this will make everything work perfectly, as there may be other problems in your code. But it will be a step in the right direction.

OpenGL uses different constants for glGet*v() than it does for glMatrixMode(). For matrix mode it's:

GL_MODELVIEW
GL_PROJECTION

For glGet*v() it's:

GL_MODELVIEW_MATRIX
GL_PROJECTION_MATRIX

So I think that's why you're getting garbage. Try this instead:

GLdouble modelview_matrix_array[16]; glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix_array);
GLdouble projection_matrix_array[16]; glGetDoublev(GL_PROJECTION_MATRIX,projection_matrix_array);

'Hope that fixes everything (and sorry I didn't notice it before).

Share this post


Link to post
Share on other sites
thanks, a lot for your support! few would have helped me so much ;)

it works all perfectly! the only thing i don't get is why changing the window z factor by 1 unit changes the result coords by 200 units (??) anyway i'll be able to manage ;)

thanks again

Share this post


Link to post
Share on other sites
Quote:
it works all perfectly! the only thing i don't get is why changing the window z factor by 1 unit changes the result coords by 200 units (??)
Great, I'm glad you got it working :)

The answer to your question is that window coordinates are mapped differently than camera coordinates. The OpenGL default is that z values in the range [near, far] in camera space are mapped to the range [0,1] in window coordinates. When you unproject you're going the other way, so [0,1] maps to [near,far]. It's a little hard to explain without getting into the whole pipeline, but that's the general idea.

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!