Bug fixes in modelloading

This commit is contained in:
4VRDriver
2020-09-05 13:21:19 +02:00
parent dead2521c8
commit fff7c0bde2
12 changed files with 52 additions and 56 deletions

4
.vscode/launch.json vendored
View File

@@ -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": [

View File

@@ -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)

View File

@@ -1 +1 @@
/home/derek/Projekte/CXX/Fall-Fever/build/Fall-Fever
build/src/Fall-Fever

View File

@@ -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;

23
src/CMakeLists.txt Normal file
View File

@@ -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)

View File

@@ -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<Texture> textures {
Texture("res/textures/tex2.png", texture_diffuse),
Texture("res/textures/tex1.png", texture_diffuse)
std::vector<Texture*> 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());

View File

@@ -1,6 +1,6 @@
#include "Mesh.h"
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices, std::vector<Texture> textures)
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices, std::vector<Texture*> 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();
}

View File

@@ -11,7 +11,7 @@ class Mesh {
public:
Mesh(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);
~Mesh() = default;
void draw(ShaderProgram *shaderProgram);
@@ -20,7 +20,7 @@ private:
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
std::vector<Texture> textures;
std::vector<Texture*> textures;
VertexBuffer vertexBuffer;

View File

@@ -47,7 +47,7 @@ void Model::processNode(aiNode *node, const aiScene *scene) {
Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) {
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
std::vector<Texture> textures;
std::vector<Texture*> 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<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, texture_diffuse);
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);
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, uint8_t textureType) {
std::vector<Texture*> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, uint8_t textureType) {
std::vector<Texture> textures;
std::vector<Texture*> textures;
for(uint i = 0; i < mat->GetTextureCount(type); i++) {
aiString filename;
mat->GetTexture(type, i, &filename);
@@ -109,7 +109,7 @@ std::vector<Texture> 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<Texture> 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;
}

View File

@@ -25,7 +25,7 @@ private:
void processNode(aiNode *node, const aiScene *scene);
Mesh processMesh(aiMesh *mesh, const aiScene *scene);
std::vector<Texture> loadMaterialTextures(aiMaterial *mat, aiTextureType type, uint8_t textureType);
std::vector<Texture*> loadMaterialTextures(aiMaterial *mat, aiTextureType type, uint8_t textureType);
std::vector<Mesh> meshes;

View File

@@ -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) {

View File

@@ -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;