## Non-class Functions.

So, with the class functions described above, it is fairly simple to construct a scene graph. So, now that you have one, what can you do with it?

      void ssgCullAndDraw ( ssgRoot *root ) ;

This call deals with the entire process of rendering the database. Your application need only call ssgInit(), build a database, create a current context and call ssgCullAndDraw using the root node of that database.

### Intersection Testing.

Most applications need to test the scenery to see if moving objects have collided with it - there are several ways to do that - but they all share the same mechanisms:

    int  ssgIsect ( ssgRoot *root, sgSphere *s, sgMat4 m, ssgHit **results ) ;
    int  ssgHOT   ( ssgRoot *root, sgVec3    s, sgMat4 m, ssgHit **results ) ;
    int  ssgLOS   ( ssgRoot *root, sgVec3    s, sgMat4 m, ssgHit **results ) ;

These three calls implement various ways to test the database for collisions, weapon impacts and such like. In each case, the search for a collision starts at 'root', and the database is transformed by the matrix 'm' before the test is evaluated - hence, 'm' is ususally the inverse of the matrix describing the test object's location.

- ssgIsect intersects a sphere against the scene.
- ssgHOT intersects a vertical line starting at the point 's' (remember, in SSG, positive Z is 'up'). This is often used to compute the Height-of-Terrain - hence the name 'HOT'.
- ssgLOS intersects an arbitary vector whose direction is defined by 's'.

The result in either case is an integer telling you how many triangles impacted the sphere/vector. If you need to know more about these intersections, pass the address of a ssgHit * variable as the last parameter and it will be returned pointing at a STATIC array of ssgHit structures. Thats a confusing explanation - and an example will help:

    ie:

       ssgHit *results ;

       int num_hits = ssgIsect ( root, &sphere, mat, &results ) ;

       for ( int i = 0 ; i < num_hits ; i++ )
       {
         ssgHit *h = &(results [ i ]) ;

         /* Do something with 'h' */
       }

Remember, you must finish using the results array before you do another ssgIsect/ssgHOT/ssgLOS because all three functions share the same results array.

By default, these functions only detect collisions with the front face of polygons. You can make them detect only the back faces using:

      void ssgSetBackFaceCollisions ( bool b ) ;

An ssgHit looks like this:

    class ssgHit
    {
      ssgLeaf *leaf ;
      int      triangle ;
      sgVec4   plane ;
      sgMat4   matrix ;

      ssgHit ()
      int getNumPathEntries () ;
      ssgEntity *getPathEntry ( int i ) ;
    } ;

The 'leaf' member points at the leaf node that impacted the sphere. The 'triangle' member tells you which triangle within the leaf did the impacting. The 'plane' member contains the plane equation of the impacting triangle and the 'matrix' element tells you the net result of concatenating all the transform nodes from the root to the leaf to the matrix you provided in the ssgIsect call.

It's possible for there to be multiple paths through the scene graph to the leaf node. Sometimes you'll need to look back up the tree to see nodes above the one that we actually impacted with. Hence, you can read all the ssgEntities that were traversed on the path from the root down to the leaf. Calling the 'getNumPathEntries' function to find the number of nodes along the path - and then 'getPathEntry(n)' to get the n'th entry in the path. The 'root' node will always be the zeroth entry in the path - and the leaf node will always be the last.

### Lights.

SSG supports the eight standard OpenGL light sources as class 'ssgLight'. Since there are only a finite number of these, they all exist all the time - you just call:

      ssgLight *ssgGetLight ( int i ) ;

