# 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.

## 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 on other sites
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 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);

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631708
• Total Posts
3001837
×