diff --git a/.vscode/launch.json b/.vscode/launch.json index f97f3a4..89b253c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,12 +9,12 @@ "name": "(gdb) Starten", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/build/Fall-Fever", + "program": "${workspaceFolder}/build/src/Fall-Fever", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], - "externalConsole": true, + "externalConsole": false, "MIMode": "gdb", "preLaunchTask": "Build", "setupCommands": [ diff --git a/CMakeLists.txt b/CMakeLists.txt index b3dfb1d..c73eeac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,34 +7,11 @@ set(OpenGL_GL_PREFERENCE "GLVND") find_package(glfw3 3.3 REQUIRED) find_package(OpenGL REQUIRED) -add_subdirectory(${PROJECT_SOURCE_DIR}/lib) - # Specify the C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG") -add_executable(Fall-Fever - src/main.cpp - src/Controller.cpp - src/Window.cpp - src/EventHandler.cpp - src/ShaderProgram.cpp - src/VertexBuffer.cpp - src/Texture.cpp - src/Camera.cpp - src/Mesh.cpp - src/Model.cpp -) - -target_link_libraries( - Fall-Fever PRIVATE - glfw - glad - stb - ${OPENGL_LIBRARIES} - assimp -) - -target_compile_options(Fall-Fever PRIVATE -Wall -Wextra -pedantic) +add_subdirectory(${PROJECT_SOURCE_DIR}/lib) +add_subdirectory(${PROJECT_SOURCE_DIR}/src) diff --git a/Fall-Fever b/Fall-Fever index a219058..0a680c7 120000 --- a/Fall-Fever +++ b/Fall-Fever @@ -1 +1 @@ -/home/derek/Projekte/CXX/Fall-Fever/build/Fall-Fever \ No newline at end of file +build/src/Fall-Fever \ No newline at end of file diff --git a/res/shaders/basic.fs b/res/shaders/basic.fs index bd518c3..5c56dbd 100644 --- a/res/shaders/basic.fs +++ b/res/shaders/basic.fs @@ -3,6 +3,8 @@ struct Material { sampler2D u_texture_diffuse0; sampler2D u_texture_diffuse1; + sampler2D u_texture_specular0; + sampler2D u_texture_specular1; }; layout(location = 0) out vec4 f_color; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..69ad5be --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,23 @@ +add_executable(Fall-Fever + main.cpp + Controller.cpp + Window.cpp + EventHandler.cpp + ShaderProgram.cpp + VertexBuffer.cpp + Texture.cpp + Camera.cpp + Mesh.cpp + Model.cpp +) + +target_link_libraries( + Fall-Fever PRIVATE + glfw + glad + stb + ${OPENGL_LIBRARIES} + assimp +) + +target_compile_options(Fall-Fever PRIVATE -Wall -Wextra -pedantic) diff --git a/src/Controller.cpp b/src/Controller.cpp index 8fa540a..3bcccd9 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -15,7 +15,6 @@ #include "Controller.h" #include "Texture.h" #include "Model.h" -//#include "Mesh.h" Controller::Controller() { if(!glfwInit()) exit(-1); @@ -82,9 +81,9 @@ void Controller::run() { 1, 2, 3 }; - std::vector textures { - Texture("res/textures/tex2.png", texture_diffuse), - Texture("res/textures/tex1.png", texture_diffuse) + std::vector textures { + new Texture("res/textures/tex2.png", texture_diffuse), + new Texture("res/textures/tex1.png", texture_diffuse) }; Model model1("res/models/backpack.obj"); @@ -111,9 +110,7 @@ void Controller::run() { // Update game // ... shaderProgram.setUniform("mix_val", (float) (sin(glfwGetTime()*0.25)*sin(glfwGetTime()*0.25))); - //model = glm::rotate(model, (float)(this->deltaTime*0.005), glm::vec3(0.0f, 1.0f, 0.0f)); - //camera->lookAtTarget(glm::vec3(0.0f, 0.0f, 0.0f)); camera->lookForward(); camera->updateVPM(); glm::mat4 modelViewProj = camera->getViewProj() * model; @@ -125,12 +122,10 @@ void Controller::run() { for(int i=0;i<20;i++) { //mesh1.draw(&shaderProgram); model1.draw(&shaderProgram); - //model = glm::translate(model, glm::vec3(0.0f, 0.0f, -1.0f)); camera->updateVPM(); glm::mat4 modelViewProj = camera->getViewProj() * model; shaderProgram.setUniform("u_modelViewProj", modelViewProj); } - //model = glm::translate(model, glm::vec3(0.0f, 0.0f, 20.0f)); glfwSwapBuffers(gameWindow->getGLFWwindow()); diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 1e5ed3e..6bb5cee 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -1,6 +1,6 @@ #include "Mesh.h" -Mesh::Mesh(std::vector vertices, std::vector indices, std::vector textures) +Mesh::Mesh(std::vector vertices, std::vector indices, std::vector textures) : vertices(vertices), indices(indices), textures(textures), @@ -17,9 +17,9 @@ void Mesh::draw(ShaderProgram *shaderProgram) { for(auto it = textures.begin(); it != textures.end(); it++) { int i = it - textures.begin(); - uint8_t currentTextureType = (*it).getTextureType(); + uint8_t currentTextureType = (*it)->getTextureType(); - (*it).bind(i, shaderProgram, typeNumberCount[currentTextureType]); + (*it)->bind(i, shaderProgram, typeNumberCount[currentTextureType]); typeNumberCount[currentTextureType] += 1; } @@ -31,7 +31,7 @@ void Mesh::draw(ShaderProgram *shaderProgram) { // Unbind all textures for(auto it = textures.begin(); it != textures.end(); it++) { - (*it).unbind(); + (*it)->unbind(); } diff --git a/src/Mesh.h b/src/Mesh.h index 9b40bde..926b2e7 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -11,7 +11,7 @@ class Mesh { public: - Mesh(std::vector vertices, std::vector indices, std::vector textures); + Mesh(std::vector vertices, std::vector indices, std::vector textures); ~Mesh() = default; void draw(ShaderProgram *shaderProgram); @@ -20,7 +20,7 @@ private: std::vector vertices; std::vector indices; - std::vector textures; + std::vector textures; VertexBuffer vertexBuffer; diff --git a/src/Model.cpp b/src/Model.cpp index 493bc5d..85b78cb 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -47,7 +47,7 @@ void Model::processNode(aiNode *node, const aiScene *scene) { Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) { std::vector vertices; std::vector indices; - std::vector textures; + std::vector textures; for(uint i = 0; i < mesh->mNumVertices; i++) { Vertex vertex; @@ -65,7 +65,7 @@ Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) { vector.z = mesh->mNormals[i].z; vertex.normalVec = vector; - // Texture + // Texture UV mapping if(mesh->mTextureCoords[0]) { glm::vec2 vec; vec.x = mesh->mTextureCoords[0][i].x; @@ -89,19 +89,19 @@ Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) { // Material if(mesh->mMaterialIndex >= 0) { aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex]; - std::vector diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, texture_diffuse); + std::vector diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, texture_diffuse); textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end()); - std::vector specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, texture_specular); + std::vector specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, texture_specular); textures.insert(textures.end(), specularMaps.begin(), specularMaps.end()); } return Mesh(vertices, indices, textures); } -std::vector Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, uint8_t textureType) { +std::vector Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, uint8_t textureType) { - std::vector textures; + std::vector textures; for(uint i = 0; i < mat->GetTextureCount(type); i++) { aiString filename; mat->GetTexture(type, i, &filename); @@ -109,7 +109,7 @@ std::vector Model::loadMaterialTextures(aiMaterial *mat, aiTextureType bool skip = 0; for(uint j = 0; j < loadedTextures.size(); j++) { if(std::strcmp(loadedTextures[j].getPath().data(), filename.C_Str()) == 0) { - textures.push_back(loadedTextures[j]); + textures.push_back(&loadedTextures[j]); skip = 1; break; } @@ -118,11 +118,10 @@ std::vector Model::loadMaterialTextures(aiMaterial *mat, aiTextureType if(!skip) { std::string path = directory + '/' + filename.C_Str(); Texture texture(path.c_str(), textureType); - textures.push_back(texture); + textures.push_back(&texture); + loadedTextures.push_back(texture); } } - loadedTextures.clear(); - return textures; } diff --git a/src/Model.h b/src/Model.h index 75fd4ef..e31d7d0 100644 --- a/src/Model.h +++ b/src/Model.h @@ -25,7 +25,7 @@ private: void processNode(aiNode *node, const aiScene *scene); Mesh processMesh(aiMesh *mesh, const aiScene *scene); - std::vector loadMaterialTextures(aiMaterial *mat, aiTextureType type, uint8_t textureType); + std::vector loadMaterialTextures(aiMaterial *mat, aiTextureType type, uint8_t textureType); std::vector meshes; diff --git a/src/Texture.cpp b/src/Texture.cpp index 0c4b6cb..1cf9125 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -9,7 +9,7 @@ Texture::Texture(const char* texturePath, uint8_t textureType) { this->textureType = textureType; stbi_set_flip_vertically_on_load(1); - stbi_uc *textureBuffer = stbi_load(texturePath, &textureWidth, &textureHeight, &bitsPerPixel, 4); + auto *textureBuffer = stbi_load(texturePath, &textureWidth, &textureHeight, &bitsPerPixel, 4); // Push texture to grahics card; glGenTextures(1, &textureId); @@ -41,7 +41,7 @@ Texture::Texture(const char* texturePath, uint8_t textureType) { } Texture::~Texture() { - //glDeleteTextures(1, &textureId); + glDeleteTextures(1, &textureId); } void Texture::bind(uint8_t textureUnit, ShaderProgram* shaderProgram, uint8_t textureTypeNum) { diff --git a/src/Window.cpp b/src/Window.cpp index 107f330..2472863 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -28,7 +28,7 @@ Window::Window() { glEnable(GL_DEPTH_TEST); // Disable mouse cursor - setGrabbedCursor(1); + setGrabbedCursor(0); #ifdef _DEBUG std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;