View Full Version : Pixel Location

Jassel41

07-26-2010, 07:22 PM

Hi,

I was wondering if there was a way to retrieve the pixel information of an OpenGL object, even if it is just the center.

I just want to know a general area of where the object is in terms of the pixels in the image.

Thank you in advance,

Jassel41

strattonbrazil

07-27-2010, 08:16 AM

What do you mean pixel information? Colors? Locations on the screen?

One function you might want to take a look at is gluProject. You can pass it the vertices of your object and it will tell you the (x,y) location in screen space of that vertex. You can do a lot with that. For example, you can do that for each vertex and get the maximum and minimum values and you'd have a bounding box of where it is drawn on the screen.

Another function is glReadPixels, where you specify a region on the screen and OpenGL will read the data back (depth, color, etc.) into an array for you.

Jassel41

07-27-2010, 08:42 PM

Hey,

Thank you for the quick reply but I am not entirely sure that is the function I am looking for, or I am just unfamiliar how to use it appropriatly which is certainly a possibility. Here is a more detailed explanation of my situation.

I create a box using glutSolidCube(10). My window is 640 x 480 pixels and I would like to know where in the frame, the cube is after I translate it, for example (100,200) could represent the center pixel location of the cube.

At the start of the program, the cube is at translated at 0,0,0

and I never translate on the z axis, only the x and y. I also know the starting pixel location of the cube, I am just wondering how to track the change in pixel locations as the transformations happen.

Do you still believe gluProject is correct, or does it have to be an object with specificed vertices to use that?

Thank you,

Jassel41

strattonbrazil

07-28-2010, 08:03 AM

gluProject takes a point and turns it from object space to screen space. The point you send it can be a vertex position, but can be any position you want.

For example, if your box is centered at (0,0,0), you can pass gluProject that point with the projection of your camera and the modelview matrix you use to draw the box (including the translation) and that will be the position on screen of the center of the box. You can do the same thing with each corner if you like. It's just a mathematical equation, so it just takes points and spits them out in screen space.

Jassel41

07-28-2010, 08:42 PM

You were right that works perfectly, I was just using it wrong. Thank you so much for your help and sorry if it was a basic question.

- Jassel41

strattonbrazil

07-28-2010, 10:27 PM

This is the place for basic questions.

Jassel41

08-02-2010, 03:29 AM

Ok, maybe I can bother you one more time for help with the same problem. I have a 640x480 window and I set the box translated at 0,0,0 in the frame and can roughly estimate the pixel location by eyeing it up. When I use gluProject(), I do get pixel coordinates back but they are not correct in either the x or y location. The y location seems to be shifted by 50-100 pixels and the x is off by at least 150 each time. Here is my code I am using (just the basic from the gluProject example).

glGetDoublev(GL_MODELVIEW_MATRIX, model_view);

glGetDoublev(GL_PROJECTION_MATRIX, projection);

glGetIntegerv(GL_VIEWPORT, viewport);

gluProject(pos_x,pos_y,pos_z, model_view, projection, viewport, &pos3D_x, &pos3D_y, &pos3D_z);

float x_coord = (float) pos3D_x;

float y_coord = (float) pos3D_y;

The 3 variables, pos_x, pos_y, and pos_z are the values at which the object is translated. Am I going about this incorrectly or is gluProject just very inefficient?

Thanks

Jassel41

strattonbrazil

08-02-2010, 08:15 AM

Unless you're using some crazy matrices, gluProject is usually pretty stable.

Does the model_view matrix you're passing it contain the translation of the object? Does the pos_x, pos_y, and pos_z contain the translation?

Jassel41

08-02-2010, 06:39 PM

I assumed that they did, I retrieve the model view matrix after the translation was performed. I may be inputting the wrong pos_x, pos_y, pos_z because those are the translations I am putting. So if I translate the cube I create using the following

glTranslatef( 5, 5, 0);

glutSolidCube( 10 );

I use my pos_x and pos_y as 5 and pos_z as 0. I thought the positions were the actual translations.

Jassel41

08-02-2010, 07:18 PM

Hey again,

Sorry I figured out what was wrong. The function is working correctly, but I was interpreting it wrong because I forgot that OpenGL treats the bottom left corner as (0,0) but it now works.

Thanks again for all your help!

- Jassel41

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.