PDA

View Full Version : gluProject giving wrong results



Rick93NL
07-22-2012, 10:11 AM
Alright, I have to find out if an object in my world is 100% visible.

What I'm doing right now to find this out is color picking. Rendering in unique colors is going fine, it was very easy to accomplish this.
After rendering in unique colors I make use of gluProject to find out which pixels I must test for possible other colors then my to be scanned object. But this is not working at all, I'm getting weird pixel locations like -y results while I have the to be scanned object in plain sight and other bad stuff like that.

At the part of drawing the to be scanned object I store the movelview, projection and viewport in public variables so that the object that is doing the scan can use send those to gluProject.

Here is the part where I'm drawing the to be scanned object and also saving the modelview, projection and viewport:


glRotatef(xRotation, 1, 0, 0); //Camera rotation
glRotatef(yRotation, 0, 1, 0); //Camera rotation

glPushMatrix();
set_color4f(0.5, 0.5, 0.5, m_Alpha); /* !!!This is not an OpenGL function, this is a local function which looks if the current render is in color picking mode or not!!! */
glTranslatef(m_Location.x,
m_Location.y,
m_Location.z);
glRotatef(huniplacer::utils::deg(m_Rotation), 0, 1, 0);

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

... drawing code - not important ...

glPopMatrix();

glSwapBuffers();


And here is the part where gluProject is used to find out at which pixels the to be scanned object should be visible if nothing is in front of it:


for (float x = -scan_target->getSize().x/2; x < scan_target->getSize().x/2; x+= precision) {
for (float z = -scan_target->getSize().z/2; z < scan_target->getSize().z/2; z+= precision) {
GLdouble pixel_x = -1;
GLdouble pixel_y = -1;
GLdouble pixel_z = -1; //This value always ends up as 1, the screen has no depth. Pay no attention to this value here & below

GLint testtt = gluProject(x + scan_target->getLocation().x,
scan_target->getLocation().y + scan_target->getSize().y/2,
z + scan_target->getLocation().z,
scan_target->get_modelview(),
scan_target->get_projection(),
scan_target->get_viewport(),
&pixel_x,
&pixel_y,
&pixel_z);

... more code that is not of interest right now ...
}
}


Am I not understanding how the matrices in OpenGL work? Because I guess it must be that I'm giving gluProject wrong matrices (the modelview, projection and viewport matrices).

V-man
07-23-2012, 10:36 AM
If you just want to determine if an object is visible, you can use occlusion query testing. Look for glGenQueries, glDeleteQueries (http://www.opengl.org/sdk/docs/man3/xhtml/glDeleteQueries.xml), glBeginQuery, glEndQuery. It is pixel accurate.

Rick93NL
07-23-2012, 12:23 PM
If you just want to determine if an object is visible, you can use occlusion query testing. Look for glGenQueries, glDeleteQueries (http://www.opengl.org/sdk/docs/man3/xhtml/glDeleteQueries.xml), glBeginQuery, glEndQuery. It is pixel accurate.

Thank you for helping me out here but I found out what I was doing wrong already.
At the moment of capturing the matrices that are used for drawing the scan target I am glTranslating towards the center of the object. GluProject gave perfect results it's just that I was adding the position of the object to the x en z values in the double for loop while with the captured matrices I was already in the center of the object! I was so focused on gluProject that I never realised this.

While it's kind of sour that I only just now find out this could've all been done in 1 hour work using the occlusion query testing, I am happy that I got this to work and I've learned a lot more about opengl then I otherwise would.

I'm not going to use the occlusion query testing even tho it's most likely a lot more efficient (I've read a couple of things about it just now and with the possibillity of letting it do the calculations in parallel using the gpu it is definitely going to be faster then my method). I had to do this for a school project and it really doesn't matter if it takes a fraction of a second longer to find out if something is blocking the view on the object.

Rick93NL
07-24-2012, 03:23 AM
Hey moderator, is there a possibility that you somehow overlooked my reply to V-man in this thread? It's been 24 hours ago since I wrote that. I'm not trying to be rude, I'm just curious.

Edit: Ok this is weird, this time my message didn't have to get approved by a moderator.. How does the system in this forum work? I'm confused.

Yesterday I wrote a reply to your post V-man, if for some reason my message got lost yesterday I'm going to write it tomorrow again. I don't need any help anymore that is one thing that I can tell you all right now.