Source Code for Project Solid


The following source files, led by gray banners, contain all the class functions needed for Project Solid.  The sections highlighted by a yellow background Color are manually entered.  Code sections with white background color are generated by the SansGUI Source Code Framework.  The source file is compiled into a dynamic linked library to be invoked by SansGUI during simulation runs.

For more details about this example, please read the Solid On-Line Manual.

Implementation in Fortran (CVF)

Functions in Class Graphics.Solid    [Go To Top]

! Graphics_Solid.f
! - DLL routines for class <Reference>Graphics.Solid
! DATE: Thursday, February 20, 2003 TIME: 02:04:29 PM
! The skeleton of this file is generated by SansGUI(tm)

! Attribute indices in class version [1.0.alpha.5]
! 1: iTracker - Selection Tracker
! 2: iSelect - Selection Area [x1 y1 x2 y2]
! 3: iWinSize - Window Size [x y]
! 4: fScale - Scaling Factors [x y z]
! 5: fRotate - Rotation Angles [x y z]
! 6: fTranslate - Translation Vector [x y z]
! 7: iFlags - Control Flags (Reserved)
! 8: sExtFile - External File (Reserved)
! 9: iShape - 3D Object Shape
! 10: iColor - Color of Object
! 11: iType - Display Method
! 12: fAngle - Current Angle

! ======================================================================
! SG_xBgnRun - Begin Run
! ----------------------------------------------------------------------
      integer function SG_xBgnRun_Graphics_Solid(self,                  &
     &                        simCtrl, chgChild,                        &
     &                        pRefObjs, iRefObjs,                       &
     &                        pAdjObjs, iAdjObjs,                       &
     &                        pLnkObjs, iLnkObjs,                       &
     &                        cMessage, cCommand, pOutFile )