...to get the i'th light should you need to manipulate it.

    class ssgLight
    {
      int isOn () ;
      void on  () ;
      void off () ;

      void setPosition ( const sgVec3 pos ) ;
      void setPosition ( float x, float y, float z ) ;

      void setColour ( GLenum which , const sgVec4 colour ) ;
      void setColour ( GLenum which , float r, float g, float b ) ;

      void setHeadlight ( int head ) ;
      int  isHeadlight () ;

      void setSpotlight ( int spot ) ;
      int  isSpotlight () ;

      void setSpotDirection ( const sgVec3 dir ) ;
      void setSpotDirection ( float x, float y, float z ) ;
      void setSpotDiffusion ( float exponent, float cutoff = 90.0f ) ;
      void setSpotAttenuation ( float constant, float linear, float quadratic ) ;
    } ;

Each light can be turned on or off - or tested to see if it's on or off.

Lights are positioned with 'setPosition()' - which can be relative to the origin of the world - or relative to the SSG camera (in 'headlight' mode).

If the 'spotlight' mode is enabled, then the light intensity has a certain distribution and attenuation. These parameters can be set with the 'setSpotXxx' methods. Otherwise, the light source is considered to be directional, or infinitely far away (in the direction of its position). The 'spotlight' mode is initially disabled.

### Miscellany.

It's convenient to find out how much texture memory has been consumed:

      int ssgGetNumTexelsLoaded () ;

