CrystalSpace

Public API Reference

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 
00027 #include "ivideo/graph3d.h"
00028 #include "ivideo/rndbuf.h"
00029 
00030 namespace CS
00031 {
00032   
00037   template<typename T>
00038   class TriangleIndicesStream
00039   {
00040     size_t stride;
00041     const uint8* index;
00042     const uint8* indexEnd;
00043     T old2, old1;
00044     bool stripFlag;
00045     int quadPart;
00046 
00047     iRenderBuffer* buf;
00048     csRenderBufferComponentType compType;
00049     csRenderMeshType meshtype;
00050 
00052     T GetNextIndex()
00053     {
00054       T r;
00055       switch (compType)
00056       {
00057         default:
00058           CS_ASSERT(false);
00059         case CS_BUFCOMP_BYTE:
00060           r = *(char*)index;
00061           break;
00062         case CS_BUFCOMP_UNSIGNED_BYTE:
00063           r = *(unsigned char*)index;
00064           break;
00065         case CS_BUFCOMP_SHORT:
00066           r = *(short*)index;
00067           break;
00068         case CS_BUFCOMP_UNSIGNED_SHORT:
00069           r = *(unsigned short*)index;
00070           break;
00071         case CS_BUFCOMP_INT:
00072           r = *(int*)index;
00073           break;
00074         case CS_BUFCOMP_UNSIGNED_INT:
00075           r = *(unsigned int*)index;
00076           break;
00077         case CS_BUFCOMP_FLOAT:
00078           r = uint (*(float*)index);
00079           break;
00080         case CS_BUFCOMP_DOUBLE:
00081           r = uint (*(double*)index);
00082           break;
00083       }
00084       index += stride;
00085       return r;
00086     }
00087     T GetIndex (size_t idx)
00088     {
00089       switch (compType)
00090       {
00091         default:
00092           CS_ASSERT(false);
00093         case CS_BUFCOMP_BYTE:
00094           return T (*(char*)(index+idx*stride));
00095         case CS_BUFCOMP_UNSIGNED_BYTE:
00096           return T (*(unsigned char*)(index+idx*stride));
00097         case CS_BUFCOMP_SHORT:
00098           return T (*(short*)(index+idx*stride));
00099         case CS_BUFCOMP_UNSIGNED_SHORT:
00100           return T (*(unsigned short*)(index+idx*stride));
00101         case CS_BUFCOMP_INT:
00102           return T (*(int*)(index+idx*stride));
00103         case CS_BUFCOMP_UNSIGNED_INT:
00104           return T (*(unsigned int*)(index+idx*stride));
00105         case CS_BUFCOMP_FLOAT:
00106           return T (*(float*)(index+idx*stride));
00107         case CS_BUFCOMP_DOUBLE:
00108           return T (*(double*)(index+idx*stride));
00109       }
00110       return 0;
00111     }
00112   public:
00118     TriangleIndicesStream () : old2(0), old1(0), buf (0) { }
00127     TriangleIndicesStream (iRenderBuffer* indices,
00128                            csRenderMeshType meshtype,
00129                            size_t indexStart = 0, 
00130                            size_t indexEnd = (size_t)~0) : old2(0), old1(0)
00131     {
00132       BeginTriangulate (indices, meshtype, indexStart, indexEnd);
00133     }
00134     ~TriangleIndicesStream()
00135     {
00136       if (buf != 0) buf->Release ();
00137     }
00138 
00147     void BeginTriangulate (const uint8* index, const uint8* indexEnd,
00148       size_t stride, csRenderBufferComponentType compType,
00149       csRenderMeshType meshtype)
00150     {
00151       this->index = index;
00152       this->indexEnd = indexEnd;
00153       this->stride = stride;
00154       stripFlag = false;
00155       quadPart = 0;
00156       this->compType = compType;
00157       this->meshtype = meshtype;
00158 
00159       switch (meshtype)
00160       {
00161       case CS_MESHTYPE_TRIANGLESTRIP:
00162       case CS_MESHTYPE_TRIANGLEFAN:
00163         {
00164           old2 = GetNextIndex();
00165           old1 = GetNextIndex();
00166           break;
00167         }
00168       default:
00169         ;
00170       }
00171     }
00172     
00181     void BeginTriangulate (iRenderBuffer* indices,
00182                             csRenderMeshType meshtype,
00183                             size_t indexStart = 0, 
00184                             size_t indexEnd = (size_t)~0)
00185     {
00186       if (indexEnd == (size_t)~0) indexEnd = indices->GetElementCount();
00187       
00188       buf = indices;
00189       uint8* indexLock = (uint8*)buf->Lock (CS_BUF_LOCK_READ);
00190   
00191       size_t stride = indices->GetElementDistance();
00192       uint8* tri = indexLock + indexStart*stride;
00193       const uint8* triEnd = indexLock + indexEnd*stride;
00194       
00195       BeginTriangulate (tri, triEnd, stride, indices->GetComponentType(), 
00196         meshtype);
00197     }
00198 
00200     bool HasNext() const
00201     {
00202       return (index < indexEnd);
00203     }
00204     CS_DEPRECATED_METHOD_MSG("Use HasNext() instead")
00205     bool HasNextTri() const { return HasNext(); }
00207     TriangleT<T> Next ()
00208     {
00209       CS_ASSERT (index < indexEnd);
00210       TriangleT<T> t;
00211       switch (meshtype)
00212       {
00213       case CS_MESHTYPE_TRIANGLES:
00214         {
00215           t.a = GetIndex (0);
00216           t.b = GetIndex (1);
00217           t.c = GetIndex (2);
00218           index += 3*csRenderBufferComponentSizes[compType];
00219         }
00220         break;
00221       case CS_MESHTYPE_TRIANGLESTRIP:
00222         {
00223           const T cur = GetNextIndex();
00224           t.a = old1;
00225           t.b = old2;
00226           t.c = cur;
00227           if (stripFlag)
00228             old2 = cur;
00229           else
00230             old1 = cur;
00231           stripFlag = !stripFlag;
00232         }
00233         break;
00234       case CS_MESHTYPE_TRIANGLEFAN:
00235         {
00236           const T cur = GetNextIndex();
00237           t.a = old2;
00238           t.b = old1;
00239           t.c = cur;
00240           old1 = cur;
00241         }
00242         break;
00243       case CS_MESHTYPE_QUADS:
00244         {
00245           if (quadPart == 0)
00246           {
00247             t.a = GetIndex (0);
00248             t.b = GetIndex (1);
00249             t.c = GetIndex (2);
00250           }
00251           else
00252           {
00253             t.a = GetIndex (0);
00254             t.b = GetIndex (2);
00255             t.c = GetIndex (3);
00256             index += 4*csRenderBufferComponentSizes[compType];
00257           }
00258           quadPart ^= 1;
00259         }
00260         break;
00261       default:
00262         CS_ASSERT_MSG("Unsupported mesh type", false);
00263         ;
00264       }
00265       return t;
00266     }
00267     CS_DEPRECATED_METHOD_MSG("Use Next() instead")
00268     bool NextTriangle (T& a, T& b, T& c)
00269     {
00270       TriangleT<T> tri = Next ();
00271       a = tri.a; b = tri.b; c = tri.c;
00272       return true;
00273     }
00274   };
00275   
00276 } // namespace CS
00277 
00278 #endif // __CS_TRIANGLESTREAM_H__

Generated for Crystal Space 1.2.1 by doxygen 1.5.3