Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
PottierLoic committed Jan 2, 2024
1 parent bb8e666 commit f4717dd
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 7 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/macos-ci.yml
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
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/ubuntu-ci.yml
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
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/windows-ci.yml
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
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "imgui"]
path = libs/imgui
url = https://github.com/ocornut/imgui
[submodule "assimp"]
path = libs/assimp
url = https://github.com/assimp/assimp.git
21 changes: 20 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cmake_minimum_required(VERSION 3.21)
set(GLAD_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/libs/glad" CACHE STRING "Path to glad")
set(GLFW_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/libs/glfw" CACHE STRING "Path to GLFW")
set(DEAR_IMGUI_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/libs/imgui" CACHE STRING "Path to Dear imgui")
set(ASSIMP_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/libs/assimp" CACHE STRING "Path to assimp")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs")

Expand Down Expand Up @@ -50,11 +51,29 @@ list(APPEND IMGUI_SOURCES
# GLM
file(GLOB_RECURSE GLM_HEADERS CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/libs/glm/*.hpp")

# ASSIMP
set(ASSIMP_BUILD_TESTS OFF)
add_subdirectory(${ASSIMP_PREFIX})
file(
GLOB_RECURSE
ASSIMP_SOURCES
CONFIGURE_DEPENDS
"${ASSIMP_PREFIX}/*.hpp"
"${ASSIMP_PREFIX}/*.h"
"${ASSIMP_PREFIX}/*.cpp"
"${ASSIMP_PREFIX}/*.c"
"${ASSIMP_PREFIX}/contrib/zip/*.h"
)

target_sources(space PRIVATE ${ASSIMP_SOURCES})
target_include_directories(space PRIVATE "${ASSIMP_PREFIX}/include" "${PROJECT_SOURCE_DIR}/build/libs/assimp/include/" "${ASSIMP_PREFIX}/code")
target_link_libraries(space ${ASSIMP_LIBRARIES})

# Space files
file(GLOB_RECURSE HEADER_LIST CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/include/*.hpp")
file(GLOB_RECURSE SOURCE_LIST CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.cpp")

target_sources(space PRIVATE ${SOURCE_LIST} ${IMGUI_SOURCES} ${HEADER_LIST} ${GLM_HEADERS})
target_sources(space PRIVATE ${SOURCE_LIST} ${IMGUI_SOURCES} ${HEADER_LIST} ${GLM_HEADERS} ${ASSIMP_SOURCES})
target_link_libraries(space PRIVATE ${OPENGL_gl_LIBRARY} glfw )
target_include_directories(space PRIVATE ${PROJECT_SOURCE_DIR}/include)

Expand Down
23 changes: 23 additions & 0 deletions include/Mesh/Mesh.hpp
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();
};
30 changes: 30 additions & 0 deletions include/Mesh/Model.hpp
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);
};
9 changes: 9 additions & 0 deletions include/Mesh/Texture.hpp
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;
};
9 changes: 9 additions & 0 deletions include/Mesh/Vertex.hpp
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;
};
1 change: 1 addition & 0 deletions libs/assimp
Submodule assimp added at ec122e
61 changes: 61 additions & 0 deletions src/Mesh/Mesh.cpp
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);
}
132 changes: 132 additions & 0 deletions src/Mesh/Model.cpp
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;
}

0 comments on commit f4717dd

Please sign in to comment.