mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-01-25 03:34:15 +00:00
feat: add a few improvements of cube logic
This commit is contained in:
24
inc/cube.hpp
24
inc/cube.hpp
@@ -1,29 +1,15 @@
|
||||
#ifndef CUBE_HPP
|
||||
#define CUBE_HPP
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "camera.hpp"
|
||||
#include "ebo.hpp"
|
||||
#include "shader.hpp"
|
||||
#include "texture.hpp"
|
||||
#include "vao.hpp"
|
||||
#include "vbo.hpp"
|
||||
#include "glm/ext/vector_float3.hpp"
|
||||
#include "shape.hpp"
|
||||
|
||||
class Cube
|
||||
class Cube : public Shape
|
||||
{
|
||||
private:
|
||||
VBO vbo;
|
||||
EBO ebo;
|
||||
VAO vao;
|
||||
|
||||
Camera &camera;
|
||||
Shader shader;
|
||||
Texture texture;
|
||||
|
||||
public:
|
||||
Cube(Camera &camera);
|
||||
void loop(int width, int height);
|
||||
Cube(Camera &camera, glm::vec3 pos, std::string texture);
|
||||
void render(int width, int height) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -14,7 +14,7 @@ class EBO
|
||||
EBO();
|
||||
~EBO();
|
||||
|
||||
void setData(unsigned int* indices, size_t size);
|
||||
void setData(const unsigned int* indices, size_t size);
|
||||
|
||||
void bind();
|
||||
void unbind();
|
||||
|
||||
@@ -25,7 +25,10 @@ class Shape
|
||||
Texture texture;
|
||||
|
||||
public:
|
||||
Shape(Camera &camera, glm::vec3 pos, Shader shader, Texture texture);
|
||||
Shape(Camera &camera, glm::vec3 pos, Shader shader, Texture texture)
|
||||
: camera(camera), pos(pos), shader(shader), texture(texture)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void render(int width, int height) = 0;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ class VBO
|
||||
VBO();
|
||||
~VBO();
|
||||
|
||||
void setData(GLfloat* vertices, size_t size);
|
||||
void setData(const GLfloat* vertices, size_t size);
|
||||
|
||||
void bind();
|
||||
void unbind();
|
||||
|
||||
@@ -58,3 +58,9 @@ constexpr unsigned int INDICE[] = {
|
||||
20, 21, 22, // 11
|
||||
22, 23, 20 // 12
|
||||
};
|
||||
|
||||
extern unsigned char *__res_render_primitives_cube_frag;
|
||||
extern unsigned char *__res_render_primitives_cube_vert;
|
||||
|
||||
extern unsigned int __res_render_primitives_cube_frag_len;
|
||||
extern unsigned int __res_render_primitives_cube_vert_len;
|
||||
212
src/cube.cpp
212
src/cube.cpp
@@ -1,174 +1,70 @@
|
||||
#include "cube.hpp"
|
||||
|
||||
#include "glm/gtc/type_ptr.hpp"
|
||||
#include "ebo.hpp"
|
||||
#include "render/primitives/cube.hpp"
|
||||
#include "vao.hpp"
|
||||
#include "vbo.hpp"
|
||||
|
||||
GLfloat cubeVertices[] = {
|
||||
// Positions // Normales // 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,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
|
||||
|
||||
// back side
|
||||
-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, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
|
||||
|
||||
// left side
|
||||
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
|
||||
|
||||
// right side
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||||
|
||||
// bottom side
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
|
||||
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,
|
||||
|
||||
// up side
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
|
||||
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,
|
||||
};
|
||||
|
||||
unsigned int cubeIndices[] = {
|
||||
0, 1, 2, // Triangle 1
|
||||
2, 3, 0, // Triangle 2
|
||||
|
||||
4, 5, 6, // Triangle 3
|
||||
6, 7, 4, // Triangle 4
|
||||
|
||||
8, 9, 10, // Triangle 5
|
||||
10, 11, 8, // Triangle 6
|
||||
|
||||
12, 13, 14, // Triangle 7
|
||||
14, 15, 12, // Triangle 8
|
||||
|
||||
16, 17, 18, // Triangle 9
|
||||
18, 19, 16, // Triangle 10
|
||||
|
||||
20, 21, 22, // Triangle 11
|
||||
22, 23, 20 // Triangle 12
|
||||
};
|
||||
|
||||
const char* cubeVertexShader = R"(
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTexCoords;
|
||||
|
||||
uniform mat4 projection;
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
|
||||
out vec3 Normal;
|
||||
out vec3 FragPos;
|
||||
out vec2 TexCoords;
|
||||
|
||||
void main() {
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
gl_Position = projection * view * vec4(FragPos, 1.0);
|
||||
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
TexCoords = aTexCoords;
|
||||
}
|
||||
)";
|
||||
|
||||
const char* cubeFragShader = R"(
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
// Définitions de struct non utilisées pour l'éclairage mais laissées pour référence
|
||||
struct Material {
|
||||
sampler2D diffuse;
|
||||
vec3 specular;
|
||||
float shininess;
|
||||
};
|
||||
struct Light {
|
||||
vec3 position;
|
||||
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform Material material; // Utilisé uniquement pour material.diffuse
|
||||
uniform Light light; // Non utilisé
|
||||
|
||||
void main()
|
||||
Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture)
|
||||
: Shape(camera, pos, Shader{}, Texture{texture})
|
||||
{
|
||||
// 1. Lire la couleur de la texture
|
||||
vec4 texColor = texture(material.diffuse, TexCoords);
|
||||
this->vao.bind();
|
||||
this->vbo.bind();
|
||||
this->ebo.bind();
|
||||
|
||||
// 2. Afficher la couleur de la texture directement
|
||||
FragColor = texColor;
|
||||
this->vbo.setData(VERTICE, sizeof(VERTICE));
|
||||
this->ebo.setData(INDICE, sizeof(INDICE));
|
||||
|
||||
// Toutes les lignes de calcul d'éclairage (ambient, diffuse, specular) sont retirées.
|
||||
}
|
||||
)";
|
||||
|
||||
Cube::Cube(Camera& camera)
|
||||
: vbo(cubeVertices, sizeof(cubeVertices)),
|
||||
ebo(cubeIndices, sizeof(cubeIndices)),
|
||||
texture("stone.png"),
|
||||
camera(camera),
|
||||
shader(cubeVertexShader, cubeFragShader)
|
||||
{
|
||||
vao.bind();
|
||||
vbo.bind();
|
||||
|
||||
GLsizei stride = 8 * sizeof(float);
|
||||
|
||||
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)));
|
||||
|
||||
ebo.bind();
|
||||
this->shader.compile((const char *)__res_render_primitives_cube_vert,
|
||||
(const char *)__res_render_primitives_cube_frag);
|
||||
}
|
||||
|
||||
void Cube::loop(int width, int height)
|
||||
{
|
||||
shader.use();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
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();
|
||||
|
||||
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);
|
||||
// GLsizei stride = 8 * sizeof(float);
|
||||
|
||||
GLint texLoc = glGetUniformLocation(shader.getProgram(), "material.diffuse");
|
||||
glUniform1i(texLoc, 0);
|
||||
// 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 projectionLoc = glGetUniformLocation(shader.getProgram(), "projection");
|
||||
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
||||
// ebo.bind();
|
||||
// }
|
||||
|
||||
GLint viewLoc = glGetUniformLocation(shader.getProgram(), "view");
|
||||
glUniformMatrix4fv(viewLoc, 1, GL_FALSE,
|
||||
glm::value_ptr(camera.getViewMatrix()));
|
||||
// void Cube::loop(int width, int height)
|
||||
// {
|
||||
// shader.use();
|
||||
// glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
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));
|
||||
// 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);
|
||||
|
||||
vao.drawElement(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(unsigned int),
|
||||
GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
// 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);
|
||||
// }
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
EBO::EBO() : id(0) {}
|
||||
|
||||
void EBO::setData(unsigned int* indices, size_t size)
|
||||
void EBO::setData(const unsigned int* indices, size_t size)
|
||||
{
|
||||
if (this->id == 0) glGenBuffers(1, &this->id);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "camera.hpp"
|
||||
#include "cube.hpp"
|
||||
#include "glm/ext/vector_float3.hpp"
|
||||
|
||||
static void glfwErrorCallback(int error, const char *description)
|
||||
{
|
||||
@@ -59,7 +60,7 @@ int main()
|
||||
glfwSwapInterval(1);
|
||||
|
||||
Camera camera(width, height, window, 0.1f);
|
||||
Cube cube{camera};
|
||||
Cube cube{camera, glm::vec3 {0.f, 0.f, 0.f}, "stone.png"};
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
@@ -79,7 +80,7 @@ int main()
|
||||
lastTime = currentTime;
|
||||
|
||||
camera.update(deltaTime);
|
||||
cube.loop(width, height);
|
||||
cube.render(width, height);
|
||||
|
||||
glfwPollEvents();
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
VBO::VBO() : id(0) {}
|
||||
|
||||
void VBO::setData(GLfloat* vertices, size_t size)
|
||||
void VBO::setData(const GLfloat* vertices, size_t size)
|
||||
{
|
||||
if (this->id == 0) glGenBuffers(1, &this->id);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->id);
|
||||
|
||||
Reference in New Issue
Block a user