Basic model loading

This commit is contained in:
4VRDriver
2020-09-04 21:39:12 +02:00
parent 0f786f0216
commit 0c930a9892
11 changed files with 198 additions and 22 deletions

View File

@@ -25,6 +25,7 @@ add_executable(Fall-Fever
Texture.cpp
Camera.cpp
Mesh.cpp
Model.cpp
)
target_link_libraries(

View File

@@ -14,7 +14,8 @@
#include "Controller.h"
#include "Texture.h"
#include "Mesh.h"
#include "Model.h"
//#include "Mesh.h"
Controller::Controller() {
if(!glfwInit()) exit(-1);
@@ -81,12 +82,14 @@ void Controller::run() {
1, 2, 3
};
std::vector<Texture*> textures {
new Texture("res/tex2.png", texture_diffuse),
new Texture("res/tex1.png", texture_diffuse)
std::vector<Texture> textures {
Texture("res/textures/tex2.png", texture_diffuse),
Texture("res/textures/tex1.png", texture_diffuse)
};
Mesh mesh1(vertices, indices, textures);
Model model1("res/models/cube.obj");
//Mesh mesh1(vertices, indices, textures);
glm::mat4 model = glm::mat4(1.0f);
@@ -120,13 +123,14 @@ void Controller::run() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(int i=0;i<20;i++) {
mesh1.draw(&shaderProgram);
model = glm::translate(model, glm::vec3(0.0f, 0.0f, -1.0f));
//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));
//model = glm::translate(model, glm::vec3(0.0f, 0.0f, 20.0f));
glfwSwapBuffers(gameWindow->getGLFWwindow());
@@ -141,8 +145,6 @@ void Controller::run() {
camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDeltaX(), gameEventHandler->getCursorDeltaY());
}
delete textures[0];
}
void Controller::limit_framerate() {

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();
}

5
Mesh.h
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,10 +20,9 @@ private:
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
std::vector<Texture*> textures;
std::vector<Texture> textures;
VertexBuffer vertexBuffer;
//ShaderProgram *shaderProgram;
void setupMesh();

128
Model.cpp Normal file
View File

@@ -0,0 +1,128 @@
#include "Model.h"
#include <iostream>
Model::Model(const char* pathToModel) {
loadModel(pathToModel);
}
void Model::draw(ShaderProgram *shaderProgram) {
// Iterate through every mesh and call the draw function
for(auto it = meshes.begin(); it != meshes.end(); it++) {
it->draw(shaderProgram);
}
}
void Model::loadModel(std::string pathToModel) {
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(pathToModel, aiProcess_Triangulate | aiProcess_FlipUVs); //aiProcess_OptimizeMeshes ?
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
std::cout << "ERROR::ASSIMP::" << importer.GetErrorString() << std::endl;
return;
}
directory = pathToModel.substr(0, pathToModel.find_last_of('/'));
processNode(scene->mRootNode, scene);
}
void Model::processNode(aiNode *node, const aiScene *scene) {
// Push the node's meshes into the mesh vector
for(uint i = 0; i < node->mNumMeshes; i++) {
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene));
}
// Process child nodes too
for(uint 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<uint32_t> indices;
std::vector<Texture> textures;
for(uint 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.normalVec = vector;
// Texture
if(mesh->mTextureCoords[0]) {
glm::vec2 vec;
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.textureCoords = vec;
} else {
vertex.textureCoords = glm::vec2(0.0f, 0.0f);
}
vertices.push_back(vertex);
}
// Indices
for(uint i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
for(uint j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
// Material
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, uint8_t textureType) {
std::vector<Texture> textures;
for(uint i = 0; i < mat->GetTextureCount(type); i++) {
aiString filename;
mat->GetTexture(type, i, &filename);
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]);
skip = 1;
break;
}
}
if(!skip) {
std::string path = directory + '/' + filename.C_Str();
Texture texture(path.c_str(), textureType);
textures.push_back(texture);
}
}
loadedTextures.clear();
return textures;
}

38
Model.h Normal file
View File

@@ -0,0 +1,38 @@
#pragma once
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <string>
#include "Mesh.h"
class Model {
public:
Model(const char* pathToModel);
~Model() = default;
void draw(ShaderProgram *shaderProgram);
private:
void loadModel(std::string pathToModel);
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<Mesh> meshes;
std::vector<Texture> loadedTextures;
void textureFromFile(aiMaterial *mat, aiTextureType type);
std::string directory;
};

View File

@@ -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) {
@@ -63,6 +63,9 @@ void Texture::bind(uint8_t textureUnit, ShaderProgram* shaderProgram, uint8_t te
break;
}
// Add material. as we store textures in a struct
uniformName = "material." + uniformName;
shaderProgram->setUniform(uniformName.c_str(), textureTypeNum);
glActiveTexture(GL_TEXTURE0 + textureUnit);

View File

@@ -19,6 +19,7 @@ public:
void unbind();
uint8_t getTextureType() { return textureType; }
std::string getPath() { return texturePath; }
private:

View File

@@ -1,18 +1,22 @@
#version 330 core
struct Material {
sampler2D u_texture_diffuse0;
sampler2D u_texture_diffuse1;
};
layout(location = 0) out vec4 f_color;
in vec4 v_color;
in vec2 v_texCoord;
uniform sampler2D u_texture_diffuse0;
uniform sampler2D u_texture_diffuse1;
uniform Material material;
uniform float mix_val;
void main() {
//f_color = v_color;
vec4 texColor1 = texture(u_texture_diffuse0, v_texCoord);
vec4 texColor2 = texture(u_texture_diffuse1, v_texCoord);
vec4 texColor1 = texture(material.u_texture_diffuse0, v_texCoord);
vec4 texColor2 = texture(material.u_texture_diffuse1, v_texCoord);
f_color = mix(texColor1, texColor2, mix_val);
}

View File

Before

Width:  |  Height:  |  Size: 176 KiB

After

Width:  |  Height:  |  Size: 176 KiB

View File

Before

Width:  |  Height:  |  Size: 693 KiB

After

Width:  |  Height:  |  Size: 693 KiB