## ssgContext

A 'context' is a set of parameters that relate to rendering one SSG image. It contains the camera parameters and some default state information.

      class ssgContext
      {
        ssgContext () ;
        void forceBasicState () { basicState -> force () ; }

        void makeCurrent () ;
        int  isCurrent () ;

        void overrideTexture   ( int on_off ) ;
        void overrideCullface  ( int on_off ) ;
        int  textureOverridden  () { return ovTexture  ; }
        int  cullfaceOverridden () { return ovCullface ; }
        void setCullface ( int on_off ) { cullFace = on_off ; }
        int  cullfaceIsEnabled () { return cullFace ; }

        sgFrustum *getFrustum () { return frustum ; }

        void getNearFar ( float *n, float *f ) ;
        void getFOV     ( float *w, float *h ) ;
        void getOrtho   ( float *w, float *h ) ;
        void setNearFar ( float  n, float  f ) ;
        void setOrtho   ( float  w, float  h ) ;
        void setFOV     ( float  w, float  h ) ;

        int  isOrtho () { return orthographic ; }

        void getCameraPosition ( sgVec3 pos ) ;
        void setCamera ( sgMat4 mat ) ;
        void setCamera ( sgCoord *coord ) ;

        void loadProjectionMatrix () ;
        void loadModelviewMatrix  () ;

        void getProjectionMatrix  ( sgMat4 dst ) ;
        void getModelviewMatrix   ( sgMat4 dst ) ;

        void  clrClipPlane   ( int i ) ;
        void  setClipPlane   ( int i, sgVec4 plane ) ;
        int   isSetClipPlane ( int i ) ;
        float *getClipPlane  ( int i ) ;
      } ;

In all 3D rendering, you need the concept of a virtual camera - or eyepoint. This is set up in SSG with the following calls:

      void ssgContext::setFOV    ( float w, float h ) ;
      void ssgContext::setNearFar( float n, float f ) ;
      void ssgContext::setCamera ( sgCoord *coord ) ;

The setFOV call sets up the vertical and horizontal fields of view (in degrees), setNearFar sets the near and far clip planes (in whatever units your model is built in). Finally, you can position the virtual camera relative to the database origin using setCamera.

Very often, an SSG program contains other non-SSG routines that are accessing OpenGL state. Even a menu drawn using PUI or an overlay in raw OpenGL or using FNT will change the current state of OpenGL. Since SSG does 'lazy' state changes (only updating those states that need to be changed) - it will be fooled into no setting some things that some ssgLeaf nodes really need.

This isn't a problem so long as the application doesn't change states DURING ssgCullAndDraw (by using derived class member functions, callbacks, etc). If you need to do that then be sure to call forceBasicState() before you return to SSG.

       void ssgContext::forceBasicState ( void ) ;

During testing, you sometimes need to disable texture rendering, or backface culling:

      void ssgContext::overrideTexture  ( int on_off ) ;
      void ssgContext::overrideCullface ( int on_off ) ;

The six standard OpenGL user-defined clipping planes can be setup and managed automatically using these calls:

        void  clrClipPlane   ( int i ) ;
        void  setClipPlane   ( int i, sgVec4 plane ) ;
        int   isSetClipPlane ( int i ) ;
        float *getClipPlane  ( int i ) ;

setClipPlane takes the number of the plane (0 through 5) and a plane equation which will be applied at the next cull/draw cycle. clrClipPlane removes a clip plane. isSetClipPlane inquires about whether the i'th plane is turned on and getClipPlane returns the plane that's currently set as the i'th plane.

Once you have your context set up as you'd like, you have to make it become the 'current' context.

      void ssgContext::makeCurrent () ;

There is a non-class function:

      ssgContext *ssgGetCurrentContext() ;

...which returns the current context.

The 'ssgContext' class was not present in PLIB 1.0.xx, so (for backwards compatibility) there is an initial context assigned inside ssgInit(). In addition to the member functions of ssgContext, there are a number of equivelent global functions that are equivelent to ssgGetCurrentContext()->member_function(). These functions are deprecated for new code:

      inline void ssgForceBasicState   () ;
      inline void ssgGetCameraPosition ( sgVec3 pos ) ;
      inline void ssgOverrideTexture   ( int on_off ) ;
      inline void ssgOverrideCullface  ( int on_off ) ;
      inline void ssgGetNearFar        ( float *n, float *f ) ;
      inline void ssgGetFOV            ( float *w, float *h ) ;
      inline void ssgSetFOV            ( float w, float h ) ;
      inline void ssgSetOrtho          ( float w, float h ) ;
      inline void ssgSetNearFar        ( float n, float f ) ;
      inline void ssgSetCamera         ( sgMat4 mat ) ;
      inline void ssgSetCamera         ( sgCoord *coord ) ;
      inline void ssgLoadProjectionMatrix () ;
      inline void ssgLoadProjectionMatrix ( sgFrustum *f ) ;
      inline void ssgGetProjectionMatrix  ( sgMat4 dst ) ;
      inline void ssgGetModelviewMatrix   ( sgMat4 dst ) ;
      inline void ssgLoadModelviewMatrix  () ;
      inline void ssgLoadModelviewMatrix  ( sgMat4 mat ) ;

<= previous = | Return to SSG Index | = next =>