GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_lod.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 *****************************************************************************/
22 
24 
25 #include "../glc_exception.h"
26 #include "glc_lod.h"
27 
28 // Class chunk id
29 quint32 GLC_Lod::m_ChunkId= 0xA708;
30 
31 
33 : m_Accuracy(0.0)
34 , m_IndexBuffer(QGLBuffer::IndexBuffer)
35 , m_IndexVector()
36 , m_IndexSize(0)
37 , m_TrianglesCount(0)
38 {
39 
40 }
41 
42 
43 GLC_Lod::GLC_Lod(double accuracy)
44 : m_Accuracy(accuracy)
45 , m_IndexBuffer(QGLBuffer::IndexBuffer)
46 , m_IndexVector()
47 , m_IndexSize(0)
48 , m_TrianglesCount(0)
49 {
50 
51 }
52 
53 
55 : m_Accuracy(lod.m_Accuracy)
56 , m_IndexBuffer(QGLBuffer::IndexBuffer)
57 , m_IndexVector(lod.indexVector())
58 , m_IndexSize(lod.m_IndexSize)
59 , m_TrianglesCount(lod.m_TrianglesCount)
60 {
61 
62 
63 }
64 
65 
67 {
68  if (this != &lod)
69  {
71  m_IndexBuffer.destroy();
75  }
76 
77  return *this;
78 }
79 
81 {
82 
83 }
84 
86 // Get Functions
88 
90 {
91  return m_ChunkId;
92 }
93 
94 
95 QVector<GLuint> GLC_Lod::indexVector() const
96 {
97  if (m_IndexBuffer.isCreated())
98  {
99  // VBO created get data from VBO
100  const int sizeOfIbo= m_IndexSize;
101  const GLsizeiptr dataSize= sizeOfIbo * sizeof(GLuint);
102  QVector<GLuint> indexVector(sizeOfIbo);
103 
104  const_cast<QGLBuffer&>(m_IndexBuffer).bind();
105  GLvoid* pIbo = const_cast<QGLBuffer&>(m_IndexBuffer).map(QGLBuffer::ReadOnly);
106  memcpy(indexVector.data(), pIbo, dataSize);
107  const_cast<QGLBuffer&>(m_IndexBuffer).unmap();
108  const_cast<QGLBuffer&>(m_IndexBuffer).release();
109  return indexVector;
110  }
111  else
112  {
113  return m_IndexVector;
114  }
115 }
116 
117 
119 {
120  if (m_IndexBuffer.isCreated() && (m_IndexVector.isEmpty()))
121  {
123  }
124 }
125 
126 
128 {
129  if(m_IndexBuffer.isCreated() && !m_IndexVector.isEmpty())
130  {
131  if (update)
132  {
133  // Copy index from client side to serveur
134  m_IndexBuffer.bind();
135 
136  const GLsizei indexNbr= static_cast<GLsizei>(m_IndexVector.size());
137  const GLsizeiptr indexSize = indexNbr * sizeof(GLuint);
138  m_IndexBuffer.allocate(m_IndexVector.data(), indexSize);
139  m_IndexBuffer.release();
140  }
141  m_IndexSize= m_IndexVector.size();
142  m_IndexVector.clear();
143  }
144 }
145 
146 void GLC_Lod::setIboUsage(bool usage)
147 {
148  if (usage && !m_IndexVector.isEmpty())
149  {
150  createIBO();
151  // Copy index from client side to serveur
152  m_IndexBuffer.bind();
153 
154  const GLsizei indexNbr= static_cast<GLsizei>(m_IndexVector.size());
155  const GLsizeiptr indexSize = indexNbr * sizeof(GLuint);
156  m_IndexBuffer.allocate(m_IndexVector.data(), indexSize);
157  m_IndexBuffer.release();
158 
159  m_IndexSize= m_IndexVector.size();
160  m_IndexVector.clear();
161 
162  }
163  else if (!usage && m_IndexBuffer.isCreated())
164  {
166  m_IndexBuffer.destroy();
167  }
168 }
169 
170 void GLC_Lod::useIBO() const
171 {
172  Q_ASSERT(m_IndexBuffer.isCreated());
173  if (!const_cast<QGLBuffer&>(m_IndexBuffer).bind())
174  {
175  GLC_Exception exception("GLC_Lod::useIBO Failed to bind index buffer");
176  throw(exception);
177  }
178 }
179 
180 
181 QDataStream &operator<<(QDataStream &stream, const GLC_Lod &lod)
182 {
183  quint32 chunckId= GLC_Lod::m_ChunkId;
184  stream << chunckId;
185 
186  stream << lod.m_Accuracy;
187  stream << lod.indexVector();
188  stream << lod.m_TrianglesCount;
189 
190  return stream;
191 }
192 QDataStream &operator>>(QDataStream &stream, GLC_Lod &lod)
193 {
194  quint32 chunckId;
195  stream >> chunckId;
196  Q_ASSERT(chunckId == GLC_Lod::m_ChunkId);
197 
198  stream >> lod.m_Accuracy;
199  stream >> lod.m_IndexVector;
200  stream >> lod.m_TrianglesCount;
201 
202  return stream;
203 }
204 

©2005-2013 Laurent Ribon