00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00026
00027 #include "glc_meshdata.h"
00028 #include "../glc_state.h"
00029
00030
00031 quint32 GLC_MeshData::m_ChunkId= 0xA704;
00032
00033
00034 GLC_MeshData::GLC_MeshData()
00035 : m_VboId(0)
00036 , m_Positions()
00037 , m_Normals()
00038 , m_Texels()
00039 , m_Colors()
00040 , m_NormalVboId(0)
00041 , m_TexelVboId(0)
00042 , m_ColorVboId(0)
00043 , m_LodList()
00044 , m_PositionSize(0)
00045 , m_TexelsSize(0)
00046 , m_ColorSize(0)
00047 {
00048
00049 }
00050
00051
00052 GLC_MeshData::GLC_MeshData(const GLC_MeshData& meshData)
00053 : m_VboId(0)
00054 , m_Positions(meshData.positionVector())
00055 , m_Normals(meshData.normalVector())
00056 , m_Texels(meshData.texelVector())
00057 , m_Colors(meshData.colorVector())
00058 , m_NormalVboId(0)
00059 , m_TexelVboId(0)
00060 , m_ColorVboId(0)
00061 , m_LodList()
00062 , m_PositionSize(meshData.m_PositionSize)
00063 , m_TexelsSize(meshData.m_TexelsSize)
00064 , m_ColorSize(meshData.m_ColorSize)
00065 {
00066
00067 const int size= meshData.m_LodList.size();
00068 for (int i= 0; i < size; ++i)
00069 {
00070 m_LodList.append(new GLC_Lod(*meshData.m_LodList.at(i)));
00071 }
00072 }
00073
00074
00075 GLC_MeshData& GLC_MeshData::operator=(const GLC_MeshData& meshData)
00076 {
00077 if (this != &meshData)
00078 {
00079
00080 clear();
00081
00082
00083 m_Positions= meshData.positionVector();
00084 m_Normals= meshData.normalVector();
00085 m_Texels= meshData.texelVector();
00086 m_Colors= meshData.colorVector();
00087 m_PositionSize= meshData.m_PositionSize;
00088 m_TexelsSize= meshData.m_TexelsSize;
00089 m_ColorSize= meshData.m_ColorSize;
00090
00091
00092 const int size= meshData.m_LodList.size();
00093 for (int i= 0; i < size; ++i)
00094 {
00095 m_LodList.append(new GLC_Lod(*meshData.m_LodList.at(i)));
00096 }
00097 }
00098 return *this;
00099 }
00100
00101 GLC_MeshData::~GLC_MeshData()
00102 {
00103 clear();
00104 }
00106
00108
00109 quint32 GLC_MeshData::chunckID()
00110 {
00111 return m_ChunkId;
00112 }
00113
00114
00115 GLfloatVector GLC_MeshData::positionVector() const
00116 {
00117 if (0 != m_VboId)
00118 {
00119
00120 const int sizeOfVbo= m_PositionSize;
00121 const GLsizeiptr dataSize= sizeOfVbo * sizeof(float);
00122 GLfloatVector positionVector(sizeOfVbo);
00123
00124 glBindBuffer(GL_ARRAY_BUFFER, m_VboId);
00125 GLvoid* pVbo = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
00126 memcpy(positionVector.data(), pVbo, dataSize);
00127 glUnmapBuffer(GL_ARRAY_BUFFER);
00128 glBindBuffer(GL_ARRAY_BUFFER, 0);
00129 return positionVector;
00130 }
00131 else
00132 {
00133 return m_Positions;
00134 }
00135 }
00136
00137
00138 GLfloatVector GLC_MeshData::normalVector() const
00139 {
00140 if (0 != m_NormalVboId)
00141 {
00142
00143 const int sizeOfVbo= m_PositionSize;
00144 const GLsizeiptr dataSize= sizeOfVbo * sizeof(GLfloat);
00145 GLfloatVector normalVector(sizeOfVbo);
00146
00147 glBindBuffer(GL_ARRAY_BUFFER, m_NormalVboId);
00148 GLvoid* pVbo = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
00149 memcpy(normalVector.data(), pVbo, dataSize);
00150 glUnmapBuffer(GL_ARRAY_BUFFER);
00151 glBindBuffer(GL_ARRAY_BUFFER, 0);
00152 return normalVector;
00153 }
00154 else
00155 {
00156 return m_Normals;
00157 }
00158 }
00159
00160
00161 GLfloatVector GLC_MeshData::texelVector() const
00162 {
00163 if (0 != m_TexelVboId)
00164 {
00165
00166 const int sizeOfVbo= m_TexelsSize;
00167 const GLsizeiptr dataSize= sizeOfVbo * sizeof(GLfloat);
00168 GLfloatVector texelVector(sizeOfVbo);
00169
00170 glBindBuffer(GL_ARRAY_BUFFER, m_TexelVboId);
00171 GLvoid* pVbo = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
00172 memcpy(texelVector.data(), pVbo, dataSize);
00173 glUnmapBuffer(GL_ARRAY_BUFFER);
00174 glBindBuffer(GL_ARRAY_BUFFER, 0);
00175 return texelVector;
00176 }
00177 else
00178 {
00179 return m_Texels;
00180 }
00181 }
00182
00183
00184 GLfloatVector GLC_MeshData::colorVector() const
00185 {
00186 if (0 != m_ColorVboId)
00187 {
00188
00189 const int sizeOfVbo= m_ColorSize;
00190 const GLsizeiptr dataSize= sizeOfVbo * sizeof(GLfloat);
00191 GLfloatVector normalVector(sizeOfVbo);
00192
00193 glBindBuffer(GL_ARRAY_BUFFER, m_ColorVboId);
00194 GLvoid* pVbo = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
00195 memcpy(normalVector.data(), pVbo, dataSize);
00196 glUnmapBuffer(GL_ARRAY_BUFFER);
00197 glBindBuffer(GL_ARRAY_BUFFER, 0);
00198 return normalVector;
00199 }
00200 else
00201 {
00202 return m_Colors;
00203 }
00204 }
00205
00207
00209
00210
00211 void GLC_MeshData::finishVbo()
00212 {
00213 m_PositionSize= m_Positions.size();
00214 m_Positions.clear();
00215 m_Normals.clear();
00216 m_TexelsSize= m_Texels.size();
00217 m_Texels.clear();
00218 m_ColorSize= m_Colors.size();
00219 m_Colors.clear();
00220
00221
00222 const int size= m_LodList.size();
00223 for (int i= 0; i < size; ++i)
00224 {
00225 m_LodList[i]->finishVbo();
00226 }
00227 }
00228
00229
00230 void GLC_MeshData::finishLod()
00231 {
00232
00233 const int size= m_LodList.size();
00234 if (size > 1)
00235 {
00236 GLC_Lod* PMasterLod= m_LodList.at(size - 1);
00237 m_LodList.removeAt(size - 1);
00238 m_LodList.prepend(PMasterLod);
00239 }
00240 }
00241
00242
00243 void GLC_MeshData::clear()
00244 {
00245 m_Positions.clear();
00246 m_Normals.clear();
00247 m_Texels.clear();
00248 m_PositionSize= 0;
00249 m_TexelsSize= 0;
00250
00251
00252 if (0 != m_VboId)
00253 {
00254 glDeleteBuffers(1, &m_VboId);
00255 m_VboId= 0;
00256 }
00257
00258
00259 if (0 != m_NormalVboId)
00260 {
00261 glDeleteBuffers(1, &m_NormalVboId);
00262 m_NormalVboId= 0;
00263 }
00264
00265
00266 if (0 != m_TexelVboId)
00267 {
00268 glDeleteBuffers(1, &m_TexelVboId);
00269 m_TexelVboId= 0;
00270 }
00271
00272 const int size= m_LodList.size();
00273 for (int i= 0; i < size; ++i)
00274 {
00275 delete m_LodList.at(i);
00276 }
00277 m_LodList.clear();
00278 }
00279
00281
00283
00284 void GLC_MeshData::createVBOs()
00285 {
00286
00287 if (0 == m_VboId)
00288 {
00289 glGenBuffers(1, &m_VboId);
00290 glGenBuffers(1, &m_NormalVboId);
00291
00292
00293 if (0 == m_TexelVboId && !m_Texels.isEmpty())
00294 {
00295 glGenBuffers(1, &m_TexelVboId);
00296 }
00297
00298
00299 if (0 == m_ColorVboId && !m_Colors.isEmpty())
00300 {
00301 glGenBuffers(1, &m_ColorVboId);
00302 }
00303
00304 const int size= m_LodList.size();
00305 for (int i= 0; i < size; ++i)
00306 {
00307 m_LodList.at(i)->createIBO();
00308 }
00309 }
00310 }
00311
00312
00313 bool GLC_MeshData::useVBO(bool use, GLC_MeshData::VboType type)
00314 {
00315 bool result= true;
00316 if (use)
00317 {
00318
00319 if (type == GLC_MeshData::GLC_Vertex)
00320 {
00321 glBindBuffer(GL_ARRAY_BUFFER, m_VboId);
00322 }
00323 else if (type == GLC_MeshData::GLC_Normal)
00324 {
00325 glBindBuffer(GL_ARRAY_BUFFER, m_NormalVboId);
00326 }
00327 else if ((type == GLC_MeshData::GLC_Texel) && (0 != m_TexelVboId))
00328 {
00329 glBindBuffer(GL_ARRAY_BUFFER, m_TexelVboId);
00330 }
00331 else if ((type == GLC_MeshData::GLC_Color) && (0 != m_ColorVboId))
00332 {
00333 glBindBuffer(GL_ARRAY_BUFFER, m_ColorVboId);
00334 }
00335
00336 else result= false;
00337 }
00338 else
00339 {
00340
00341 glBindBuffer(GL_ARRAY_BUFFER, 0);
00342 }
00343 return result;
00344
00345 }
00346
00347
00348 QDataStream &operator<<(QDataStream &stream, const GLC_MeshData &meshData)
00349 {
00350 quint32 chunckId= GLC_MeshData::m_ChunkId;
00351 stream << chunckId;
00352
00353 stream << meshData.positionVector();
00354 stream << meshData.normalVector();
00355 stream << meshData.texelVector();
00356 stream << meshData.colorVector();
00357
00358
00359 const int lodCount= meshData.m_LodList.size();
00360 QList<GLC_Lod> lodsList;
00361 for (int i= 0; i < lodCount; ++i)
00362 {
00363 lodsList.append(*(meshData.m_LodList[i]));
00364 }
00365 stream << lodsList;
00366
00367 return stream;
00368 }
00369
00370 QDataStream &operator>>(QDataStream &stream, GLC_MeshData &meshData)
00371 {
00372 quint32 chunckId;
00373 stream >> chunckId;
00374 Q_ASSERT(chunckId == GLC_MeshData::m_ChunkId);
00375
00376 meshData.clear();
00377
00378 GLfloatVector position, normal, texel, color;
00379
00380 stream >> *(meshData.positionVectorHandle());
00381 stream >> *(meshData.normalVectorHandle());
00382 stream >> *(meshData.texelVectorHandle());
00383 stream >> *(meshData.colorVectorHandle());
00384
00385
00386 QList<GLC_Lod> lodsList;
00387 stream >> lodsList;
00388 const int lodCount= lodsList.size();
00389 for (int i= 0; i < lodCount; ++i)
00390 {
00391 meshData.m_LodList.append(new GLC_Lod(lodsList.at(i)));
00392 }
00393
00394 return stream;
00395 }
00396