Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: Translate, Rotate and mouse coordinates

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    5

    Translate, Rotate and mouse coordinates

    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, but mouse coordinates that I calculate are wrong. 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 and Keyboard events:
    Code :
          private void openGL_MouseWheel(object sender, MouseEventArgs e)
            {
                if (e.Delta > 0)
                        zoom += 1;
                else
                        zoom -= 1f;
     
                Invalidate();
                glDraw();
            }
     
            private void openGL_MouseMove(object sender, MouseEventArgs e)
            {
                if (MouseD == 2)
                {
                    yrot -= (mouX - e.Location.X)/5;
                    if ((yrot > 360) || (yrot < -360)) yrot = 0;
                    xrot -= (mouY - e.Location.Y)/5;
                    if ((xrot > 360) || (xrot < -360)) xrot = 0;
                }
     
                if (MouseD == 1)
                {
                    xpos -= (mouX - e.Location.X) / 100;
                    ypos += (mouY - e.Location.Y) / 100;
                }
     
                if ((MouseD == 1) | (MouseD == 2))
                {
                    Invalidate();
                    glDraw();
                }
     
                mouX = e.Location.X;
                mouY = e.Location.Y;
     
            }
     
            private void openGL_MouseUp(object sender, MouseEventArgs e)
            {
                BodGL bod;
     
                if ((AtrBod == true) & (MouseD == 0))
                {
                    pocetAtrBodov++;
     
                    bod = new BodGL();
                    bod.Quad = GLU.gluNewQuadric();
                    bod.bod = new Bod();
                    bod.bod.Pozicia = openGLPos(e.Location.X, e.Location.Y);
                    bod.bod.Typ = 1;
                    bod.bod.Farba = new Vektor(0.5, 0.5, 0.5);
     
                    poleBodov.Add(bod);
     
                    Invalidate();
                    glDraw();
                }
     
                MouseD = 0;
     
            }
     
            private void openGL_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                    MouseD = 2;
                else
                    if (AtrBod) MouseD = 0; else MouseD = 1;
            }
     
            private void openGL_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.A) AtrBod = true;
            }
     
            private void openGL_KeyUp(object sender, KeyEventArgs e)
            {
                AtrBod = false;
            }

    Calculate mouse coordinates(I thing, here is mistake):
    Code :
    private Vektor openGLPos(int x, int y)
            {
                int[] viewport = new int[4];
                double[] modelview = new double[16];
                double[] projection = new double[16];
                double outX, outY, outZ;
     
                GL.glGetDoublev( GL.GL_MODELVIEW_MATRIX, modelview );
                GL.glGetDoublev( GL.GL_PROJECTION_MATRIX, projection );
                GL.glGetIntegerv( GL.GL_VIEWPORT, viewport );
     
                Vektor win = new Vektor((float)x, (float)viewport[3] - y, 0f);
     
                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, (float)win.Z, modelview, projection, viewport, out outX, out outY, out outZ);
     
                Vektor temp = new Vektor(outX, outY, outZ - 10);
     
                return temp;
            }

    And draw method:
    Code :
    	public override void glDraw()
    	{
     
                GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);	
     
                GL.glPushMatrix();
                GL.glLoadIdentity();
     
                GL.glTranslatef(xpos, ypos, zoom);
                GL.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
                GL.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
     
     
                if (pocetAtrBodov > 0)
                {
                    foreach (BodGL B in poleBodov)
                    {
                        VykresliAtrBod(B.Quad, B.bod.Priemer, B.bod.Farba);
                    }
                }
     
                GL.glPopMatrix();
    	}
     
     
            private void VykresliAtrBod(GLUquadric Q, double r , Vektor farba)
            {
                unsafe
                {
                    GL.glColor3f((float)farba.X, (float)farba.Y, (float)farba.Z);
     
                    GL.gluQuadricDrawStyle(Q, GL.GLU_SILHOUETTE); /* smooth shaded */
                    GL.gluQuadricNormals(Q, GL.GLU_SMOOTH);
     
     
                    GL.gluSphere(Q, r, 10, 10);
     
                }
            }

    Please is here somebody, who can help me?

    Thank you very much and please sorry about my english.
    Last edited by hybr1D; 11-06-2012 at 05:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •