glc_lod.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 
00026 #include "glc_lod.h"
00027 
00028 // Class chunk id
00029 quint32 GLC_Lod::m_ChunkId= 0xA708;
00030 
00031 
00032 GLC_Lod::GLC_Lod()
00033 : m_Accuracy(0.0)
00034 , m_IboId(0)
00035 , m_IndexVector()
00036 , m_IndexSize(0)
00037 , m_TrianglesCount(0)
00038 {
00039 
00040 }
00041 
00042 
00043 GLC_Lod::GLC_Lod(double accuracy)
00044 : m_Accuracy(accuracy)
00045 , m_IboId(0)
00046 , m_IndexVector()
00047 , m_IndexSize(0)
00048 , m_TrianglesCount(0)
00049 {
00050 
00051 }
00052 
00053 
00054 GLC_Lod::GLC_Lod(const GLC_Lod& lod)
00055 : m_Accuracy(lod.m_Accuracy)
00056 , m_IboId(0)
00057 , m_IndexVector(lod.indexVector())
00058 , m_IndexSize(lod.m_IndexSize)
00059 , m_TrianglesCount(lod.m_TrianglesCount)
00060 {
00061 
00062 
00063 }
00064 
00065 
00066 GLC_Lod& GLC_Lod::operator=(const GLC_Lod& lod)
00067 {
00068         if (this != &lod)
00069         {
00070                 m_Accuracy= lod.m_Accuracy;
00071                 m_IboId= 0;
00072                 m_IndexVector= lod.indexVector();
00073                 m_IndexSize= lod.m_IndexSize;
00074                 m_TrianglesCount= lod.m_TrianglesCount;
00075         }
00076 
00077         return *this;
00078 }
00079 
00080 GLC_Lod::~GLC_Lod()
00081 {
00082         // Delete IBO
00083         if (0 != m_IboId)
00084         {
00085                 glDeleteBuffers(1, &m_IboId);
00086         }
00087 }
00088 
00090 // Get Functions
00092 
00093 quint32 GLC_Lod::chunckID()
00094 {
00095         return m_ChunkId;
00096 }
00097 
00098 
00099 QVector<GLuint> GLC_Lod::indexVector() const
00100 {
00101         if (0 != m_IboId)
00102         {
00103                 // VBO created get data from VBO
00104                 const int sizeOfIbo= m_IndexSize;
00105                 const GLsizeiptr dataSize= sizeOfIbo * sizeof(GLuint);
00106                 QVector<GLuint> indexVector(sizeOfIbo);
00107 
00108                 useIBO();
00109                 GLvoid* pIbo = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
00110                 memcpy(indexVector.data(), pIbo, dataSize);
00111                 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
00112                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
00113                 return indexVector;
00114         }
00115         else
00116         {
00117                 return m_IndexVector;
00118         }
00119 }
00120 
00121 
00122 void GLC_Lod::copyIboToClientSide()
00123 {
00124         if ((0 != m_IboId) && (m_IndexVector.isEmpty()))
00125         {
00126                 m_IndexVector= indexVector();
00127         }
00128 }
00129 
00130 
00131 void GLC_Lod::releaseIboClientSide(bool update)
00132 {
00133         if((0 != m_IboId) && !m_IndexVector.isEmpty())
00134         {
00135                 if (update)
00136                 {
00137                         // Copy index from client side to serveur
00138                         useIBO();
00139 
00140                         const GLsizei indexNbr= static_cast<GLsizei>(m_IndexVector.size());
00141                         const GLsizeiptr indexSize = indexNbr * sizeof(GLuint);
00142                         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize, m_IndexVector.data(), GL_STATIC_DRAW);
00143                         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
00144                 }
00145                 m_IndexVector.clear();
00146         }
00147 }
00148 
00149 
00150 QDataStream &operator<<(QDataStream &stream, const GLC_Lod &lod)
00151 {
00152         quint32 chunckId= GLC_Lod::m_ChunkId;
00153         stream << chunckId;
00154 
00155         stream << lod.m_Accuracy;
00156         stream << lod.indexVector();
00157         stream << lod.m_TrianglesCount;
00158 
00159         return stream;
00160 }
00161 QDataStream &operator>>(QDataStream &stream, GLC_Lod &lod)
00162 {
00163         quint32 chunckId;
00164         stream >> chunckId;
00165         Q_ASSERT(chunckId == GLC_Lod::m_ChunkId);
00166 
00167         stream >> lod.m_Accuracy;
00168         stream >> lod.m_IndexVector;
00169         stream >> lod.m_TrianglesCount;
00170 
00171         return stream;
00172 }
00173 

SourceForge.net Logo

©2005-2011 Laurent Ribon