How to Apply NeHe gluUnProject example to OpenGL Gadget?

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

How to Apply NeHe gluUnProject example to OpenGL Gadget?

Post by IdeasVacuum »

From C code: http://nehe.gamedev.net/article/using_g ... ject/16013
PB Cube Example (cut-down)

Code: Select all

Enumeration
#Win
#Ogl
#Timer
EndEnumeration

Procedure DrawCube()
;#-------------------
  SetGadgetAttribute(#Ogl, #PB_OpenGL_SetContext, #True)

  glPushMatrix_()              ;Save the original Matrix coordinates
  glMatrixMode_(#GL_MODELVIEW)

  glTranslatef_(0, 0, 1)  ;move it forward a bit

     gluLookAt_(0, 0, 1, 0, 0, 0, 0, 1, 0)
     glRotated_(40, 1, 0, 0)
     glRotated_(45, 0, 1, 0)
        glHint_(#GL_PERSPECTIVE_CORRECTION_HINT, #GL_NICEST)
       glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)

  ; draw the faces of a cube

  ; draw colored faces

  glDisable_(#GL_LIGHTING)
  glBegin_  (#GL_QUADS)

  glNormal3f_ (0,0,1.0)
  glColor3f_  (0,0,1.0)
  glVertex3f_ (0.5,0.5,0.5)
  glColor3f_  (0,1.0,1.0)
  glVertex3f_ (-0.5,0.5,0.5)
  glColor3f_  (1.0,1.0,1.0)
  glVertex3f_ (-0.5,-0.5,0.5)
  glColor3f_  (0,0,0)
  glVertex3f_ (0.5,-0.5,0.5)

  glNormal3f_ (0,0,-1.0)
  glColor3f_  (0,0,1.0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glColor3f_  (0,0,1.0)
  glVertex3f_ (-0.5,0.5,-0.5)
  glColor3f_  (1.0,1.0,1.0)
  glVertex3f_ (0.5,0.5,-0.5)
  glColor3f_  (1.0,1.0,1.0)
  glVertex3f_ (0.5,-0.5,-0.5)

  glEnd_()

  ; draw shaded faces

  glEnable_(#GL_LIGHTING)
  glEnable_(#GL_LIGHT0)
  glBegin_ (#GL_QUADS)

  glNormal3f_ (   0, 1.0,   0)
  glVertex3f_ ( 0.5, 0.5, 0.5)
  glVertex3f_ ( 0.5, 0.5,-0.5)
  glVertex3f_ (-0.5, 0.5,-0.5)
  glVertex3f_ (-0.5, 0.5, 0.5)

  glNormal3f_ (0,-1.0,0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glVertex3f_ (0.5,-0.5,-0.5)
  glVertex3f_ (0.5,-0.5,0.5)
  glVertex3f_ (-0.5,-0.5,0.5)

  glNormal3f_ (1.0,0,0)
  glVertex3f_ (0.5,0.5,0.5)
  glVertex3f_ (0.5,-0.5,0.5)
  glVertex3f_ (0.5,-0.5,-0.5)
  glVertex3f_ (0.5,0.5,-0.5)

  glNormal3f_ (-1.0,   0,   0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glVertex3f_ (-0.5,-0.5, 0.5)
  glVertex3f_ (-0.5, 0.5, 0.5)
  glVertex3f_ (-0.5, 0.5,-0.5)

  glEnd_()

  glPopMatrix_()
  glFinish_()

  SetGadgetAttribute(#Ogl, #PB_OpenGL_FlipBuffers, #True)

EndProcedure

Procedure GetPickPoint()
;#----------------------
;http://nehe.gamedev.net/article/using_gluunproject/16013
Protected Dim         Viewport.l(3)  ;X, Y, Width, Height
Protected Dim  ModelViewMatrix.d(15)
Protected Dim ProjectionMatrix.d(15)
Protected  dWinX.d, dWinY.d, dWinZ.d
Protected  *dPosX = AllocateMemory(SizeOf(Double))
Protected  *dPosY = AllocateMemory(SizeOf(Double))
Protected  *dPosZ = AllocateMemory(SizeOf(Double))

              glGetIntegerv_(#GL_VIEWPORT, @Viewport(0))
               glGetDoublev_(#GL_MODELVIEW_MATRIX, @ModelViewMatrix(0))
               glGetDoublev_(#GL_PROJECTION_MATRIX, @ProjectionMatrix(0))

                   dWinX = GetGadgetAttribute(#Ogl, #PB_OpenGL_MouseX)
                   dWinY = GetGadgetAttribute(#Ogl, #PB_OpenGL_MouseY)

               glReadPixels_(dWinX, dWinY, 1, 1, #GL_DEPTH_COMPONENT, #GL_DOUBLE, @dWinZ) ;surely always 0.0?
               gluUnProject_(dWinX, dWinY, dWinZ, @ModelViewMatrix(0), @ProjectionMatrix(0), @Viewport(0), *dPosX, *dPosY, *dPosZ)

               ;Debug "dWinX-->" + StrD(dWinX,4) + "<--"
               ;Debug "dWinY-->" + StrD(dWinY,4) + "<--"
               ;Debug "dWinZ-->" + StrD(dWinZ,4) + "<--"

               ;Debug "Viewport(0)-->" + StrD(Viewport(0),2) + "<--"
               ;Debug "Viewport(1)-->" + StrD(Viewport(1),2) + "<--"
               ;Debug "Viewport(2)-->" + StrD(Viewport(2),2) + "<--"
               ;Debug "Viewport(3)-->" + StrD(Viewport(3),2) + "<--"

               Debug "dPosX-->" + PeekD(*dPosX) + "<--"
               Debug "dPosY-->" + PeekD(*dPosY) + "<--"
               Debug "dPosZ-->" + PeekD(*dPosZ) + "<--"
               Debug "========================"
EndProcedure

Procedure SetupGL()
;#------------------
    glMatrixMode_(#GL_PROJECTION)
  gluPerspective_(30.0, 590/390, 1.0, 10.0)

  ; position viewer
  glMatrixMode_(#GL_MODELVIEW)

  glTranslatef_(0, 0, -5.0)

      glEnable_(#GL_DEPTH_TEST)
      ;glEnable_(#GL_CULL_FACE)
  glShadeModel_(#GL_SMOOTH)
 glPolygonMode_(#GL_FRONT_AND_BACK, #GL_LINE)

EndProcedure

OpenWindow(#Win, 0, 0, 600, 400, "OpenGL Gadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

OpenGLGadget(#Ogl, 5, 5, 590, 390)
SetupGL()

AddWindowTimer(#Win, #Timer, 16) ; about 60 fps

Repeat

  Event = WaitWindowEvent()

  Select Event

          Case #PB_Event_Timer: If(EventTimer() = #Timer) : DrawCube() : EndIf

          Case #PB_Event_Gadget

               Select EventGadget()

                     Case #Ogl

                         Select EventType()

                                    Case #PB_EventType_LeftButtonUp: GetPickPoint()
                         EndSelect
               EndSelect
  EndSelect

Until Event = #PB_Event_CloseWindow

End
In theory, OpenGL coordinates should be returned but that doesn't happen......
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
alter Mann
User
User
Posts: 39
Joined: Fri Oct 17, 2014 8:52 pm

Re: How to Apply NeHe gluUnProject example to OpenGL Gadget?

Post by alter Mann »

glReadPixels_(dWinX, dWinY, 1, 1, #GL_DEPTH_COMPONENT, #GL_DOUBLE, @dWinZ) ;surely always 0.0?
only returns a float value
It should be

Code: Select all

Protected dWinX.f, dWinY.f, dWinZ.f
glReadPixels_(dWinX, dWinY, 1, 1, #GL_DEPTH_COMPONENT, #GL_FLOAT, @dWinZ) ;surely always 0.0?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: How to Apply NeHe gluUnProject example to OpenGL Gadget?

Post by IdeasVacuum »

Hi alter Mann

I changed those values to floats but it has no effect, good or bad.

Edit: - just different.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
alter Mann
User
User
Posts: 39
Joined: Fri Oct 17, 2014 8:52 pm

Re: How to Apply NeHe gluUnProject example to OpenGL Gadget?

Post by alter Mann »

the problem is

Code: Select all

glPushMatrix_()
and

Code: Select all

glPopMatrix_()
before getting the ModelViewMatrix.
Therefore the matrix is always the same.

I've changed you code a little bit :wink: with a long time for changing the view

Code: Select all

Enumeration
#Win
#Ogl
#Timer
EndEnumeration

Procedure DrawCube()
;#-------------------
  SetGadgetAttribute(#Ogl, #PB_OpenGL_SetContext, #True)


  ; draw the faces of a cube

  ; draw colored faces

  glDisable_(#GL_LIGHTING)
  glBegin_  (#GL_QUADS)

  glNormal3f_ (0,0,1.0)
  glColor3f_  (0,0,1.0)
  glVertex3f_ (0.5,0.5,0.5)
  glColor3f_  (0,1.0,1.0)
  glVertex3f_ (-0.5,0.5,0.5)
  glColor3f_  (1.0,1.0,1.0)
  glVertex3f_ (-0.5,-0.5,0.5)
  glColor3f_  (0,0,0)
  glVertex3f_ (0.5,-0.5,0.5)

  glNormal3f_ (0,0,-1.0)
  glColor3f_  (0,0,1.0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glColor3f_  (0,0,1.0)
  glVertex3f_ (-0.5,0.5,-0.5)
  glColor3f_  (1.0,1.0,1.0)
  glVertex3f_ (0.5,0.5,-0.5)
  glColor3f_  (1.0,1.0,1.0)
  glVertex3f_ (0.5,-0.5,-0.5)

  glEnd_()

  ; draw shaded faces

  glEnable_(#GL_LIGHTING)
  glEnable_(#GL_LIGHT0)
  glBegin_ (#GL_QUADS)

  glNormal3f_ (   0, 1.0,   0)
  glVertex3f_ ( 0.5, 0.5, 0.5)
  glVertex3f_ ( 0.5, 0.5,-0.5)
  glVertex3f_ (-0.5, 0.5,-0.5)
  glVertex3f_ (-0.5, 0.5, 0.5)

  glNormal3f_ (0,-1.0,0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glVertex3f_ (0.5,-0.5,-0.5)
  glVertex3f_ (0.5,-0.5,0.5)
  glVertex3f_ (-0.5,-0.5,0.5)

  glNormal3f_ (1.0,0,0)
  glVertex3f_ (0.5,0.5,0.5)
  glVertex3f_ (0.5,-0.5,0.5)
  glVertex3f_ (0.5,-0.5,-0.5)
  glVertex3f_ (0.5,0.5,-0.5)

  glNormal3f_ (-1.0,   0,   0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glVertex3f_ (-0.5,-0.5, 0.5)
  glVertex3f_ (-0.5, 0.5, 0.5)
  glVertex3f_ (-0.5, 0.5,-0.5)

  glEnd_()

  glFinish_()

  SetGadgetAttribute(#Ogl, #PB_OpenGL_FlipBuffers, #True)

EndProcedure

Procedure GetPickPoint()
;#----------------------
;http://nehe.gamedev.net/article/using_gluunproject/16013
Protected Dim         Viewport.l(3)  ;X, Y, Width, Height
Protected Dim  ModelViewMatrix.d(15)
Protected Dim ProjectionMatrix.d(15)
Protected  dWinX.f, dWinY.f, dWinZ.f
Protected  *dPosX = AllocateMemory(SizeOf(Double))
Protected  *dPosY = AllocateMemory(SizeOf(Double))
Protected  *dPosZ = AllocateMemory(SizeOf(Double))

glGetIntegerv_(#GL_VIEWPORT, @Viewport(0))

               glGetDoublev_(#GL_MODELVIEW_MATRIX, @ModelViewMatrix(0))
               glGetDoublev_(#GL_PROJECTION_MATRIX, @ProjectionMatrix(0))

                   dWinX = GetGadgetAttribute(#Ogl, #PB_OpenGL_MouseX)
                   dWinY = GetGadgetAttribute(#Ogl, #PB_OpenGL_MouseY)

               glReadPixels_(dWinX, dWinY, 1, 1, #GL_DEPTH_COMPONENT, #GL_FLOAT, @dWinZ) ;surely always 0.0?
               gluUnProject_(dWinX, dWinY, dWinZ, @ModelViewMatrix(0), @ProjectionMatrix(0), @Viewport(0), *dPosX, *dPosY, *dPosZ)

               ;Debug "dWinX-->" + StrD(dWinX,4) + "<--"
               ;Debug "dWinY-->" + StrD(dWinY,4) + "<--"
               ;Debug "dWinZ-->" + StrD(dWinZ,4) + "<--"

               ;Debug "Viewport(0)-->" + StrD(Viewport(0),2) + "<--"
               ;Debug "Viewport(1)-->" + StrD(Viewport(1),2) + "<--"
               ;Debug "Viewport(2)-->" + StrD(Viewport(2),2) + "<--"
               ;Debug "Viewport(3)-->" + StrD(Viewport(3),2) + "<--"

               Debug "dPosX-->" + PeekD(*dPosX) + "<--"
               Debug "dPosY-->" + PeekD(*dPosY) + "<--"
               Debug "dPosZ-->" + PeekD(*dPosZ) + "<--"
               Debug "========================"
EndProcedure

Procedure SetupGL()
;#------------------
    glMatrixMode_(#GL_PROJECTION)
  gluPerspective_(30.0, 590/390, 1.0, 10.0)

  ; position viewer
  glMatrixMode_(#GL_MODELVIEW)

  glTranslatef_(0, 0, -5.0)

    glPolygonMode_(#GL_FRONT_AND_BACK, #GL_FILL )
    glDepthFunc_(#GL_LEQUAL)
    glClearDepth_(1.0)
    
    glEnable_(#GL_DEPTH_TEST)
    glLightModeli_(#GL_LIGHT_MODEL_TWO_SIDE,#True)
    
    glShadeModel_(#GL_SMOOTH) 
    
    glEnable_(#GL_LIGHTING) ;Enable Lighting
    
    glShadeModel_(#GL_SMOOTH)
    
    glMatrixMode_(#GL_MODELVIEW)


     gluLookAt_(0, 0, 1, 0, 0, 0, 0, 1, 0)
   ;  glRotated_(40, 1, 0, 0)
   ;  glRotated_(45, 0, 1, 0)
        glHint_(#GL_PERSPECTIVE_CORRECTION_HINT, #GL_NICEST)
       glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)

EndProcedure

OpenWindow(#Win, 0, 0, 600, 400, "OpenGL Gadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

OpenGLGadget(#Ogl, 5, 5, 590, 390)
SetupGL()

AddWindowTimer(#Win, #Timer, 10000) ; about 60 fps
DrawCube()

Repeat

  Event = WaitWindowEvent(5)

  Select Event

    Case #PB_Event_Timer
      If(EventTimer() = #Timer) 
        glRotated_(0.2, 1, 0, 0)
        glRotated_(0.2, 0, 1, 0)
  
        glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)

        DrawCube()
      EndIf
          Case #PB_Event_Gadget

               Select EventGadget()

                     Case #Ogl

                         Select EventType()

                                    Case #PB_EventType_LeftButtonUp: GetPickPoint()
                         EndSelect
               EndSelect
  EndSelect

Until Event = #PB_Event_CloseWindow

End
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: How to Apply NeHe gluUnProject example to OpenGL Gadget?

Post by IdeasVacuum »

Right - the values are making some sense now :)

Neat idea to build a coffee break into the code! :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply