feat: rework few fonctions and change glew to glad

- game files deleted
This commit is contained in:
2025-12-06 14:53:19 +01:00
parent 71ad3a034c
commit d2942242b4
21 changed files with 355 additions and 422 deletions

6
.gitmodules vendored
View File

@@ -1,6 +1,3 @@
[submodule "lib/glew"]
path = lib/glew
url = https://github.com/edoren/glew
[submodule "lib/glm"] [submodule "lib/glm"]
path = lib/glm path = lib/glm
url = https://github.com/g-truc/glm url = https://github.com/g-truc/glm
@@ -10,3 +7,6 @@
[submodule "lib/stb"] [submodule "lib/stb"]
path = lib/stb path = lib/stb
url = https://github.com/nothings/stb.git url = https://github.com/nothings/stb.git
[submodule "lib/glad"]
path = lib/glad
url = https://github.com/DawidLokiec/glad.git

View File

@@ -6,6 +6,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
file(GLOB_RECURSE CPP_SOURCES CONFIGURE_DEPENDS src/*.cpp) file(GLOB_RECURSE CPP_SOURCES CONFIGURE_DEPENDS src/*.cpp)
file(GLOB_RECURSE CPP_HEADERS CONFIGURE_DEPENDS src/*.hpp) 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}) add_executable(main ${CPP_SOURCES} ${CPP_HEADERS})
# GLFW # GLFW
@@ -14,12 +16,8 @@ set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
add_subdirectory(lib/glfw) add_subdirectory(lib/glfw)
# GLEW # GLAD
add_definitions(-DGLEW_STATIC) target_include_directories(main PRIVATE lib/glad/include)
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)
# GLM # GLM
target_include_directories(main PRIVATE lib/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_include_directories(main PRIVATE inc/)
target_link_libraries(main PRIVATE glfw glew) target_link_libraries(main PRIVATE glfw)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
target_link_libraries(main PRIVATE OpenGL::GL) target_link_libraries(main PRIVATE OpenGL::GL)

View File

@@ -1,7 +1,10 @@
#ifndef EBO_HPP #ifndef EBO_HPP
#define EBO_HPP #define EBO_HPP
#include <GL/glew.h> #include <stddef.h>
#include "glad/glad.h"
class EBO class EBO
{ {

View File

@@ -1,7 +1,9 @@
#ifndef VAO_HPP #ifndef VAO_HPP
#define VAO_HPP #define VAO_HPP
#include <gl/glew.h> #include <stddef.h>
#include "glad/glad.h"
class VAO class VAO
{ {
@@ -15,8 +17,10 @@ public:
void bind(); void bind();
void unbind(); void unbind();
void setAttributePointer(GLuint index, GLuint size, GLenum type, GLsizei stride, const void* offset); void setAttributePointer(GLuint index, GLuint size, GLenum type,
void drawElement(GLenum mode, GLsizei count, GLenum type, const void *indices); 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 drawArray(GLenum mode, GLint first, GLsizei count);
}; };

View File

@@ -1,7 +1,9 @@
#ifndef VBO_HPP #ifndef VBO_HPP
#define VBO_HPP #define VBO_HPP
#include <gl/glew.h> #include <stddef.h>
#include "glad/glad.h"
class VBO class VBO
{ {
@@ -14,7 +16,6 @@ public:
void bind(); void bind();
void unbind(); void unbind();
}; };
#endif #endif

View File

@@ -1,11 +1,13 @@
#ifndef CAMERA_HPP #ifndef CAMERA_HPP
#define CAMERA_HPP #define CAMERA_HPP
#include <glfw/glfw3.h> #include "glm/glm.hpp"
#include <glm/glm.hpp> #include "glm/gtc/matrix_transform.hpp"
#include <glm/gtc/matrix_transform.hpp>
class Camera { struct GLFWwindow;
class Camera
{
private: private:
int screenWidth; int screenWidth;
int screenHeight; int screenHeight;

View File

@@ -1,12 +1,14 @@
#ifndef CUBE_HPP #ifndef CUBE_HPP
#define CUBE_HPP #define CUBE_HPP
#include "vbo.hpp" #include <stddef.h>
#include "camera.hpp"
#include "ebo.hpp" #include "ebo.hpp"
#include "vao.hpp"
#include "shader.hpp" #include "shader.hpp"
#include "texture.hpp" #include "texture.hpp"
#include "camera.hpp" #include "vao.hpp"
#include "vbo.hpp"
class Cube class Cube
{ {

View File

@@ -1,29 +0,0 @@
#ifndef GAME_HPP
#define GAME_HPP
#include "GLFW/glfw3.h"
#include <string>
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

View File

@@ -1,10 +1,7 @@
#ifndef SHADER_HPP #ifndef SHADER_HPP
#define SHADER_HPP #define SHADER_HPP
#include <gl/glew.h> #include "glad/glad.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
class Shader class Shader
{ {

View File

@@ -1,7 +1,7 @@
#ifndef TEXTUTE_HPP #ifndef TEXTUTE_HPP
#define TEXTUTE_HPP #define TEXTUTE_HPP
#include <GL/glew.h> #include "glad/glad.h"
#include <string> #include <string>
class Texture class Texture

1
lib/glad Submodule

Submodule lib/glad added at 379a9432ec

Submodule lib/glew deleted from 5b995cab14

View File

@@ -1,4 +1,4 @@
#include "EBO.hpp" #include "ebo.hpp"
EBO::EBO(unsigned int* indices, size_t size) EBO::EBO(unsigned int* indices, size_t size)
{ {
@@ -7,17 +7,8 @@ EBO::EBO(unsigned int* indices, size_t size)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
} }
EBO::~EBO() EBO::~EBO() { glDeleteBuffers(1, &id); }
{
glDeleteBuffers(1, &id);
}
void EBO::bind() void EBO::bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); }
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
}
void EBO::unbind() void EBO::unbind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

View File

@@ -1,33 +1,23 @@
#include "VAO.hpp" #include "vao.hpp"
VAO::VAO() VAO::VAO() { glGenVertexArrays(1, &id); }
{
glGenVertexArrays(1, &id);
}
VAO::~VAO() VAO::~VAO() { glDeleteVertexArrays(1, &id); }
{
glDeleteVertexArrays(1, &id);
}
void VAO::bind() void VAO::bind() { glBindVertexArray(id); }
{
glBindVertexArray(id);
}
void VAO::unbind() void VAO::unbind() { glBindVertexArray(0); }
{
glBindVertexArray(0);
}
void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type, GLsizei stride, const void* offset) void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type,
GLsizei stride, const void* offset)
{ {
bind(); bind();
glEnableVertexAttribArray(index); glEnableVertexAttribArray(index);
glVertexAttribPointer(index, size, type, GL_FALSE, stride, offset); glVertexAttribPointer(index, size, type, GL_FALSE, stride, offset);
} }
void VAO::drawElement(GLenum mode, GLsizei count, GLenum type, const void* indices) void VAO::drawElement(GLenum mode, GLsizei count, GLenum type,
const void* indices)
{ {
bind(); bind();
glDrawElements(mode, count, type, indices); glDrawElements(mode, count, type, indices);

View File

@@ -1,4 +1,4 @@
#include "VBO.hpp" #include "vbo.hpp"
VBO::VBO(GLfloat* vertices, size_t size) VBO::VBO(GLfloat* vertices, size_t size)
{ {
@@ -7,17 +7,8 @@ VBO::VBO(GLfloat* vertices, size_t size)
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
} }
VBO::~VBO() VBO::~VBO() { glDeleteBuffers(1, &id); }
{
glDeleteBuffers(1, &id);
}
void VBO::bind() void VBO::bind() { glBindBuffer(GL_ARRAY_BUFFER, id); }
{
glBindBuffer(GL_ARRAY_BUFFER, id);
}
void VBO::unbind() void VBO::unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); }
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

View File

@@ -1,5 +1,8 @@
#include "camera.hpp" #include "camera.hpp"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity) Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
: screenWidth(width), : screenWidth(width),
screenHeight(height), screenHeight(height),
@@ -15,6 +18,7 @@ Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
firstMouse(true) firstMouse(true)
{ {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
updateCameraVectors();
} }
void Camera::update(float deltaTime) void Camera::update(float deltaTime)
@@ -47,10 +51,9 @@ void Camera::processInput(float deltaTime)
cameraPosition -= cameraUp * velocity; cameraPosition -= cameraUp * velocity;
// Zoom // Zoom
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) fov = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
fov = 35.0f; ? 35.f
else : 45.f;
fov = 45.0f;
} }
void Camera::processMouseMovement() void Camera::processMouseMovement()
@@ -58,7 +61,8 @@ void Camera::processMouseMovement()
double mouseX, mouseY; double mouseX, mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY); glfwGetCursorPos(window, &mouseX, &mouseY);
if (firstMouse) { if (firstMouse)
{
mousePosX = mouseX; mousePosX = mouseX;
mousePosY = mouseY; mousePosY = mouseY;
firstMouse = false; firstMouse = false;

View File

@@ -1,5 +1,7 @@
#include "cube.hpp" #include "cube.hpp"
#include "VAO.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "vao.hpp"
#include "vbo.hpp" #include "vbo.hpp"
GLfloat cubeVertices[] = { GLfloat cubeVertices[] = {
@@ -124,8 +126,8 @@ const char* cubeFragShader = R"(
} }
)"; )";
Cube::Cube(Camera &camera) : Cube::Cube(Camera& camera)
vbo(cubeVertices, sizeof(cubeVertices)), : vbo(cubeVertices, sizeof(cubeVertices)),
ebo(cubeIndices, sizeof(cubeIndices)), ebo(cubeIndices, sizeof(cubeIndices)),
texture("stone.png"), texture("stone.png"),
camera(camera), camera(camera),
@@ -148,13 +150,10 @@ void Cube::loop(int width, int height)
shader.use(); shader.use();
glActiveTexture(GL_TEXTURE0); 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::mat4 projection = glm::perspective(
glm::radians(camera.fov), glm::radians(camera.fov),
static_cast<float>(width) / static_cast<float>(height), static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.0f);
0.1f,
100.0f
);
GLint texLoc = glGetUniformLocation(shader.getProgram(), "material.diffuse"); GLint texLoc = glGetUniformLocation(shader.getProgram(), "material.diffuse");
glUniform1i(texLoc, 0); glUniform1i(texLoc, 0);
@@ -163,16 +162,13 @@ void Cube::loop(int width, int height)
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
GLint viewLoc = glGetUniformLocation(shader.getProgram(), "view"); 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); glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate);
GLint modelLoc = glGetUniformLocation(shader.getProgram(), "model"); GLint modelLoc = glGetUniformLocation(shader.getProgram(), "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
vao.drawElement( vao.drawElement(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(unsigned int),
GL_TRIANGLES, GL_UNSIGNED_INT, 0);
sizeof(cubeIndices) / sizeof(unsigned int),
GL_UNSIGNED_INT,
0
);
} }

View File

@@ -1,87 +0,0 @@
#include "GL/glew.h"
#include "game.hpp"
#include "GLFW/glfw3.h"
#include <cstdlib>
#include <iostream>
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;
}

View File

@@ -1,16 +1,98 @@
#define STB_IMAGE_IMPLEMENTATION #include <glad/glad.h>
#include "stb_image.h" #include <GLFW/glfw3.h>
#include "game.hpp" #include <cstdlib>
#include <iostream>
#include "camera.hpp"
#include "cube.hpp"
static void glfwErrorCallback(int error, const char *description)
{
std::cerr << "GLFW Error " << error << ": " << description << std::endl;
}
int main() int main()
{ {
Game game(800, 600, "game"); const char *title = "window";
int width = 800, height = 600;
auto quit = [](Game *g) { glfwSetErrorCallback(glfwErrorCallback);
return !glfwWindowShouldClose(g->getWindow());
};
game.run(quit); 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; return 0;
} }

View File

@@ -36,17 +36,8 @@ void Shader::compileInProgram()
glDeleteShader(fragmentShader); glDeleteShader(fragmentShader);
} }
Shader::~Shader() Shader::~Shader() { glDeleteProgram(shaderProgram); }
{
glDeleteProgram(shaderProgram);
}
void Shader::use() const void Shader::use() const { glUseProgram(shaderProgram); }
{
glUseProgram(shaderProgram);
}
GLuint Shader::getProgram() const GLuint Shader::getProgram() const { return shaderProgram; }
{
return shaderProgram;
}

View File

@@ -1,6 +1,7 @@
#include "texture.hpp" #include "texture.hpp"
#include <iostream>
#include <filesystem> #include <filesystem>
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
@@ -25,7 +26,8 @@ Texture::Texture(const std::string& filename)
int imgW, imgH, numColCh; int imgW, imgH, numColCh;
stbi_set_flip_vertically_on_load(true); stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load(path.string().c_str(), &imgW, &imgH, &numColCh, 0); unsigned char* data =
stbi_load(path.string().c_str(), &imgW, &imgH, &numColCh, 0);
if (!data) if (!data)
{ {
@@ -45,16 +47,14 @@ Texture::Texture(const std::string& filename)
GLenum format = (numColCh == 4) ? GL_RGBA : GL_RGB; GLenum format = (numColCh == 4) ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, format, imgW, imgH, 0, format, GL_UNSIGNED_BYTE, data); glTexImage2D(GL_TEXTURE_2D, 0, format, imgW, imgH, 0, format,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data); stbi_image_free(data);
} }
Texture::~Texture() Texture::~Texture() { glDeleteTextures(1, &id); }
{
glDeleteTextures(1, &id);
}
void Texture::bind(GLenum textureUnit) const void Texture::bind(GLenum textureUnit) const
{ {
@@ -62,7 +62,4 @@ void Texture::bind(GLenum textureUnit) const
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
} }
unsigned int Texture::getID() const unsigned int Texture::getID() const { return id; }
{
return id;
}