feat: triangle is perfectly draw

This commit is contained in:
2026-01-07 19:52:30 +01:00
parent 3c7e931a89
commit 8dd67e5ca9
10 changed files with 277 additions and 136 deletions

View File

@@ -61,7 +61,7 @@ add_dependencies(main Shaders)
target_include_directories(main PRIVATE target_include_directories(main PRIVATE
inc # project inc # project
res/inc # ressources (primitive) res/render # ressources
lib/glad/include # glad lib/glad/include # glad
lib/glfw/include # glfw lib/glfw/include # glfw
lib/glm # glm lib/glm # glm

View File

@@ -76,10 +76,10 @@ constexpr const std::size_t P_CUBE_INDICE_LEN =
extern "C" extern "C"
{ {
extern const unsigned char CUBE_VERT[]; extern const unsigned char P_CUBE_VERT[];
extern const unsigned int CUBE_VERT_LEN; extern const unsigned int P_CUBE_VERT_LEN;
extern const unsigned char CUBE_FRAG[]; extern const unsigned char P_CUBE_FRAG[];
extern const unsigned int CUBE_FRAG_LEN; extern const unsigned int P_CUBE_FRAG_LEN;
} }
} // namespace mesh } // namespace mesh

View File

@@ -0,0 +1,8 @@
#version 330 core
out vec4 color;
void main()
{
color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

View File

@@ -0,0 +1,37 @@
#ifndef P_TRIANGLE_HPP
#define P_TRIANGLE_HPP
#include <cstddef>
#include "GLFW/glfw3.h"
namespace mesh
{
// clang-format off
constexpr GLfloat P_TRIANGLE_VERTICE[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
// clang-format on
constexpr const std::size_t P_TRIANGLE_VERTICE_LEN =
sizeof(P_TRIANGLE_VERTICE) / sizeof(GLfloat);
constexpr unsigned int P_TRIANGLE_INDICE[] = {
0, 1, 2, // 1
};
constexpr const std::size_t P_TRIANGLE_INDICE_LEN =
sizeof(P_TRIANGLE_VERTICE) / sizeof(unsigned int);
extern "C"
{
extern const unsigned char P_TRIANGLE_VERT[];
extern const unsigned int P_TRIANGLE_VERT_LEN;
extern const unsigned char P_TRIANGLE_FRAG[];
extern const unsigned int P_TRIANGLE_FRAG_LEN;
}
} // namespace mesh
#endif

View File

@@ -0,0 +1,7 @@
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos , 1.0);
}

View File

@@ -1,6 +1,5 @@
#include "camera.hpp" #include "core/camera.hpp"
#include "glad/glad.h"
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "glm/ext/matrix_transform.hpp" #include "glm/ext/matrix_transform.hpp"

View File

@@ -1,4 +1,4 @@
#include "texture.hpp" #include "core/texture.hpp"
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>

View File

@@ -1,64 +1,65 @@
#include "res/inc/cube.hpp" // #include "primitives/cube.hpp"
#include "ebo.hpp" // #include "core/vbo.hpp"
#include "glm/ext/matrix_clip_space.hpp" // #include "ebo.hpp"
#include "glm/ext/matrix_transform.hpp" // #include "glm/ext/matrix_clip_space.hpp"
#include "glm/gtc/type_ptr.hpp" // #include "glm/ext/matrix_transform.hpp"
#include "p_cube.hpp" // #include "glm/gtc/type_ptr.hpp"
#include "vao.hpp" // #include "p_cube.hpp"
#include "vbo.hpp" // #include "vao.hpp"
// #include "vbo.hpp"
static GLsizei stride = 8 * sizeof(float); // static GLsizei stride = 8 * sizeof(float);
Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture) // Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture)
: Shape(camera, pos, Shader{}, Texture{texture}) // : Shape(camera, pos, Shader{}, Texture{texture})
{ // {
this->vao.bind(); // this->vao.bind();
this->vbo.bind(); // this->vbo.bind();
this->ebo.bind(); // this->ebo.bind();
this->vbo.setData(P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE)); // this->vbo.setData(P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE));
// positions // // positions
this->vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void *)(0)); // this->vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void *)(0));
// normales // // normales
this->vao.setAttributePointer(1, 3, GL_FLOAT, stride, // this->vao.setAttributePointer(1, 3, GL_FLOAT, stride,
(void *)(3 * sizeof(float))); // (void *)(3 * sizeof(float)));
// texture // // texture
this->vao.setAttributePointer(2, 2, GL_FLOAT, stride, // this->vao.setAttributePointer(2, 2, GL_FLOAT, stride,
(void *)(6 * sizeof(float))); // (void *)(6 * sizeof(float)));
this->ebo.setData(P_CUBE_INDICE, sizeof(P_CUBE_INDICE)); // this->ebo.setData(P_CUBE_INDICE, sizeof(P_CUBE_INDICE));
this->shader.compile((char *)P_CUBE_VERT, (char *)P_CUBE_FRAG); // this->shader.compile((char *)P_CUBE_VERT, (char *)P_CUBE_FRAG);
} // }
void Cube::render(int width, int height) // void Cube::render(int width, int height)
{ // {
shader.use(); // shader.use();
glActiveTexture(GL_TEXTURE0); // glActiveTexture(GL_TEXTURE0);
glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f); // glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f);
glm::mat4 projection = glm::perspective( // glm::mat4 projection = glm::perspective(
glm::radians(this->camera.getFov()), // glm::radians(this->camera.getFov()),
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 = // GLint texLoc =
glGetUniformLocation(shader.getShaderProgramID(), "material.diffuse"); // glGetUniformLocation(shader.getShaderProgramID(), "material.diffuse");
glUniform1i(texLoc, 0); // glUniform1i(texLoc, 0);
GLint projectionLoc = // GLint projectionLoc =
glGetUniformLocation(shader.getShaderProgramID(), "projection"); // glGetUniformLocation(shader.getShaderProgramID(), "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); // glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
GLint viewLoc = glGetUniformLocation(shader.getShaderProgramID(), "view"); // GLint viewLoc = glGetUniformLocation(shader.getShaderProgramID(), "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, // glUniformMatrix4fv(viewLoc, 1, GL_FALSE,
glm::value_ptr(camera.getViewMatrix())); // 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.getShaderProgramID(), "model"); // GLint modelLoc = glGetUniformLocation(shader.getShaderProgramID(), "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); // glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
vao.drawElement(GL_TRIANGLES, sizeof(P_CUBE_INDICE) / sizeof(unsigned int), // vao.drawElement(GL_TRIANGLES, sizeof(P_CUBE_INDICE) / sizeof(unsigned int),
GL_UNSIGNED_INT, 0); // GL_UNSIGNED_INT, 0);
} // }

