# Thread: How Do I Convert Mouse Position Cordinates to OpenGL cordinates?

1. Thank you I will try this today or tomorrow defently I think I might finally start understanding how to snap to certain areas in opengl thanks too yours truely

2. Okay I've tried to do what you ask and having issues first of all the quad doesn't show anymore even tho I followed everything you have written in that post above here is my code

glTexCoord2f (0.0, 0.0);
glVertex3f(pos.x, SCREEN_HEIGHT - pos.y, 0);

glTexCoord2f (1.0, 0.0);
glVertex3f(pos.x , SCREEN_HEIGHT - currentMouseY, 0);

glTexCoord2f (1.0, 1.0);
glVertex3f(currentMouseX , SCREEN_HEIGHT - currentMouseY , 0);

glTexCoord2f (0.0, 1.0);
glVertex3f(currentMouseX , SCREEN_HEIGHT - pos.y , 0);

glEnd();

the values currentMouseX, currentMouseY are being set when the WM_LBUTTONDOWN command is called in Win32 which represents the left button down mouse click

maybe if you see my full source you can pin point where I might be going wrong then I can learn from this in the future if I ever have this issue again .

here is the link to the full source the code is abit messy sorry about that -> http://pastebin.com/0WUAh9t4 line 432 to 510 is where most of the code we talked about is being called.

I hope this helps you helping me figure this problem out.

3. anyone?

4. Did you use an orthographic projection? Depending on how you set up the orthographic projection, you can make matrix transformations follow pixels - a glTranslatef(200, 200, 0); would move it 200 pixels in the x and y directions. Something like this:

Code :
glMatrixMode(GL_PROJECTION);
glOrtho(0, screenWidth, 0, screenHeight, -1, 1);
glMatrixMode(GL_MODELVIEW);

glDisable(GL_DEPTH_TEST);

glTranslatef(mouse_window_posX, mouse_window_posY, 0.0f);

glEnable(GL_DEPTH_TEST);

// .........

5. Using orthographic projection wouldn't help me in this situation would It Oh I see! your saying that orthographic projection will be the closest to mouse cursor position so go into orthographic projection and render?

6. With only skimming certain parts of the code, I want to ask a couple questions that can help point you in the right direction. (Please note I have little to no experiece in C++ and primarily work with Java. However, I did comprehend much of the code).

As said by cireneikual:
Did you use an orthographic projection?
This is important because you are giving screen coorindates to OpenGL. If you are in perspective mode, your quad will be HUGE.

1. I noticed you are using shaders. Are your shaders working properly? Will they render geometry given you specify everything right?
2. Is texturing working? Make sure you are not drawing an invisible quad. Try setting the color of the quad to something different than the clear color.
3. Make sure that your viewing paramters are correct. You won't be able to see the quad if your camera is facing the wrong direction.
For debugging you may want to insert the following into your modelview projection area:
Code :
//cameraPos is the coordinate of the camera, lookAtPos is where the camera is looking and the last 3 values specify an upward vector.
gluLookAt(cameraPos.x, cameraPos.y, cameraPos.z, lookAtPos.x, lookAtPos.y, lookAtPos.z, 0, 1, 0);

4. Can you render anything at all? Try putting some piece of code that you know will work and see if you can render a simple object using constant coordinate values.
5. I don't know if this is a big C++ problem, but in Java, thread concurrency matters a ton: Are the variables you have setup NULL or 0? Make sure that pos.x actually gives the value you want it to.
6. What is the glTranslatef() doing? Do you still need it? If you are not in orthographic projection mode, it could be moving the quad far out of the scene. (Originally, I didn't think you needed it, and you could instead create dynamic geometry. This is why I said to put variables in place of the quad's location/dimension.)

Also I want to suggest that you setup projection and modelview matrices each pass through the display() method. If you are rendering perspective and orthographic projections, you MUST do this. Otherwise, you will stay in the last mode specified.

Also, I noticed a subtle but big error in your perspective mode setup in the init() function:

This is what you have
Code :
gluPerspective(90 SCREEN_HEIGHT/SCREEN_WIDTH, -1.0, 1.0);
This is what you should have
Code :
gluPerspective(90 SCREEN_HEIGHT/SCREEN_WIDTH, [COLOR=#ff0000]0.01[/COLOR], 1.0);
This is because the near-clipping plane cannot be less than or equal to zero. Therefore, make it barely positive.

Best of luck!