Skip to content

Commit

Permalink
Fix texture loader, the model is still not being displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
desatur committed Nov 3, 2024
1 parent b7166f4 commit aa55076
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 30 deletions.
13 changes: 9 additions & 4 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [

{
"label": "Build sandbox",
"type": "shell",
"command": "cmake --build ./build --config Debug --target FutureSandbox --"
"command": "cmd.exe",
"args": [
"/c",
"cmake --build ./build --config Debug --target FutureSandbox"
]
},

{
"label": "Build and run sandbox",
"type": "shell",
"command": "cmd.exe",
"args": [
"/c",
"cmake --build ./build --config Debug --target FutureSandbox && ./build/Debug/FutureSandbox.exe"
"cmake --build ./build --config Debug --target FutureSandbox && .\\build\\FutureSandbox\\Debug\\FutureSandbox.exe"
]
}

]
}
2 changes: 0 additions & 2 deletions Future/src/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ namespace Future {
window->Init();
auto renderer = new Future::Renderer(window);
renderer->Init();
//window->Init();
//window->Loop();
}
}
2 changes: 1 addition & 1 deletion Future/src/EntryPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern Future::Engine* Future::CreateApplication();

int main(int argc, char *argv[]) { // DO NOT CHANGE THOSE VALUES!!!!!!! REQUIRED BY SDL2
Future::Log::Init();
FE_CORE_INFO("Hello World!");
//FE_CORE_INFO("Hello World!");
auto app = Future::CreateApplication();
app->Run();
delete app;
Expand Down
1 change: 0 additions & 1 deletion Future/src/Rendering/Model/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "Mesh.hpp"


namespace Future
{
Mesh::Mesh(std::vector <Vertex>& vertices, std::vector <GLuint>& indices, std::vector<Texture>& textures)
Expand Down
27 changes: 17 additions & 10 deletions Future/src/Rendering/Model/Model.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "Model.hpp"

#include <iostream>

namespace Future
{
Model::Model(const char* file)
{
directory = file;
model_path = file;
std::filesystem::path filePath(file);
directory = filePath.parent_path().string();
//std::cout << model_path << "\n";
LoadModel();
}

Expand All @@ -21,10 +23,11 @@ namespace Future
void Model::LoadModel()
{
Assimp::Importer import;
const aiScene* scene = import.ReadFile(directory, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
const aiScene* scene = import.ReadFile(model_path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);

if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
std::cout << import.GetErrorString() << "\n";
return;
}

Expand All @@ -33,11 +36,13 @@ namespace Future

void Model::ProcessNode(const aiNode* node, const aiScene* scene)
{
// process all the node's meshes (if any)
for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(ProcessMesh(mesh, scene));
}
// then do the same for each of its children
for (unsigned int i = 0; i < node->mNumChildren; i++)
{
ProcessNode(node->mChildren[i], scene);
Expand Down Expand Up @@ -85,19 +90,22 @@ namespace Future
{
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; j++)
{
indices.push_back(face.mIndices[j]);
}
}

if (mesh->mMaterialIndex >= 0)
{
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];

std::vector<Texture> diffuseMaps = LoadMaterialTextures(material, aiTextureType_DIFFUSE, "diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());

std::vector<Texture> specularMaps = LoadMaterialTextures(material, aiTextureType_SPECULAR, "specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());

// NOT IMPLEMENTED
//std::vector<Texture> normalMaps = LoadMaterialTextures(material, aiTextureType_NORMALS, "normal");
//textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
}
Expand All @@ -108,14 +116,13 @@ namespace Future
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++)
for(unsigned int i = 0; i < mat->GetTextureCount(type); i++)
{
aiString str;
aiString str; // This does not get full path
mat->GetTexture(type, i, &str);
std::string filename = std::string(str.C_Str());
filename = directory + '/' + filename;

Texture texture(filename.c_str(), typeName.c_str(), i);
std::string texture_path = directory + '/' + std::string(str.C_Str());
std::cout << i << " : " << texture_path << "\n";
Texture texture(texture_path.c_str(), typeName.c_str(), i);
textures.push_back(texture);
}
return textures;
Expand Down
3 changes: 2 additions & 1 deletion Future/src/Rendering/Model/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace Future
private:
// model data
std::vector<Mesh> meshes;
const char *directory;
const char *model_path;
std::string directory;

void LoadModel();
void ProcessNode(const aiNode *node, const aiScene *scene);
Expand Down
5 changes: 2 additions & 3 deletions Future/src/Rendering/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Renderer.hpp"
#define BACKEND "OPENGL"

namespace Future
{
Expand Down Expand Up @@ -40,7 +39,7 @@ namespace Future

Camera m_mainCamera(1920, 1080, glm::vec3(0.0f, 0.0f, 1.0f));

Model test("Sponza/glTF/sponza.gltf");
Model demo("D:/Sponza/glTF/sponza.gltf"); // Testing model

while (m_window->IsRunning())
{
Expand All @@ -49,7 +48,7 @@ namespace Future

m_mainCamera.UpdateMatrix(45.0f, 0.1f, 100.0f); // Updates and exports the camera matrix to the Vertex Shader

test.Draw(m_shaderProgram, m_mainCamera);
demo.Draw(m_shaderProgram, m_mainCamera);

SDL_Event e = m_window->Tick();
}
Expand Down
2 changes: 0 additions & 2 deletions Future/src/Rendering/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

namespace Future
{
enum class Backend : std::uint8_t;

class FE_API Renderer
{
public:
Expand Down
12 changes: 6 additions & 6 deletions Future/src/Rendering/Textures/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

namespace Future
{
Texture::Texture(const char* image, const char* texType, GLuint slot)
Texture::Texture(const char* image_path, const char* texType, GLuint slot)
{
type = texType;
int widthImg, heightImg, numColCh;
stbi_set_flip_vertically_on_load(true);
unsigned char* bytes = stbi_load(image, &widthImg, &heightImg, &numColCh, 0);
unsigned char* bytes = stbi_load(image_path, &widthImg, &heightImg, &numColCh, 0);

glGenTextures(1, &ID);
// Assigns the texture to a Texture Unit
glActiveTexture(GL_TEXTURE0 + slot);
unit == slot;
unit = slot; // TODO: check if ==
glBindTexture(GL_TEXTURE_2D, ID);

// Configures the type of algorithm that is used to make the image smaller or bigger
// Configures the type of algorithm that is used to make the image_path smaller or bigger
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

Expand Down Expand Up @@ -68,8 +68,8 @@ namespace Future
bytes
);
else
throw std::invalid_argument("Automatic Texture type recognition failed"); // I fucking hate this slow(?) ass throw

std::cout << "Automatic Texture type recognition failed | numColCh: " << numColCh << " dims: " << widthImg << "x" << heightImg << "\n";
//throw std::invalid_argument("Automatic Texture type recognition failed"); // I fucking hate this slow(?) ass throw

// Deletes the image data as it is already in the OpenGL Texture object
stbi_image_free(bytes);
Expand Down
1 change: 1 addition & 0 deletions Future/src/Rendering/Textures/Texture.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef TEXTURE_HPP
#define TEXTURE_HPP

#include <iostream>
#include <glad/glad.h>
#include <stb_image.h>
#include "../../Log.hpp"
Expand Down

0 comments on commit aa55076

Please sign in to comment.