Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. How to draw cross hair cursor in opengl while rotating camera ??

How to draw cross hair cursor in opengl while rotating camera ??

Scheduled Pinned Locked Moved Graphics
c++graphicsgame-devhelptutorial
6 Posts 3 Posters 33 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maheshbhoir home
    wrote on last edited by
    #1

    Hi, I am displaying 3D models using opengl and VC++. I have implemented code to rotate, move, zoom the model by moving camera (using gluLookAt() ) Now I want to add one cross hair cursor in front of camera view port(at center of screen), but when I tried to rotate or zoom the model the cursor also rotates or zoom along with model. How I can make cursor lie at same position while rotating model. Please help me :((

    A J 2 Replies Last reply
    0
    • M maheshbhoir home

      Hi, I am displaying 3D models using opengl and VC++. I have implemented code to rotate, move, zoom the model by moving camera (using gluLookAt() ) Now I want to add one cross hair cursor in front of camera view port(at center of screen), but when I tried to rotate or zoom the model the cursor also rotates or zoom along with model. How I can make cursor lie at same position while rotating model. Please help me :((

      A Offline
      A Offline
      ARon_
      wrote on last edited by
      #2

      After you are done rendering the rest of the scene, reset your view matrix and projection matrix to width and height of your screen and with and ortho projection. Then Draw your cross hair in the center. It does not have to exist in world space. Basically, it is a HUB display on the near plan of your view.

      ARon

      M 1 Reply Last reply
      0
      • A ARon_

        After you are done rendering the rest of the scene, reset your view matrix and projection matrix to width and height of your screen and with and ortho projection. Then Draw your cross hair in the center. It does not have to exist in world space. Basically, it is a HUB display on the near plan of your view.

        ARon

        M Offline
        M Offline
        maheshbhoir home
        wrote on last edited by
        #3

        But I want to do only rotation on cursor (it is same as three unit axes rotates about one fixed point) which was done on entire 3d model but to avoid pan and zoom operations. How to do this ???

        A 1 Reply Last reply
        0
        • M maheshbhoir home

          But I want to do only rotation on cursor (it is same as three unit axes rotates about one fixed point) which was done on entire 3d model but to avoid pan and zoom operations. How to do this ???

          A Offline
          A Offline
          ARon_
          wrote on last edited by
          #4

          I not sure what you are trying to do but this is how I draw an object(s) in space. Forgive me it I c#.

          static public void RenderScence()
          {
          Object LockThis = new Object();
          lock (LockThis)
          {
          Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

                      #region Draw Scene From Camera View
                      #region Set the Current Camera Matrix
                      //  Get the Current Cameras View Transform and Load it in to OpenGl Projection Matrix
                      Matrix\_44 HTotal = CurrentCamera.HTotal;
                      Gl.glMatrixMode(Gl.GL\_PROJECTION);
                      Gl.glLoadIdentity();
                      //  Then I clear the ModelView Matrix as it is overriden by each Drawable Object
                      Gl.glLoadMatrixf(HTotal.GetListValues());
                      Gl.glMatrixMode(Gl.GL\_MODELVIEW);
                      Gl.glLoadIdentity();
          
                      #endregion
                      //  Draw each visible object
                      foreach (ISpatialNode obj in WorldObjectList)
                          if (obj.IsVisible) obj.RenderOpenGL();
                      #endregion
          
                      Gl.glFinish();
                  }
              }
          

          Base on your description I would create a crosshair class and maintain it orientation and position in the world. When it is time to render the crosshair I would just loaded its Position and Rotation in to the ModelView matrix and then draw it as if it were in free space. References. My article on transforms easily adapted to c++. Space and Matrix Transformations - Building a 3D Engine[^] Others probably better Interactive Techniques in Three-dimensional Scenes (Part 1): Moving 3D Objects with the Mouse using OpenGL 2.1[^] A New Perspective on Viewing[^] Arcball Module in C# - Tao.OpenGL[^]

          M 1 Reply Last reply
          0
          • A ARon_

            I not sure what you are trying to do but this is how I draw an object(s) in space. Forgive me it I c#.

            static public void RenderScence()
            {
            Object LockThis = new Object();
            lock (LockThis)
            {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

                        #region Draw Scene From Camera View
                        #region Set the Current Camera Matrix
                        //  Get the Current Cameras View Transform and Load it in to OpenGl Projection Matrix
                        Matrix\_44 HTotal = CurrentCamera.HTotal;
                        Gl.glMatrixMode(Gl.GL\_PROJECTION);
                        Gl.glLoadIdentity();
                        //  Then I clear the ModelView Matrix as it is overriden by each Drawable Object
                        Gl.glLoadMatrixf(HTotal.GetListValues());
                        Gl.glMatrixMode(Gl.GL\_MODELVIEW);
                        Gl.glLoadIdentity();
            
                        #endregion
                        //  Draw each visible object
                        foreach (ISpatialNode obj in WorldObjectList)
                            if (obj.IsVisible) obj.RenderOpenGL();
                        #endregion
            
                        Gl.glFinish();
                    }
                }
            

            Base on your description I would create a crosshair class and maintain it orientation and position in the world. When it is time to render the crosshair I would just loaded its Position and Rotation in to the ModelView matrix and then draw it as if it were in free space. References. My article on transforms easily adapted to c++. Space and Matrix Transformations - Building a 3D Engine[^] Others probably better Interactive Techniques in Three-dimensional Scenes (Part 1): Moving 3D Objects with the Mouse using OpenGL 2.1[^] A New Perspective on Viewing[^] Arcball Module in C# - Tao.OpenGL[^]

            M Offline
            M Offline
            maheshbhoir home
            wrote on last edited by
            #5

            Thanks for ur reply. I have tried to reset the modelview matrix and projection matrix and I am successful to display axes 90 percent, the remaining problem is that while rotating the 3d model flickers (at start zoom - in and at end at normal size and position). I have tried this code after finishing of rest 3d model drawing :

            glMatrixMode(GL_PROJECTION);
            glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
            glLoadIdentity(); // reset projection matrix

            GLdouble gldAspectRatio = (GLdouble) ViewportSize.right/(GLdouble) ViewportSize.bottom;

            glOrtho ( pViewVolumeAxe[0], pViewVolumeAxe[1],
            pViewVolumeAxe[2]/gldAspectRatio,
            pViewVolumeAxe[3]/gldAspectRatio,
            pViewVolumeAxe[4], pViewVolumeAxe[5] );

            glMultMatrixd(projMatrix);

            glMatrixMode(GL_MODELVIEW);
            glGetDoublev (GL_MODELVIEW_MATRIX ,modelMatrix);
            glLoadIdentity(); // reset model view matrix

            // do reverse translation as to keep axes at same position
            glTranslated(-modelMatrix[12], -modelMatrix[13], -modelMatrix[13]);

            glMultMatrixd(modelMatrix);

            // translate whole setup to model center currently I am // displaying at origin(0,0,0)
            glPushMatrix();
            glTranslatef(fXTrans, fYTrans, fZTrans);

            // then all axes draw stuff

            1 Reply Last reply
            0
            • M maheshbhoir home

              Hi, I am displaying 3D models using opengl and VC++. I have implemented code to rotate, move, zoom the model by moving camera (using gluLookAt() ) Now I want to add one cross hair cursor in front of camera view port(at center of screen), but when I tried to rotate or zoom the model the cursor also rotates or zoom along with model. How I can make cursor lie at same position while rotating model. Please help me :((

              J Offline
              J Offline
              Jazimo Printing
              wrote on last edited by
              #6

              To create a crosshair cursor that remains fixed at the center of the viewport while allowing your 3D model to rotate and zoom, you'll need to render the crosshair after the 3D scene is drawn, but before you swap buffers or display the result. Here’s how you can achieve this: Step-by-Step Approach Render the 3D Scene: Continue using your current camera transformations (like gluLookAt()) to render the 3D model. Set Up the 2D View for the Crosshair: After rendering the 3D scene, switch to orthographic projection mode for rendering the crosshair. Use glMatrixMode(GL_PROJECTION) and glLoadIdentity() to set up the orthographic projection. Set the view volume using glOrtho(). Draw the Crosshair: Once in the orthographic projection mode: Set the color and draw the crosshair at the center of the screen. Use glBegin() and glEnd() to create lines or a texture for the crosshair. Return to the 3D View: After drawing the crosshair, switch back to the perspective projection to prepare for the next frame. Jazimo

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups