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 #include "../glc_exception.h"
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_IndexBuffer(QGLBuffer::IndexBuffer)
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_IndexBuffer(QGLBuffer::IndexBuffer)
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_IndexBuffer(QGLBuffer::IndexBuffer)
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_IndexBuffer.destroy();
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 
00083 }
00084 
00086 // Get Functions
00088 
00089 quint32 GLC_Lod::chunckID()
00090 {
00091         return m_ChunkId;
00092 }
00093 
00094 
00095 QVector<GLuint> GLC_Lod::indexVector() const
00096 {
00097         if (m_IndexBuffer.isCreated())
00098         {
00099                 // VBO created get data from VBO
00100                 const int sizeOfIbo= m_IndexSize;
00101                 const GLsizeiptr dataSize= sizeOfIbo * sizeof(GLuint);
00102                 QVector<GLuint> indexVector(sizeOfIbo);
00103 
00104                 const_cast<QGLBuffer&>(m_IndexBuffer).bind();
00105                 GLvoid* pIbo = const_cast<QGLBuffer&>(m_IndexBuffer).map(QGLBuffer::ReadOnly);
00106                 memcpy(indexVector.data(), pIbo, dataSize);
00107                 const_cast<QGLBuffer&>(m_IndexBuffer).unmap();
00108                 const_cast<QGLBuffer&>(m_IndexBuffer).release();
00109                 return indexVector;
00110         }
00111         else
00112         {
00113                 return m_IndexVector;
00114         }
00115 }
00116 
00117 
00118 void GLC_Lod::copyIboToClientSide()
00119 {
00120         if (m_IndexBuffer.isCreated() && (m_IndexVector.isEmpty()))
00121         {
00122                 m_IndexVector= indexVector();
00123         }
00124 }
00125 
00126 
00127 void GLC_Lod::releaseIboClientSide(bool update)
00128 {
00129         if(m_IndexBuffer.isCreated() && !m_IndexVector.isEmpty())
00130         {
00131                 if (update)
00132                 {
00133                         // Copy index from client side to serveur
00134                         m_IndexBuffer.bind();
00135 
00136                         const GLsizei indexNbr= static_cast<GLsizei>(m_IndexVector.size());
00137                         const GLsizeiptr indexSize = indexNbr * sizeof(GLuint);
00138                         m_IndexBuffer.allocate(m_IndexVector.data(), indexSize);
00139                         m_IndexBuffer.release();
00140                 }
00141                 m_IndexSize= m_IndexVector.size();
00142                 m_IndexVector.clear();
00143         }
00144 }
00145 
00146 void GLC_Lod::setIboUsage(bool usage)
00147 {
00148         if (usage && !m_IndexVector.isEmpty())
00149         {
00150                 createIBO();
00151                 // Copy index from client side to serveur
00152                 m_IndexBuffer.bind();
00153 
00154                 const GLsizei indexNbr= static_cast<GLsizei>(m_IndexVector.size());
00155                 const GLsizeiptr indexSize = indexNbr * sizeof(GLuint);
00156                 m_IndexBuffer.allocate(m_IndexVector.data(), indexSize);
00157                 m_IndexBuffer.release();
00158 
00159                 m_IndexSize= m_IndexVector.size();
00160                 m_IndexVector.clear();
00161 
00162         }
00163         else if (!usage && m_IndexBuffer.isCreated())
00164         {
00165                 m_IndexVector= indexVector();
00166                 m_IndexBuffer.destroy();
00167         }
00168 }
00169 
00170 void GLC_Lod::useIBO() const
00171 {
00172         Q_ASSERT(m_IndexBuffer.isCreated());
00173         if (!const_cast<QGLBuffer&>(m_IndexBuffer).bind())
00174         {
00175                 GLC_Exception exception("GLC_Lod::useIBO  Failed to bind index buffer");
00176                 throw(exception);
00177         }
00178 }
00179 
00180 
00181 QDataStream &operator<<(QDataStream &stream, const GLC_Lod &lod)
00182 {
00183         quint32 chunckId= GLC_Lod::m_ChunkId;
00184         stream << chunckId;
00185 
00186         stream << lod.m_Accuracy;
00187         stream << lod.indexVector();
00188         stream << lod.m_TrianglesCount;
00189 
00190         return stream;
00191 }
00192 QDataStream &operator>>(QDataStream &stream, GLC_Lod &lod)
00193 {
00194         quint32 chunckId;
00195         stream >> chunckId;
00196         Q_ASSERT(chunckId == GLC_Lod::m_ChunkId);
00197 
00198         stream >> lod.m_Accuracy;
00199         stream >> lod.m_IndexVector;
00200         stream >> lod.m_TrianglesCount;
00201 
00202         return stream;
00203 }
00204 

SourceForge.net Logo

©2005-2011 Laurent Ribon