fix: rework cube.cpp file

This commit is contained in:
2026-01-02 20:06:30 +01:00
parent 379203eadf
commit 721a3ceaa0
6 changed files with 129 additions and 133 deletions

View File

@@ -13,7 +13,7 @@ def write_binary(content: str) -> str:
text += f"0x{byte:02x}, "
if (i + 1) % line == 0:
text += "\n"
text += "\n"
text += "0x00\n"
return text
def main() -> int:
@@ -31,14 +31,14 @@ def main() -> int:
dst = Path(sys.argv[2]) if len(sys.argv) == 3 else src.with_suffix(".c")
# variable name
varname = f"{src.stem}_{src.suffix[1:]}"
varname = f"{src.stem}_{src.suffix[1:]}".upper()
# read binary
content = src.read_bytes()
with open(dst, "w") as f:
f.write(f"unsigned char {varname}[] = {{\n{write_binary(content)}}};\n")
f.write(f"unsigned int {varname}_len = {len(content)};\n")
f.write(f"unsigned int {varname}_LEN = {len(content)};\n")
return 0

View File

@@ -1,7 +1,7 @@
#include "GLFW/glfw3.h"
// Positions // Normales // texture coordinate
constexpr GLfloat VERTICE[] = {
constexpr GLfloat P_CUBE_VERTICE[] = {
// Positions // _LEN;ales // texture coordinate
// front side
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
@@ -39,7 +39,7 @@ constexpr GLfloat VERTICE[] = {
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
};
constexpr unsigned int INDICE[] = {
constexpr unsigned int P_CUBE_INDICE[] = {
0, 1, 2, // 1
2, 3, 0, // 2
@@ -59,8 +59,8 @@ constexpr unsigned int INDICE[] = {
22, 23, 20 // 12
};
extern unsigned char cube_frag[];
extern unsigned int cube_frag_len;
extern const unsigned char P_CUBE_FRAG[];
extern const unsigned int P_CUBE_FRAG_LEN;
extern unsigned char cube_vert[];
extern unsigned int cube_vert_len;
extern const unsigned char P_CUBE_VERT[];
extern const unsigned int P_CUBE_VERT_LEN;

View File

@@ -1,10 +1,15 @@
#include "cube.hpp"
#include "ebo.hpp"
#include "render/primitives/cube.hpp"
#include "glm/ext/matrix_clip_space.hpp"
#include "glm/ext/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "primitives/p_cube.hpp"
#include "vao.hpp"
#include "vbo.hpp"
static GLsizei stride = 8 * sizeof(float);
Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture)
: Shape(camera, pos, Shader{}, Texture{texture})
{
@@ -12,59 +17,48 @@ Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture)
this->vbo.bind();
this->ebo.bind();
this->vbo.setData(VERTICE, sizeof(VERTICE));
this->ebo.setData(INDICE, sizeof(INDICE));
this->vbo.setData(P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE));
this->shader.compile((const char *)__res_render_primitives_cube_vert,
(const char *)__res_render_primitives_cube_frag);
// 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->shader.compile((char *)P_CUBE_VERT, (char *)P_CUBE_FRAG);
}
void Cube::render(int width, int height) {}
// Cube::Cube(Camera& camera)
// : vbo(cubeVertices, sizeof(cubeVertices)),
// ebo(cubeIndices, sizeof(cubeIndices)),
// texture("stone.png"),
// camera(camera),
// shader(cubeVertexShader, cubeFragShader)
// {
// vao.bind();
// vbo.bind();
void Cube::render(int width, int height)
{
shader.use();
glActiveTexture(GL_TEXTURE0);
// GLsizei stride = 8 * sizeof(float);
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);
// 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)));
GLint texLoc =
glGetUniformLocation(shader.getShaderProgramID(), "material.diffuse");
glUniform1i(texLoc, 0);
// ebo.bind();
// }
GLint projectionLoc =
glGetUniformLocation(shader.getShaderProgramID(), "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
// void Cube::loop(int width, int height)
// {
// shader.use();
// glActiveTexture(GL_TEXTURE0);
GLint viewLoc = glGetUniformLocation(shader.getShaderProgramID(), "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE,
glm::value_ptr(camera.getViewMatrix()));
// 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);
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));
// GLint texLoc = glGetUniformLocation(shader.getProgram(),
// "material.diffuse"); glUniform1i(texLoc, 0);
// GLint projectionLoc = glGetUniformLocation(shader.getProgram(),
// "projection"); 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()));
// 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(P_CUBE_INDICE) / sizeof(unsigned int),
GL_UNSIGNED_INT, 0);
}

View File

@@ -1,8 +1,10 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include <cstdlib>
#include <cstdio>
// #include <cstdlib>
#include <iostream>
#include <ostream>
#include "camera.hpp"
#include "cube.hpp"
@@ -15,85 +17,85 @@ static void glfwErrorCallback(int error, const char *description)
int main()
{
const char *title = "window";
int width = 800, height = 600;
const char *title = "window";
int width = 800, height = 600;
glfwSetErrorCallback(glfwErrorCallback);
glfwSetErrorCallback(glfwErrorCallback);
if (glfwInit() == GLFW_FALSE)
{
std::cerr << "ERROR(GLFW): initialisation error" << std::endl;
exit(EXIT_FAILURE);
}
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
// 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);
}
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);
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, 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);
}
}
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, 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 << std::endl;
// exit(1);
}
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}