!DEC$ IF DEFINED (_DLL)
!DEC$ ATTRIBUTES DLLEXPORT :: SG_xBgnRun_Graphics_Solid
!DEC$ END IF

      use dfopngl

      include "SGdllf.h"

      ! TODO: declare your local variables here

      include "../Solid_1_0F/Solid_1_0F.h"
      integer, dimension(*) :: iTracker
      logical :: bIsPrinting, bIsEditing
      POINTER(PTR_iTracker, iTracker)

      if (self%nSGobjSchema .ne. SG_OBJ_SCHEMA) then
          SG_xBgnRun_Graphics_Solid = SG_R_SCHM
          return
      end if

      ! TODO: put your simulator code here

      ! NOTE: When the SG_STAT_EDIT bit is set, this routine is called by the
      ! SansGUI Framework in the case that the rendering context of the
      ! OpenGL(R) window needs to be set up. If the bit is not set,
      ! this routine is called during a simulation run with the regular
      ! SansGUI DLL function call semantics.

      if (JIAND(self%iStatus, SG_STAT_EDIT) .ne. 0) then
          ! when the OpenGL rendering context needs to be set up
          PTR_zValues = self%pzValues
          PTR_iTracker = zValues(SG_NDX_ITRACKER)%vData
          iTracker(1) = TRACK_RECT ! set selection tracker type

          call FGLENABLE(GL_DEPTH_TEST) ! hidden surface removal
          call FGLENABLE(GL_COLOR_MATERIAL)

          if (JIAND(self%iStatus, SG_STAT_PRINT) .ne. 0) then ! print to printer
              call FGLCLEARCOLOR(1.0, 1.0, 1.0, 1.0) ! white background
          else
              call FGLCLEARCOLOR(0.0, 0.0, 0.7, 1.0) ! dark blue background
          end if

          call FGLENABLE(GL_LIGHTING);
          call FGLLIGHTFV(GL_LIGHT0, GL_AMBIENT, LOC(fAmbientLight))
          call FGLLIGHTFV(GL_LIGHT0, GL_DIFFUSE, LOC(fDiffuseLight))
          call FGLLIGHTFV(GL_LIGHT0, GL_SPECULAR, LOC(fSpecular))
          call FGLLIGHTFV(GL_LIGHT0, GL_POSITION, LOC(fLightPos))
          call FGLENABLE(GL_LIGHT0)

          call FGLCOLORMATERIAL(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
          call FGLMATERIALFV(GL_FRONT, GL_SPECULAR, LOC(fSpecular))
          call FGLMATERIALI(GL_FRONT, GL_SHININESS, 100)
      end if

      SG_xBgnRun_Graphics_Solid = SG_R_OK
      return
      end

! ======================================================================
! SG_xPreEval - Pre-Evaluation
! ----------------------------------------------------------------------
      integer function SG_xPreEval_Graphics_Solid(self,                 &
     &                        simCtrl, chgChild,                        &
     &                        pRefObjs, iRefObjs,                       &
     &                        pAdjObjs, iAdjObjs,                       &
     &                        pLnkObjs, iLnkObjs,                       &
     &                        cMessage, cCommand, pOutFile )
!DEC$ IF DEFINED (_DLL)
!DEC$ ATTRIBUTES DLLEXPORT :: SG_xPreEval_Graphics_Solid
!DEC$ END IF

      use dfopngl

      include "SGdllf.h"

      ! TODO: declare your local variables here

      include "../Solid_1_0F/Solid_1_0F.h"
      integer, dimension(*) :: iSize
      integer :: iWidth, iHeight
      real*4, dimension(*) :: fScale, fTrans
      real*4 :: fRange, fRatio, fScaleFac
      POINTER(PTR_iWinSize, iSize)
      POINTER(PTR_fScale, fScale)
      POINTER(PTR_fTrans, fTrans)

      if (self%nSGobjSchema .ne. SG_OBJ_SCHEMA) then
          SG_xPreEval_Graphics_Solid = SG_R_SCHM
          return
      end if

      ! TODO: put your simulator code here

      ! NOTE: When the SG_STAT_EDIT bit is set, this routine is called by the
      ! SansGUI Framework in the case that the graphics window is being
      ! resized. If the bit is not set, this routine is called during
      ! a simulation run and only when the screen refreshing cycle is
      ! reached.

      if (JIAND(self%iStatus, SG_STAT_EDIT) .ne. 0) then
          PTR_zValues = self%pzValues
          PTR_iWinSize = zValues(SG_NDX_IWINSIZE)%vData
          PTR_fScale = zValues(SG_NDX_FSCALE)%vData
          PTR_fTrans = zValues(SG_NDX_FTRANSLATE)%vData

          fRange = 100.
          iWidth = iSize(1)
          iHeight = iSize(2)
          if (iWidth .le. iHeight) then
              fRatio = fRange * REAL(iHeight) / REAL(iWidth)
          else
              fRatio = fRange * REAL(iWidth) / REAL(iHeight)
          end if
          fScaleFac = 30. * fScale(3) ! use only Z scaling factor

          ! Set Viewport to window's full client area
          call FGLVIEWPORT(0, 0, iWidth, iHeight)

          ! Reset coordinate system
          call FGLMATRIXMODE(GL_PROJECTION)
          call FGLLOADIDENTITY()

          ! Establish clipping volume (left, right, bottom, top, near, far)
          if (iWidth .le. iHeight) then
              call FGLORTHO(-fRange,fRange,-fRatio,fRatio,-fRange,fRange)
          else
              call FGLORTHO(-fRatio,fRatio,-fRange,fRange,-fRange,fRange)
          end if

          ! Set up transformation to move objects away from the origin
          ! SansGUI normalizes X and Y elements to the range [-1..1]
          call FGLTRANSLATEF(fTrans(1) * 100., fTrans(2) * 100., -5.)

          ! Respond to user selected zoom factor, apply fScale universally
          ! The developer can use X and Y elements for their own purposes
          call FGLSCALEF(fScaleFac, fScaleFac, fScaleFac)

          call FGLMATRIXMODE(GL_MODELVIEW)
      end if

      SG_xPreEval_Graphics_Solid = SG_R_OK
      return
      end

! ======================================================================
! SG_xEval - Evaluation
! ----------------------------------------------------------------------
      integer function SG_xEval_Graphics_Solid(self,                    &
     &                        simCtrl, chgChild,                        &
     &                        pRefObjs, iRefObjs,                       &
     &                        pAdjObjs, iAdjObjs,                       &
     &                        pLnkObjs, iLnkObjs,                       &
     &                        cMessage, cCommand, pOutFile )
!DEC$ IF DEFINED (_DLL)
!DEC$ ATTRIBUTES DLLEXPORT :: SG_xEval_Graphics_Solid
!DEC$ END IF

      use dfopngl

      include "SGdllf.h"

      ! TODO: declare your local variables here

      include "../Solid_1_0F/Solid_1_0F.h"
      real*4, dimension(*) :: fRotate
      real*4, dimension(*) :: fAngle
      integer, dimension(*) :: iSelect
      character*256 :: cStr
      POINTER(PTR_fRotate, fRotate)
      POINTER(PTR_fAngle, fAngle)
      POINTER(PTR_iSelect, iSelect)

      if (self%nSGobjSchema .ne. SG_OBJ_SCHEMA) then
          SG_xEval_Graphics_Solid = SG_R_SCHM
          return
      end if

      ! TODO: put your simulator code here

      ! NOTE: When the SG_STAT_EDIT bit is set, this routine is called by the
      ! SansGUI Framework in the case that the user made a selection
      ! rectangle or, in a special case, a selection point. If the bit
      ! is not set, this routine is called during a simulation run with
      ! the regular SansGUI DLL function call semantics.

      PTR_zValues = self%pzValues ! fetch data object values

      if (JIAND(self%iStatus, SG_STAT_EDIT) .ne. 0) then
          ! when the user selected a rectangular area of the graphics window
          ! iSelect[4] is an integer array containing four elements, or two
          ! pairs of X-Y coordinates corresponding to two corners of the user
          ! selection box:
          ! iSelect[0] - the X coordinate of the selection area when the mouse
          ! button is down
          ! iSelect[1] - the Y coordinate of the selection area when the mouse
          ! button is down
          ! iSelect[2] - the X coordinate of the selection area when the mouse
          ! button is released
          ! iSelect[3] - the Y coordinate of the selection area when the mouse
          ! button is released
          ! The values of these coordinates are not normalized, meaning that
          ! they are raw window coordinates in pixels (0, 0 at upper left
          ! corner of the graphics window) and reflecting the first and the
          ! second points of the rectangular area the user selected. They are
          ! NOT sorted to reflect upper-left and then lower-right corners.
          ! When the user clicks on a point without dragging, the two points
          ! contains the same coordinate pair.

          PTR_iSelect = zValues(SG_NDX_ISELECT)%vData
          WRITE (cStr, 100) iSelect(1),iSelect(2),iSelect(3),iSelect(4)
  100 FORMAT('user selection is (',I,', ',I,') (',I,', ',I,')')

          ! In this example, we simply let SansGUI to display the message. The
          ! number 17 is just an arbitary message code defined by the developer.
          ! the message will be displayed in the Message View at the bottom pane
          ! of the document window within SansGUI.
          cMessage = TRIM(cStr)//CHAR(0)
          SG_xEval_Graphics_Solid = SG_R_LMSG + 17
      else
          ! when this routine is called in a sumulation run, simply rotate the object
          PTR_fAngle = zValues(SG_NDX_FANGLE)%vData
          PTR_fRotate = zValues(SG_NDX_FROTATE)%vData
          fAngle(1) = fRotate(2) + 5. ! use rotation angle about Y axis
          if (fAngle(1) .gt. 180.) then
              fAngle(1) = -(360. - fAngle(1))
          end if
          fRotate(2) = fAngle(1) ! rotate about the Y (vertical) axis

          SG_xEval_Graphics_Solid = SG_R_OK
      end if

      return
      end

! ======================================================================
! SG_xPostEval - Post-Evaluation
! ----------------------------------------------------------------------
      integer function SG_xPostEval_Graphics_Solid(self,                &
     &                        simCtrl, chgChild,                        &
     &                        pRefObjs, iRefObjs,                       &
     &                        pAdjObjs, iAdjObjs,                       &
     &                        pLnkObjs, iLnkObjs,                       &
     &                        cMessage, cCommand, pOutFile )
!DEC$ IF DEFINED (_DLL)
!DEC$ ATTRIBUTES DLLEXPORT :: SG_xPostEval_Graphics_Solid
!DEC$ END IF

      use dfopngl

      include "SGdllf.h"

      ! TODO: declare your local variables here

      include "../Solid_1_0F/Solid_1_0F.h"
      real*4, dimension(*) :: fRotate
      integer, dimension(*) :: iShape
      integer, dimension(*) :: iType
      integer, dimension(*) :: iColor
      POINTER(PTR_fRotate, fRotate)
      POINTER(PTR_iShape, iShape)
      POINTER(PTR_iType, iType)
      POINTER(PTR_iColor, iColor)

      if (self%nSGobjSchema .ne. SG_OBJ_SCHEMA) then
          SG_xPostEval_Graphics_Solid = SG_R_SCHM
          return
      end if

      ! TODO: put your simulator code here

      ! NOTE: When the SG_STAT_EDIT bit is set, this routine is called by the
      ! SansGUI Framework in the case that the graphics window needs to
      ! be redrawn, as to render the scene. If the bit is not set, this
      ! routine is called during a simulation run and only when the
      ! screen refreshing cycle is reached.
      !
      ! In this example, we perform scene rendering whether the SG_STAT_EDIT
      ! bit is set or not.

      PTR_zValues = self%pzValues
      PTR_fRotate = zValues(SG_NDX_FROTATE)%vData
      PTR_iShape = zValues(SG_NDX_ISHAPE)%vData
      PTR_iType = zValues(SG_NDX_ITYPE)%vData
      PTR_iColor = zValues(SG_NDX_ICOLOR)%vData

      ! Clear the window with current clearing color
      call FGLCLEAR(JIOR(GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT))

      ! Save the original matrix
      call FGLPUSHMATRIX()

      ! Rotate the object back to (0, 0, 0) to rotate with absolute angles
      call FGLLOADIDENTITY()

      ! Do rotation with absolute angles
      call FGLROTATEF(fRotate(1), 1.0, 0.0, 0.0)
      call FGLROTATEF(fRotate(2), 0.0, 1.0, 0.0)
      call FGLROTATEF(fRotate(3), 0.0, 0.0, 1.0)

 

      select case (iColor(1))
      case (COLOR_CYAN)
          call FGLCOLOR3F(0.0, 1.0, 1.0)
      case (COLOR_MAGENTA)
          call FGLCOLOR3F(1.0, 0.0, 1.0)
      case (COLOR_YELLOW)
          call FGLCOLOR3F(1.0, 1.0, 0.0)
      case default ! white object
          call FGLCOLOR3F(1.0, 1.0, 1.0)
      end select

 

      select case (iShape(1))
      case (SHAPE_TETRAHEDRON)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDTETRAHEDRON(2.0)
          else
              call FAUXWIRETETRAHEDRON(2.0)
          end if
      case (SHAPE_CUBE)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDCUBE(2.0)
          else
              call FAUXWIRECUBE(2.0)
          end if
      case (SHAPE_OCTAHEDRON)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDOCTAHEDRON(1.5)
          else
              call FAUXWIREOCTAHEDRON(1.5)
          end if
      case (SHAPE_DODECAHEDRON)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDDODECAHEDRON(1.5)
          else
              call FAUXWIREDODECAHEDRON(1.5)
          end if
      case (SHAPE_ICOSAHEDRON)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDICOSAHEDRON(2.0)
          else
              call FAUXWIREICOSAHEDRON(2.0)
          end if
      case (SHAPE_BOX)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDBOX(2.0, 1.0, 3.0)
          else
              call FAUXWIREBOX(2.0, 1.0, 3.0)
          end if
      case (SHAPE_CYLINDER)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDCYLINDER(1.0, 2.0)
          else
              call FAUXWIRECYLINDER(1.0, 2.0)
          end if
      case (SHAPE_CONE)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDCONE(1.0, 2.0)
          else
              call FAUXWIRECONE(1.0, 2.0)
          end if
      case (SHAPE_SPHERE)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDSPHERE(1.0)
          else
              call FAUXWIRESPHERE(1.0)
          end if
      case (SHAPE_TORUS)
          if (iType(1) .eq. 0) then
              call FAUXSOLIDTORUS(1.0, 2.0)
          else
              call FAUXWIRETORUS(1.0, 2.0)
          end if

      case (SHAPE_TEAPOT)

          if (iType(1) .eq. 0) then

              call FAUXSOLIDTEAPOT(2.0)

          else

              call FAUXWIRETEAPOT(2.0)

          end if
      case default ! blank screen, should not have come to this point
      end select

      ! Restore the original matrix
      call FGLPOPMATRIX()

      ! Flush drawing commands
      call FGLFLUSH()

      SG_xPostEval_Graphics_Solid = SG_R_OK
      return
      end
Contents of Solid_1_0F.h    [Go To Top]

! Solid_1_0F.h - common parameters used by Solid class functions
!
      integer, parameter :: SG_NDX_ITRACKER = 1
      integer, parameter :: SG_NDX_ISELECT = 2
      integer, parameter :: SG_NDX_IWINSIZE = 3
      integer, parameter :: SG_NDX_FSCALE = 4
      integer, parameter :: SG_NDX_FROTATE = 5
      integer, parameter :: SG_NDX_FTRANSLATE = 6
      integer, parameter :: SG_NDX_IFLAGS = 7
      integer, parameter :: SG_NDX_SEXTFILE = 8
      integer, parameter :: SG_NDX_ISHAPE = 9
      integer, parameter :: SG_NDX_ICOLOR = 10
      integer, parameter :: SG_NDX_ITYPE = 11
      integer, parameter :: SG_NDX_FANGLE = 12

 

      integer, parameter :: TRACK_NONE = 0
      integer, parameter :: TRACK_POINT = 1
      integer, parameter :: TRACK_LINE = 2
      integer, parameter :: TRACK_RECT = 3

 

      integer, parameter :: COLOR_CYAN = 0
      integer, parameter :: COLOR_MAGENTA = 1
      integer, parameter :: COLOR_YELLOW = 2

 

      integer, parameter :: SHAPE_TETRAHEDRON = 0
      integer, parameter :: SHAPE_CUBE = 1
      integer, parameter :: SHAPE_OCTAHEDRON = 2
      integer, parameter :: SHAPE_DODECAHEDRON = 3
      integer, parameter :: SHAPE_ICOSAHEDRON = 4
      integer, parameter :: SHAPE_BOX = 5
      integer, parameter :: SHAPE_CYLINDER = 6
      integer, parameter :: SHAPE_CONE = 7
      integer, parameter :: SHAPE_SPHERE = 8
      integer, parameter :: SHAPE_TORUS = 9
      integer, parameter :: SHAPE_TEAPOT = 10

 

      real(4), parameter :: fAmbientLight(4) = (/ 0.2, 0.2, 0.2, 1.0 /)
      real(4), parameter :: fDiffuseLight(4) = (/ 0.6, 0.6, 0.6, 1.0 /)
      real(4), parameter :: fSpecular(4) = (/ 1.0, 0.5, 0.5, 1.0 /)
      real(4), parameter :: fLightPos(4) = (/ 1.0, 2.0, 0.0, 1.0 /)

 


SansGUI Modeling and Simulation Environment version 1.2

Copyright © 2000-2003 ProtoDesign, Inc. All rights reserved.

http://protodesign-inc.com