(Bear in mind that a texel could be 16 or 32 bits depending on the hardware - and with MIPmapping enabled, 25% of the texels will be in the MIPmaps - so ssgGetNumTexelsLoaded will return a larger number than the total of the sizes of the input images might suggest.

### Loading Database Files.

To load a model file into SSG, you can either call a loader function that is specific to the format of the file you wish to load - or you can call 'ssgLoad' - which parses the filename extension and calls the appropriate format-specific loader.

At time or writing, there are MANY loaders for SSG:

- ssgLoadSSG - for '.ssg' files - the 'native' SSG format.
- ssgLoadAC - for '.ac' files - a somewhat obscure format produced by the 'AC3D' modelling tool - reasonably well tried and tested.
- ssgLoad3ds - for '.3ds' files as produced by 3DStudio.
- ssgLoadASE - 3DSMAX ASCII EXPORT Version 2.00, well tested
- ssgLoadDXF - AutoCADs famous DXF format. well tested
- ssgLoadFLT - OpenFlight. Works for OpenFlight files generated by recent versions of MultiGen - but not for those generated by some other tools such as Designers' Workbench.
- ssgLoadMD2 - Quake MD2
- ssgLoadOBJ - Wavefront, works well
- ssgLoadTRI - simple Tri format from "Andy Colbournes Editor".
- ssgLoadX - Microsofts DirectX-Format. Most features work.
- ssgLoadOFF - Geomview's OFF
- ssgLoadM - ???
- ssgLoadATG - Ascii TerraGear. Used by Flight Gear Flight Sim.
- ssgLoadVRML1 - VRML1 format, only partially implemented.
- ssgLoadIV - Inventor format, only partially implemented.
- ssgLoadStrip - The format of a stripifier.

      typedef ssgBranch *(*ssgHookFunc)(char *) ;

      ssgEntity *ssgLoad    ( const char *fname, const ssgLoaderOptions *options = NULL ) ;

      ssgEntity *ssgLoadSSG ( const char *fname, const ssgLoaderOptions *options = NULL ) ;
      ssgEntity *ssgLoadAC  ( const char *fname, const ssgLoaderOptions *options = NULL ) ;
      ssgEntity *ssgLoad3ds ( const char *fname, const ssgLoaderOptions *options = NULL ) ;
      ...etc...

Minimally, all you need to do is to call ssgLoadAC/ssgLoad3ds with the name of the file to load. However, most file formats (AC3D's and 3Dstudio's included) lack many desirable features, and it is also often necessary to store application-specific information in the file.

SSG's loaders will decode the comment fields found in the nodes of many common file formats and pass these onto the application via 'hookfunc'. This function should decode the string and construct whatever kind of SSG node it considers appropriate.

Similarly, the application may wish to embellish the ssgState of a loaded node - and since state information rarely has a comment field in most file formats, we pass the texture filename instead and expect the application to construct the entire ssgState:

      void ssgSetAppStateCallback ( ssgState *(*cb)(char *) ) ;

One common problem with file loaders is that it's often possible to refer to a second file from inside the first - but the path to that file is often not adequately defined by the original file. Hence, the application can specify a file path to be prepended to all model or texture file names.

      void ssgModelPath   ( char *path ) ;
      void ssgTexturePath ( char *path ) ;

You can only supply one path. If you need additional features, use the function `ulFindFile` (for more see util-library-doc). The last three functions simply set values in the _ssgCurrentOptions (type ssgLoaderOptions), for example:

    inline void ssgModelPath   ( const char *path )
    {
      _ssgCurrentOptions -> setModelDir ( path ) ;
    }

For more on ssgLoaderOptions see also the next page of this doc.

Some loaders for file formats that use texture formats not supported by ssg use this functions to find textures:

    void ssgFindOptConvertTexture( char * filepath, char * tfname )

It finds and optionally (= if necessary) converts the texture. This is only really implemented for Windo$ :-(, for all others it should just be the two lines you will find in the comment. I didn't test it, that's the reason I commented it out. But it will warn you when you have to convert something, so even for non-Windo$-users the new function is already a step forward. For the actual conversion, at first I wanted to use GIMP. So, in Deja, I looked for "+gimp +convert +batch" and similar strings. I found several people asking, but almost all the answer were to use ImageMagick instead. ImageMagick is free as in beer. Also, the docs I got with the newest gimp is extremely sparse :-(. Therefore, I tried ImageMagick and this worked straight away. You should find ImageMagick under

http://www.wizards.dupont.com/cristy/ImageMagick.html

Plib uses the ImageMagick application "convert". Since there are other convert.exe-programs on my computer, I had to copy the ImageMagick stuff into the directory of my plib-application, so that it uses the correct convert.exe.

Most file formats contain considerable numbers of redundant nodes (because of the way people build using these tools). This function walks a database sub-tree multiplying out any ssgTransform nodes and replacing them with ssgBranch'ed - unless they have userdata associated with them. Any branch nodes with zero kids are deleted - any with just one kid are eliminated and the child node pushed up one level.

      void ssgFlatten ( ssgEntity *ent ) ;

It's important for 3D performance to optimise triangles into triangle strips or fans. Since most file formats don't record strip/fan information, it's useful to call:

      void ssgStripify ( ssgEntity *ent ) ;

### Saving Database Files.

Most SSG programs will simply load a file and display it in some way - but occasionally, it's useful to be able to write a file back out again.

To write a model file from SSG, you can either call a writer function that is specific to the format of the file you wish to save - or you can call 'ssgSave' - which parses the filename extension and calls the appropriate format-specific writer.

Saving into ssg-format is done with these functions:

- ssgSaveSSG - for '.ssg' files - the 'native' SSG format.

      int ssgSave    ( char *fname, ssgEntity *ent ) ;
      int ssgSaveSSG ( char *fname, ssgEntity *ent ) ;

This returns TRUE if the operation worked - FALSE if it failed. You will also find writers for the 3DS, AC, ASE, ATG, DXF, M, OBJ, OFF, QHI, TRI and X formats.

### Features of the file formats and status of the loaders/writers

Here comes a table of the features of all the file formats and loaders and writers. VRML doesn't really work, so you won't find it here. The "QHI" (QHull Input - a format used by a toll to create convex hulls) writer only writes a point cloud and suppoorts no other features, so I also didn't add it to the table. BTW, if you want to create convex hulls of existing geometry, you just need a few lines. Look into PPE (prettypoly editor, on SourceForge as well), into the file ppeCoreFuncs.cxx, function addConvexHull. The SSG loaders/writers support all features of the ssg-lib, so missing features mean they miss in the library (normaly because they miss in OpenGL) The letters mean:
A = feature not in file format
B = feature in file format and not implemented, not planned.
C = feature in file format and not implemented, but planned.
D = feature in file format and partly implemented or not tested.
E = feature in file format and implemented.
And here are the features with some explanaitions:

- Filled polys - Planar polygons. In the ssg-lib, this is the only primitive you can use to make "solid" looking models. This is a 2D primitive; it has no volume but an area.
- # sides - No of sides per poly. This is of course equal to number of vertices per poly. Some formats support only triangles, some also quads and some support n-sided polys, that is they have no restrictions.
- Lines - for example used for high voltage lines in a flight simulator. This is a 1D primitive; it has no area but a length.
- Points - for example used for small lights. This is a "0D" primitive; it has no length.
- Sub objects - The complete model may consist of several sub models. There are several reasons one wants this, for example to make the structure of the model clearer to humans, to easily enable humans or programs to move, colour, texture, animate etc one part, for example one wheel of the complete model.
- Hierarchie - You not only have sub objects, but there is a parent-kid relation between some, for example, the upper arm of a human model may be "under" the body and the lower arm "under" the upper arm etc.
- DAG - direct acyclic graph means that the hierarchie may not only be a tree where each node has exactly one parent (apart from the root, which has zero parents), but that nodes may have more than one parent. Used for example in cars to have the wheel geometry only once. If the one wheel-node has four parents, this means it will be rendered four times when the scene is drawn.
- Colours - Things can have a colour. Uncoloured objects are always white.
- Textures - Texture things with 2D bitmaps.
- Texture coordinates - The texture mapping is arbitrary.
- Texture coord. per face AND vertex - If a vertex is part of three faces, then this feature allows it to have three texture coordinates. For example, a cube may have 18 texture coordinates, even if it has much fewer vertices.
- Texture per face - each face may have another texture bitmap. Formats (and the ssg lib) that don't have this have to increase the number of nodes to get this effect.
- Transparency - Some polys may be partially or completely transparent.
- Animation - There may be many different sorts of animation.
- Billboards - geometry that automatically rotates so it always faces the viewer. Normaly flat. As an example, can be used to model trees in a driving simulator.
- LOD - stands for "Level of Detail" and means there can be geometries of varying complexity for the model, so for example, if it is near the viewer you use many polys and if it is far away you use few polys so that the impact on rendering speed is low.
- Other switches - switch means you can switch the visibility of some nodes on and off.
- Ascii or Binary - A stands for Ascii, B for Binary files.

Feature | 3DS load | AC load/ save | ASC save | ASE load/ save | ATG load/ save | DXF load | DXF save | FLT load | M load/ save | MD2 load | OBJ load | OBJ save | OFF load/ save | SSG load/ save | Strip load | TRI load/ save | X load | IV load | VRML1 load
Filled polys | E | E | E | E | E | E | E(5) | E | E/D | ? | E | E | E | E | E |  | E | E | E
# sides |  | n/3 | 3 | 3 | n | 3,4(4) | 3 | n |  |  | n? | 3 | n | n(4) |  | 3? | n | 3,4 | 3,4
Lines | A | D/C | C | E | A(?) | E | E | A | A | ? | E | E | A? | E | A |  | A | B | B
Points | A | A | E | C | A(?) | A | A | B | A | ? | A | A | A | E | A |  | A | B | B
Sub objects | E | E/D | E | E | A(?) | A | A | E | A | ? | C-D | C-D | A | E | A |  | E | E | E
Hierarchie | A | E/D | E? | E | A(?) | A | A | E | A | ? | C | C | A | E | A |  | C | E | E
DAG | A | A | A | A | A(?) | A | A | E | A | ? | A | A | A | E | A |  | A(?) | E | E
Colours | E | E/D | E? | E | ? | E | E | D(1) | A | ? | D | C | A | E | A |  | E | B | B
Textures | D | E/D | E | E | E | A | A | E | A | ? | D | C | B | E | A |  | E | E | E
Texture coord. | D | E | E | E | E | A | A | E | A | ? | E | C | B | E | A |  | E | E | E
Texture coord. per face AND vertex | D(?) | D(?) | A | E | E | A | A | A | A | ? | E | C | B(?) | A | A |  | C | E | E
Texture per face |  | D(?)/- | C | A | E | A | A | E | A | ? | A | A | B(?) | A | A |  | C | B | A
Transparency | D | D | D | D | A(?) | A | A | E | A | ? | A? | A? | B(?) | E | A |  | E | B | B
Animation | B | A | A | E | A | A | A | B(2) | A | ? | A? | A? | A | E | A |  | B or C | B | A
Billboards |  | A | A | A | A | A | A | E | A | ? | A | A | A | E | A |  | A | B | A
LOD |  | A | A | A | A | A | A | E | A | ? | A | A | A | E | A |  | A | B | B
Other switches |  | A | A | A | A | A | A | D | A | ? | A | A | A | E | A |  | A | D | D
A=Ascii or B=Binary | B | A | A | A | A | A | A | B | A | B | A | A | A | B | ? | A | A+B(3) | A | A

footnotes:
(1) problematic for ancient files
(2) DOFs are not implemented
(3) There is an ascii- and a binary X-file-format. I only implemented the ascii-one and probably wont implement the binary one. There is a free (as in beer) converter by Microsoft running under Windo$.
(4) It is possible to have polys with an arbitrary number of vertices in ssg. But then you may only use one poly per node. Also, you can not mix 3 and 4 sided polys in one node.
(5) Some OpenGL modes, like for example Quads, are ignored. This means some parts of the model may dissappear when saving into DXF files.
Additional info on some formats:
ATG:
ATG stands for ascii TerraGear. Loading of TriStrips and TriFans not implemented yet. Untextured parts are lost, for example when you save and load Steve Bakers Tuxedo, you will loose the feet, since they are not textured. The files written are not optimal, they use no strips/fans, only have triangles etc. Also, one vertex that needs several texture coords is written out several times.
3ds-writer:
I (Per) have one basicly working, but since there seems to be so much else to do with plib right now, I'm a bit reluctant to put my energy into it. Also, the only 3ds-loader I've tested files written by it with is the one in ssg :-)
OFF loader:
Warning: There are two formats called OFF! We support the OFF from GeomView, not the one from DEC. We support 2D and 3D, but no higher dimensions.
.SSG file loader/writer:
We number .SSG file formats so that for example the loaders knows what to expect. Until now, there is
format "0" - Used by plib .1.2.0, 1.3.x
format "1" - Will be used by 1.4.0
Currently, we are between "0" and "1". Normally, we want to keep at least our loader compatible, but the changes from zero to one would bloat the code so much and it seems .SSG file version zero were only used by people for temporary files, so we decided to make an incompatible break. If you need to read or write version zero, either get plib from cvs with the date 14.1.2001, or, if you are a Windo$ person, get prettypoly.sourceforge.net/download/ppewinbin_ssg_version_zero.zip This allows you to save and load .ssg version zero.
One of the new features in format "1" might be worth mentioning. It is now possible to load and save *any* class derived from ssgBase (notably the ssgAux node types). All that is needed for saving to work is an implementation of the virtual save() method. Loading requires the corresponing load() method, and also means for creating an instance of the class, which is accomplished by a call to ssgRegisterType(). See the ssgAux implementation for example usage; the convenience function ssgaInit() registers the ssgAux classes.
.X file writer:
Planned.
.X file loader:
The .X-entity "Frame" is not yet implemented. If you convert files into .X with "3D Exploration", you can check the option "without frames".
VRML1/IV file loaders:
These are only partially implemented, and are useful primarily for mesh-based models (textured or untextured). It would be useful to add material support to these loaders, as well as explicit normal definition.

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