View File

@@ -1,90 +1,83 @@
#include "glad/glad.h" // #include "glad/glad.h"
#include "GLFW/glfw3.h" // #include "GLFW/glfw3.h"
#include "game.hpp" // #include "game.hpp"
#include <time.h> // #include <time.h>
#include <cstdlib> // #include <cstdlib>
#include <format> // #include <format>
#include <iostream> // #include <iostream>
Game::Game(int width, int height, const char *window_name) // Game::Game(int width, int height, const char *window_name)
: width(width), height(height), WINDOW_NAME(window_name) // : width(width), height(height), WINDOW_NAME(window_name)
{ // {
glfwSetErrorCallback([](int error, const char *desc) // glfwSetErrorCallback([](int error, const char *desc)
{ Game::error("GLFW", error, desc); }); // { Game::error("GLFW", error, desc); });
if (glfwInit() == GLFW_FALSE) // if (glfwInit() == GLFW_FALSE)
{ // {
Game::error("GLFW", 0, "Failed to initialize GLFW."); // Game::error("GLFW", 0, "Failed to initialize GLFW.");
std::exit(EXIT_FAILURE); // std::exit(EXIT_FAILURE);
} // }
// OpenGL CORE 4.1 // // OpenGL CORE 4.1
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// window properties // // window properties
glfwWindowHint(GLFW_DEPTH_BITS, 24); // request a 24 bits depth buffer // glfwWindowHint(GLFW_DEPTH_BITS, 24); // request a 24 bits depth buffer
glfwWindowHint(GLFW_STENCIL_BITS, 8); // request an 8 bits stencil buffer // glfwWindowHint(GLFW_STENCIL_BITS, 8); // request an 8 bits stencil buffer
glfwWindowHint(GLFW_SAMPLES, 4); // activate MSAA (x4) // glfwWindowHint(GLFW_SAMPLES, 4); // activate MSAA (x4)
#ifdef __APPLE__ // disable deprecated functionalities on Apple devices // #ifdef __APPLE__ // disable deprecated functionalities on Apple devices
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif // #endif
this->window = glfwCreateWindow(width, height, WINDOW_NAME, nullptr, nullptr); // this->window = glfwCreateWindow(width, height, WINDOW_NAME, nullptr, nullptr);
if (window == NULL) // if (window == NULL)
{ // {
Game::error("GLFW", 0, "Failed to create GLFW window."); // Game::error("GLFW", 0, "Failed to create GLFW window.");
glfwTerminate(); // glfwTerminate();
std::exit(EXIT_FAILURE); // std::exit(EXIT_FAILURE);
} // }
glfwMakeContextCurrent(window); // glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) // if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{ // {
Game::error("GLAD", 0, "Failed to initialize GLAD."); // Game::error("GLAD", 0, "Failed to initialize GLAD.");
glfwDestroyWindow(window); // glfwDestroyWindow(window);
glfwTerminate(); // glfwTerminate();
std::exit(EXIT_FAILURE); // std::exit(EXIT_FAILURE);
} // }
glfwSwapInterval(1); // activate vsync // glfwSwapInterval(1); // activate vsync
glEnable(GL_DEPTH_TEST); // glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE); // glEnable(GL_MULTISAMPLE);
} // }
void Game::error(const char *type, int errortype, const char *desc) // void Game::run()
{ // {
std::cerr << type << "ERROR" // time.start();
<< ((errortype) ? std::format("({})", errortype) : "") << ": " // while (!glfwWindowShouldClose(window))
<< desc << std::endl; // {
} // glfwGetFramebufferSize(window, &width, &height);
// glfwGetWindowSize(window, &width, &height);
// glViewport(0, 0, width, height);
void Game::run() // glClearColor(0.5f, 0.2f, 0.2f, 1.0f);
{ // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
time.start();
while (!glfwWindowShouldClose(window))
{
glfwGetFramebufferSize(window, &width, &height);
glfwGetWindowSize(window, &width, &height);
glViewport(0, 0, width, height);
glClearColor(0.5f, 0.2f, 0.2f, 1.0f); // glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // glfwSwapBuffers(window);
glfwPollEvents(); // GLenum error = glGetError();
glfwSwapBuffers(window); // if (error != GL_NO_ERROR)
// {
// Game::error("OpenGL", error, "");
// std::exit(EXIT_FAILURE);
// }
GLenum error = glGetError(); // time.update();
if (error != GL_NO_ERROR) // }
{ // }
Game::error("OpenGL", error, "");
std::exit(EXIT_FAILURE);
}
time.update();
}
}

