Jump to content
  • Advertisement
Sign in to follow this  
cutovoi

OpenGL Problems in conversion

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

Hi fellows I have a problem when I converting mouse coordinates to OpenGL coordiantes. I have this macro that converts window coordinate to OpenGL coordinate.
#define TransformCoordinate(pos, size) ((float)pos/(((float)size-1)/2)-1)

This is my WM_MOUSEMOVE message:
case WM_MOUSEMOVE:
	  {
		 mouseX =  TransformCoordinate((float)LOWORD(lParam), (float)WIDTH);
		 iVal1 = LOWORD(lParam);
		 iVal2 = HIWORD(lParam);
		 fX = mouseX;
		 sprintf(cTemp, "X: %d Y: %d  x transf: %0.9f y transf: %0.9f Difference X: %0.9f\n", iVal1, iVal2, mouseX, mouseY, fX);
	     break;
	  }

And this is when I use the converted coordinates to show a text in my window:
glPushMatrix();
		    glLoadIdentity();
			glRasterPos3f(mouseX, mouseY, -1.0);
			PrintText(cText, uiFont);

Now the problem. When the mouse is in the center of my window, the first letter of the text is shown right above of the mouse cursor. When I move the mouse to the left or to the rigth, the text position doesn't remain in the mouse position. It stays far away. I don't have any idea how caj I correct this bug? My calc is wrong? I need a help. Thanks fellows. Thanks for the support. Look at this pictures. NOTE: the white part of the picture is the mouse position: Image Hosted by ImageShack.us Image Hosted by ImageShack.us

Share this post


Link to post
Share on other sites
Advertisement
for text etc
youre best off setting the frustum to the same size as the window
gluOrtho2d(0,width,0,height);
so whatever u draw at say pos 300,100 appears at 300pixels from the left + 100 pixels from the bottom

Share this post


Link to post
Share on other sites
Quote:
Original post by zedzeek
for text etc
youre best off setting the frustum to the same size as the window
gluOrtho2d(0,width,0,height);
so whatever u draw at say pos 300,100 appears at 300pixels from the left + 100 pixels from the bottom

'at's the truth. For when the problem comes up again, though, for picking and other sorts of raycasting, the correct way to change object coordinates into window coordinates (and vice-versa) is gluProject( ) (and gluUnProject( )).

-jouley

Share this post


Link to post
Share on other sites
But fellows, I'm using to setup my window the glViewport, gluPerspective and gluLookAt, like you see I'm using perspective matrices, not orthogonal matrices.

Share this post


Link to post
Share on other sites
Quote:
Original post by cutovoi
But fellows, I'm using to setup my window the glViewport, gluPerspective and gluLookAt, like you see I'm using perspective matrices, not orthogonal matrices.

This is exactly the situation for which glu(Un)Project is used. They turn your world coordinates (x, y, z) into orthogonal coordinates (x, y) and vice versa. An example from NeHe, lesson 13:

CVector3 GetOGLPos(int x, int y)
{
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

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

winX = (float)x;
winY = (float)viewport[3] - (float)y;

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

return CVector3(posX, posY, posZ);
}

Using gluProject is similar, and a similar google should turn up results fairly quickly.

While we're on it, though, why not use an orthogonal projection for text? You can render your 3D items, then switch into "text" mode for the 2D overlay. Here's a discussion on doing so.

Hope that clears something up!
-jouley

Share this post


Link to post
Share on other sites
I got the idea form your information guys. Based on this and in the link provided by jouley works.
I done this. Is the best way or I forgot somenthing. Don't you mind to tell me what is or isn't right thing that I done? Here is the code:

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(x1 ,0, -4.0);
CreateCube(uiTexture[iTexture]);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glGetDoublev(GL_MODELVIEW_MATRIX, fModelViewMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, fProjectionMatrix);
glGetIntegerv(GL_VIEWPORT, iViewportMatrix);
gluUnProject((float)mouseX, (float)(iViewportMatrix[3] - (float)mouseY), mouseZ, fModelViewMatrix, fProjectionMatrix, iViewportMatrix, &X, &Y, &Z);
glDisable(GL_LIGHTING);
glColor3f(1.0,0.0,0.0);
glRasterPos3f(X, Y, Z);
sprintf(cStr, "Valores: X: %0.9f Y: %0.9f Z: %0.9f", X, Y, Z);
PrintText(cStr, uiFont);//This is my function that show the text
glEnable(GL_LIGHTING);
glPopMatrix();

Share this post


Link to post
Share on other sites
Quote:
Original post by cutovoi
Is this the best way?

If it works for you, then it works. I'll remind you of orthographic projections, though, as that method (mentioned in my last post) has different behavior than your current method, and may be useful to you in the future, when you want your text to act differently. It's probably worth trying now, just so you can see the difference and decide which is for you. (It may even be faster, what with all those GL calls associated with glu(Un)Projecting.)

Good work!
-jouley

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!