From: Peter Schaefer Date: Tue, 14 Apr 2015 22:50:50 +0000 (+0200) Subject: removed import from Game X-Git-Url: https://git.leopard-lacewing.eu/?a=commitdiff_plain;h=e3897d2cff6a8ad35f3a987f71311d7285f233f2;p=cgue_weave.git removed import from Game added MetaModel updated Model & import --- diff --git a/.gitignore b/.gitignore index 3eba649..e4baabc 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ bin/*.exe bin/*.pdb Submission* +Debug/*.ilk Debug/*.exe Debug/*.pdb RES diff --git a/Debug/Weave.ilk b/Debug/Weave.ilk deleted file mode 100644 index f704428..0000000 Binary files a/Debug/Weave.ilk and /dev/null differ diff --git a/Weave/Game.cpp b/Weave/Game.cpp index be0a8ef..80fac06 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -176,78 +176,4 @@ void Game::draw() const else l_menu->draw(); -} - -bool import(const string& path, Scene* scene, Shader* shader) -{ - Assimp::Importer importer; - - const aiScene* file = importer.ReadFile("../models/" + path, aiProcess_GenUVCoords | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices); - if (!file) - { - Message::error("The file " + path + " couldn't be read.\n" + importer.GetErrorString()); - return false; - } - - if (file->HasMeshes()) - { - for (uint n = 0; n < file->mNumMeshes; n++) - { - aiMesh* mesh = file->mMeshes[n]; - - uint numvertices = mesh->mNumVertices; - uint numfaces = mesh->mNumFaces; - float *vertex = new float[numvertices * 3]; - float *uvs = new float[numvertices * 2]; - uint *index = new uint[numfaces * 3]; - float *normals = new float[numvertices * 3]; - //aiFace* faces = mesh->mFaces; - - - //load vertices from file - for (uint i = 0; i < numvertices; i++) - { - vertex[3 * i] = mesh->mVertices[i].x; - vertex[3 * i + 1] = mesh->mVertices[i].y; - vertex[3 * i + 2] = mesh->mVertices[i].z; - } - - //load UVs from file - for (uint i = 0; i < numvertices; i++) - { - uvs[2 * i] = mesh->mTextureCoords[0][i].x;//[i]->x; - uvs[2 * i + 1] = mesh->mTextureCoords[0][i].y;//[i]->y; - } - - //load indices from file - for (uint i = 0; i < numfaces; i++) - { - index[3 * i] = mesh->mFaces[i].mIndices[0]; - index[3 * i + 1] = mesh->mFaces[i].mIndices[1]; - index[3 * i + 2] = mesh->mFaces[i].mIndices[2]; - } - - //load normals from file - for (uint i = 0; i < numvertices; i++) - { - normals[3 * i] = mesh->mNormals[i].x; - normals[3 * i + 1] = mesh->mNormals[i].y; - normals[3 * i + 2] = mesh->mNormals[i].z; - } - - Model *model = new Model(numvertices, numfaces, vertex, uvs, normals, index); - delete vertex, normals, uvs, index; - - //TODO enable different textures - scene->addObject(new SceneObject(shader, translate(vec3(1.f, 0.f, 1.f)), model, "model_cow.jpg")); - //file->mRootNode->mChildren[n]->mTransformation.a1 ... d4 - } - } - else - { - Message::error("The file " + path + " doesn't contain any nodes."); - return false; - } - - return true; } \ No newline at end of file diff --git a/Weave/Graphix/MetaModel.cpp b/Weave/Graphix/MetaModel.cpp new file mode 100644 index 0000000..8ca79d0 --- /dev/null +++ b/Weave/Graphix/MetaModel.cpp @@ -0,0 +1,64 @@ +#include "MetaModel.h" + +#include +#include +#include +#include + +#include "Model.h" +#include "Shader.h" +#include "../Message.h" + + +MetaModel::MetaModel(const string& modelpath) +{ + Assimp::Importer importer; + + const aiScene* scene = importer.ReadFile("../models/" + modelpath, aiProcess_GenUVCoords | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType); //aiProcess_PreTransformVertices + if (!scene) + { + Message::error("The file " + modelpath + " couldn't be read.\n" + importer.GetErrorString()); + return; + } + + if (scene->HasMeshes())// && scene->mNumMeshes > mindex) + { + Model* tmp_model=nullptr; + for (unsigned int i = 0; i < scene->mNumMeshes; i++) + { + models.push_back(new Model(scene->mMeshes[i])); + } + + } + else + { + Message::error("The file " + modelpath + " doesn't contain any nodes."); + return; + } +} + +MetaModel::~MetaModel() +{ + for (auto i = models.begin(); i != models.end(); ++i) + { + delete (*i); + } +} + +void MetaModel::bindShader(Shader* _shader) +{ + for (auto i = models.begin(); i != models.end(); ++i) + { + (*i)->bindShader(_shader); + } +} + +void MetaModel::drawModel(Shader* _shader, Texture* _texture, const mat4& _model) const +{ + for (auto i = models.begin(); i != models.end(); ++i) + { + (*i)->drawModel(_shader,_texture,_model); + } +} + + diff --git a/Weave/Graphix/MetaModel.h b/Weave/Graphix/MetaModel.h new file mode 100644 index 0000000..f4c63c1 --- /dev/null +++ b/Weave/Graphix/MetaModel.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include "GLM.h" + +using std::string; +using std::list; + +class Model; +class Shader; +class Texture; + +typedef list ModelList; + +class MetaModel +{ +public: + MetaModel(const string& modelpath); + ~MetaModel(); + + void bindShader(Shader* shader); + + void drawModel(Shader* shader, Texture* texture, const mat4& model) const; + +private: + ModelList models; +}; + diff --git a/Weave/Graphix/Model.cpp b/Weave/Graphix/Model.cpp index 6110fba..23bb731 100644 --- a/Weave/Graphix/Model.cpp +++ b/Weave/Graphix/Model.cpp @@ -6,31 +6,54 @@ #include #include "Shader.h" +#include "Texture.h" #include "../Message.h" typedef unsigned int uint; -Model::Model(const string& _modelpath, unsigned int _mindex) : - numfaces(-1), - numvertices(-1), - vertexBuffer(-1), - normalBuffer(-1), - uvBuffer(-1), - indexBuffer(-1) +Model::Model(const string& _modelpath, unsigned int _mindex) : +numfaces(-1), +numvertices(-1), +vertexBuffer(-1), +normalBuffer(-1), +uvBuffer(-1), +indexBuffer(-1) { - float *vertex = nullptr, *normals = nullptr, *uvs = nullptr; - uint *index = nullptr; + float *vertex = nullptr, *normals = nullptr, *uvs = nullptr; + uint *index = nullptr; + import(_modelpath, numvertices, numfaces, vertex, uvs, normals, index, _mindex); + + genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); + genBuffer(normalBuffer, numvertices * 3 * sizeof(float), (void*)normals); + genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs); + genBuffer(indexBuffer, numfaces * 3 * sizeof(uint), (void*)index); + + delete vertex, normals, uvs, index; + +} - import(_modelpath, numvertices, numfaces, vertex, uvs, normals, index, _mindex); +Model::Model(const aiMesh* _mesh) : +numfaces(-1), +numvertices(-1), +vertexBuffer(-1), +normalBuffer(-1), +uvBuffer(-1), +indexBuffer(-1) +{ - genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); - genBuffer(normalBuffer, numvertices * 3 * sizeof(float), (void*)normals); - genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs); - genBuffer(indexBuffer, numfaces * 3 * sizeof(uint), (void*)index); + float *vertex = nullptr, *normals = nullptr, *uvs = nullptr; + uint *index = nullptr; - delete vertex, normals, uvs, index; + import(_mesh, numvertices, numfaces, vertex, uvs, normals, index); + + genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); + genBuffer(normalBuffer, numvertices * 3 * sizeof(float), (void*)normals); + genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs); + genBuffer(indexBuffer, numfaces * 3 * sizeof(uint), (void*)index); + + delete vertex, normals, uvs, index; } @@ -59,15 +82,15 @@ indexBuffer(-1) Model::~Model() { - for (auto i = shader_map.begin(); i != shader_map.end(); ++i){ - glDeleteVertexArrays(1, &(i->second)); - } - glDeleteBuffers(1, &vertexBuffer); - glDeleteBuffers(1, &normalBuffer); - glDeleteBuffers(1, &uvBuffer); - glDeleteBuffers(1, &indexBuffer); - - + for (auto i = shader_map.begin(); i != shader_map.end(); ++i){ + glDeleteVertexArrays(1, &(i->second)); + } + glDeleteBuffers(1, &vertexBuffer); + glDeleteBuffers(1, &normalBuffer); + glDeleteBuffers(1, &uvBuffer); + glDeleteBuffers(1, &indexBuffer); + + } //void Model::useModel(Shader* _shader) @@ -98,9 +121,32 @@ void Model::useModel(Shader* _shader) const } -void Model::drawModel() const +void Model::useTexture(Texture* _texture, Shader* _shader) const { + if (_texture != nullptr) + { + int unit = 0; + _texture->bind(unit); + glUniform1i(_shader->getUniformLocation("ColorTexture"), unit); + } +} +void Model::useMMatrix(const mat4& _model, Shader* _shader) const +{ + glUniformMatrix4fv(_shader->getUniformLocation("model"), 1, GL_FALSE, value_ptr(_model)); +} + +void Model::drawModel(Shader* _shader, Texture* _texture, const mat4& _model) const +{ + useModel(_shader); + useTexture(_texture, _shader); + useMMatrix(_model, _shader); + glDrawElements(GL_TRIANGLES, numfaces * 3, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); +} + +void Model::drawModel() const +{ glDrawElements(GL_TRIANGLES, numfaces * 3, GL_UNSIGNED_INT, 0); glBindVertexArray(0); } @@ -135,7 +181,7 @@ void Model::genBuffer(uint &buffer, uint size, void* value) glBindBuffer(GL_ARRAY_BUFFER, 0); } -void Model::bindBuffer(const uint &buffer,const uint index,const uint dim) +void Model::bindBuffer(const uint &buffer, const uint index, const uint dim) { glBindBuffer(GL_ARRAY_BUFFER, buffer); glEnableVertexAttribArray(index); @@ -144,8 +190,7 @@ void Model::bindBuffer(const uint &buffer,const uint index,const uint dim) } - -bool Model::import(const string& path,uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index, uint mindex) +bool Model::import(const string& path, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index, uint mindex) { Assimp::Importer importer; @@ -158,47 +203,7 @@ bool Model::import(const string& path,uint& numvertices, uint& numfaces, float*& if (scene->HasMeshes() && scene->mNumMeshes > mindex) { - aiMesh* mesh = scene->mMeshes[mindex]; - numvertices = mesh->mNumVertices; - numfaces = mesh->mNumFaces; - vertex = new float[numvertices * 3]; - uvs = new float[numvertices * 2]; - index = new uint[numfaces * 3]; - normals = new float[numvertices * 3]; - //aiFace* faces = mesh->mFaces; - - - //load vertices from file - for (uint i = 0; i < numvertices; i++) - { - vertex[3 * i] = mesh->mVertices[i].x; - vertex[3 * i + 1] = mesh->mVertices[i].y; - vertex[3 * i + 2] = mesh->mVertices[i].z; - } - - //load UVs from file - for (uint i = 0; i < numvertices; i++) - { - uvs[2 * i] = mesh->mTextureCoords[0][i].x;//[i]->x; - uvs[2 * i + 1] = mesh->mTextureCoords[0][i].y;//[i]->y; - } - - //load indices from file - for (uint i = 0; i < numfaces; i++) - { - index[3 * i] = mesh->mFaces[i].mIndices[0]; - index[3 * i + 1] = mesh->mFaces[i].mIndices[1]; - index[3 * i + 2] = mesh->mFaces[i].mIndices[2]; - } - - //load normals from file - for (uint i = 0; i < numvertices; i++) - { - normals[3 * i] = mesh->mNormals[i].x; - normals[3 * i + 1] = mesh->mNormals[i].y; - normals[3 * i + 2] = mesh->mNormals[i].z; - } - + import(scene->mMeshes[mindex], numvertices, numfaces, vertex, uvs, normals, index); } else { @@ -206,5 +211,52 @@ bool Model::import(const string& path,uint& numvertices, uint& numfaces, float*& return false; } + return true; +} + +bool Model::import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index) +{ + + numvertices = mesh->mNumVertices; + numfaces = mesh->mNumFaces; + vertex = new float[numvertices * 3]; + uvs = new float[numvertices * 2]; + index = new uint[numfaces * 3]; + normals = new float[numvertices * 3]; + //aiFace* faces = mesh->mFaces; + + + //load vertices from Mesh + for (uint i = 0; i < numvertices; i++) + { + vertex[3 * i] = mesh->mVertices[i].x; + vertex[3 * i + 1] = mesh->mVertices[i].y; + vertex[3 * i + 2] = mesh->mVertices[i].z; + } + + //load UVs from Mesh + for (uint i = 0; i < numvertices; i++) + { + uvs[2 * i] = mesh->mTextureCoords[0][i].x;//[i]->x; + uvs[2 * i + 1] = mesh->mTextureCoords[0][i].y;//[i]->y; + } + + //load indices from Mesh + for (uint i = 0; i < numfaces; i++) + { + index[3 * i] = mesh->mFaces[i].mIndices[0]; + index[3 * i + 1] = mesh->mFaces[i].mIndices[1]; + index[3 * i + 2] = mesh->mFaces[i].mIndices[2]; + } + + //load normals from Mesh + for (uint i = 0; i < numvertices; i++) + { + normals[3 * i] = mesh->mNormals[i].x; + normals[3 * i + 1] = mesh->mNormals[i].y; + normals[3 * i + 2] = mesh->mNormals[i].z; + } + + return true; } diff --git a/Weave/Graphix/Model.h b/Weave/Graphix/Model.h index 7786709..82bb41e 100644 --- a/Weave/Graphix/Model.h +++ b/Weave/Graphix/Model.h @@ -2,6 +2,8 @@ #include #include +#include "GLM.h" + using std::string; using std::unordered_map; @@ -10,18 +12,24 @@ using std::unordered_map; typedef unordered_map int2int_map; class Shader; +class Texture; +struct aiMesh; class Model { public: Model(const string& modelpath, unsigned int index=0); + Model(const aiMesh* mesh); Model(unsigned int numvertices, unsigned int numfaces, float *vertex, float *uvs, float *normals, unsigned int *index); virtual ~Model(); // void useModel(Shader* shader); void useModel(Shader* shader) const; + void useTexture(Texture* texture, Shader* shader) const; + void useMMatrix(const mat4& model,Shader* shader) const; void drawModel() const; + void drawModel(Shader* _shader, Texture* texture, const mat4& model) const; unsigned int bindShader(Shader* shader); private: @@ -30,7 +38,9 @@ private: int2int_map shader_map; - bool import(const string& path, unsigned int& numvertices, unsigned int& numfaces, float*& vertex, float*& uvs, float*& normals, unsigned int*& index, unsigned int mindex=0); + bool import(const string& path, unsigned int& numvertices, unsigned int& numfaces, float*& vertex, float*& uvs, float*& normals, unsigned int*& index, unsigned int mindex = 0); + bool import(const aiMesh* mesh, unsigned int& numvertices, unsigned int& numfaces, float*& vertex, float*& uvs, float*& normals, unsigned int*& index); + void genBuffer(unsigned int &buffer, unsigned int size, void* value); void bindBuffer(const unsigned int &buffer, const unsigned int index,const unsigned int dim = 3); diff --git a/Weave/Graphix/SceneObject.cpp b/Weave/Graphix/SceneObject.cpp index a0df510..43c499d 100644 --- a/Weave/Graphix/SceneObject.cpp +++ b/Weave/Graphix/SceneObject.cpp @@ -22,13 +22,14 @@ using std::cout; using std::endl; SceneObject::SceneObject(Shader* _shader, mat4& _model, string _modelpath, string texturepath, unsigned int _model_index) : -Model(_modelpath, _model_index), +MetaModel(_modelpath), model(_model), shader(_shader), mainScene(NULL), collision_ignore(false), texture(nullptr) { + new MetaModel(_modelpath); modelID = _shader->getUniformLocation("model"); if (texturepath!="") texture = new Texture(texturepath); @@ -36,20 +37,20 @@ texture(nullptr) bindShader(shader); } -SceneObject::SceneObject(Shader* _shader, mat4& _model, Model* model_obj, string texturepath) : -Model(*model_obj), -model(_model), -shader(_shader), -mainScene(NULL), -collision_ignore(false), -texture(nullptr) -{ - modelID = _shader->getUniformLocation("model"); - if (texturepath != "") - texture = new Texture(texturepath); - - bindShader(shader); -} +//SceneObject::SceneObject(Shader* _shader, mat4& _model, Model* model_obj, string texturepath) : +//Model(*model_obj), +//model(_model), +//shader(_shader), +//mainScene(NULL), +//collision_ignore(false), +//texture(nullptr) +//{ +// modelID = _shader->getUniformLocation("model"); +// if (texturepath != "") +// texture = new Texture(texturepath); +// +// bindShader(shader); +//} SceneObject::~SceneObject() @@ -69,19 +70,17 @@ void SceneObject::update(float deltaT) void SceneObject::draw() const { - useModel(shader); +// useModel(shader); - if (texture!=nullptr) - { - int unit = 0; - texture->bind(unit); - glUniform1i(shader->getUniformLocation("ColorTexture"), unit); - } - glUniformMatrix4fv(modelID, 1, GL_FALSE, value_ptr(model)); +// useTexture(texture, shader); + +// useMMatrix(model,shader); // glUniform1i(shader->getUniformLocation ("inv"), 1); - drawModel(); +// drawModel(); + + drawModel(shader, texture, model); } void SceneObject::collisions(SceneObject* _other) diff --git a/Weave/Graphix/SceneObject.h b/Weave/Graphix/SceneObject.h index 78e2799..89b815c 100644 --- a/Weave/Graphix/SceneObject.h +++ b/Weave/Graphix/SceneObject.h @@ -4,6 +4,7 @@ #include "Drawable.h" #include "Model.h" +#include "MetaModel.h" #include @@ -17,13 +18,13 @@ class Shader; class SceneObject : public Drawable, - public Model + public MetaModel { public: //SceneObject(Shader* _shader, mat4& model); SceneObject(Shader* _shader, mat4& model, string modelpath, string texturepath, unsigned int model_index=0); - SceneObject(Shader* _shader, mat4& model, Model* model_obj, string texturepath); + //SceneObject(Shader* _shader, mat4& model, Model* model_obj, string texturepath); virtual ~SceneObject(); virtual void update(float); diff --git a/Weave/Weave.vcxproj b/Weave/Weave.vcxproj index 916439a..64ce305 100644 --- a/Weave/Weave.vcxproj +++ b/Weave/Weave.vcxproj @@ -102,6 +102,7 @@ + @@ -118,6 +119,7 @@ + diff --git a/Weave/Weave.vcxproj.filters b/Weave/Weave.vcxproj.filters index 47bc040..0aa0e29 100644 --- a/Weave/Weave.vcxproj.filters +++ b/Weave/Weave.vcxproj.filters @@ -57,6 +57,9 @@ Source Files + + Source Files + @@ -101,5 +104,8 @@ Header Files + + Header Files + \ No newline at end of file