Jump to content
  • Advertisement
Sign in to follow this  
Tryllion

OpenGL C# mouse to OpenGL , error.

This topic is 3560 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 ve some problem with C# mouse translation to OPENGL. My code: int[] viewport = new int[4] { 0, 0, this.Width, this.Height }; double x1, y1, z1; double[] mvmatrix = new double[16]; double[] projmatrix = new double[16]; Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport); Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, mvmatrix); Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX, projmatrix); Glu.gluUnProject(x, y, 1, mvmatrix, projmatrix, viewport, out x1, out y1, out z1); return new Vector2((float)x1, (float)y1); it works, but if i zoom in or zoom out, the point where I clik is not the same as OpenGl shows, the error growht near the Frame border and is 0 in the center of Frame. Any ideas ? :(

Share this post


Link to post
Share on other sites
Advertisement
try
Glu.gluUnProject(x, this.Height-y, 1, mvmatrix, projmatrix, viewport, out x1, out y1, out z1);

or

Glu.gluUnProject(x, this.Height-y-1, 1, mvmatrix, projmatrix, viewport, out x1, out y1, out z1);

Share this post


Link to post
Share on other sites
Sorry for my English.
No its not Y issue, but thx.

I found on forum code:
int[] viewport = new int[4]{0,0,this.Width,this.Height};
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.284 come from? No idea, trial and error, it gives OK results...
float zFixed = (float)Math.Pow(camera.FarPlane, 1.2766);
//zFixed = camera.FarPlane;
//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);

it gives the same effect, but for each Z zoom value I must put different zFixed value( 1.284 works for z==6.0f), if I zoom in mouse is drawing wrong again :(

I think its some mathematics problem, but it's too much for me :(

EDIT:
OK, THIS CODE WORKS GOOD enough FOR Z>1 && Z<20 its precision is good for me
Mary Christmas,thx for code :)

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!