mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-01-25 07:34:16 +00:00
fix: rework cube.cpp file
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
90
src/cube.cpp
90
src/cube.cpp
@@ -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);
|
|
||||||
// }
|
|
||||||
152
src/main.cpp
152
src/main.cpp
@@ -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"
|
||||||
@@ -15,85 +17,85 @@ static void glfwErrorCallback(int error, const char *description)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
const char *title = "window";
|
const char *title = "window";
|
||||||
int width = 800, height = 600;
|
int width = 800, height = 600;
|
||||||
|
|
||||||
glfwSetErrorCallback(glfwErrorCallback);
|
glfwSetErrorCallback(glfwErrorCallback);
|
||||||
|
|
||||||
if (glfwInit() == GLFW_FALSE)
|
if (glfwInit() == GLFW_FALSE)
|
||||||
{
|
{
|
||||||
std::cerr << "ERROR(GLFW): initialisation error" << std::endl;
|
std::cerr << "ERROR(GLFW): initialisation error" << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenGL 3.3
|
// OpenGL 3.3
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
glfwWindowHint(GLFW_DEPTH_BITS, 24);
|
glfwWindowHint(GLFW_DEPTH_BITS, 24);
|
||||||
glfwWindowHint(GLFW_STENCIL_BITS, 8);
|
glfwWindowHint(GLFW_STENCIL_BITS, 8);
|
||||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
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."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
exit(EXIT_FAILURE);
|
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);
|
glfwDestroyWindow(window);
|
||||||
glfwTerminate();
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user