revamped resource system
This commit is contained in:
80
src/main.cpp
80
src/main.cpp
@@ -11,8 +11,11 @@
|
||||
#include "stb_image.h"
|
||||
|
||||
#include "Model.h"
|
||||
#include "ModelLoader.h"
|
||||
#include "ShaderLoader.h"
|
||||
#include "ShaderProgram.h"
|
||||
#include "Texture.h"
|
||||
#include "TextureLoader.h"
|
||||
|
||||
GLFWwindow* gWindow = nullptr;
|
||||
int gWindowWidth = 800;
|
||||
@@ -25,10 +28,6 @@ auto cameraPosition = glm::vec3{0, 0, 0};
|
||||
auto view = glm::mat4{0};
|
||||
auto projection = glm::mat4{0};
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<Model>> gModels = {};
|
||||
std::unordered_map<std::string, std::shared_ptr<Texture>> gTextures = {};
|
||||
std::unordered_map<std::string, std::shared_ptr<ShaderProgram>> gShaders = {};
|
||||
|
||||
void glfw_error_callback(int error, const char* description);
|
||||
void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
void glfw_framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
@@ -37,6 +36,7 @@ void init_glfw();
|
||||
void create_main_window();
|
||||
void load_gl();
|
||||
void init_camera();
|
||||
void loop();
|
||||
|
||||
std::shared_ptr<Model> load_model(std::string_view _path);
|
||||
|
||||
@@ -55,26 +55,21 @@ int main() {
|
||||
|
||||
// create a default texture
|
||||
unsigned char defaultDiffuseData[4] = {static_cast<unsigned char>(255), 255, 255, 255};
|
||||
gTextures["default_diffuse"] = Texture::load_from_data(reinterpret_cast<unsigned char*>(&defaultDiffuseData), 1, 1, 4);
|
||||
TextureLoader::textures["default_diffuse"] = TextureLoader::load_from_data(reinterpret_cast<unsigned char*>(&defaultDiffuseData), 1, 1, 4);
|
||||
|
||||
// create a default specular map
|
||||
unsigned char defaultSpecularData[4] = {static_cast<unsigned char>(128), 128, 128, 255};
|
||||
gTextures["default_specular"] = Texture::load_from_data(reinterpret_cast<unsigned char*>(&defaultSpecularData), 1, 1, 4);
|
||||
TextureLoader::textures["default_specular"] = TextureLoader::load_from_data(reinterpret_cast<unsigned char*>(&defaultSpecularData), 1, 1, 4);
|
||||
|
||||
gShaders["phong_shader"] = ShaderProgram::load("./resources/standard.frag", "./resources/standard.vert");
|
||||
if (gShaders["phong_shader"] == nullptr)
|
||||
ShaderLoader::shaders["phong_shader"] = ShaderLoader::load("./resources/standard.vert", "./resources/standard.frag");
|
||||
if (ShaderLoader::shaders["phong_shader"] == nullptr)
|
||||
{
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
while (!glfwWindowShouldClose(gWindow)) {
|
||||
glfwPollEvents();
|
||||
ModelLoader::models["cube"] = ModelLoader::load_from_file("./resources/cube.obj");
|
||||
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glfwSwapBuffers(gWindow);
|
||||
}
|
||||
loop();
|
||||
|
||||
glfwDestroyWindow(gWindow);
|
||||
glfwTerminate();
|
||||
@@ -153,7 +148,56 @@ void init_camera()
|
||||
projection = glm::perspective(glm::radians(75.f), aspectRatio, 0.1f, 100.0f);
|
||||
}
|
||||
|
||||
std::shared_ptr<Model> load_model(std::string_view _path)
|
||||
{
|
||||
return std::make_shared<Model>();
|
||||
void loop() {
|
||||
Texture* defaultDiffuse = TextureLoader::textures["default_diffuse"].get();
|
||||
Texture* defaultSpecular = TextureLoader::textures["default_specular"].get();
|
||||
|
||||
while (!glfwWindowShouldClose(gWindow)) {
|
||||
auto modelMatrix = glm::identity<glm::mat4>();
|
||||
glfwPollEvents();
|
||||
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
ShaderProgram* shader = ShaderLoader::shaders["phong_shader"].get();
|
||||
Model* model = ModelLoader::models["cube"].get();
|
||||
|
||||
if (shader && model) {
|
||||
Material* mat = model->materials[0].get();
|
||||
shader->bind();
|
||||
|
||||
shader->setMat4("projection", projection);
|
||||
shader->setMat4("view", view);
|
||||
shader->setMat4("model", modelMatrix);
|
||||
|
||||
shader->setVec3("viewPosition", cameraPosition);
|
||||
|
||||
if (mat) {
|
||||
shader->setVec3("phongAmbient", mat->phong.ambient);
|
||||
shader->setVec3("phongDiffuse", mat->phong.diffuse);
|
||||
shader->setVec3("phongSpecular", mat->phong.specular);
|
||||
shader->setFloat("phongShininess", mat->phong.shininess);
|
||||
}
|
||||
|
||||
shader->setVec3("lightPosition", glm::vec3(5.f, 5.f, 5.f));
|
||||
shader->setVec3("lightAmbient", glm::vec3{.1f, .1f, .1f});
|
||||
shader->setVec3("lightDiffuse", glm::vec3{.8f, .8f, .8f});
|
||||
shader->setVec3("lightSpecular", glm::vec3{1.f, 1.f, 1.f});
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
defaultDiffuse->bind();
|
||||
shader->setInt("diffuse1", 0);
|
||||
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
defaultSpecular->bind();
|
||||
shader->setInt("specular1", 1);
|
||||
|
||||
for (auto mesh: model->meshes) {
|
||||
glBindVertexArray(mesh.get()->vao);
|
||||
glDrawElements(GL_TRIANGLES, mesh->numIndices, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
}
|
||||
|
||||
glfwSwapBuffers(gWindow);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user