Files
b_engine/src/main.cpp

159 lines
4.4 KiB
C++
Raw Normal View History

2026-04-29 00:17:00 -04:00
#include "glad/gl.h"
#include "GLFW/glfw3.h"
#include "spdlog/spdlog.h"
#include "glm/glm.hpp"
#include "glm/ext/matrix_clip_space.hpp"
#include "glm/ext/matrix_transform.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "Model.h"
#include "ShaderProgram.h"
#include "Texture.h"
GLFWwindow* gWindow = nullptr;
2026-04-29 00:17:00 -04:00
int gWindowWidth = 800;
int gWindowHeight = 600;
int glVersionMajor = 0;
int glVersionMinor = 0;
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 = {};
2026-04-29 00:17:00 -04:00
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);
void init_glfw();
void create_main_window();
void load_gl();
void init_camera();
std::shared_ptr<Model> load_model(std::string_view _path);
2026-04-29 00:17:00 -04:00
int main() {
spdlog::info("b_engine start");
init_glfw();
2026-04-29 00:17:00 -04:00
create_main_window();
2026-04-29 00:17:00 -04:00
load_gl();
2026-04-29 00:17:00 -04:00
stbi_set_flip_vertically_on_load(true);
2026-04-29 00:17:00 -04:00
init_camera();
2026-04-29 00:17:00 -04:00
// 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);
2026-04-29 00:17:00 -04:00
// 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);
2026-04-29 00:17:00 -04:00
gShaders["phong_shader"] = ShaderProgram::load("./resources/standard.frag", "./resources/standard.vert");
if (gShaders["phong_shader"] == nullptr)
{
std::exit(1);
}
2026-04-29 00:17:00 -04:00
while (!glfwWindowShouldClose(gWindow)) {
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(gWindow);
}
glfwDestroyWindow(gWindow);
glfwTerminate();
return 0;
}
void glfw_error_callback(int error, const char* description) {
spdlog::error("glfw error: {}", description);
}
void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
void glfw_framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
void init_glfw()
{
if (!glfwInit()) {
spdlog::error("could not initialize glfw");
std::exit(1);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwSetErrorCallback(glfw_error_callback);
}
void create_main_window()
{
gWindow = glfwCreateWindow(gWindowWidth, gWindowHeight, "b_engine v0.0.1", nullptr, nullptr);
if (!gWindow) {
spdlog::error("failed to create glfw window");
std::exit(1);
}
glfwMakeContextCurrent(gWindow);
glfwSetKeyCallback(gWindow, glfw_key_callback);
glfwSetFramebufferSizeCallback(gWindow, glfw_framebuffer_size_callback);
}
void load_gl()
{
int version = gladLoadGL(glfwGetProcAddress);
glVersionMajor = GLAD_VERSION_MAJOR(version);
glVersionMinor = GLAD_VERSION_MINOR(version);
spdlog::info("gl version {}.{}", glVersionMajor, glVersionMinor);
if (gWindow)
{
int fbWidth, fbHeight;
glfwGetFramebufferSize(gWindow, &fbWidth, &fbHeight);
glViewport(0, 0, fbWidth, fbHeight);
}
}
void init_camera()
{
cameraPosition = glm::vec3{0, 5, 5};
auto cameraTarget = glm::vec3{0, 0, 0};
auto behind = glm::normalize(cameraPosition - cameraTarget);
auto right = glm::normalize(glm::cross(glm::vec3{0, 1, 0}, behind));
auto up = glm::normalize(glm::cross(right, behind));
view = glm::lookAt(cameraPosition, cameraTarget, up);
float aspectRatio = static_cast<float>(gWindowWidth) / static_cast<float>(gWindowHeight);
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>();
}