1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include "mesh.h"
#include "assert.h"
#include "../materials/texture.h"
#include <QDebug>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
Mesh::Mesh() : m_funcs(0) {}
Mesh::~Mesh()
{
Clear();
}
Mesh::MeshEntry::MeshEntry() {}
Mesh::MeshEntry::MeshEntry(Mesh* mesh) : m_mesh(mesh)
{
VB = INVALID_OGL_VALUE;
IB = INVALID_OGL_VALUE;
NumIndices = 0;
MaterialIndex = INVALID_MATERIAL;
}
Mesh::MeshEntry::MeshEntry(const MeshEntry& other)
{
if(other.m_mesh)
this->m_mesh = new Mesh(*other.m_mesh);
else
this->m_mesh = NULL;
}
Mesh::MeshEntry::~MeshEntry()
{
if(VB != INVALID_OGL_VALUE)
m_mesh->m_funcs->glDeleteBuffers(1, &VB);
if(IB != INVALID_OGL_VALUE)
m_mesh->m_funcs->glDeleteBuffers(1, &IB);
delete this->m_mesh;
}
Mesh::MeshEntry& Mesh::MeshEntry::operator=(const MeshEntry& other)
{
Mesh::MeshEntry tmp(other);
std::swap(tmp.m_mesh, this->m_mesh);
return *this;
}
void Mesh::MeshEntry::Init(const vector<Vertex>& Vertices, const vector<unsigned int>& Indices)
{
NumIndices = static_cast<unsigned int>(Indices.size());
m_mesh->m_funcs->glGenBuffers(1, &VB);
m_mesh->m_funcs->glBindBuffer(GL_ARRAY_BUFFER, VB);
m_mesh->m_funcs->glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * Vertices.size(), &Vertices[0], GL_STATIC_DRAW);
m_mesh->m_funcs->glGenBuffers(1, &IB);
m_mesh->m_funcs->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
m_mesh->m_funcs->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * NumIndices, &Indices[0], GL_STATIC_DRAW);
}
void Mesh::Clear()
{
for(unsigned int i = 0; i < m_Textures.size(); i++)
{
SAFE_DELETE(m_Textures[i]);
}
}
void Mesh::Init()
{
QOpenGLContext* context = QOpenGLContext::currentContext();
Q_ASSERT(context);
m_funcs = context->functions();
m_funcs->initializeOpenGLFunctions();
}
void Mesh::LoadMesh(const string& Filename)
{
Clear();
Assimp::Importer Importer;
const aiScene* pScene = Importer.ReadFile(Filename.c_str(), aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs);
if(pScene)
InitFromScene(pScene, Filename);
else
qDebug() << "Error parsing : " << Filename.c_str() << " -> " << Importer.GetErrorString() << endl;
}
void Mesh::InitFromScene(const aiScene* pScene, const string& Filename)
{
m_Entries.resize(pScene->mNumMeshes);
m_Textures.resize(pScene->mNumMaterials);
for(unsigned int i = 0; i < m_Entries.size(); i++)
{
const aiMesh* paiMesh = pScene->mMeshes[i];
InitMesh(i, paiMesh);
}
InitMaterials(pScene, Filename);
}
bool Mesh::InitMaterials(const aiScene* pScene, const string& Filename)
{
Q_UNUSED(pScene);
Q_UNUSED(Filename);
return true;
}
void Mesh::InitMesh(unsigned int Index, const aiMesh* paiMesh)
{
Q_UNUSED(Index);
Q_UNUSED(paiMesh);
}
void Mesh::Render()
{
} |
Partager