Back to index

supertuxkart  0.5+dfsg1
ssg_help.cpp
Go to the documentation of this file.
00001 //  $Id: ssg_help.cpp 837 2006-10-23 07:43:05Z hiker $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2004-2005 Steve Baker <sjbaker1@airmail.net>
00005 //  Copyright (C) 2006 SuperTuxKart-Team, Joerg Henrichs, Steve Baker
00006 //
00007 //  This program is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU General Public License
00009 //  as published by the Free Software Foundation; either version 2
00010 //  of the License, or (at your option) any later version.
00011 //
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 
00022 #include <iostream>
00023 #include <plib/ssg.h>
00024 #include "ssg_help.hpp"
00025 
00026 // -----------------------------------------------------------------------------
00032 void createDisplayLists(ssgEntity* entity)
00033 {
00034     if (!entity) return;
00035 
00036     ssgVtxTable* table = dynamic_cast<ssgVtxTable*>(entity);
00037     if(table)
00038     {
00039         if(table->getNumTriangles()>1) table->makeDList();
00040     }
00041     ssgBranch* branch = dynamic_cast<ssgBranch*>(entity);
00042 
00043     if (branch)
00044     {
00045         for(ssgEntity* i = branch->getKid(0); i != NULL;
00046             i = branch->getNextKid())
00047         {
00048             createDisplayLists(i);
00049         }   // for
00050     }   // if branch
00051 
00052 }  // createDisplayLists
00053 
00054 // -----------------------------------------------------------------------------
00062 ssgTransform* add_transform(ssgBranch* branch)
00063 {
00064     if (!branch) return 0;
00065 
00066     ssgTransform* transform = new ssgTransform;
00067     transform->ref();
00068     for(ssgEntity* i = branch->getKid(0); i != NULL; i = branch->getNextKid())
00069     {
00070         transform->addKid(i);
00071     }
00072 
00073     branch->removeAllKids();
00074     branch->addKid(transform);
00075 
00076     // Set some user data, so that the wheel isn't ssgFlatten()'ed
00077     branch->setUserData(new ssgBase());
00078     transform->setUserData(new ssgBase());
00079 
00080     return transform;
00081 }   // add_transform
00082 
00083 // -----------------------------------------------------------------------------
00093 void print_model(ssgEntity* entity, const int indent, const int maxLevel)
00094 {
00095     if(maxLevel <0) return;
00096     if (entity)
00097     {
00098         for(int i = 0; i < indent; ++i)
00099             std::cout << "  ";
00100 
00101         std::cout << entity->getTypeName() << " " << entity->getType() << " '"
00102         << entity->getPrintableName()
00103         << "' '"
00104         << (entity->getName() ? entity->getName() : "null")
00105         << "' " << entity << std::endl;
00106 
00107         ssgBranch* branch = dynamic_cast<ssgBranch*>(entity);
00108 
00109         if (branch)
00110         {
00111             for(ssgEntity* i = branch->getKid(0); i != NULL;
00112                 i = branch->getNextKid())
00113             {
00114                 print_model(i, indent + 1, maxLevel-1);
00115             }
00116         }   // if branch
00117     }   // if entity
00118 }   // print_model
00119 
00120 // -----------------------------------------------------------------------------
00133 void MinMax(ssgEntity* p, float *x_min, float *x_max,
00134             float *y_min, float *y_max,
00135             float *z_min, float *z_max)
00136 {
00137     sgMat4 mat;
00138     sgMakeIdentMat4(mat);
00139     *x_min = *y_min =  10000.0f; if(z_min) *z_min =  10000.0f;
00140     *x_max = *y_max = -10000.0f; if(z_max) *z_max = -10000.0f;
00141     MinMax(p, mat, x_min, x_max, y_min, y_max, z_min, z_max);
00142 
00143 }   // MinMax
00144 
00146 void MinMax(ssgEntity* p, sgMat4 m, float* x_min, float* x_max,
00147             float* y_min, float* y_max,
00148             float* z_min, float* z_max)
00149 {
00150     if(p->isAKindOf(ssgTypeLeaf()))
00151     {
00152         ssgLeaf* l=(ssgLeaf*)p;
00153         for(int i=0; i<l->getNumTriangles(); i++)
00154         {
00155             short v1,v2,v3;
00156             sgVec3 vv1, vv2, vv3;
00157 
00158             l->getTriangle(i, &v1, &v2, &v3);
00159 
00160             sgXformPnt3 ( vv1, l->getVertex(v1), m );
00161             sgXformPnt3 ( vv2, l->getVertex(v2), m );
00162             sgXformPnt3 ( vv3, l->getVertex(v3), m );
00163             *x_min = std::min(*x_min, vv1[0]); *x_max = std::max(*x_max, vv1[0]);
00164             *x_min = std::min(*x_min, vv2[0]); *x_max = std::max(*x_max, vv2[0]);
00165             *x_min = std::min(*x_min, vv3[0]); *x_max = std::max(*x_max, vv3[0]);
00166             *y_min = std::min(*y_min, vv1[1]); *y_max = std::max(*y_max, vv1[1]);
00167             *y_min = std::min(*y_min, vv2[1]); *y_max = std::max(*y_max, vv2[1]);
00168             *y_min = std::min(*y_min, vv3[1]); *y_max = std::max(*y_max, vv3[1]);
00169             if(z_min)
00170             {
00171                 *z_min = std::min(*z_min, vv1[2]);
00172                 *z_min = std::min(*z_min, vv2[2]);
00173                 *z_min = std::min(*z_min, vv3[2]);
00174             }
00175             if(z_max)
00176             {
00177                 *z_max = std::max(*z_max, vv1[2]);
00178                 *z_max = std::max(*z_max, vv2[2]);
00179                 *z_max = std::max(*z_max, vv3[2]);
00180             }
00181 
00182         }   // for i<p->getNumTriangles
00183     }
00184     else if (p->isAKindOf(ssgTypeTransform()))
00185     {
00186         ssgBaseTransform* t=(ssgBaseTransform*)p;
00187 
00188         sgMat4 tmpT, tmpM;
00189         t->getTransform(tmpT);
00190         sgCopyMat4(tmpM, m);
00191         sgPreMultMat4(tmpM,tmpT);
00192 
00193         for(ssgEntity* e=t->getKid(0); e!=NULL; e=t->getNextKid())
00194         {
00195             MinMax(e, tmpM, x_min, x_max, y_min, y_max, z_min, z_max);
00196         }   // for i<getNumKids
00197 
00198     }
00199     else if (p->isAKindOf(ssgTypeBranch()))
00200     {
00201         ssgBranch* b =(ssgBranch*)p;
00202         for(ssgEntity* e=b->getKid(0); e!=NULL; e=b->getNextKid())
00203         {
00204             MinMax(e, m, x_min, x_max, y_min, y_max, z_min, z_max);
00205         }   // for i<getNumKids
00206     }
00207     else
00208     {
00209         printf("StaticSSG::MinMax: unkown type\n");
00210         p->print(stdout, 0, 0);
00211     }
00212 }   // MinMax
00213 
00214 // -----------------------------------------------------------------------------
00215 
00216 /* EOF */