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"]
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

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_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)

View File

@@ -1,14 +1,17 @@
#ifndef EBO_HPP
#define EBO_HPP
#include <GL/glew.h>
#include <stddef.h>
#include "glad/glad.h"
class EBO
{
private:
private:
GLuint id;
public:
public:
EBO(unsigned int* indices, size_t size);
~EBO();

View File

@@ -1,22 +1,26 @@
#ifndef VAO_HPP
#define VAO_HPP
#include <gl/glew.h>
#include <stddef.h>
#include "glad/glad.h"
class VAO
{
private:
private:
unsigned int id;
public:
public:
VAO();
~VAO();
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 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);
};

View File

@@ -1,20 +1,21 @@
#ifndef VBO_HPP
#define VBO_HPP
#include <gl/glew.h>
#include <stddef.h>
#include "glad/glad.h"
class VBO
{
private:
private:
unsigned int id;
public:
public:
VBO(GLfloat* vertices, size_t size);
~VBO();
void bind();
void unbind();
};
#endif

View File

@@ -1,12 +1,14 @@
#ifndef CAMERA_HPP
#define CAMERA_HPP
#include <glfw/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
class Camera {
private:
struct GLFWwindow;
class Camera
{
private:
int screenWidth;
int screenHeight;
@@ -28,7 +30,7 @@ private:
void processMouseMovement();
void updateCameraVectors();
public:
public:
Camera(int width, int height, GLFWwindow* window, float sensitivity);
void update(float deltaTime);

View File

@@ -1,16 +1,18 @@
#ifndef CUBE_HPP
#define CUBE_HPP
#include "vbo.hpp"
#include <stddef.h>
#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);
};

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,14 +1,11 @@
#ifndef SHADER_HPP
#define SHADER_HPP
#include <gl/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "glad/glad.h"
class Shader
{
private:
private:
GLuint vertexShader;
GLuint fragmentShader;
GLuint shaderProgram;
@@ -17,7 +14,7 @@ private:
void addFragShader(const char* fragmentShaderSource);
void compileInProgram();
public:
public:
Shader(const char* vertexShaderSource, const char* fragmentShaderSource);
~Shader();

View File

@@ -1,7 +1,7 @@
#ifndef TEXTUTE_HPP
#define TEXTUTE_HPP
#include <GL/glew.h>
#include "glad/glad.h"
#include <string>
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)
{
@@ -7,17 +7,8 @@ EBO::EBO(unsigned int* indices, size_t size)
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);
}
void EBO::unbind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }

View File

@@ -1,33 +1,23 @@
#include "VAO.hpp"
#include "vao.hpp"
VAO::VAO()
{
glGenVertexArrays(1, &id);
}
VAO::VAO() { glGenVertexArrays(1, &id); }
VAO::~VAO()
{
glDeleteVertexArrays(1, &id);
}
VAO::~VAO() { glDeleteVertexArrays(1, &id); }
void VAO::bind()
{
glBindVertexArray(id);
}
void VAO::bind() { glBindVertexArray(id); }
void VAO::unbind()
{
glBindVertexArray(0);
}
void VAO::unbind() { 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();
glEnableVertexAttribArray(index);
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();
glDrawElements(mode, count, type, indices);

View File

@@ -1,4 +1,4 @@
#include "VBO.hpp"
#include "vbo.hpp"
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);
}
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);
}
void VBO::unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); }

View File

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

View File

@@ -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,8 +126,8 @@ const char* cubeFragShader = R"(
}
)";
Cube::Cube(Camera &camera) :
vbo(cubeVertices, sizeof(cubeVertices)),
Cube::Cube(Camera& camera)
: vbo(cubeVertices, sizeof(cubeVertices)),
ebo(cubeIndices, sizeof(cubeIndices)),
texture("stone.png"),
camera(camera),
@@ -134,11 +136,11 @@ Cube::Cube(Camera &camera) :
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<float>(width) / static_cast<float>(height),
0.1f,
100.0f
);
static_cast<float>(width) / static_cast<float>(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);
}

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 "stb_image.h"
#include <glad/glad.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()
{
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);
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;
}

View File

@@ -36,17 +36,8 @@ void Shader::compileInProgram()
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;
}
GLuint Shader::getProgram() const { return shaderProgram; }

View File

@@ -1,6 +1,7 @@
#include "texture.hpp"
#include <iostream>
#include <filesystem>
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
@@ -25,7 +26,8 @@ Texture::Texture(const std::string& filename)
int imgW, imgH, numColCh;
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)
{
@@ -45,16 +47,14 @@ Texture::Texture(const std::string& filename)
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);
stbi_image_free(data);
}
Texture::~Texture()
{
glDeleteTextures(1, &id);
}
Texture::~Texture() { glDeleteTextures(1, &id); }
void Texture::bind(GLenum textureUnit) const
{
@@ -62,7 +62,4 @@ void Texture::bind(GLenum textureUnit) const
glBindTexture(GL_TEXTURE_2D, id);
}
unsigned int Texture::getID() const
{
return id;
}
unsigned int Texture::getID() const { return id; }