Jump to content
  • Advertisement
Sign in to follow this  

OpenGL [Tao, OpenGL, C#] trying to get the world mouse position

This topic is 4295 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, I posted this topic on the Tao Framework forum in this topic, but I didn't get much replies. So I'll ask here too.
I'm trying to get the world mouse position, but it doesn't work. I always get something really close to zero [-1.1, 1.1] if my camera is in the center of the screen. and when I move my camera, well, it's close to the camera. Always in the middle of the screen. In my previous project, I did something really similar to this but with Tao 1.3.0-1 Now I'm using Tao 2.0 RC1 with C# 2.0 for some reason in:
Gl.glReadPixels((int)winX, (int)winY, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, (object)winZ);
winZ is always equal to 0.0. Is there a bug in this function? It's PROBABLY my camera that does weird stuff??? I don't know, it used to work... I need some help download my project http://iarus.awardspace.com/proto1.03.html 339kB it's really small, you should find the error rapidly the error could be in :
- OpenGLForm class :
     - ScreenToWorldCoordinates(int x, int y)
     - ReSizeGLScene(int width, int height)
- Camera class : 
     - ApplyCameraTransform()
Maybe elsewhere, but I don't think so. Thank's
press F1 to see the help screen, it's in French, I know, but just press buttons and you'll see what they do

Share this post

Link to post
Share on other sites
Hmm, just an idea but I would try moving your Input call to before your SwapBuffer call and see if that does anything for your ReadPixel call.

Share this post

Link to post
Share on other sites
I'm pretty sure you need to get the viewport and then for the winY value use

viewport[1] - winY

Its because the bottom left is the origin of the screen in OpenGL. And your winY value is probably assuming the origin is in the top left.

Share this post

Link to post
Share on other sites
I got tired of waiting for someone else to do the job, so I fixed it my self

I don't use the readPixel + UnProject methods, just some simple maths plus some weird fix that I don't really understand... I guess things are not linear or something like that

      /// <summary>
/// or else... boom!!!
/// </summary>
/// <param name="x">MousePosition.x</param>
/// <param name="y">MousePosition.y</param>
/// <returns>world mouse position</returns>
public Vector2 ScreenToWorldCoordinates(int x, int y)
int[] viewport = new int[4];
Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);

y = viewport[3] - y;

float halfVpHeight = viewport[3] / 2f;
float halfVpWidth = viewport[2] / 2f;
float halfFovVert = camera.FOV / 2f;
float halfFovHoriz = halfFovVert * viewport[2] / (float)viewport[3];

//camera.Z is how far the camera is pushed back
//Why I "fix" the Z? to "scale" the results
//Where does the Z^1.524 come from? No idea, trial and error, it gives OK results...
float zFixed = (float)Math.Pow(camera.Z, 1.524);
//180 * PI
float worldY = camera.Position.y + (float)Math.Sin((((y - halfVpHeight) / halfVpHeight) * halfFovVert) / 565.4866776f) * zFixed;
float worldX = camera.Position.x + (float)Math.Sin((((x - halfVpWidth) / halfVpWidth) * halfFovHoriz) / 565.4866776f) * zFixed;

return new Vector2(worldX, worldY);

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!