you're reading...

How To Obtain Object & World Coordinates From Screen Coordinates in OpenGL

When you click at the screen intuitively you would picture you clicking on certain objects. How can we obtain the information of such clicks? OpenGL offers a self-explanatory way of obtaining the Object Coordinates. This article has elaborated this problem gracefully.

However, we should note that Object Coordinates are different from World Coordinates. The former one is like the position within the object while the latter one is the position in the entire environment. For example, if you render two cubes in two different positions and suppose you use the method above and obtain the object coordinates, you are likely to find out that when you click at (almost) the same positions of the two cubes, what you get are (almost) the same coordinates, regardless of the relative distance of the two cubes. How can we find out the exact positions of the click, in the world coordinates?

The only thing to do is to modify the modelview matrix. Instead of obtaining the matrix from state variables, you should manually set it as an identity matrix so that it will not *roll back* to its object coordinates. The original code is something like:

glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

And the modified code is something like:

for(int i=0; i<4; i++)
    for(int j=0; j<4; j++)
        if(i == j)
            modelview[4*i+j] = 1.0;
            modelview[4*i+j] = 0.0;

Then what you get would be world coordinates.


About Xiang 'Anthony' Chen

Making an Impact in Your Life


No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Twitter Updates

%d bloggers like this: