Sign in to follow this  

Tao.GL Selection and gluUnProject Help?

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

I'm trying to make a map editor for an isometric engine right now in C#, and I'm having some serious issues trying to figure out Mouse Coordinates in a GLControl. The basic jist is that on the X/Z axis are the tiles. The mouse is dealt with on a SimpleGLControl MouseMove event:
        private void MapGLControl_MouseMove( object sender, MouseEventArgs e )
        {
            double Output_X, Output_Y, Output_Z;

            double[] ModelviewMatrix = new double[16];
            double[] ProjectionMatrix = new double[16];
            int[] Viewport = new int[4];
            float[] Pixels = new float[1];
            
            IntPtr PixelPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal( sizeof( float ) );
            
            Gl.glGetDoublev( Gl.GL_MODELVIEW_MATRIX, ModelviewMatrix );
            Gl.glGetDoublev( Gl.GL_PROJECTION_MATRIX, ProjectionMatrix );
            Gl.glGetIntegerv( Gl.GL_VIEWPORT, Viewport );

            System.Runtime.InteropServices.Marshal.Copy( Pixels, 0, PixelPtr, 1 );

            Gl.glReadPixels( e.X, Viewport[3] - e.Y, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, PixelPtr );
            
            System.Runtime.InteropServices.Marshal.Copy( PixelPtr, Pixels, 0, 1 );
            System.Runtime.InteropServices.Marshal.FreeHGlobal( PixelPtr );

            Glu.gluUnProject( (double) e.X, (double) ( Viewport[3] - e.Y ), (double) Pixels[0], ModelviewMatrix, ProjectionMatrix, Viewport, out Output_X, out Output_Y, out Output_Z );

            MouseLabel.Text = "X: " + Output_X.ToString( "#.##" ) + " Y: " + Output_Y.ToString( "#.##" ) + " Z: " + Output_Z.ToString( "#.##" );
        }

The scene is drawn ala a SimpleGLControl Paint event. It calls DrawScene, which does all of the resetting/scene-drawing-setup.
        private void DrawScene()
        {
            Gl.glEnable( Gl.GL_DEPTH_TEST );

            Gl.glClear( Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT );
            Gl.glClearColor( ( (float) Map.BackgroundColor.R ) / 255, ( (float) Map.BackgroundColor.G ) / 255, ( (float) Map.BackgroundColor.B ) / 255, 0.0f );

            Gl.glMatrixMode( Gl.GL_PROJECTION );
            Gl.glLoadIdentity();

            Gl.glOrtho( -ScreenSize, ScreenSize, -ScreenSize, ScreenSize, -ScreenSize, ScreenSize );

            Gl.glMatrixMode( Gl.GL_MODELVIEW );
            Gl.glLoadIdentity();
            Gl.glScalef( Zoom, Zoom, Zoom );
            Gl.glRotatef( 45.0f, 1.0f, 0.0f, 0.0f );
            Gl.glRotatef( Rotation, 0.0f, 1.0f, 0.0f );

            CenterScreen(); /* Just essentially will glTranslate to where the screen is centered at */

            DrawBase(); /* Draws the base of the Isometric map -- i.e. the tiles */

            Gl.glFlush();
        }

EDIT: Editted so that these were "Source" tags -- sorry about that! I'm new to this! My issue with this code right now is that it works somewhat right. But all of the numbers are off for some reason or another. Not easy enough that I could just do a glTranslate to "correct" it -- the rotation is slightly off as well... Is there anything inherently wrong with what I'm doing? Is there a better method to do this, or are there any issues with Tao GL's usage of the gluUnProject? Thanks!

Share this post


Link to post
Share on other sites
You might edit your post to use [ source ] tags, and/or break some of the longer lines. As is, those of us with lower resolution settings have to scroll back and forth to read your post (well, at least I do :).

Share this post


Link to post
Share on other sites

This topic is 3460 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this