mouse coordinates and rotation

Hello.
I have problem with my application. When I click left mouse button and move with mouse, it change translation. When I click right mouse button and move with mouse, it change rotation. When I press ‘a’ and click, gluSphere will be draw at mouse coordinates in openGL. Drawing , rotation and translation work fine, until i rotate scene. After rotation calculate false coordinates, so spheres will draw at a strange places. I read a lot of tutorials and forums, but I still dont know where I do mistake. I make it in C#.

Here is a part of my code:

Mouse events:

 
private void openGL_MouseUp(object sender, MouseEventArgs e)
        {
            BodGL bod;
            
            if (((AtrBod == true) || (BasBod == true) || (ListBod == true)) & (MouseD == 0))  
            {
                bod = new BodGL();
                bod.Quad = GLU.gluNewQuadric(); 
                bod.bod = new clsBod(); 
                bod.bod.Pozicia = openGLPos(e.Location.X, e.Location.Y);  
               .
               .
               .

function openGLPos


private Vektor openGLPos(int x, int y)
        {  

            double outX, outY, outZ;    
            float z = zoom/100;


            Vektor win = new Vektor((float)x, (float)viewport[3] - y, z);
            unsafe  
            {
                GL.glReadPixels(x, (int)win.Y, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, (&outZ)); 
            }            

            GL.gluUnProject((float)win.X, (float)win.Y, outZ, modelview, projection, viewport, out outX, out outY, out outZ);            

            Vektor temp = new Vektor(outX - xpos / 100, outY - ypos / 100, outZ - zoom / 100); // outZ - zoom / 100

            return temp;
        }

draw procedure


		public void glDraw()
		{


            GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            InitGLContext();
            GL.glLoadIdentity();

            GL.glPushMatrix();  

            GL.glTranslatef(xpos, ypos, zoom);

            GL.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
            GL.glRotatef(yrot, 0.0f, 1.0f, 0.0f);

            foreach (BodGL B in poleBodov)  
            {
                GL.glTranslatef((float)(B.bod.Pozicia.X * 100), (float)(B.bod.Pozicia.Y * 100), (float)(B.bod.Pozicia.Z * 100));   
                if (Math.Round((B.bod.Pozicia.Z * 100 + 10) + zoom) == 0) B.bod.Farba = Color.FromArgb(B.bod.Farba.R, B.bod.Farba.G, B.bod.Farba.B);    
                    
                VykresliAtrBod(B.Quad, B.bod.Priemer, B.bod.Farba, B.bod.Fill); 
                    
                if (Math.Round((B.bod.Pozicia.Z * 100 + 10) + zoom) == 0) B.bod.Farba = Color.FromArgb(B.bod.Farba.R,B.bod.Farba.G, B.bod.Farba.B);  
                GL.glTranslatef(-(float)(B.bod.Pozicia.X * 100), -(float)(B.bod.Pozicia.Y * 100), -(float)(B.bod.Pozicia.Z * 100));
            }

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

            GL.glPopMatrix();  

		}

matrics


        private double[] modelview = new double[16];
        int[] viewport = new int[4];
        double[] projection = new double[16];

are as global variable in my class.

Please help me and sorry about my english.

Thanks.

I correct my code but it still dont work. When i dont rotate and translate the scene, it work fine. But when i rotate or translate the scene, it print nothing. Calculate coordinates looks be good and all matrics are the same in glDraw and openGLPos function. Z coordinate I set to 0 because when I calculate it by glReadPixels it return strange number (about 10 or -10) and print absolutly nothing even when I dont rotate or translate the scene. Please help :frowning:


private Vektor openGLPos(int x, int y)
        {   //prepocet suradnic kliku v okne, na suradnice v 3D OpenGL

            double outX, outY, outZ;    //vysupne suradnice
            float z = 0;

            Vektor win = new Vektor((float)x, (float)viewport[3] - y, z);
            unsafe  //pracujem s pointrami tak musim byt v unsafe mode
            {
                GL.glReadPixels(x, (int)win.Y, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, (&outZ));  //zizkam Ztovu suradnicu
            }            

            //funkcia prepocita suradnice
            GL.gluUnProject((float)win.X, (float)win.Y, z, modelview, projection, viewport, out outX, out outY, out outZ);            


            //vratime prepocitane suradnice uz aj s posunom sceny a zoomo
            Vektor temp = new Vektor(outX, outY, 0); // outZ - zoom / 100

            return temp;
        }

        public override void glDraw()
		{
            //vykreslenie sceny

            GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            InitGLContext();
            //GL.glLoadIdentity();

            GL.glPushMatrix();  //pushnem si maticu

            //posun
            GL.glTranslatef(xpos, ypos, zoom);

            //rotacia
            GL.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
            GL.glRotatef(yrot, 0.0f, 1.0f, 0.0f);

            foreach (BodGL B in poleBodov)  //prejdem vsetky body
            {
                GL.glTranslatef((float)(B.bod.Pozicia.X * 100), (float)(B.bod.Pozicia.Y * 100), (float)(B.bod.Pozicia.Z * 100));    //nastavim sa na poziciu bodu
                //if (Math.Round((B.bod.Pozicia.Z * 100 + 10) + zoom) == 0) B.bod.Farba = Color.FromArgb(B.bod.Farba.R, B.bod.Farba.G, B.bod.Farba.B);    //zjasnenie bodov na aktualnej rovine zoomu
                    
                VykresliAtrBod(B.Quad, B.bod.Priemer, B.bod.Farba, B.bod.Fill); //vykreslim gluSphere
                    
                //if (Math.Round((B.bod.Pozicia.Z * 100 + 10) + zoom) == 0) B.bod.Farba = Color.FromArgb(B.bod.Farba.R,B.bod.Farba.G, B.bod.Farba.B);    //ztmavnutie bodov mimo akt. roviny zoomu
                GL.glTranslatef(-(float)(B.bod.Pozicia.X * 100), -(float)(B.bod.Pozicia.Y * 100), -(float)(B.bod.Pozicia.Z * 100)); //vratim sa naspet na povodnu poziciu
            }

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

            GL.glPopMatrix();   //popnem si maticu

		}