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. C / C++ / MFC
  4. Perspective Projection Opengl

Perspective Projection Opengl

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsgame-devhelpquestion
8 Posts 2 Posters 0 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.
  • J Offline
    J Offline
    jk chan
    wrote on last edited by
    #1

    hi all Currently i am doing an 3d Graphics application , which allows to calculate the center of gravity of an object. The user can control the foreces upon the object. (stuffs like that).. Here is my problem ------------------- My application using perspective projection. So While creating an object (for Ex: consider a rectangle) using mouse , the objects is not correctly positoned with mouse cursor. ie when the user click and drag in the window , the object size is different than what he/she just dragged. I am using 45 degree the angle of view and depth is from 1 to 500 Any !deas :) ? please give it to me warm regards krishnan

    If u can Dream... U can do it

    Steve EcholsS 1 Reply Last reply
    0
    • J jk chan

      hi all Currently i am doing an 3d Graphics application , which allows to calculate the center of gravity of an object. The user can control the foreces upon the object. (stuffs like that).. Here is my problem ------------------- My application using perspective projection. So While creating an object (for Ex: consider a rectangle) using mouse , the objects is not correctly positoned with mouse cursor. ie when the user click and drag in the window , the object size is different than what he/she just dragged. I am using 45 degree the angle of view and depth is from 1 to 500 Any !deas :) ? please give it to me warm regards krishnan

      If u can Dream... U can do it

      Steve EcholsS Offline
      Steve EcholsS Offline
      Steve Echols
      wrote on last edited by
      #2

      Not 100% sure, but I think you can use gluProject/gluUnProject: double modelMatrix[16]; double projMatrix[16]; int viewPort[4]; glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix); glGetDoublev(GL_PROJECTION_MATRIX, projMatrix); glGetIntegerv(GL_VIEWPORT, viewPort); double x, y, z; gluUnProject(viewPort[0] + mousex, viewPort[1] + mousey, 0.0f, modelMatrix, projMatrix, viewPort, &x, &y, &z); Might have to play around with the Y coordinate, it may be inverted.


      - S 50 cups of coffee and you know it's on!

      • S
        50 cups of coffee and you know it's on!
        Code, follow, or get out of the way.
      J 1 Reply Last reply
      0
      • Steve EcholsS Steve Echols

        Not 100% sure, but I think you can use gluProject/gluUnProject: double modelMatrix[16]; double projMatrix[16]; int viewPort[4]; glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix); glGetDoublev(GL_PROJECTION_MATRIX, projMatrix); glGetIntegerv(GL_VIEWPORT, viewPort); double x, y, z; gluUnProject(viewPort[0] + mousex, viewPort[1] + mousey, 0.0f, modelMatrix, projMatrix, viewPort, &x, &y, &z); Might have to play around with the Y coordinate, it may be inverted.


        - S 50 cups of coffee and you know it's on!

        J Offline
        J Offline
        jk chan
        wrote on last edited by
        #3

        Thanks for the reply & code One more thing to clarify ( currently i am thinking about it). Pls go through this. i think you know the function gluPersPective(...,near,Far); For a perspective projection the coordiantes are normalised. ie (-1,0) , (1,0)is equal to left to right through the Orgin .. is it ? I want to know the units of this near and Far parameters :confused: ? is that also normalised coordinates ? Thanks in advance krishnan

        If u can Dream... U can do it

        Steve EcholsS 1 Reply Last reply
        0
        • J jk chan

          Thanks for the reply & code One more thing to clarify ( currently i am thinking about it). Pls go through this. i think you know the function gluPersPective(...,near,Far); For a perspective projection the coordiantes are normalised. ie (-1,0) , (1,0)is equal to left to right through the Orgin .. is it ? I want to know the units of this near and Far parameters :confused: ? is that also normalised coordinates ? Thanks in advance krishnan

          If u can Dream... U can do it

          Steve EcholsS Offline
          Steve EcholsS Offline
          Steve Echols
          wrote on last edited by
          #4

          krishnadevank wrote:

          For a perspective projection the coordiantes are normalised. ie (-1,0) , (1,0)is equal to left to right through the Orgin .. is it ?

          Yes, if your glScale is setup normally. But because of perspective, (-1, 0, 0)- (1, 0, 0) will not visually equal (0, 0, 1)-(0, 0, -1).

          krishnadevank wrote:

          I want to know the units of this near and Far parameters ? is that also normalised coordinates ?

          OpenGL is inherently unit-less - you define what 1 means (i.e. it could be an inch, foot, meter, or an A.U.) The near and far planes, have to do with the z-buffer, which is non-linear. Objects up close have more precision. Google on gluPerspective, z-fighting and the depth buffer for more info. Here's some info (not a very pretty site): http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html[^] (My old computer died, otherwise I'd provide you with a better link :)).


          - S 50 cups of coffee and you know it's on!

          • S
            50 cups of coffee and you know it's on!
            Code, follow, or get out of the way.
          J 1 Reply Last reply
          0
          • Steve EcholsS Steve Echols

            krishnadevank wrote:

            For a perspective projection the coordiantes are normalised. ie (-1,0) , (1,0)is equal to left to right through the Orgin .. is it ?

            Yes, if your glScale is setup normally. But because of perspective, (-1, 0, 0)- (1, 0, 0) will not visually equal (0, 0, 1)-(0, 0, -1).

            krishnadevank wrote:

            I want to know the units of this near and Far parameters ? is that also normalised coordinates ?

            OpenGL is inherently unit-less - you define what 1 means (i.e. it could be an inch, foot, meter, or an A.U.) The near and far planes, have to do with the z-buffer, which is non-linear. Objects up close have more precision. Google on gluPerspective, z-fighting and the depth buffer for more info. Here's some info (not a very pretty site): http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html[^] (My old computer died, otherwise I'd provide you with a better link :)).


            - S 50 cups of coffee and you know it's on!

            J Offline
            J Offline
            jk chan
            wrote on last edited by
            #5

            Once again Thanks :rose: Suppose i would setup the my projection with 45 as fov and near = 1 and far = 500. According to my knowledge the default eye location is at Orgin and looking towards in to the screen. so if i drawn a polygon/rectangle like this (Covering the entire screen. ) , it maynot be visible. glBegin(GL_POLYGON); glVertex3f(-1,1,0); glVertex3f(1,1,0); glVertex3f(1,-1,0); glVertex3f(-1,-1,0); glEnds(); For making this visible(Convering the entire screen ) how much exact translation i want to do in Z direction . ? Here i am confused with the parameters of gluPersPective function. What you thinks ?

            If u can Dream... U can do it

            Steve EcholsS 1 Reply Last reply
            0
            • J jk chan

              Once again Thanks :rose: Suppose i would setup the my projection with 45 as fov and near = 1 and far = 500. According to my knowledge the default eye location is at Orgin and looking towards in to the screen. so if i drawn a polygon/rectangle like this (Covering the entire screen. ) , it maynot be visible. glBegin(GL_POLYGON); glVertex3f(-1,1,0); glVertex3f(1,1,0); glVertex3f(1,-1,0); glVertex3f(-1,-1,0); glEnds(); For making this visible(Convering the entire screen ) how much exact translation i want to do in Z direction . ? Here i am confused with the parameters of gluPersPective function. What you thinks ?

              If u can Dream... U can do it

              Steve EcholsS Offline
              Steve EcholsS Offline
              Steve Echols
              wrote on last edited by
              #6

              Not sure you can ever get an exact z position, but you could try backing up on the z-axis by a small increment just outside of the near plane, like glTranslatef(0, 0, -1.001). You might have better results switching into ortho mode, using glOrtho.


              - S 50 cups of coffee and you know it's on!

              • S
                50 cups of coffee and you know it's on!
                Code, follow, or get out of the way.
              J 2 Replies Last reply
              0
              • Steve EcholsS Steve Echols

                Not sure you can ever get an exact z position, but you could try backing up on the z-axis by a small increment just outside of the near plane, like glTranslatef(0, 0, -1.001). You might have better results switching into ortho mode, using glOrtho.


                - S 50 cups of coffee and you know it's on!

                J Offline
                J Offline
                jk chan
                wrote on last edited by
                #7

                Actually first i did my program in orthographic projection :). it lacks good looks. that why i decided to change it to perspective projection. I will try your suggestion today and will inform you. Thanks & regards krishnan

                If u can Dream... U can do it

                1 Reply Last reply
                0
                • Steve EcholsS Steve Echols

                  Not sure you can ever get an exact z position, but you could try backing up on the z-axis by a small increment just outside of the near plane, like glTranslatef(0, 0, -1.001). You might have better results switching into ortho mode, using glOrtho.


                  - S 50 cups of coffee and you know it's on!

                  J Offline
                  J Offline
                  jk chan
                  wrote on last edited by
                  #8

                  Hi steve I tried with a value -1.0 for tranlation. I think there still exists some problems. I put a point on the screen using GL_POINT. When the point location was at (0,0) every thing loooks fine , ie the point was on the orgin(0,0,0). then i tried with value (1,0,0) , and translation in z direction glTranslate(0,0,-1). But the result is intersting. it vanished from the screen. ( :(( ) . Then i checked it by resizing the window , then it became visible at some time. There must be problem with aspect ratio i think.(the asepect ration i put was width/height). Any thoughts ? Thanks krishnan

                  If u can Dream... U can do it

                  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