diff --git a/Debug/LearnOpenGL.exe b/Debug/LearnOpenGL.exe
index 81e3322..c842fa4 100644
Binary files a/Debug/LearnOpenGL.exe and b/Debug/LearnOpenGL.exe differ
diff --git a/Libs/assimp.dll b/Debug/assimp-vc142-mt.dll
similarity index 100%
rename from Libs/assimp.dll
rename to Debug/assimp-vc142-mt.dll
diff --git a/Debug/zlibd1.dll b/Debug/zlibd1.dll
new file mode 100644
index 0000000..6ac11fc
Binary files /dev/null and b/Debug/zlibd1.dll differ
diff --git a/HelloTexture.h b/HelloTexture.h
index 6babdba..88f09ae 100644
--- a/HelloTexture.h
+++ b/HelloTexture.h
@@ -1,6 +1,5 @@
#pragma once
#include "Painter.h"
-#include "SOIL.h"
#include "Resource.h"
class HelloTexture :public Painter
diff --git a/LearnOpenGL.vcxproj b/LearnOpenGL.vcxproj
index 3a4878e..4e702d0 100644
--- a/LearnOpenGL.vcxproj
+++ b/LearnOpenGL.vcxproj
@@ -155,9 +155,12 @@
+
+
+
@@ -260,9 +263,11 @@
-
+
+
+
diff --git a/LearnOpenGL.vcxproj.filters b/LearnOpenGL.vcxproj.filters
index 0e0103a..a7d9f4a 100644
--- a/LearnOpenGL.vcxproj.filters
+++ b/LearnOpenGL.vcxproj.filters
@@ -49,6 +49,12 @@
{4b80d85b-704c-40f8-961d-59db6514584d}
+
+ {a6695adb-8455-4d8c-9fe1-8fcc7011372d}
+
+
+ {1a0dc422-6439-4f39-b825-6d7c05cc9632}
+
@@ -114,6 +120,15 @@
12_Mesh
+
+ 13_Model
+
+
+ 13_Model
+
+
+ lib\stb_image
+
@@ -134,9 +149,6 @@
lib
-
- lib
-
3_HelloTexture
@@ -429,6 +441,15 @@
12_Mesh
+
+ 13_Model
+
+
+ 13_Model
+
+
+ lib\stb_image
+
diff --git a/Libs/assimp-vc142-mt.dll b/Libs/assimp-vc142-mt.dll
new file mode 100644
index 0000000..0214753
Binary files /dev/null and b/Libs/assimp-vc142-mt.dll differ
diff --git a/Mesh.cpp b/Mesh.cpp
index 57fa188..64d91e7 100644
--- a/Mesh.cpp
+++ b/Mesh.cpp
@@ -1,5 +1,10 @@
#include "Mesh.h"
+const char* DIFFUSE_TEX_NAME = "texture_diffuse";
+const char* SPECULAR_TEX_NAME = "texture_specular";
+const char* NORMAL_TEX_NAME = "texture_normal";
+const char* HEIGHT_TEX_NAME = "texture_height";
+
Mesh::Mesh(vector vertices, vector indices, vector textures)
{
this->vertices = vertices;
@@ -11,60 +16,73 @@ Mesh::Mesh(vector vertices, vector indices, vector text
Mesh::~Mesh()
{
- glDeleteBuffers(1, &VBO);
- glDeleteBuffers(1, &EBO);
- glDeleteVertexArrays(1, &VAO);
+ //glDeleteBuffers(1, &VBO);
+ //glDeleteBuffers(1, &EBO);
+ //glDeleteVertexArrays(1, &VAO);
}
void Mesh::Draw(Shader& shader)
{
- GLuint diffuseNr = 1;
- GLuint specularNr = 1;
- for (size_t i = 0; i < textures.size(); ++i)
- {
- glActiveTexture(GL_TEXTURE0 + i);
- string number;
- string name = textures[i].type;
- if (name == DIFFUSE_TEX_NAME)
- number = std::to_string(diffuseNr++);
- else if (name == SPECULAR_TEX_NAME)
- number = std::to_string(specularNr++);
+ unsigned int diffuseNr = 1;
+ unsigned int specularNr = 1;
+ unsigned int normalNr = 1;
+ unsigned int heightNr = 1;
+ for (unsigned int i = 0; i < textures.size(); i++)
+ {
+ glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
+ // retrieve texture number (the N in diffuse_textureN)
+ string number;
+ string name = textures[i].type;
+ if (name == DIFFUSE_TEX_NAME)
+ number = std::to_string(diffuseNr++);
+ else if (name == SPECULAR_TEX_NAME)
+ number = std::to_string(specularNr++); // transfer unsigned int to stream
+ else if (name == NORMAL_TEX_NAME)
+ number = std::to_string(normalNr++); // transfer unsigned int to stream
+ else if (name == HEIGHT_TEX_NAME)
+ number = std::to_string(heightNr++); // transfer unsigned int to stream
- shader.setFloat("material" + name + number, i);
- glBindTexture(GL_TEXTURE_2D, textures[i].id);
- }
- glActiveTexture(GL_TEXTURE0);
+ shader.setInt((name + number).c_str(), i);
+ glBindTexture(GL_TEXTURE_2D, textures[i].id);
+ }
+ glActiveTexture(GL_TEXTURE0);
- //Draw
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
+ glBindVertexArray(VAO);
+ glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
+ glBindVertexArray(0);
}
void Mesh::SetUpMesh()
{
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
+ glGenVertexArrays(1, &VAO);
+ glGenBuffers(1, &VBO);
+ glGenBuffers(1, &EBO);
+
+ glBindVertexArray(VAO);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
+ glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
+ glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
- //Position
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), (void*)0);
- //Normal
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 3, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, Normal) );
- //Texture Coordinates
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
+ // vertex Positions
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
+ // vertex normals
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
+ // vertex texture coords
+ glEnableVertexAttribArray(2);
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
+ // vertex tangent
+ glEnableVertexAttribArray(3);
+ glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
+ // vertex bitangent
+ glEnableVertexAttribArray(4);
+ glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
- glBindVertexArray(0);
+ glBindVertexArray(0);
}
diff --git a/Mesh.h b/Mesh.h
index 3184d62..aebd9b1 100644
--- a/Mesh.h
+++ b/Mesh.h
@@ -1,5 +1,6 @@
#pragma once
#include "glheader.h"
+#include
using std::vector;
using std::string;
@@ -9,18 +10,17 @@ struct Vertex
glm::vec3 Position;
glm::vec3 Normal;
glm::vec2 TexCoords;
+ glm::vec3 Tangent;
+ glm::vec3 Bitangent;
};
struct Texture
{
GLuint id;
std::string type;
+ aiString Path;
};
-
-const char* DIFFUSE_TEX_NAME = "texture_diffuse";
-const char* SPECULAR_TEX_NAME = "texture_specular";
-
class Mesh
{
public:
diff --git a/Model.cpp b/Model.cpp
new file mode 100644
index 0000000..3a4f860
--- /dev/null
+++ b/Model.cpp
@@ -0,0 +1,116 @@
+#include "Model.h"
+
+void Model::Draw(Shader& shader)
+{
+ for (auto mesh : meshes)
+ {
+ mesh.Draw(shader);
+ }
+}
+
+void Model::LoadModel(string path)
+{
+ Assimp::Importer importer;
+ const aiScene* scene = importer.ReadFile(path, aiProcess_FlipUVs | aiProcess_Triangulate);
+ if (scene == nullptr || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
+ {
+ std::cout << "ERROR::ASSIMP::" << importer.GetErrorString() << std::endl;
+ return;
+ }
+ directory = path.substr(0, path.find_last_of('/'));
+ ProcessNode(scene->mRootNode, scene);
+}
+
+void Model::ProcessNode(aiNode* node, const aiScene* scene)
+{
+ for (auto i = 0; i < node->mNumMeshes; ++i)
+ {
+ auto mesh = scene->mMeshes[node->mMeshes[i]];
+ meshes.push_back(ProcessMesh(mesh, scene));
+ }
+
+ for (auto i = 0; i < node->mNumChildren; ++i)
+ {
+ ProcessNode(node->mChildren[i], scene);
+ }
+}
+
+Mesh Model::ProcessMesh(aiMesh* mesh, const aiScene* scene)
+{
+ vector vertices;
+ vector indices;
+ vector textures;
+
+ //Position
+ for (auto i = 0; i < mesh->mNumVertices; ++i)
+ {
+ Vertex vertex;
+ glm::vec3 vec(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
+ vertex.Position = vec;
+
+ vec.x = mesh->mNormals[i].x;
+ vec.y = mesh->mNormals[i].y;
+ vec.z = mesh->mNormals[i].z;
+ vertex.Normal = vec;
+
+ if (mesh->mTextureCoords[0])
+ {
+ vertex.TexCoords = glm::vec2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y);
+ }
+ else
+ {
+ vertex.TexCoords = glm::vec2(0);
+ }
+
+ vertices.push_back(vertex);
+ }
+
+ //Indices
+ for (unsigned int i = 0; i < mesh->mNumFaces; i++)
+ {
+ aiFace face = mesh->mFaces[i];
+ for (unsigned int j = 0; j < face.mNumIndices; j++)
+ indices.push_back(face.mIndices[j]);
+ }
+
+ //Material
+ if (mesh->mMaterialIndex >= 0)
+ {
+ auto material = scene->mMaterials[mesh->mMaterialIndex];
+ vector diffuseMaps = LoadMaterialTextures(material,
+ aiTextureType_DIFFUSE, "texture_diffuse");
+ textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
+ vector specularMaps = LoadMaterialTextures(material,
+ aiTextureType_SPECULAR, "texture_specular");
+ textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
+ }
+ return Mesh(vertices, indices, textures);
+}
+
+vector Model::LoadMaterialTextures(aiMaterial* mat, aiTextureType type, string typeName)
+{
+ vector textures;
+
+ for (auto i = 0; i < mat->GetTextureCount(type); ++i)
+ {
+ aiString name;
+ mat->GetTexture(type, i, &name);
+ auto toFind = textures_loaded.find(name.C_Str());
+ if (toFind != textures_loaded.end())
+ {
+ textures.push_back(toFind->second);
+ }
+ else
+ {
+ Texture tex;
+ auto filePath = StringUtil::Format("%s/%s", directory.c_str(), name.C_Str());
+ tex.id = Resource::LoadTexture(filePath.c_str(), GL_REPEAT, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR);
+ tex.type = typeName;
+ tex.Path = name;
+ textures.push_back(tex);
+ textures_loaded[name.C_Str()] = tex;
+ }
+ }
+
+ return textures;
+}
diff --git a/Model.h b/Model.h
new file mode 100644
index 0000000..6f524e3
--- /dev/null
+++ b/Model.h
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "glheader.h"
+#include "Mesh.h"
+
+#include
+#include
+#include
+#include