Crystal Space: csgfx/trianglestream.h Source File (Crystal Space Public API Reference)

| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csgfx/trianglestream.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2006 by Jorrit Tyberghein
    00003               (C) 2006 by Frank Richter
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_TRIANGLESTREAM_H__
    00021 #define __CS_TRIANGLESTREAM_H__
    00022
    00026 #include "ivideo/graph3d.h"
    00027 #include "ivideo/rndbuf.h"
    00028
    00029 namespace CS
    00030 {
    00031
    00036   template<typename T>
    00037   class TriangleIndicesStream
    00038   {
    00039     size_t stride;
    00040     const uint8* index;
    00041     const uint8* indexEnd;
    00042     T old2, old1;
    00043     bool stripFlag;
    00044     int quadPart;
    00045
    00046     csRenderBufferComponentType compType;
    00047     csRenderMeshType meshtype;
    00048
    00050     T GetNextIndex()
    00051     {
    00052       T r;
    00053       switch (compType)
    00054       {
    00055         default:
    00056           CS_ASSERT(false);
    00057         case CS_BUFCOMP_BYTE:
    00058           r = *(char*)index;
    00059           break;
    00060         case CS_BUFCOMP_UNSIGNED_BYTE:
    00061           r = *(unsigned char*)index;
    00062           break;
    00063         case CS_BUFCOMP_SHORT:
    00064           r = *(short*)index;
    00065           break;
    00066         case CS_BUFCOMP_UNSIGNED_SHORT:
    00067           r = *(unsigned short*)index;
    00068           break;
    00069         case CS_BUFCOMP_INT:
    00070           r = *(int*)index;
    00071           break;
    00072         case CS_BUFCOMP_UNSIGNED_INT:
    00073           r = *(unsigned int*)index;
    00074           break;
    00075         case CS_BUFCOMP_FLOAT:
    00076           r = uint (*(float*)index);
    00077           break;
    00078         case CS_BUFCOMP_DOUBLE:
    00079           r = uint (*(double*)index);
    00080           break;
    00081       }
    00082       index += stride;
    00083       return r;
    00084     }
    00085     T GetIndex (size_t idx)
    00086     {
    00087       switch (compType)
    00088       {
    00089         default:
    00090           CS_ASSERT(false);
    00091         case CS_BUFCOMP_BYTE:
    00092           return T (*(char*)(index+idx*stride));
    00093         case CS_BUFCOMP_UNSIGNED_BYTE:
    00094           return T (*(unsigned char*)(index+idx*stride));
    00095         case CS_BUFCOMP_SHORT:
    00096           return T (*(short*)(index+idx*stride));
    00097         case CS_BUFCOMP_UNSIGNED_SHORT:
    00098           return T (*(unsigned short*)(index+idx*stride));
    00099         case CS_BUFCOMP_INT:
    00100           return T (*(int*)(index+idx*stride));
    00101         case CS_BUFCOMP_UNSIGNED_INT:
    00102           return T (*(unsigned int*)(index+idx*stride));
    00103         case CS_BUFCOMP_FLOAT:
    00104           return T (*(float*)(index+idx*stride));
    00105         case CS_BUFCOMP_DOUBLE:
    00106           return T (*(double*)(index+idx*stride));
    00107       }
    00108       return 0;
    00109     }
    00110   public:
    00111     TriangleIndicesStream ()
    00112     {
    00113     }
    00114
    00122     void BeginTriangulate (const uint8* index, const uint8* indexEnd,
    00123       size_t stride, csRenderBufferComponentType compType,
    00124       csRenderMeshType meshtype)
    00125     {
    00126       this->index = index;
    00127       this->indexEnd = indexEnd;
    00128       this->stride = stride;
    00129       stripFlag = false;
    00130       quadPart = 0;
    00131       this->compType = compType;
    00132       this->meshtype = meshtype;
    00133
    00134       switch (meshtype)
    00135       {
    00136       case CS_MESHTYPE_TRIANGLESTRIP:
    00137       case CS_MESHTYPE_TRIANGLEFAN:
    00138         {
    00139           old2 = GetNextIndex();
    00140           old1 = GetNextIndex();
    00141           break;
    00142         }
    00143       default:
    00144         ;
    00145       }
    00146     }
    00147
    00149     bool HasNextTri() const
    00150     {
    00151       return (index < indexEnd);
    00152     }
    00154     bool NextTriangle (T& a, T& b, T& c)
    00155     {
    00156       if (index < indexEnd)
    00157       {
    00158         switch (meshtype)
    00159         {
    00160         case CS_MESHTYPE_TRIANGLES:
    00161           {
    00162             a = GetIndex (0);
    00163             b = GetIndex (1);
    00164             c = GetIndex (2);
    00165             index += 3*csRenderBufferComponentSizes[compType];
    00166             return true;
    00167           }
    00168           break;
    00169         case CS_MESHTYPE_TRIANGLESTRIP:
    00170           {
    00171             const T cur = GetNextIndex();
    00172             a = old1;
    00173             b = old2;
    00174             c = cur;
    00175             if (stripFlag)
    00176               old2 = cur;
    00177             else
    00178               old1 = cur;
    00179             stripFlag = !stripFlag;
    00180             return true;
    00181           }
    00182           break;
    00183         case CS_MESHTYPE_TRIANGLEFAN:
    00184           {
    00185             const T cur = GetNextIndex();
    00186             a = old2;
    00187             b = old1;
    00188             c = cur;
    00189             old1 = cur;
    00190             return true;
    00191           }
    00192           break;
    00193         case CS_MESHTYPE_QUADS:
    00194           {
    00195             if (quadPart == 0)
    00196             {
    00197               a = GetIndex (0);
    00198               b = GetIndex (1);
    00199               c = GetIndex (2);
    00200             }
    00201             else
    00202             {
    00203               a = GetIndex (0);
    00204               b = GetIndex (2);
    00205               c = GetIndex (3);
    00206               index += 4*csRenderBufferComponentSizes[compType];
    00207             }
    00208             quadPart ^= 1;
    00209             return true;
    00210           }
    00211           break;
    00212         default:
    00213           ;
    00214         }
    00215       }
    00216       return false;
    00217     }
    00218   };
    00219
    00220 } // namespace CS
    00221
    00222 #endif // __CS_TRIANGLESTREAM_H__