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
#include <assimp/postprocess.h>
#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;
}
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)
}
-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);
}
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);
}
-
-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;
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
{
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;
}
#include <string>
#include <unordered_map>
+#include "GLM.h"
+
using std::string;
using std::unordered_map;
typedef unordered_map<unsigned int, unsigned int> 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:
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);
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);
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()
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)