glc_primitivegroup.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib 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 Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00022 
00024 
00025 #include "glc_primitivegroup.h"
00026 #include "../glc_state.h"
00027 
00028 // Class chunk id
00029 quint32 GLC_PrimitiveGroup::m_ChunkId= 0xA700;
00030 
00031 // Default constructor
00032 GLC_PrimitiveGroup::GLC_PrimitiveGroup(GLC_uint materialId)
00033 : m_Id(materialId)
00034 , m_TrianglesIndex()
00035 , m_TrianglesGroupsSizes()
00036 , m_TrianglesGroupOffset()
00037 , m_TrianglesGroupOffseti()
00038 , m_TrianglesId()
00039 , m_StripsIndex()
00040 , m_StripIndexSizes()
00041 , m_StripIndexOffset()
00042 , m_StripIndexOffseti()
00043 , m_StripsId()
00044 , m_FansIndex()
00045 , m_FansIndexSizes()
00046 , m_FanIndexOffset()
00047 , m_FanIndexOffseti()
00048 , m_FansId()
00049 , m_IsFinished(false)
00050 , m_TrianglesIndexSize(0)
00051 , m_TrianglesStripSize(0)
00052 , m_TrianglesFanSize(0)
00053 {
00054 
00055 
00056 }
00058 GLC_PrimitiveGroup::GLC_PrimitiveGroup(const GLC_PrimitiveGroup& group)
00059 : m_Id(group.m_Id)
00060 , m_TrianglesIndex(group.m_TrianglesIndex)
00061 , m_TrianglesGroupsSizes(group.m_TrianglesGroupsSizes)
00062 , m_TrianglesGroupOffset(group.m_TrianglesGroupOffset)
00063 , m_TrianglesGroupOffseti(group.m_TrianglesGroupOffseti)
00064 , m_TrianglesId(group.m_TrianglesId)
00065 , m_StripsIndex(group.m_StripsIndex)
00066 , m_StripIndexSizes(group.m_StripIndexSizes)
00067 , m_StripIndexOffset(group.m_StripIndexOffset)
00068 , m_StripIndexOffseti(group.m_StripIndexOffseti)
00069 , m_StripsId(group.m_StripsId)
00070 , m_FansIndex(group.m_FansIndex)
00071 , m_FansIndexSizes(group.m_FansIndexSizes)
00072 , m_FanIndexOffset(group.m_FanIndexOffset)
00073 , m_FanIndexOffseti(group.m_FanIndexOffseti)
00074 , m_FansId(group.m_FansId)
00075 , m_IsFinished(group.m_IsFinished)
00076 , m_TrianglesIndexSize(group.m_TrianglesIndexSize)
00077 , m_TrianglesStripSize(group.m_TrianglesStripSize)
00078 , m_TrianglesFanSize(group.m_TrianglesFanSize)
00079 {
00080 
00081 
00082 }
00083 
00085 GLC_PrimitiveGroup::GLC_PrimitiveGroup(const GLC_PrimitiveGroup& group, GLC_uint id)
00086 : m_Id(id)
00087 , m_TrianglesIndex(group.m_TrianglesIndex)
00088 , m_TrianglesGroupsSizes(group.m_TrianglesGroupsSizes)
00089 , m_TrianglesGroupOffset(group.m_TrianglesGroupOffset)
00090 , m_TrianglesGroupOffseti(group.m_TrianglesGroupOffseti)
00091 , m_TrianglesId(group.m_TrianglesId)
00092 , m_StripsIndex(group.m_StripsIndex)
00093 , m_StripIndexSizes(group.m_StripIndexSizes)
00094 , m_StripIndexOffset(group.m_StripIndexOffset)
00095 , m_StripIndexOffseti(group.m_StripIndexOffseti)
00096 , m_StripsId(group.m_StripsId)
00097 , m_FansIndex(group.m_FansIndex)
00098 , m_FansIndexSizes(group.m_FansIndexSizes)
00099 , m_FanIndexOffset(group.m_FanIndexOffset)
00100 , m_FanIndexOffseti(group.m_FanIndexOffseti)
00101 , m_FansId(group.m_FansId)
00102 , m_IsFinished(group.m_IsFinished)
00103 , m_TrianglesIndexSize(group.m_TrianglesIndexSize)
00104 , m_TrianglesStripSize(group.m_TrianglesStripSize)
00105 , m_TrianglesFanSize(group.m_TrianglesFanSize)
00106 {
00107 
00108 
00109 }
00110 
00111 // = operator
00112 GLC_PrimitiveGroup& GLC_PrimitiveGroup::operator=(const GLC_PrimitiveGroup& group)
00113 {
00114         if (this != &group)
00115         {
00116                 m_Id= group.m_Id;
00117                 m_TrianglesIndex= group.m_TrianglesIndex;
00118                 m_TrianglesGroupsSizes= group.m_TrianglesGroupsSizes;
00119                 m_TrianglesGroupOffset= group.m_TrianglesGroupOffset;
00120                 m_TrianglesGroupOffseti= group.m_TrianglesGroupOffseti;
00121                 m_TrianglesId= group.m_TrianglesId;
00122                 m_StripsIndex= group.m_StripsIndex;
00123                 m_StripIndexSizes= group.m_StripIndexSizes;
00124                 m_StripIndexOffset= group.m_StripIndexOffset;
00125                 m_StripIndexOffseti= group.m_StripIndexOffseti;
00126                 m_StripsId= group.m_StripsId;
00127                 m_FansIndex= group.m_FansIndex;
00128                 m_FansIndexSizes= group.m_FansIndexSizes;
00129                 m_FanIndexOffset= group.m_FanIndexOffset;
00130                 m_FanIndexOffseti= group.m_FanIndexOffseti;
00131                 m_FansId= group.m_FansId;
00132                 m_IsFinished= group.m_IsFinished;
00133                 m_TrianglesIndexSize= group.m_TrianglesIndexSize;
00134                 m_TrianglesStripSize= group.m_TrianglesStripSize;
00135                 m_TrianglesFanSize= group.m_TrianglesFanSize;
00136         }
00137         return *this;
00138 }
00139 
00140 GLC_PrimitiveGroup::~GLC_PrimitiveGroup()
00141 {
00142 
00143 }
00144 // Return the class Chunk ID
00145 quint32 GLC_PrimitiveGroup::chunckID()
00146 {
00147         return m_ChunkId;
00148 }
00149 
00150 // Add triangles to the group
00151 void GLC_PrimitiveGroup::addTriangles(const IndexList& input, GLC_uint id)
00152 {
00153         m_TrianglesIndex+= input;
00154         m_TrianglesIndexSize= m_TrianglesIndex.size();
00155 
00156         m_TrianglesGroupsSizes.append(static_cast<GLsizei>(input.size()));
00157 
00158         if (m_TrianglesGroupOffseti.isEmpty())
00159         {
00160                 m_TrianglesGroupOffseti.append(0);
00161         }
00162         int offset= m_TrianglesGroupOffseti.last() + m_TrianglesGroupsSizes.last();
00163         m_TrianglesGroupOffseti.append(offset);
00164 
00165         // The Triangles group id
00166         if (0 != id) m_TrianglesId.append(id);
00167         else Q_ASSERT(m_TrianglesId.isEmpty());
00168 }
00169 
00170 // Add triangle strip to the group
00171 void GLC_PrimitiveGroup::addTrianglesStrip(const IndexList& input, GLC_uint id)
00172 {
00173         m_StripsIndex+= input;
00174         m_TrianglesStripSize= m_StripsIndex.size();
00175 
00176         m_StripIndexSizes.append(static_cast<GLsizei>(input.size()));
00177 
00178         if (m_StripIndexOffseti.isEmpty())
00179         {
00180                 m_StripIndexOffseti.append(0);
00181         }
00182         int offset= m_StripIndexOffseti.last() + m_StripIndexSizes.last();
00183         m_StripIndexOffseti.append(offset);
00184 
00185         // The strip id
00186         if (0 != id) m_StripsId.append(id);
00187         else Q_ASSERT(m_StripsId.isEmpty());
00188 }
00189 // Set the triangle index offset
00190 void GLC_PrimitiveGroup::setTrianglesOffset(GLvoid* pOffset)
00191 {
00192         //m_TrianglesGroupOffseti.pop_back();
00193         const int size= m_TrianglesGroupOffseti.size();
00194         for (int i= 0; i < size; ++i)
00195         {
00196                 m_TrianglesGroupOffset.append(BUFFER_OFFSET(static_cast<GLsizei>(m_TrianglesGroupOffseti[i]) * sizeof(GLuint) + reinterpret_cast<GLsizeiptr>(pOffset)));
00197         }
00198 }
00199 
00200 // Set the triangle index offset
00201 void GLC_PrimitiveGroup::setTrianglesOffseti(int offset)
00202 {
00203         m_TrianglesGroupOffseti.pop_back();
00204         const int size= m_TrianglesGroupOffseti.size();
00205         for (int i= 0; i < size; ++i)
00206         {
00207                 m_TrianglesGroupOffseti[i]= m_TrianglesGroupOffseti[i] + offset;
00208         }
00209 }
00210 
00211 // Set base triangle strip offset
00212 void GLC_PrimitiveGroup::setBaseTrianglesStripOffset(GLvoid* pOffset)
00213 {
00214         //m_StripIndexOffseti.pop_back();
00215         const int size= m_StripIndexOffseti.size();
00216         for (int i= 0; i < size; ++i)
00217         {
00218                 m_StripIndexOffset.append(BUFFER_OFFSET(static_cast<GLsizei>(m_StripIndexOffseti[i]) * sizeof(GLuint) + reinterpret_cast<GLsizeiptr>(pOffset)));
00219         }
00220 }
00221 
00222 // Set base triangle strip offset
00223 void GLC_PrimitiveGroup::setBaseTrianglesStripOffseti(int offset)
00224 {
00225         m_StripIndexOffseti.pop_back();
00226         const int size= m_StripIndexOffseti.size();
00227         for (int i= 0; i < size; ++i)
00228         {
00229                 m_StripIndexOffseti[i]= m_StripIndexOffseti[i] + offset;
00230         }
00231 }
00232 
00234 void GLC_PrimitiveGroup::addTrianglesFan(const IndexList& input, GLC_uint id)
00235 {
00236         m_FansIndex+= input;
00237         m_TrianglesFanSize= m_FansIndex.size();
00238 
00239         m_FansIndexSizes.append(static_cast<GLsizei>(input.size()));
00240 
00241         if (m_FanIndexOffseti.isEmpty())
00242         {
00243                 m_FanIndexOffseti.append(0);
00244         }
00245         int offset= m_FanIndexOffseti.last() + m_FansIndexSizes.last();
00246         m_FanIndexOffseti.append(offset);
00247 
00248         // The fan id
00249         if (0 != id) m_FansId.append(id);
00250         else Q_ASSERT(m_FansId.isEmpty());
00251 
00252 
00253 }
00254 
00255 // Set base triangle fan offset
00256 void GLC_PrimitiveGroup::setBaseTrianglesFanOffset(GLvoid* pOffset)
00257 {
00258         //m_FanIndexOffseti.pop_back();
00259         const int size= m_FanIndexOffseti.size();
00260         for (int i= 0; i < size; ++i)
00261         {
00262                 m_FanIndexOffset.append(BUFFER_OFFSET(static_cast<GLsizei>(m_FanIndexOffseti[i]) * sizeof(GLuint) + reinterpret_cast<GLsizeiptr>(pOffset)));
00263         }
00264 }
00265 
00266 // Set base triangle fan offset
00267 void GLC_PrimitiveGroup::setBaseTrianglesFanOffseti(int offset)
00268 {
00269         m_FanIndexOffseti.pop_back();
00270         const int size= m_FanIndexOffseti.size();
00271         for (int i= 0; i < size; ++i)
00272         {
00273                 m_FanIndexOffseti[i]= m_FanIndexOffseti[i] + offset;
00274         }
00275 }
00276 
00277 // Change index to VBO mode
00278 void GLC_PrimitiveGroup::computeVboOffset()
00279 {
00280         m_TrianglesGroupOffset.clear();
00281         const int triangleOffsetSize= m_TrianglesGroupOffseti.size();
00282         for (int i= 0; i < triangleOffsetSize; ++i)
00283         {
00284                 m_TrianglesGroupOffset.append(BUFFER_OFFSET(static_cast<GLsizei>(m_TrianglesGroupOffseti.at(i)) * sizeof(GLuint)));
00285         }
00286 
00287         m_StripIndexOffset.clear();
00288         const int stripOffsetSize= m_StripIndexOffseti.size();
00289         for (int i= 0; i < stripOffsetSize; ++i)
00290         {
00291                 m_StripIndexOffset.append(BUFFER_OFFSET(static_cast<GLsizei>(m_StripIndexOffseti.at(i)) * sizeof(GLuint)));
00292         }
00293 
00294         m_FanIndexOffset.clear();
00295         const int fanOffsetSize= m_FanIndexOffseti.size();
00296         for (int i= 0; i < fanOffsetSize; ++i)
00297         {
00298                 m_FanIndexOffset.append(BUFFER_OFFSET(static_cast<GLsizei>(m_FanIndexOffseti.at(i)) * sizeof(GLuint)));
00299         }
00300 }
00301 
00302 // Clear the group
00303 void GLC_PrimitiveGroup::clear()
00304 {
00305         m_TrianglesIndex.clear();
00306         m_TrianglesGroupsSizes.clear();
00307         m_TrianglesGroupOffset.clear();
00308         m_TrianglesGroupOffseti.clear();
00309         m_StripsIndex.clear();
00310         m_StripIndexSizes.clear();
00311         m_StripIndexOffset.clear();
00312         m_StripIndexOffseti.clear();
00313         m_FansIndex.clear();
00314         m_FansIndexSizes.clear();
00315         m_FanIndexOffset.clear();
00316         m_FanIndexOffseti.clear();
00317         m_IsFinished= false;
00318         m_TrianglesIndexSize= 0;
00319         m_TrianglesStripSize= 0;
00320         m_TrianglesFanSize= 0;
00321 }
00322 
00323 
00324 // Non Member methods
00325 // Non-member stream operator
00326 QDataStream &operator<<(QDataStream &stream, const GLC_PrimitiveGroup &primitiveGroup)
00327 {
00328         Q_ASSERT(primitiveGroup.isFinished());
00329         quint32 chunckId= GLC_PrimitiveGroup::m_ChunkId;
00330         stream << chunckId;
00331 
00332         // Primitive group id
00333         stream << primitiveGroup.m_Id;
00334 
00335         // Triangles, strips and fan offset index
00336         OffsetVectori trianglesGroupOffseti;
00337         OffsetVectori stripIndexOffseti;
00338         OffsetVectori fanIndexOffseti;
00339 
00340         // Get triangles, strips and fans offset
00341         trianglesGroupOffseti= primitiveGroup.m_TrianglesGroupOffseti;
00342         stripIndexOffseti= primitiveGroup.m_StripIndexOffseti;
00343         fanIndexOffseti= primitiveGroup.m_FanIndexOffseti;
00344 
00345         // Triangles index
00346         stream << primitiveGroup.m_TrianglesIndexSize;
00347         stream << trianglesGroupOffseti;
00348         stream << primitiveGroup.m_TrianglesGroupsSizes;
00349         stream << primitiveGroup.m_TrianglesId;
00350 
00351         // Triangles strips index
00352         stream << primitiveGroup.m_TrianglesStripSize;
00353         stream << stripIndexOffseti;
00354         stream << primitiveGroup.m_StripIndexSizes;
00355         stream << primitiveGroup.m_StripsId;
00356 
00357         // Triangles fans index
00358         stream << primitiveGroup.m_TrianglesFanSize;
00359         stream << fanIndexOffseti;
00360         stream << primitiveGroup.m_FansIndexSizes;
00361         stream << primitiveGroup.m_FansId;
00362 
00363         return stream;
00364 }
00365 QDataStream &operator>>(QDataStream &stream, GLC_PrimitiveGroup &primitiveGroup)
00366 {
00367         quint32 chunckId;
00368         stream >> chunckId;
00369         Q_ASSERT(chunckId == GLC_PrimitiveGroup::m_ChunkId);
00370         stream >> primitiveGroup.m_Id;
00371 
00372         // Triangles index
00373         stream >> primitiveGroup.m_TrianglesIndexSize;
00374         stream >> primitiveGroup.m_TrianglesGroupOffseti;
00375         stream >> primitiveGroup.m_TrianglesGroupsSizes;
00376         stream >> primitiveGroup.m_TrianglesId;
00377 
00378         // Triangles strips index
00379         stream >> primitiveGroup.m_TrianglesStripSize;
00380         stream >> primitiveGroup.m_StripIndexOffseti;
00381         stream >> primitiveGroup.m_StripIndexSizes;
00382         stream >> primitiveGroup.m_StripsId;
00383 
00384         // Triangles fans index
00385         stream >> primitiveGroup.m_TrianglesFanSize;
00386         stream >> primitiveGroup.m_FanIndexOffseti;
00387         stream >> primitiveGroup.m_FansIndexSizes;
00388         stream >> primitiveGroup.m_FansId;
00389 
00390 
00391         primitiveGroup.finish();
00392 
00393         return stream;
00394 }
00395 

SourceForge.net Logo

©2005-2011 Laurent Ribon