mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-01-25 03:34:15 +00:00
feat(p_cube.hpp): add vertice & indice length
This commit is contained in:
@@ -61,12 +61,12 @@ add_dependencies(main Shaders)
|
||||
|
||||
target_include_directories(main PRIVATE
|
||||
inc # project
|
||||
res/render # ressources (primitive)
|
||||
res/inc # ressources (primitive)
|
||||
lib/glad/include # glad
|
||||
lib/glfw/include # glfw
|
||||
lib/glm # glm
|
||||
lib/imgui # imgui
|
||||
"lib/imgui/backends # imgui backends"
|
||||
lib/imgui/backends # imgui backends
|
||||
lib/stb # stb
|
||||
)
|
||||
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
#ifndef P_CUBE_HPP
|
||||
#define P_CUBE_HPP
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "GLFW/glfw3.h"
|
||||
|
||||
namespace mesh
|
||||
{
|
||||
// clang-format off
|
||||
constexpr GLfloat P_CUBE_VERTICE[] = {
|
||||
// Positions // normales // texture coordinate
|
||||
// front side
|
||||
@@ -38,6 +46,10 @@ constexpr GLfloat P_CUBE_VERTICE[] = {
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
constexpr const std::size_t P_CUBE_VERTICE_LEN =
|
||||
sizeof(P_CUBE_VERTICE) / sizeof(GLfloat);
|
||||
|
||||
constexpr unsigned int P_CUBE_INDICE[] = {
|
||||
0, 1, 2, // 1
|
||||
@@ -59,7 +71,16 @@ constexpr unsigned int P_CUBE_INDICE[] = {
|
||||
22, 23, 20 // 12
|
||||
};
|
||||
|
||||
extern "C" const unsigned char P_CUBE_FRAG[];
|
||||
extern "C" const unsigned int P_CUBE_FRAG_LEN;
|
||||
extern "C" const unsigned char P_CUBE_VERT[];
|
||||
extern "C" const unsigned int P_CUBE_VERT_LEN;
|
||||
constexpr const std::size_t P_CUBE_INDICE_LEN =
|
||||
sizeof(P_CUBE_VERTICE) / sizeof(unsigned int);
|
||||
|
||||
extern "C"
|
||||
{
|
||||
extern const unsigned char CUBE_VERT[];
|
||||
extern const unsigned int CUBE_VERT_LEN;
|
||||
extern const unsigned char CUBE_FRAG[];
|
||||
extern const unsigned int CUBE_FRAG_LEN;
|
||||
}
|
||||
} // namespace mesh
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "shader.hpp"
|
||||
#include "core/shader.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "cube.hpp"
|
||||
#include "res/inc/cube.hpp"
|
||||
|
||||
#include "ebo.hpp"
|
||||
#include "glm/ext/matrix_clip_space.hpp"
|
||||
|
||||
90
src/game.cpp
Normal file
90
src/game.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#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); });
|
||||
|
||||
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)
|
||||
|
||||
#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);
|
||||
|
||||
if (window == NULL)
|
||||
{
|
||||
Game::error("GLFW", 0, "Failed to create GLFW window.");
|
||||
glfwTerminate();
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
Game::error("GLAD", 0, "Failed to initialize GLAD.");
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
glfwSwapInterval(1); // activate vsync
|
||||
|
||||
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);
|
||||
|
||||
glClearColor(0.5f, 0.2f, 0.2f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glfwPollEvents();
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
GLenum error = glGetError();
|
||||
if (error != GL_NO_ERROR)
|
||||
{
|
||||
Game::error("OpenGL", error, "");
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
time.update();
|
||||
}
|
||||
}
|
||||
117
src/main.cpp
117
src/main.cpp
@@ -1,114 +1,9 @@
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include "core/core.hpp"
|
||||
#include "core/logger.hpp"
|
||||
|
||||
#include "glad/glad.h"
|
||||
#include "GLFW/glfw3.h"
|
||||
// #include "imgui.h"
|
||||
// #include "backends/imgui_impl_glfw.h"
|
||||
// #include "backends/imgui_impl_opengl3.h"
|
||||
|
||||
#include "camera.hpp"
|
||||
#include "cube.hpp"
|
||||
|
||||
constexpr const char *WINDOW_NAME = "window";
|
||||
|
||||
int width = 800;
|
||||
int height = 600;
|
||||
|
||||
int main(void)
|
||||
int main()
|
||||
{
|
||||
// define error callback
|
||||
glfwSetErrorCallback(
|
||||
[](int error, const char *desc)
|
||||
{ std::cerr << "GLFW ERROR (" << error << "): " << desc << std::endl; });
|
||||
|
||||
if (glfwInit() == GLFW_FALSE)
|
||||
{
|
||||
std::cerr << "GLFW ERROR: Failed to initialize glfw." << std::endl;
|
||||
return 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); // set depth buffer to 3 bytes (24 bits)
|
||||
glfwWindowHint(GLFW_STENCIL_BITS, 8); // set stencil buffer to 1 byte (8 bits)
|
||||
glfwWindowHint(GLFW_SAMPLES, 4); // activate MSAA (4 samples per pixels)
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,
|
||||
GL_TRUE); // disable deprecated functionalities
|
||||
#endif
|
||||
|
||||
GLFWwindow *window =
|
||||
glfwCreateWindow(width, height, WINDOW_NAME, nullptr, nullptr);
|
||||
|
||||
if (window == NULL)
|
||||
{
|
||||
std::cerr << "GLFW ERROR: Failed to create GLFW window." << std::endl;
|
||||
glfwTerminate();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
std::cerr << "GLAD ERROR: Failed to initialize GLAD" << std::endl;
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
glfwSwapInterval(1);
|
||||
|
||||
// IMGUI_CHECKVERSION();
|
||||
// ImGui::CreateContext();
|
||||
// ImGuiIO &io = ImGui::GetIO();
|
||||
// (void)io;
|
||||
|
||||
// ImGui::StyleColorsDark();
|
||||
|
||||
// ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||
// ImGui_ImplOpenGL3_Init("#version 410");
|
||||
|
||||
Camera camera(width, height, window, 0.1f);
|
||||
Cube cube{camera, glm::vec3{0.f, 0.f, 0.f}, "stone.png"};
|
||||
|
||||
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.render(width, height);
|
||||
|
||||
glfwPollEvents();
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
GLenum error = glGetError();
|
||||
if (error != GL_NO_ERROR)
|
||||
{
|
||||
std::cerr << "error:" << error << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return EXIT_SUCCESS;
|
||||
Game game{800, 600, "window"};
|
||||
game.run();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user