mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-01-25 04:34:14 +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
|
#ifndef CUBE_HPP
|
||||||
#define CUBE_HPP
|
#define CUBE_HPP
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#include "camera.hpp"
|
#include "camera.hpp"
|
||||||
#include "ebo.hpp"
|
#include "glm/ext/vector_float3.hpp"
|
||||||
#include "shader.hpp"
|
#include "shape.hpp"
|
||||||
#include "texture.hpp"
|
|
||||||
#include "vao.hpp"
|
|
||||||
#include "vbo.hpp"
|
|
||||||
|
|
||||||
class Cube
|
class Cube : public Shape
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
VBO vbo;
|
|
||||||
EBO ebo;
|
|
||||||
VAO vao;
|
|
||||||
|
|
||||||
Camera &camera;
|
|
||||||
Shader shader;
|
|
||||||
Texture texture;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Cube(Camera &camera);
|
Cube(Camera &camera, glm::vec3 pos, std::string texture);
|
||||||
void loop(int width, int height);
|
void render(int width, int height) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -14,7 +14,7 @@ class EBO
|
|||||||
EBO();
|
EBO();
|
||||||
~EBO();
|
~EBO();
|
||||||
|
|
||||||
void setData(unsigned int* indices, size_t size);
|
void setData(const unsigned int* indices, size_t size);
|
||||||
|
|
||||||
void bind();
|
void bind();
|
||||||
void unbind();
|
void unbind();
|
||||||
|
|||||||
@@ -25,7 +25,10 @@ class Shape
|
|||||||
Texture texture;
|
Texture texture;
|
||||||
|
|
||||||
public:
|
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;
|
virtual void render(int width, int height) = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class VBO
|
|||||||
VBO();
|
VBO();
|
||||||
~VBO();
|
~VBO();
|
||||||
|
|
||||||
void setData(GLfloat* vertices, size_t size);
|
void setData(const GLfloat* vertices, size_t size);
|
||||||
|
|
||||||
void bind();
|
void bind();
|
||||||
void unbind();
|
void unbind();
|
||||||
|
|||||||
@@ -57,4 +57,10 @@ constexpr unsigned int INDICE[] = {
|
|||||||
|
|
||||||
20, 21, 22, // 11
|
20, 21, 22, // 11
|
||||||
22, 23, 20 // 12
|
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 "cube.hpp"
|
||||||
|
|
||||||
#include "glm/gtc/type_ptr.hpp"
|
#include "ebo.hpp"
|
||||||
|
#include "render/primitives/cube.hpp"
|
||||||
#include "vao.hpp"
|
#include "vao.hpp"
|
||||||
#include "vbo.hpp"
|
#include "vbo.hpp"
|
||||||
|
|
||||||
GLfloat cubeVertices[] = {
|
Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture)
|
||||||
// Positions // Normales // texture coordinate
|
: Shape(camera, pos, Shader{}, Texture{texture})
|
||||||
|
|
||||||
// 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()
|
|
||||||
{
|
|
||||||
// 1. Lire la couleur de la texture
|
|
||||||
vec4 texColor = texture(material.diffuse, TexCoords);
|
|
||||||
|
|
||||||
// 2. Afficher la couleur de la texture directement
|
|
||||||
FragColor = texColor;
|
|
||||||
|
|
||||||
// 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();
|
this->vao.bind();
|
||||||
vbo.bind();
|
this->vbo.bind();
|
||||||
|
this->ebo.bind();
|
||||||
|
|
||||||
GLsizei stride = 8 * sizeof(float);
|
this->vbo.setData(VERTICE, sizeof(VERTICE));
|
||||||
|
this->ebo.setData(INDICE, sizeof(INDICE));
|
||||||
|
|
||||||
vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void*)0);
|
this->shader.compile((const char *)__res_render_primitives_cube_vert,
|
||||||
vao.setAttributePointer(1, 3, GL_FLOAT, stride, (void*)(3 * sizeof(float)));
|
(const char *)__res_render_primitives_cube_frag);
|
||||||
vao.setAttributePointer(2, 2, GL_FLOAT, stride, (void*)(6 * sizeof(float)));
|
|
||||||
|
|
||||||
ebo.bind();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cube::loop(int width, int height)
|
void Cube::render(int width, int height) {}
|
||||||
{
|
// Cube::Cube(Camera& camera)
|
||||||
shader.use();
|
// : vbo(cubeVertices, sizeof(cubeVertices)),
|
||||||
glActiveTexture(GL_TEXTURE0);
|
// 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);
|
// GLsizei stride = 8 * sizeof(float);
|
||||||
glm::mat4 projection = glm::perspective(
|
|
||||||
glm::radians(camera.fov),
|
|
||||||
static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.0f);
|
|
||||||
|
|
||||||
GLint texLoc = glGetUniformLocation(shader.getProgram(), "material.diffuse");
|
// vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void*)0);
|
||||||
glUniform1i(texLoc, 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");
|
// ebo.bind();
|
||||||
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
// }
|
||||||
|
|
||||||
GLint viewLoc = glGetUniformLocation(shader.getProgram(), "view");
|
// void Cube::loop(int width, int height)
|
||||||
glUniformMatrix4fv(viewLoc, 1, GL_FALSE,
|
// {
|
||||||
glm::value_ptr(camera.getViewMatrix()));
|
// shader.use();
|
||||||
|
// glActiveTexture(GL_TEXTURE0);
|
||||||
|
|
||||||
glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate);
|
// glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||||
GLint modelLoc = glGetUniformLocation(shader.getProgram(), "model");
|
// glm::mat4 projection = glm::perspective(
|
||||||
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
|
// 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),
|
// GLint texLoc = glGetUniformLocation(shader.getProgram(),
|
||||||
GL_UNSIGNED_INT, 0);
|
// "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) {}
|
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);
|
if (this->id == 0) glGenBuffers(1, &this->id);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include "camera.hpp"
|
#include "camera.hpp"
|
||||||
#include "cube.hpp"
|
#include "cube.hpp"
|
||||||
|
#include "glm/ext/vector_float3.hpp"
|
||||||
|
|
||||||
static void glfwErrorCallback(int error, const char *description)
|
static void glfwErrorCallback(int error, const char *description)
|
||||||
{
|
{
|
||||||
@@ -59,7 +60,7 @@ int main()
|
|||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
|
|
||||||
Camera camera(width, height, window, 0.1f);
|
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_DEPTH_TEST);
|
||||||
glEnable(GL_MULTISAMPLE);
|
glEnable(GL_MULTISAMPLE);
|
||||||
@@ -79,7 +80,7 @@ int main()
|
|||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
|
|
||||||
camera.update(deltaTime);
|
camera.update(deltaTime);
|
||||||
cube.loop(width, height);
|
cube.render(width, height);
|
||||||
|
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
VBO::VBO() : id(0) {}
|
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);
|
if (this->id == 0) glGenBuffers(1, &this->id);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, this->id);
|
glBindBuffer(GL_ARRAY_BUFFER, this->id);
|
||||||
|
|||||||
Reference in New Issue
Block a user