From d2942242b4d5ff04c8b9fa0f8e0ba3b21b85d912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Sat, 6 Dec 2025 14:53:19 +0100 Subject: [PATCH] feat: rework few fonctions and change glew to glad - game files deleted --- .gitmodules | 6 +-- CMakeLists.txt | 12 +++--- inc/EBO.hpp | 19 +++++---- inc/VAO.hpp | 26 ++++++------ inc/VBO.hpp | 19 ++++----- inc/camera.hpp | 60 ++++++++++++++-------------- inc/cube.hpp | 12 +++--- inc/game.hpp | 29 -------------- inc/shader.hpp | 31 +++++++-------- inc/texture.hpp | 2 +- lib/glad | 1 + lib/glew | 1 - src/EBO.cpp | 23 ++++------- src/VAO.cpp | 50 ++++++++++-------------- src/VBO.cpp | 23 ++++------- src/camera.cpp | 102 +++++++++++++++++++++++++----------------------- src/cube.cpp | 42 +++++++++----------- src/game.cpp | 87 ----------------------------------------- src/main.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++----- src/shader.cpp | 49 ++++++++++------------- src/texture.cpp | 81 ++++++++++++++++++-------------------- 21 files changed, 355 insertions(+), 422 deletions(-) delete mode 100644 inc/game.hpp create mode 160000 lib/glad delete mode 160000 lib/glew delete mode 100644 src/game.cpp diff --git a/.gitmodules b/.gitmodules index 170edd7..08861f5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "lib/glew"] - path = lib/glew - url = https://github.com/edoren/glew [submodule "lib/glm"] path = lib/glm url = https://github.com/g-truc/glm @@ -10,3 +7,6 @@ [submodule "lib/stb"] path = lib/stb url = https://github.com/nothings/stb.git +[submodule "lib/glad"] + path = lib/glad + url = https://github.com/DawidLokiec/glad.git diff --git a/CMakeLists.txt b/CMakeLists.txt index da3727b..3aee1f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) file(GLOB_RECURSE CPP_SOURCES CONFIGURE_DEPENDS src/*.cpp) file(GLOB_RECURSE CPP_HEADERS CONFIGURE_DEPENDS src/*.hpp) + +list(APPEND CPP_SOURCES lib/glad/src/glad.c) add_executable(main ${CPP_SOURCES} ${CPP_HEADERS}) # GLFW @@ -14,12 +16,8 @@ set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) add_subdirectory(lib/glfw) -# GLEW -add_definitions(-DGLEW_STATIC) -include_directories(lib/glew/include) -add_library(glew STATIC lib/glew/src/glew.c) -target_include_directories(glew PUBLIC lib/glew/include) -target_compile_definitions(glew PRIVATE GLEW_STATIC) +# GLAD +target_include_directories(main PRIVATE lib/glad/include) # GLM target_include_directories(main PRIVATE lib/glm) @@ -29,7 +27,7 @@ target_include_directories(main PRIVATE lib/stb) target_include_directories(main PRIVATE inc/) -target_link_libraries(main PRIVATE glfw glew) +target_link_libraries(main PRIVATE glfw) find_package(OpenGL REQUIRED) target_link_libraries(main PRIVATE OpenGL::GL) diff --git a/inc/EBO.hpp b/inc/EBO.hpp index 44eec39..43aa831 100755 --- a/inc/EBO.hpp +++ b/inc/EBO.hpp @@ -1,18 +1,21 @@ #ifndef EBO_HPP #define EBO_HPP -#include +#include + +#include "glad/glad.h" + class EBO { -private: - GLuint id; + private: + GLuint id; -public: - EBO(unsigned int* indices, size_t size); - ~EBO(); + public: + EBO(unsigned int* indices, size_t size); + ~EBO(); - void bind(); - void unbind(); + void bind(); + void unbind(); }; #endif \ No newline at end of file diff --git a/inc/VAO.hpp b/inc/VAO.hpp index f13cfb7..fed009d 100755 --- a/inc/VAO.hpp +++ b/inc/VAO.hpp @@ -1,23 +1,27 @@ #ifndef VAO_HPP #define VAO_HPP -#include +#include + +#include "glad/glad.h" class VAO { -private: - unsigned int id; + private: + unsigned int id; -public: - VAO(); - ~VAO(); + public: + VAO(); + ~VAO(); - void bind(); - void unbind(); + void bind(); + void unbind(); - void setAttributePointer(GLuint index, GLuint size, GLenum type, GLsizei stride, const void* offset); - void drawElement(GLenum mode, GLsizei count, GLenum type, const void *indices); - void drawArray(GLenum mode, GLint first, GLsizei count); + void setAttributePointer(GLuint index, GLuint size, GLenum type, + GLsizei stride, const void* offset); + void drawElement(GLenum mode, GLsizei count, GLenum type, + const void* indices); + void drawArray(GLenum mode, GLint first, GLsizei count); }; #endif \ No newline at end of file diff --git a/inc/VBO.hpp b/inc/VBO.hpp index bfbccb2..3d4a856 100755 --- a/inc/VBO.hpp +++ b/inc/VBO.hpp @@ -1,20 +1,21 @@ #ifndef VBO_HPP #define VBO_HPP -#include +#include + +#include "glad/glad.h" class VBO { -private: - unsigned int id; + private: + unsigned int id; -public: - VBO(GLfloat* vertices, size_t size); - ~VBO(); - - void bind(); - void unbind(); + public: + VBO(GLfloat* vertices, size_t size); + ~VBO(); + void bind(); + void unbind(); }; #endif \ No newline at end of file diff --git a/inc/camera.hpp b/inc/camera.hpp index be3b1d4..178e533 100755 --- a/inc/camera.hpp +++ b/inc/camera.hpp @@ -1,44 +1,46 @@ #ifndef CAMERA_HPP #define CAMERA_HPP -#include -#include -#include +#include "glm/glm.hpp" +#include "glm/gtc/matrix_transform.hpp" -class Camera { -private: - int screenWidth; - int screenHeight; +struct GLFWwindow; - double mousePosX; - double mousePosY; - bool firstMouse = true; +class Camera +{ + private: + int screenWidth; + int screenHeight; - float cameraYaw; - float cameraPitch; + double mousePosX; + double mousePosY; + bool firstMouse = true; - glm::vec3 cameraFront; - glm::vec3 cameraUp; - glm::vec3 cameraRight; - glm::vec3 worldUp; + float cameraYaw; + float cameraPitch; - GLFWwindow* window; + glm::vec3 cameraFront; + glm::vec3 cameraUp; + glm::vec3 cameraRight; + glm::vec3 worldUp; - void processInput(float deltaTime); - void processMouseMovement(); - void updateCameraVectors(); + GLFWwindow* window; -public: - Camera(int width, int height, GLFWwindow* window, float sensitivity); + void processInput(float deltaTime); + void processMouseMovement(); + void updateCameraVectors(); - void update(float deltaTime); - glm::mat4 getViewMatrix(); + public: + Camera(int width, int height, GLFWwindow* window, float sensitivity); - float speed; - float cameraSensitivity; - float fov; + void update(float deltaTime); + glm::mat4 getViewMatrix(); - glm::vec3 cameraPosition; + float speed; + float cameraSensitivity; + float fov; + + glm::vec3 cameraPosition; }; -#endif \ No newline at end of file +#endif diff --git a/inc/cube.hpp b/inc/cube.hpp index 958da33..ecfa5e5 100644 --- a/inc/cube.hpp +++ b/inc/cube.hpp @@ -1,16 +1,18 @@ #ifndef CUBE_HPP #define CUBE_HPP -#include "vbo.hpp" +#include + +#include "camera.hpp" #include "ebo.hpp" -#include "vao.hpp" #include "shader.hpp" #include "texture.hpp" -#include "camera.hpp" +#include "vao.hpp" +#include "vbo.hpp" class Cube { -private: + private: VBO vbo; EBO ebo; VAO vao; @@ -19,7 +21,7 @@ private: Shader shader; Texture texture; -public: + public: Cube(Camera &camera); void loop(int width, int height); }; diff --git a/inc/game.hpp b/inc/game.hpp deleted file mode 100644 index e60d100..0000000 --- a/inc/game.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef GAME_HPP -#define GAME_HPP - -#include "GLFW/glfw3.h" -#include - -using namespace std; - -class Game -{ -private: - int width; - int height; - const char *name; - GLFWwindow *window; - -public: - Game(int width, int height, string name); - ~Game(); - - void run(bool (*func)(Game *g)); - - GLFWwindow *getWindow(); - const char *getName(); - int getWidth(); - int getHeight(); -}; - -#endif \ No newline at end of file diff --git a/inc/shader.hpp b/inc/shader.hpp index df74202..55433dd 100755 --- a/inc/shader.hpp +++ b/inc/shader.hpp @@ -1,28 +1,25 @@ #ifndef SHADER_HPP #define SHADER_HPP -#include -#include -#include -#include +#include "glad/glad.h" class Shader { -private: - GLuint vertexShader; - GLuint fragmentShader; - GLuint shaderProgram; + private: + GLuint vertexShader; + GLuint fragmentShader; + GLuint shaderProgram; - void addVertShader(const char* vertexShaderSource); - void addFragShader(const char* fragmentShaderSource); - void compileInProgram(); + void addVertShader(const char* vertexShaderSource); + void addFragShader(const char* fragmentShaderSource); + void compileInProgram(); -public: - Shader(const char* vertexShaderSource, const char* fragmentShaderSource); - ~Shader(); - - GLuint getProgram() const; - void use() const; + public: + Shader(const char* vertexShaderSource, const char* fragmentShaderSource); + ~Shader(); + + GLuint getProgram() const; + void use() const; }; #endif \ No newline at end of file diff --git a/inc/texture.hpp b/inc/texture.hpp index dbf1d6f..4732541 100755 --- a/inc/texture.hpp +++ b/inc/texture.hpp @@ -1,7 +1,7 @@ #ifndef TEXTUTE_HPP #define TEXTUTE_HPP -#include +#include "glad/glad.h" #include class Texture diff --git a/lib/glad b/lib/glad new file mode 160000 index 0000000..379a943 --- /dev/null +++ b/lib/glad @@ -0,0 +1 @@ +Subproject commit 379a9432ecb5c4b938cb5f63a2a389a93bec37c7 diff --git a/lib/glew b/lib/glew deleted file mode 160000 index 5b995ca..0000000 --- a/lib/glew +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 5b995cab14f97231b48bc86b447be5e23b899626 diff --git a/src/EBO.cpp b/src/EBO.cpp index 4c7e167..a325529 100755 --- a/src/EBO.cpp +++ b/src/EBO.cpp @@ -1,23 +1,14 @@ -#include "EBO.hpp" +#include "ebo.hpp" EBO::EBO(unsigned int* indices, size_t size) { - glGenBuffers(1, &id); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW); + glGenBuffers(1, &id); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW); } -EBO::~EBO() -{ - glDeleteBuffers(1, &id); -} +EBO::~EBO() { glDeleteBuffers(1, &id); } -void EBO::bind() -{ - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); -} +void EBO::bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); } -void EBO::unbind() -{ - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); -} \ No newline at end of file +void EBO::unbind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } \ No newline at end of file diff --git a/src/VAO.cpp b/src/VAO.cpp index 8164fec..b666f3d 100755 --- a/src/VAO.cpp +++ b/src/VAO.cpp @@ -1,40 +1,30 @@ -#include "VAO.hpp" +#include "vao.hpp" -VAO::VAO() +VAO::VAO() { glGenVertexArrays(1, &id); } + +VAO::~VAO() { glDeleteVertexArrays(1, &id); } + +void VAO::bind() { glBindVertexArray(id); } + +void VAO::unbind() { glBindVertexArray(0); } + +void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type, + GLsizei stride, const void* offset) { - glGenVertexArrays(1, &id); + bind(); + glEnableVertexAttribArray(index); + glVertexAttribPointer(index, size, type, GL_FALSE, stride, offset); } -VAO::~VAO() +void VAO::drawElement(GLenum mode, GLsizei count, GLenum type, + const void* indices) { - glDeleteVertexArrays(1, &id); -} - -void VAO::bind() -{ - glBindVertexArray(id); -} - -void VAO::unbind() -{ - glBindVertexArray(0); -} - -void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type, GLsizei stride, const void* offset) -{ - bind(); - glEnableVertexAttribArray(index); - glVertexAttribPointer(index, size, type, GL_FALSE, stride, offset); -} - -void VAO::drawElement(GLenum mode, GLsizei count, GLenum type, const void* indices) -{ - bind(); - glDrawElements(mode, count, type, indices); + bind(); + glDrawElements(mode, count, type, indices); } void VAO::drawArray(GLenum mode, GLint first, GLsizei count) { - bind(); - glDrawArrays(mode, first, count); + bind(); + glDrawArrays(mode, first, count); } \ No newline at end of file diff --git a/src/VBO.cpp b/src/VBO.cpp index f87cfeb..cec3b3a 100755 --- a/src/VBO.cpp +++ b/src/VBO.cpp @@ -1,23 +1,14 @@ -#include "VBO.hpp" +#include "vbo.hpp" VBO::VBO(GLfloat* vertices, size_t size) { - glGenBuffers(1, &id); - glBindBuffer(GL_ARRAY_BUFFER, id); - glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW); + glGenBuffers(1, &id); + glBindBuffer(GL_ARRAY_BUFFER, id); + glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW); } -VBO::~VBO() -{ - glDeleteBuffers(1, &id); -} +VBO::~VBO() { glDeleteBuffers(1, &id); } -void VBO::bind() -{ - glBindBuffer(GL_ARRAY_BUFFER, id); -} +void VBO::bind() { glBindBuffer(GL_ARRAY_BUFFER, id); } -void VBO::unbind() -{ - glBindBuffer(GL_ARRAY_BUFFER, 0); -} \ No newline at end of file +void VBO::unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); } \ No newline at end of file diff --git a/src/camera.cpp b/src/camera.cpp index 0bece0e..7f6ebd6 100755 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -1,5 +1,8 @@ #include "camera.hpp" +#include +#include + Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity) : screenWidth(width), screenHeight(height), @@ -14,86 +17,87 @@ Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity) worldUp(0.0f, 1.0f, 0.0f), firstMouse(true) { - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + updateCameraVectors(); } void Camera::update(float deltaTime) { - processInput(deltaTime); - processMouseMovement(); - updateCameraVectors(); + processInput(deltaTime); + processMouseMovement(); + updateCameraVectors(); } void Camera::processInput(float deltaTime) { - float velocity = speed * deltaTime; + float velocity = speed * deltaTime; - if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) - cameraPosition += cameraFront * velocity; + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) + cameraPosition += cameraFront * velocity; - if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) - cameraPosition -= cameraFront * velocity; + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) + cameraPosition -= cameraFront * velocity; - if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) - cameraPosition += cameraRight * velocity; + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) + cameraPosition += cameraRight * velocity; - if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) - cameraPosition -= cameraRight * velocity; + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) + cameraPosition -= cameraRight * velocity; - if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) - cameraPosition += cameraUp * velocity; + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) + cameraPosition += cameraUp * velocity; - if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) - cameraPosition -= cameraUp * velocity; + if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) + cameraPosition -= cameraUp * velocity; - // Zoom - if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) - fov = 35.0f; - else - fov = 45.0f; + // Zoom + fov = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) + ? 35.f + : 45.f; } void Camera::processMouseMovement() { - double mouseX, mouseY; - glfwGetCursorPos(window, &mouseX, &mouseY); - - if (firstMouse) { - mousePosX = mouseX; - mousePosY = mouseY; - firstMouse = false; - return; - } - - float deltaX = static_cast(mouseX - mousePosX); - float deltaY = static_cast(mousePosY - mouseY); + double mouseX, mouseY; + glfwGetCursorPos(window, &mouseX, &mouseY); + if (firstMouse) + { mousePosX = mouseX; mousePosY = mouseY; + firstMouse = false; + return; + } - deltaX *= cameraSensitivity; - deltaY *= cameraSensitivity; + float deltaX = static_cast(mouseX - mousePosX); + float deltaY = static_cast(mousePosY - mouseY); - cameraYaw += deltaX; - cameraPitch += deltaY; + mousePosX = mouseX; + mousePosY = mouseY; - if (cameraPitch > 89.0f) cameraPitch = 89.0f; - if (cameraPitch < -89.0f) cameraPitch = -89.0f; + deltaX *= cameraSensitivity; + deltaY *= cameraSensitivity; + + cameraYaw += deltaX; + cameraPitch += deltaY; + + if (cameraPitch > 89.0f) cameraPitch = 89.0f; + if (cameraPitch < -89.0f) cameraPitch = -89.0f; } void Camera::updateCameraVectors() { - glm::vec3 front; - front.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch)); - front.y = sin(glm::radians(cameraPitch)); - front.z = sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch)); - cameraFront = glm::normalize(front); + glm::vec3 front; + front.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch)); + front.y = sin(glm::radians(cameraPitch)); + front.z = sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch)); + cameraFront = glm::normalize(front); - cameraRight = glm::normalize(glm::cross(cameraFront, worldUp)); - cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront)); + cameraRight = glm::normalize(glm::cross(cameraFront, worldUp)); + cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront)); } glm::mat4 Camera::getViewMatrix() { - return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp); -} \ No newline at end of file + return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp); +} diff --git a/src/cube.cpp b/src/cube.cpp index 5f57b48..51ae6f5 100644 --- a/src/cube.cpp +++ b/src/cube.cpp @@ -1,5 +1,7 @@ #include "cube.hpp" -#include "VAO.hpp" + +#include "glm/gtc/type_ptr.hpp" +#include "vao.hpp" #include "vbo.hpp" GLfloat cubeVertices[] = { @@ -124,21 +126,21 @@ const char* cubeFragShader = R"( } )"; -Cube::Cube(Camera &camera) : - vbo(cubeVertices, sizeof(cubeVertices)), - ebo(cubeIndices, sizeof(cubeIndices)), - texture("stone.png"), - camera(camera), - shader(cubeVertexShader, cubeFragShader) +Cube::Cube(Camera& camera) + : vbo(cubeVertices, sizeof(cubeVertices)), + ebo(cubeIndices, sizeof(cubeIndices)), + texture("stone.png"), + camera(camera), + shader(cubeVertexShader, cubeFragShader) { vao.bind(); vbo.bind(); - GLsizei stride = 8*sizeof(float); + GLsizei stride = 8 * sizeof(float); vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void*)0); - vao.setAttributePointer(1, 3, GL_FLOAT, stride, (void*)(3*sizeof(float))); - vao.setAttributePointer(2, 2, GL_FLOAT, stride, (void*)(6*sizeof(float))); + vao.setAttributePointer(1, 3, GL_FLOAT, stride, (void*)(3 * sizeof(float))); + vao.setAttributePointer(2, 2, GL_FLOAT, stride, (void*)(6 * sizeof(float))); ebo.bind(); } @@ -148,13 +150,10 @@ void Cube::loop(int width, int height) shader.use(); glActiveTexture(GL_TEXTURE0); - glm::vec3 coordinate = glm::vec3(0.0f, -1.0f, 0.0f); + glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f); glm::mat4 projection = glm::perspective( - glm::radians(camera.fov), - static_cast(width) / static_cast(height), - 0.1f, - 100.0f - ); + glm::radians(camera.fov), + static_cast(width) / static_cast(height), 0.1f, 100.0f); GLint texLoc = glGetUniformLocation(shader.getProgram(), "material.diffuse"); glUniform1i(texLoc, 0); @@ -163,16 +162,13 @@ void Cube::loop(int width, int height) glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); GLint viewLoc = glGetUniformLocation(shader.getProgram(), "view"); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(camera.getViewMatrix())); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, + glm::value_ptr(camera.getViewMatrix())); glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate); GLint modelLoc = glGetUniformLocation(shader.getProgram(), "model"); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - vao.drawElement( - GL_TRIANGLES, - sizeof(cubeIndices) / sizeof(unsigned int), - GL_UNSIGNED_INT, - 0 - ); + vao.drawElement(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(unsigned int), + GL_UNSIGNED_INT, 0); } \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp deleted file mode 100644 index 269b072..0000000 --- a/src/game.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "GL/glew.h" -#include "game.hpp" -#include "GLFW/glfw3.h" -#include -#include - - -Game::Game(int width, int height, string name) : - width(width), - height(height), - name(name.c_str()) -{ - if(glfwInit() == GLFW_FALSE) - std::cerr << "error glfw" << std::endl; - - this->window = glfwCreateWindow( - this->width, - this->height, - this->name, - nullptr, - nullptr - ); - - // OpenGL 3.3 - glfwWindowHint(GLFW_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_VERSION_MINOR, 3); - - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - glfwWindowHint(GLFW_DEPTH_BITS, 24); - glfwWindowHint(GLFW_STENCIL_BITS, 8); - - glfwWindowHint(GLFW_SAMPLES, 4); - glfwMakeContextCurrent(this->window); - - glewInit(); - - glEnable(GL_DEPTH_TEST); - glEnable(GL_MULTISAMPLE); -} - -void Game::run(bool (*func)(Game *g)) -{ - while(func(this)) - { - glfwGetWindowSize(this->window, &this->width, &this->height); - glViewport(0, 0, this->width, this->height); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glClearColor(0.5f, 0.2f, 0.2f, 1.0f); - - glDisable(GL_MULTISAMPLE); - glfwPollEvents(); - glfwSwapBuffers(this->window); - - GLenum error = glGetError(); - if (error != glGetError()) - { - std::cerr << error << std::endl; - exit(1); - } - } -} - -Game::~Game() -{ - glfwTerminate(); -} - -GLFWwindow *Game::getWindow() -{ - return this->window; -} -const char *Game::getName() -{ - return this->name; -} - -int Game::getWidth() -{ - return this->height; -} - -int Game::getHeight() -{ - return this->width; -} diff --git a/src/main.cpp b/src/main.cpp index 1e92858..5514bcd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,16 +1,98 @@ -#define STB_IMAGE_IMPLEMENTATION -#include "stb_image.h" +#include +#include -#include "game.hpp" +#include +#include + +#include "camera.hpp" +#include "cube.hpp" + +static void glfwErrorCallback(int error, const char *description) +{ + std::cerr << "GLFW Error " << error << ": " << description << std::endl; +} int main() { - Game game(800, 600, "game"); + const char *title = "window"; + int width = 800, height = 600; - auto quit = [](Game *g) { - return !glfwWindowShouldClose(g->getWindow()); - }; + glfwSetErrorCallback(glfwErrorCallback); - game.run(quit); - return 0; -} \ No newline at end of file + if (glfwInit() == GLFW_FALSE) + { + std::cerr << "ERROR(GLFW): initialisation error" << std::endl; + exit(EXIT_FAILURE); + } + + // OpenGL 3.3 + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_DEPTH_BITS, 24); + glfwWindowHint(GLFW_STENCIL_BITS, 8); + glfwWindowHint(GLFW_SAMPLES, 4); +#ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + + GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr); + if (window == nullptr) + { + std::cerr + << "ERROR: Failed to create GLFW window with requested GL version." + << std::endl; + glfwTerminate(); + exit(EXIT_FAILURE); + } + + glfwMakeContextCurrent(window); + + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) + { + std::cerr << "ERROR: Failed to initialize GLAD" << std::endl; + glfwDestroyWindow(window); + glfwTerminate(); + exit(EXIT_FAILURE); + } + + glfwSwapInterval(1); + + Camera camera(width, height, window, 0.1f); + Cube cube{camera}; + + glEnable(GL_DEPTH_TEST); + glEnable(GL_MULTISAMPLE); + + double deltaTime = 0.f; + double lastTime = glfwGetTime(); + while (!glfwWindowShouldClose(window)) + { + glfwGetFramebufferSize(window, &width, &height); + glViewport(0, 0, width, height); + + glClearColor(0.5f, 0.2f, 0.2f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + double currentTime = glfwGetTime(); + deltaTime = currentTime - lastTime; + lastTime = currentTime; + + camera.update(deltaTime); + cube.loop(width, height); + + glfwPollEvents(); + glfwSwapBuffers(window); + + GLenum error = glGetError(); + if (error != GL_NO_ERROR) + { + std::cerr << error << std::endl; + // exit(1); + } + } + + glfwDestroyWindow(window); + glfwTerminate(); + return 0; +} diff --git a/src/shader.cpp b/src/shader.cpp index fa9dd71..4222030 100755 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -2,51 +2,42 @@ Shader::Shader(const char* vertexShaderSource, const char* fragmentShaderSource) { - vertexShader = glCreateShader(GL_VERTEX_SHADER); - fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - shaderProgram = glCreateProgram(); + vertexShader = glCreateShader(GL_VERTEX_SHADER); + fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + shaderProgram = glCreateProgram(); - addVertShader(vertexShaderSource); - addFragShader(fragmentShaderSource); + addVertShader(vertexShaderSource); + addFragShader(fragmentShaderSource); - compileInProgram(); + compileInProgram(); } void Shader::addVertShader(const char* vertexShaderSource) { - glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr); - glCompileShader(vertexShader); + glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr); + glCompileShader(vertexShader); } void Shader::addFragShader(const char* fragmentShaderSource) { - glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr); - glCompileShader(fragmentShader); + glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr); + glCompileShader(fragmentShader); } void Shader::compileInProgram() { - glAttachShader(shaderProgram, vertexShader); - glAttachShader(shaderProgram, fragmentShader); - glLinkProgram(shaderProgram); + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); - glDetachShader(shaderProgram, vertexShader); - glDetachShader(shaderProgram, fragmentShader); - glDeleteShader(vertexShader); - glDeleteShader(fragmentShader); + glDetachShader(shaderProgram, vertexShader); + glDetachShader(shaderProgram, fragmentShader); + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); } -Shader::~Shader() -{ - glDeleteProgram(shaderProgram); -} +Shader::~Shader() { glDeleteProgram(shaderProgram); } -void Shader::use() const -{ - glUseProgram(shaderProgram); -} +void Shader::use() const { glUseProgram(shaderProgram); } -GLuint Shader::getProgram() const -{ - return shaderProgram; -} \ No newline at end of file +GLuint Shader::getProgram() const { return shaderProgram; } \ No newline at end of file diff --git a/src/texture.cpp b/src/texture.cpp index 092ecf0..4ceb7ec 100755 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,6 +1,7 @@ #include "texture.hpp" -#include + #include +#include #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" @@ -9,60 +10,56 @@ namespace fs = std::filesystem; Texture::Texture(const std::string& filename) { - fs::path path = fs::absolute(fs::current_path() / "res" / filename); + fs::path path = fs::absolute(fs::current_path() / "res" / filename); - int maxTextureSize; - glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); + int maxTextureSize; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); - glGenTextures(1, &id); - glBindTexture(GL_TEXTURE_2D, id); + glGenTextures(1, &id); + glBindTexture(GL_TEXTURE_2D, id); - 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_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - int imgW, imgH, numColCh; - stbi_set_flip_vertically_on_load(true); - unsigned char* data = stbi_load(path.string().c_str(), &imgW, &imgH, &numColCh, 0); + int imgW, imgH, numColCh; + stbi_set_flip_vertically_on_load(true); + unsigned char* data = + stbi_load(path.string().c_str(), &imgW, &imgH, &numColCh, 0); - if (!data) - { - std::cerr << "Error loading image: " << path << std::endl; - return; - } + if (!data) + { + std::cerr << "Error loading image: " << path << std::endl; + return; + } - int loadedTextureSize = imgW * imgH; - - if (loadedTextureSize >= maxTextureSize) - { - std::cerr << "Error: max texture size is " << maxTextureSize - << " pixels, but image size = " << loadedTextureSize << std::endl; - stbi_image_free(data); - return; - } - - GLenum format = (numColCh == 4) ? GL_RGBA : GL_RGB; - - glTexImage2D(GL_TEXTURE_2D, 0, format, imgW, imgH, 0, format, GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); + int loadedTextureSize = imgW * imgH; + if (loadedTextureSize >= maxTextureSize) + { + std::cerr << "Error: max texture size is " << maxTextureSize + << " pixels, but image size = " << loadedTextureSize << std::endl; stbi_image_free(data); + return; + } + + GLenum format = (numColCh == 4) ? GL_RGBA : GL_RGB; + + glTexImage2D(GL_TEXTURE_2D, 0, format, imgW, imgH, 0, format, + GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + + stbi_image_free(data); } -Texture::~Texture() -{ - glDeleteTextures(1, &id); -} +Texture::~Texture() { glDeleteTextures(1, &id); } void Texture::bind(GLenum textureUnit) const { - glActiveTexture(textureUnit); - glBindTexture(GL_TEXTURE_2D, id); + glActiveTexture(textureUnit); + glBindTexture(GL_TEXTURE_2D, id); } -unsigned int Texture::getID() const -{ - return id; -} \ No newline at end of file +unsigned int Texture::getID() const { return id; } \ No newline at end of file