/*#ifdef _MSC_VER #pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup") #endif*/ #include #include #include #include "../lib/glew/GL/glew.h" #include "../lib/glfw/glfw3.h" #include "../lib/glm/glm.hpp" #include "../lib/glm/gtc/matrix_transform.hpp" #include "vertex.h" #include "shader.h" #include "buffer.h" #include "state.h" #include "mesh.h" #include "entity.h" #include "model.h" #include "camera.h" #include "world.h" #define SCREEN_WIDTH 800 #define SCREEN_HEIGHT 600 int main() { // Initialize OpenGL if (!glfwInit()) { std::cerr << "Failed to initialize glfw\n"; return 1; } glfwWindowHint(GLFW_RESIZABLE, false); glfwWindowHint(GLFW_SAMPLES, 8); // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow* win = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Daniel Poveda", nullptr, nullptr); if (win == nullptr) { std::cerr << "Failed to create opengl window\n"; glfwTerminate(); return 1; } glfwMakeContextCurrent(win); glEnable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); std::cout << "OpenGL initialized, version: " << glGetString(GL_VERSION) << "\n"; // Initialize GLEW glewExperimental = GL_TRUE; GLenum err = glewInit(); if (err != GLEW_OK) { std::cerr << "Failed to initialize GLEW: " << glewGetErrorString(err) << "\n"; glfwTerminate(); return 1; } // Initialize default shader State::defaultShader = std::make_shared("data/vertex.glsl", "data/fragment.glsl"); if (std::strlen(State::defaultShader->getError()) > 0) { std::cerr << "Failed to initialize shaders: " << State::defaultShader->getError() << "\n"; } // Create world World world; // Create camera auto camera = std::make_shared(); camera->setPosition(glm::vec3(0.0f, 0.0f, 6.0f)); camera->setProjection(glm::perspective( glm::radians(45.0f), static_cast(SCREEN_WIDTH) / SCREEN_HEIGHT, 0.1f, 100.0f)); camera->setViewport(glm::ivec4(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)); camera->setClearColor(glm::vec3(0.1f, 0.1f, 0.1f)); world.addEntity(camera); // Create triangle mesh std::vector vertices = { {{0.0f, 0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}}, {{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}}, {{0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}}}; std::vector indices = {0, 1, 2}; auto buffer = std::make_shared(vertices, indices); auto mesh = std::make_shared(); mesh->addBuffer(buffer); // Create 9 models in a 3x3 grid std::vector> models; for (int row = 0; row < 3; ++row) { for (int col = 0; col < 3; ++col) { auto model = std::make_shared(mesh); model->setPosition( glm::vec3(-3.0f + static_cast(col) * 3.0f, 0.0f, static_cast(-row) * 3.0f)); models.push_back(model); world.addEntity(model); } } // Main loop double angle = 0.0; double lastTime = glfwGetTime(); while (!glfwWindowShouldClose(win) && !glfwGetKey(win, GLFW_KEY_ESCAPE)) { // Delta time double currentTime = glfwGetTime(); double deltaTime = currentTime - lastTime; lastTime = currentTime; // Update angle angle += 32.0 * deltaTime; // Update rotation for all models for (auto& model : models) { model->setRotation(glm::vec3( 0.0f, glm::radians(static_cast(angle)), 0.0f)); } // Update and draw world world.update(static_cast(deltaTime)); world.draw(); // Refresh screen glfwSwapBuffers(win); glfwPollEvents(); } // Shutdown glfwTerminate(); return 0; }