View File

@@ -1,9 +1,105 @@
#include "core/core.hpp" #include "core/core.hpp"
#include "core/logger.hpp" #include "core/ebo.hpp"
#include "core/shader.hpp"
#include "primitives/p_triangle.hpp"
int main() int main()
{ {
Game game{800, 600, "window"}; // Game game{800, 600, "window"};
game.run(); // game.run();
Time time{};
int width = 800;
int height = 600;
const char *WINDOW_NAME = "window";
glfwSetErrorCallback([](int error, const char *desc)
{ core::log::error(desc, error); });
if (glfwInit() == GLFW_FALSE)
{
core::log::error("Failed to initialize GLFW.");
std::exit(EXIT_FAILURE);
}
// OpenGL CORE 4.1
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// window properties
glfwWindowHint(GLFW_DEPTH_BITS, 24); // request a 24 bits depth buffer
glfwWindowHint(GLFW_STENCIL_BITS, 8); // request an 8 bits stencil buffer
glfwWindowHint(GLFW_SAMPLES, 4); // activate MSAA (x4)
#ifdef __APPLE__ // disable deprecated functionalities on Apple devices
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow *window =
glfwCreateWindow(width, height, WINDOW_NAME, nullptr, nullptr);
if (window == NULL)
{
core::log::error("Failed to create GLFW window.");
glfwTerminate();
std::exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
core::log::error("Failed to initialize GLAD.");
glfwDestroyWindow(window);
glfwTerminate();
std::exit(EXIT_FAILURE);
}
glfwSwapInterval(1); // activate vsync
// glEnable(GL_DEPTH_TEST);
// glEnable(GL_MULTISAMPLE);
core::VAO vao{};
core::VBO vbo{mesh::P_TRIANGLE_VERTICE, sizeof(mesh::P_TRIANGLE_VERTICE)};
core::EBO ebo{mesh::P_TRIANGLE_INDICE, sizeof(mesh::P_TRIANGLE_INDICE)};
core::Shader shader{(char *)mesh::P_TRIANGLE_VERT,
(char *)mesh::P_TRIANGLE_FRAG};
vao.bind();
vbo.bind();
ebo.bind();
vao.setAttributePointer(0, 3, GL_FLOAT, 3 * sizeof(GLfloat), (void *)(0));
time.start();
while (!glfwWindowShouldClose(window))
{
glfwGetFramebufferSize(window, &width, &height);
glfwGetWindowSize(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);
shader.use();
vao.drawElement(GL_TRIANGLES, mesh::P_TRIANGLE_INDICE_LEN, GL_UNSIGNED_INT, 0);
glfwPollEvents();
glfwSwapBuffers(window);
GLenum error = glGetError();
if (error != GL_NO_ERROR)
{
core::log::error("", error);
std::exit(EXIT_FAILURE);
}
time.update();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0; return 0;
} }