Jump to content
  • Advertisement
Sign in to follow this  
elleestcrimi

OpenGL How to get 2D coordinates of a 3D object after transformation

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

Hello,

I am fairly new to openGL and I am having problems getting the screen (2D coordinates) of a transformed object.

I have a 3D cube which I rotate around using the arcball technique. I use gluProject to get the 2D bounds of one of the faces of the cube. But after I rotate the cube, it wont return the updated 2D coordinates based on the rotation. And in order to detect the face I must rotate it back to the original position.

So my question is as simple as this: How can I get the 2D coordinates of that face after I have rotated it?

This is the code I am using:


void isOnCube(int x, int y)
{
double projection [16];
double modelView [16];
int viewport[4];

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

//Front face bounds
gluProject( 6.0, 6.0, -6.0, modelView, projection, viewport, &corners[0][0], &corners[0][1], &corners[0][2]);
gluProject( 6.0,-6.0, -6.0, modelView, projection, viewport, &corners[1][0], &corners[1][1], &corners[1][2]);
gluProject(-6.0,-6.0, -6.0, modelView, projection, viewport, &corners[2][0], &corners[2][1], &corners[2][2]);
gluProject(-6.0, 6.0, -6.0, modelView, projection, viewport, &corners[3][0], &corners[3][1], &corners[3][2]);

float minX = corners[0][0];
float minY = corners[0][1];
float maxX = corners[0][0];
float maxY = corners[0][1];
for (int i = 0; i < 4; i++)
{
if(minX > (float) corners[0])
minX = (float) corners[0];
if(minY > (float) corners[1])
minY = (float) corners[1];
if(maxX < (float) corners[0])
maxX = (float) corners[0];
if(maxY < (float) corners[1])
maxY = (float) corners[1];
}

if ((minX <= x && x <= maxX && minY<= y && y <= maxY))
{
printf("is on front face");
}
}

Share this post


Link to post
Share on other sites
Advertisement
It sounds when you call glGetDoublev(GL_MODELVIEW_MATRIX, modelView), you don't have the rotations applied.
Perhaps you have a glPushMatrix()
glRotate()
Draw Cube
glPopMatrix()

Share this post


Link to post
Share on other sites
No I don't use any glPushMatrix nor glPopMatrix. I used this


glMultMatrixf(quat);
drawCube();


But I assumed that the modelView is updated when calling glMultMatrixf(quat);

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!