-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb8e666
commit f4717dd
Showing
12 changed files
with
288 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
name: MacOS build | ||
|
||
on: [push] | ||
|
||
jobs: | ||
macos-build: | ||
runs-on: macos-latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
name: Ubuntu build | ||
|
||
on: [push] | ||
|
||
jobs: | ||
ubuntu-build: | ||
runs-on: ubuntu-latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
name: Windows build | ||
|
||
on: [push] | ||
|
||
jobs: | ||
windows-build: | ||
runs-on: windows-latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include "glad/glad.h" | ||
|
||
#include "Mesh/Vertex.hpp" | ||
#include "Mesh/Texture.hpp" | ||
#include "Shader.hpp" | ||
|
||
class Mesh { | ||
public: | ||
std::vector<Vertex> vertices; | ||
std::vector<unsigned int> indices; | ||
std::vector<Texture> textures; | ||
|
||
Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<Texture> textures); | ||
void draw(Shader &shader); | ||
|
||
private: | ||
unsigned int VAO, VBO, EBO; | ||
void setupMesh(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <string> | ||
|
||
#include "assimp/Importer.hpp" | ||
#include "assimp/scene.h" | ||
#include "assimp/postprocess.h" | ||
#include "stb_image.h" | ||
|
||
#include "Mesh/Mesh.hpp" | ||
#include "Mesh/Texture.hpp" | ||
#include "Shader.hpp" | ||
|
||
unsigned int textureFromFile(const char *path, const std::string &directory, bool gamma = false); | ||
|
||
class Model { | ||
public: | ||
Model(char *path); | ||
void draw(Shader &shader); | ||
|
||
private: | ||
std::vector<Mesh> meshes; | ||
std::string directory; | ||
|
||
void loadModel(std::string path); | ||
void processNode(aiNode *node, const aiScene *scene); | ||
Mesh processMesh(aiMesh *mesh, const aiScene *scene); | ||
std::vector<Texture> loadMaterialTextures(aiMaterial *mat, aiTextureType type, std::string typeName); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
struct Texture { | ||
unsigned int id; | ||
std::string type; | ||
std::string path; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include "glm/glm.hpp" | ||
|
||
struct Vertex { | ||
glm::vec3 position; | ||
glm::vec3 normal; | ||
glm::vec2 texCoords; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "Mesh/Mesh.hpp" | ||
|
||
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<Texture> textures) { | ||
this->vertices = vertices; | ||
this->indices = indices; | ||
this->textures = textures; | ||
|
||
setupMesh(); | ||
} | ||
|
||
void Mesh::setupMesh() { | ||
glGenVertexArrays(1, &VAO); | ||
glGenBuffers(1, &VBO); | ||
glGenBuffers(1, &EBO); | ||
|
||
glBindVertexArray(VAO); | ||
|
||
glBindBuffer(GL_ARRAY_BUFFER, VBO); | ||
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(unsigned int), &indices[0], GL_STATIC_DRAW); | ||
|
||
// 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)); | ||
|
||
glBindVertexArray(0); | ||
} | ||
|
||
void Mesh::draw(Shader &shader) { | ||
unsigned int diffuseNr = 1; | ||
unsigned int specularNr = 1; | ||
|
||
for(unsigned int i = 0; i < textures.size(); i++) { | ||
glActiveTexture(GL_TEXTURE0 + i); | ||
std::string number; | ||
std::string name = textures[i].type; | ||
if (name == "texture_diffuse") { | ||
number = std::to_string(diffuseNr++); | ||
} else if (name == "texture_specular") { | ||
number = std::to_string(specularNr++); | ||
} | ||
shader.setInt(("material." + name + number).c_str(), i); | ||
glBindTexture(GL_TEXTURE_2D, textures[i].id); | ||
} | ||
|
||
glActiveTexture(GL_TEXTURE0); | ||
|
||
glBindVertexArray(VAO); | ||
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); | ||
glBindVertexArray(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#include "Mesh/Model.hpp" | ||
|
||
Model::Model(char *path) { | ||
loadModel(path); | ||
} | ||
|
||
void Model::loadModel(std::string path) { | ||
Assimp::Importer import; | ||
const aiScene *scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs); | ||
|
||
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { | ||
std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl; | ||
return; | ||
} | ||
|
||
directory = path.substr(0, path.find_last_of('/')); | ||
|
||
processNode(scene->mRootNode, scene); | ||
} | ||
|
||
void Model::processNode(aiNode *node, const aiScene *scene) { | ||
/* Processing all meshes */ | ||
for(unsigned int i = 0; i < node->mNumMeshes; i++) { | ||
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]]; | ||
meshes.push_back(processMesh(mesh, scene)); | ||
} | ||
|
||
/* Processing all childrens */ | ||
for(unsigned int i = 0; i < node->mNumChildren; i++) { | ||
processNode(node->mChildren[i], scene); | ||
} | ||
} | ||
|
||
Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) { | ||
std::vector<Vertex> vertices; | ||
std::vector<unsigned int> indices; | ||
std::vector<Texture> textures; | ||
|
||
for(unsigned int i = 0; i < mesh->mNumVertices; i++) { | ||
Vertex vertex; | ||
|
||
// position | ||
glm::vec3 vector; | ||
vector.x = mesh->mVertices[i].x; | ||
vector.y = mesh->mVertices[i].y; | ||
vector.z = mesh->mVertices[i].z; | ||
vertex.position = vector; | ||
|
||
// normals | ||
vector.x = mesh->mNormals[i].x; | ||
vector.y = mesh->mNormals[i].y; | ||
vector.z = mesh->mNormals[i].z; | ||
vertex.normal = vector; | ||
|
||
// process 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]); | ||
} | ||
} | ||
|
||
// process materials | ||
if(mesh->mTextureCoords[0]) { | ||
glm::vec2 vec; | ||
vec.x = mesh->mTextureCoords[0][i].x; | ||
vec.y = mesh->mTextureCoords[0][i].y; | ||
vertex.texCoords = vec; | ||
} else { | ||
vertex.texCoords = glm::vec2(0.0f, 0.0f); | ||
} | ||
|
||
vertices.push_back(vertex); | ||
} | ||
|
||
// if(mesh->mMaterialIndex >= 0) { | ||
aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex]; | ||
std::vector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse"); | ||
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end()); | ||
std::vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular"); | ||
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end()); | ||
// } | ||
|
||
return Mesh(vertices, indices, textures); | ||
} | ||
|
||
std::vector<Texture> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, std::string typeName) { | ||
std::vector<Texture> textures; | ||
for(unsigned int i = 0; i < mat->GetTextureCount(type); i++) { | ||
aiString str; | ||
mat->GetTexture(type, i, &str); | ||
Texture texture; | ||
texture.id = textureFromFile(str.C_Str(), directory); | ||
texture.type = typeName; | ||
texture.path = str.C_Str(); | ||
textures.push_back(texture); | ||
} | ||
return textures; | ||
} | ||
|
||
unsigned int textureFromFile(const char *path, const std::string &directory, bool /*gamma*/) { | ||
std::string filename = std::string(path); | ||
filename = directory + '/' + filename; | ||
unsigned int textureID; | ||
glGenTextures(1, &textureID); | ||
|
||
int width, height, nrComponents; | ||
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0); | ||
|
||
if(data) { | ||
GLenum format; | ||
if(nrComponents == 1) { format = GL_RED; } | ||
else if (nrComponents == 3) { format = GL_RGB; } | ||
else if (nrComponents == 4) { format = GL_RGBA; } | ||
|
||
glBindTexture(GL_TEXTURE_2D, textureID); | ||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); | ||
glGenerateMipmap(GL_TEXTURE_2D); | ||
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
|
||
stbi_image_free(data); | ||
} else { | ||
std::cout << "Texture failed to load at path: " << path << std::endl; | ||
stbi_image_free(data); | ||
} | ||
|
||
return textureID; | ||
} |