Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(glm CONFIG REQUIRED)
find_package(SDL2_image CONFIG REQUIRED)
find_package(assimp CONFIG REQUIRED)

find_package(yaml-cpp CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)

Expand Down Expand Up @@ -259,7 +259,6 @@ function(build_engine_stclib lib_name)
SDL2::SDL2main
SDL2_image::SDL2_image
glm::glm
assimp::assimp
yaml-cpp::yaml-cpp
spdlog::spdlog
EnTT::EnTT
Expand Down
86 changes: 41 additions & 45 deletions Sources/Engine/RenderSystem/Model.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <Common.hh>


#define DEFAULT_TEXTURE_NAME "defaulttexture.png"

#include <string>
Expand All @@ -11,16 +10,10 @@
#include <optional>
#include <filesystem>


#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <GL/glew.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/Exceptional.h>
#include <tiny_gltf.h>

#include <tinygltf/tiny_gltf.h>

#include <Shader.hh>
#include <Texture.hh>
Expand All @@ -33,8 +26,9 @@ using namespace std;

NAMESPACE_BEGIN

enum class TextureType{
BASE_COLOR=0,
enum class TextureType
{
BASE_COLOR = 0,
ROUGHNESS,
METALIC,
NORMAL,
Expand All @@ -59,7 +53,8 @@ struct Texture

class Model;

struct Primitive {
struct Primitive
{
int indicessizes = 0;
int indexMaterial;
GLuint VAO = 0, VBO = 0, EBO = 0;
Expand All @@ -68,98 +63,99 @@ struct Primitive {
vector<Vertex> vertices;
vector<GLuint> indices;


Primitive(vector<Vertex> vertices, vector<GLuint> indices, GLenum enumIndexMode, int indexMaterial) :
vertices(vertices), indices(indices),
indicessizes(indices.size()), enumIndexMode(enumIndexMode),
indexMaterial(indexMaterial)
Primitive(vector<Vertex> vertices, vector<GLuint> indices, GLenum enumIndexMode, int indexMaterial) : vertices(vertices), indices(indices),
indicessizes(indices.size()), enumIndexMode(enumIndexMode),
indexMaterial(indexMaterial)
{
this->setupPrimitive();
}

void setupPrimitive();
void processPrimitive();
void drawPrimitive(const Shader& shader, const glm::mat4 transform, const Model* parentmodel) const;
void drawPrimitive(const Shader &shader, const glm::mat4 transform, const Model *parentmodel) const;
};

class Model
class Model
{
public:
class Material{
class Material
{
public:
vector<Texture> textures;
};
class Mesh{
class Mesh
{
public:
glm::mat4 localtransform;
vector<Primitive> primitives;

Mesh(glm::mat4 transform, vector<Primitive> primitives) :
localtransform(transform), primitives(primitives){
Mesh(glm::mat4 transform, vector<Primitive> primitives) : localtransform(transform), primitives(primitives)
{
this->setupMesh();
};

private:
void setupMesh();
};

Model(const fs::path path, bool flipuv=true){
Model(const fs::path path, bool flipuv = true)
{

ENGINE_VALIDLOCATION(path);
this->modelnameandpath = path;

if(path.extension() != fs::path{".glb"} && path.extension() != fs::path{".gltf"}){
if (path.extension() != fs::path{".glb"} && path.extension() != fs::path{".gltf"})
{
ENGINE_ERROR("you are loading a model file with non glb/gltf extension, make sure assimp support it\n");
return;
}
ENGINE_DEBUG("path after extension judge: {}", path.string());

this->loadModel(path.string(), flipuv);
}
// void Draw(const Shader& shader) const;
//vaos, render relative infos.
// void Draw(const Shader& shader) const;
// vaos, render relative infos.
vector<Mesh> getMeshes() const { return meshes; }
fs::path getDirectory() const { return directory; }
glm::mat4 getTransform() const { return transform; }
void setTransform(glm::mat4 transform) { this->transform = transform; }
Shader* getShader() const { return shader; }
void setShader(Shader* shader) { this->shader = shader; }
Shader *getShader() const { return shader; }
void setShader(Shader *shader) { this->shader = shader; }
vector<Material> getMaterials() const { return materials; }

protected:
std::unordered_map<int, GLuint>* loadedTextures;
std::unordered_map<int, GLuint> *loadedTextures;
glm::mat4 transform = glm::mat4(1.0f);
vector<Mesh> meshes;
vector<Material> materials;
fs::path directory;
fs::path modelnameandpath;

Shader* shader;

Material PBRload(tinygltf::Material& gltfmat, tinygltf::Model& gltfmodel);
Shader *shader;

Material PBRload(tinygltf::Material &gltfmat, tinygltf::Model &gltfmodel);
void loadModel(string path, bool flipUVy = false);
void loadMaterials(tinygltf::Model& model);
void processNode(tinygltf::Node* node, tinygltf::Model* model, glm::mat4 transformParent);
optional<Model::Mesh> processMesh(const tinygltf::Node* node, const tinygltf::Model* model, glm::mat4 transform);
optional<Texture> loadMaterialTextures(aiMaterial* mat, aiTextureType type, string typeName);
void loadMaterials(tinygltf::Model &model);
void processNode(tinygltf::Node *node, tinygltf::Model *model, glm::mat4 transformParent);
optional<Model::Mesh> processMesh(const tinygltf::Node *node, const tinygltf::Model *model, glm::mat4 transform);
Texture loadTexturefromGLB(
tinygltf::Model& gltfmodel,
tinygltf::Material& mat,
tinygltf::Model &gltfmodel,
tinygltf::Material &mat,
int index,
TextureType textype
);
TextureType textype);

static Texture loadDefaultTexture() {
static Texture loadDefaultTexture()
{

Texture texture;
ENGINE_DEBUG("loading default texture\n");

texture.id = TextureSdlGl{ fs::path{fs::current_path() / DEFAULT_TEXTURE_NAME}.string() }.getTextureId();
texture.id = TextureSdlGl{fs::path{fs::current_path() / DEFAULT_TEXTURE_NAME}.string()}.getTextureId();
texture.type = TextureType::COUNT;
texture.type_str = { DEFAULT_TEXTURE_NAME };
texture.type_str = {DEFAULT_TEXTURE_NAME};

return texture;
}

};

NAMESPACE_END
184 changes: 41 additions & 143 deletions Sources/Engine/RenderSystem/Texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,155 +2,53 @@

NAMESPACE_BEGIN

TextureSdlGl::TextureSdlGl(string filepathTexture) {
SDL_Surface *surface = IMG_Load(filepathTexture.c_str());
if(surface == NULL){
ENGINE_ERROR("Creating SDL_Surface from filepath Error: %s\n", IMG_GetError());
}

//safely force convert into rgba32 format under sdl_surface
surface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32, 0);
if (surface == NULL){
ENGINE_ERROR("Safely convert raw SDL_Surface into rgba32 SDL_Surface Error: %s\n", SDL_GetError());
}


glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);

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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);


glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
surface->w,
surface->h,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
surface->pixels
);
GLenum errorClass;
while((errorClass = glGetError()) != GL_NO_ERROR){
ENGINE_ERROR("SDL surface to GL texture Error: %u\n", errorClass);
}

glGenerateMipmap(GL_TEXTURE_2D);


while((errorClass = glGetError()) != GL_NO_ERROR){
ENGINE_ERROR("GL generate Mipmap Error: %u\n", errorClass);
}


SDL_FreeSurface(surface);
glBindTexture(GL_TEXTURE_2D, 0);
TextureSdlGl::TextureSdlGl(string filepathTexture)
{
SDL_Surface *surface = IMG_Load(filepathTexture.c_str());
if (surface == NULL)
{
ENGINE_ERROR("Creating SDL_Surface from filepath Error: %s\n", IMG_GetError());
}

// safely force convert into rgba32 format under sdl_surface
surface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32, 0);
if (surface == NULL)
{
ENGINE_ERROR("Safely convert raw SDL_Surface into rgba32 SDL_Surface Error: %s\n", SDL_GetError());
}

TextureSdlGl::TextureSdlGl(const aiTexture* inmemptr) {
SDL_RWops* rw = nullptr;
SDL_Surface* surface = nullptr;
if (inmemptr == nullptr) {
ENGINE_ERROR("Creating TextureSdlGl from aiTexture* Error: input pointer is null\n");
return;
}
if (inmemptr->mHeight == 0) {
ENGINE_INFO("Creating TextureSDLGL from compressed file format");

rw = SDL_RWFromConstMem(inmemptr->pcData, inmemptr->mWidth);
surface = IMG_Load_RW(rw, 1);
} else {
ENGINE_INFO("Creating TextureSDLGL from uncompressed file format");
surface = SDL_CreateRGBSurfaceFrom(
(void*)inmemptr->pcData,
inmemptr->mWidth,
inmemptr->mHeight,
32,
inmemptr->mWidth * 4,
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
);

}
auto sdl_error = IMG_GetError();
if (string{sdl_error} != "") {
ENGINE_ERROR("Creating SDL_Surface from aiTexture* Error: {}\n", string{sdl_error});
} else {
ENGINE_DEBUG("SDL_Surface created from aiTexture* successfully\n");
}
//what is this?????
// //load from compressed format
// if (inmemptr->mFormatHint != "rgba8888" && inmemptr->mFormatHint != "argb8888") {
// ENGINE_ERROR("Creating TextureSdlGl from aiTexture* Error: only support rgba8888 and argb8888 format, got {}\n", inmemptr->mFormatHint);
// return;
// }
//
// SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom(
// (void*)inmemptr->pcData,
// inmemptr->mWidth,
// inmemptr->mHeight,
// 32,
// inmemptr->mWidth * 4,
// SDL_PIXELFORMAT_RGBA32
// );
if(surface == NULL){
ENGINE_ERROR("Creating SDL_Surface from aiTexture* Error: {}\n", SDL_GetError());
return;
}

glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);

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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

//convert to rgba32 format
surface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32, 0);
if (surface == NULL){
ENGINE_ERROR("Safely convert raw SDL_Surface into rgba32 SDL_Surface Error: {}\n", SDL_GetError());
return;
}

glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
surface->w,
surface->h,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
surface->pixels
);


GLenum errorClass;
while((errorClass = glGetError()) != GL_NO_ERROR){
ENGINE_ERROR("SDL surface to GL texture Error: %u\n", errorClass);
}


glGenerateMipmap(GL_TEXTURE_2D);


while((errorClass = glGetError()) != GL_NO_ERROR){
ENGINE_ERROR("GL generate Mipmap Error: {}\n", errorClass);
}


SDL_FreeSurface(surface);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);

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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
surface->w,
surface->h,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
surface->pixels);
GLenum errorClass;
while ((errorClass = glGetError()) != GL_NO_ERROR)
{
ENGINE_ERROR("SDL surface to GL texture Error: %u\n", errorClass);
}

glGenerateMipmap(GL_TEXTURE_2D);

while ((errorClass = glGetError()) != GL_NO_ERROR)
{
ENGINE_ERROR("GL generate Mipmap Error: %u\n", errorClass);
}

SDL_FreeSurface(surface);
glBindTexture(GL_TEXTURE_2D, 0);
}

NAMESPACE_END
Loading