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

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

View File

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

View File

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

View File

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

View File

@@ -1,9 +1,105 @@
#include "core/core.hpp"
#include "core/logger.hpp"
#include "core/ebo.hpp"
#include "core/shader.hpp"
#include "primitives/p_triangle.hpp"
int main()
{
Game game{800, 600, "window"};
game.run();
// Game game{800, 600, "window"};
// 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;
}