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

View File

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

View File

@@ -1,10 +1,15 @@
#include "cube.hpp" #include "cube.hpp"
#include "ebo.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 "vao.hpp"
#include "vbo.hpp" #include "vbo.hpp"
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})
{ {
@@ -12,59 +17,48 @@ Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture)
this->vbo.bind(); this->vbo.bind();
this->ebo.bind(); this->ebo.bind();
this->vbo.setData(VERTICE, sizeof(VERTICE)); this->vbo.setData(P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE));
this->ebo.setData(INDICE, sizeof(INDICE));
this->shader.compile((const char *)__res_render_primitives_cube_vert, // positions
(const char *)__res_render_primitives_cube_frag); 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) {} void Cube::render(int width, int height)
// Cube::Cube(Camera& camera) {
// : vbo(cubeVertices, sizeof(cubeVertices)), shader.use();
// ebo(cubeIndices, sizeof(cubeIndices)), glActiveTexture(GL_TEXTURE0);
// texture("stone.png"),
// camera(camera),
// shader(cubeVertexShader, cubeFragShader)
// {
// vao.bind();
// vbo.bind();
// 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); GLint texLoc =
// vao.setAttributePointer(1, 3, GL_FLOAT, stride, (void*)(3 * glGetUniformLocation(shader.getShaderProgramID(), "material.diffuse");
// sizeof(float))); vao.setAttributePointer(2, 2, GL_FLOAT, stride, (void*)(6 glUniform1i(texLoc, 0);
// * sizeof(float)));
// ebo.bind(); GLint projectionLoc =
// } glGetUniformLocation(shader.getShaderProgramID(), "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
// void Cube::loop(int width, int height) GLint viewLoc = glGetUniformLocation(shader.getShaderProgramID(), "view");
// { glUniformMatrix4fv(viewLoc, 1, GL_FALSE,
// shader.use(); glm::value_ptr(camera.getViewMatrix()));
// glActiveTexture(GL_TEXTURE0);
// glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f); glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate);
// glm::mat4 projection = glm::perspective( GLint modelLoc = glGetUniformLocation(shader.getShaderProgramID(), "model");
// glm::radians(camera.fov), glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
// static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.0f);
// GLint texLoc = glGetUniformLocation(shader.getProgram(), vao.drawElement(GL_TRIANGLES, sizeof(P_CUBE_INDICE) / sizeof(unsigned int),
// "material.diffuse"); glUniform1i(texLoc, 0); GL_UNSIGNED_INT, 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);
// }

View File

@@ -1,8 +1,10 @@
#include <glad/glad.h> #include "glad/glad.h"
#include <GLFW/glfw3.h> #include "GLFW/glfw3.h"
#include <cstdlib> #include <cstdio>
// #include <cstdlib>
#include <iostream> #include <iostream>
#include <ostream>
#include "camera.hpp" #include "camera.hpp"
#include "cube.hpp" #include "cube.hpp"
@@ -37,8 +39,8 @@ int main()
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif #endif
GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr); GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr,
if (window == nullptr) nullptr); if (window == nullptr)
{ {
std::cerr std::cerr
<< "ERROR: Failed to create GLFW window with requested GL version." << "ERROR: Failed to create GLFW window with requested GL version."
@@ -88,8 +90,8 @@ int main()
GLenum error = glGetError(); GLenum error = glGetError();
if (error != GL_NO_ERROR) if (error != GL_NO_ERROR)
{ {
std::cerr << error << std::endl; std::cerr << "error:" << error << std::endl;
// exit(1); exit(1);
} }
} }