mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-03-28 18:03:44 +00:00
feat: replace triangle test by cube
This commit is contained in:
@@ -40,11 +40,11 @@ foreach(SHADER ${SHADERS})
|
||||
add_custom_command(
|
||||
OUTPUT ${OUTPUT_C}
|
||||
COMMAND python3
|
||||
${CMAKE_SOURCE_DIR}/glsl2c.py
|
||||
${CMAKE_SOURCE_DIR}/scripts/glsl2c.py
|
||||
${SHADER}
|
||||
${OUTPUT_C}
|
||||
DEPENDS ${SHADER}
|
||||
${CMAKE_SOURCE_DIR}/glsl2c.py
|
||||
${CMAKE_SOURCE_DIR}/scripts/glsl2c.py
|
||||
COMMENT "Converting shader ${NAME_WE}${EXT} to C file"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include "glm/ext/matrix_float4x4.hpp"
|
||||
#include "glm/ext/vector_float3.hpp"
|
||||
|
||||
namespace core
|
||||
{
|
||||
class Camera
|
||||
{
|
||||
private:
|
||||
@@ -63,5 +65,5 @@ class Camera
|
||||
void setFov(float newFov);
|
||||
void setPosition(const glm::vec3& newPosition);
|
||||
};
|
||||
|
||||
} // namespace core
|
||||
#endif
|
||||
|
||||
@@ -29,6 +29,8 @@ class Shader
|
||||
// Activates the shader program for rendering
|
||||
// All subsequent draw calls will use this program
|
||||
void use() const;
|
||||
|
||||
GLuint getId() const { return id; }
|
||||
};
|
||||
} // namespace core
|
||||
|
||||
|
||||
22
inc/cube.hpp
22
inc/cube.hpp
@@ -1,14 +1,14 @@
|
||||
#ifndef CUBE_HPP
|
||||
#define CUBE_HPP
|
||||
// #ifndef CUBE_HPP
|
||||
// #define CUBE_HPP
|
||||
|
||||
#include "glm/ext/vector_float3.hpp"
|
||||
#include "shape.hpp"
|
||||
// #include "glm/ext/vector_float3.hpp"
|
||||
// #include "shape.hpp"
|
||||
|
||||
class Cube : public Shape
|
||||
{
|
||||
public:
|
||||
Cube(Camera &camera, glm::vec3 pos, const char *texture);
|
||||
void render(int width, int height) override;
|
||||
};
|
||||
// class Cube : public Shape
|
||||
// {
|
||||
// public:
|
||||
// Cube(Camera &camera, glm::vec3 pos, const char *texture);
|
||||
// void render(int width, int height) override;
|
||||
// };
|
||||
|
||||
#endif
|
||||
// #endif
|
||||
@@ -1,31 +1,11 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
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;
|
||||
uniform Light light;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 texColor = texture(material.diffuse , TexCoords);
|
||||
FragColor = texColor;
|
||||
FragColor = vec4(0.8, 0.8, 1.0, 1.0);
|
||||
}
|
||||
|
||||
@@ -8,15 +8,7 @@ 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;
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 color;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||
color = vec4(0.2, 0.51, 1.0, 1.0);
|
||||
}
|
||||
@@ -13,7 +13,6 @@ namespace mesh
|
||||
0.5f, -0.5f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
constexpr const std::size_t P_TRIANGLE_VERTICE_LEN =
|
||||
sizeof(P_TRIANGLE_VERTICE) / sizeof(GLfloat);
|
||||
|
||||
@@ -5,6 +5,7 @@ from pathlib import Path
|
||||
|
||||
line: int = 12
|
||||
|
||||
|
||||
def write_binary(content: bytes) -> str:
|
||||
text = ""
|
||||
for i, byte in enumerate(content):
|
||||
@@ -16,6 +17,7 @@ def write_binary(content: bytes) -> str:
|
||||
text += "0x00\n"
|
||||
return text
|
||||
|
||||
|
||||
def main() -> int:
|
||||
# check arguments
|
||||
if len(sys.argv) < 2 or len(sys.argv) > 3:
|
||||
@@ -37,7 +39,8 @@ def main() -> int:
|
||||
content = src.read_bytes()
|
||||
|
||||
with open(dst, "w") as f:
|
||||
f.write(f"const unsigned char {varname}[] = {{\n{write_binary(content)}}};\n")
|
||||
f.write(
|
||||
f"const unsigned char {varname}[] = {{\n{write_binary(content)}}};\n")
|
||||
f.write(f"const unsigned int {varname}_LEN = {len(content)};\n")
|
||||
return 0
|
||||
|
||||
20
scripts/glsl2pak.py
Executable file
20
scripts/glsl2pak.py
Executable file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from scripts.glsl2c import write_binary
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) < 2:
|
||||
print("glsl2pak.py <shader_file>... output")
|
||||
return 1
|
||||
|
||||
output = sys.argv.pop()
|
||||
print(sys.argv)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "glm/ext/matrix_transform.hpp"
|
||||
#include "glm/ext/matrix_clip_space.hpp"
|
||||
|
||||
Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
|
||||
core::Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
|
||||
: width(width),
|
||||
height(height),
|
||||
window(window),
|
||||
@@ -15,7 +15,7 @@ Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void Camera::update(float deltaTime)
|
||||
void core::Camera::update(float deltaTime)
|
||||
{
|
||||
processInput(deltaTime);
|
||||
processMouseMovement();
|
||||
@@ -23,7 +23,7 @@ void Camera::update(float deltaTime)
|
||||
}
|
||||
|
||||
// TODO: callback management
|
||||
void Camera::processInput(float deltaTime)
|
||||
void core::Camera::processInput(float deltaTime)
|
||||
{
|
||||
float velocity = speed * deltaTime;
|
||||
|
||||
@@ -51,7 +51,7 @@ void Camera::processInput(float deltaTime)
|
||||
: 45.f;
|
||||
}
|
||||
|
||||
void Camera::processMouseMovement()
|
||||
void core::Camera::processMouseMovement()
|
||||
{
|
||||
double mouseX, mouseY;
|
||||
glfwGetCursorPos(window, &mouseX, &mouseY);
|
||||
@@ -80,7 +80,7 @@ void Camera::processMouseMovement()
|
||||
if (cameraPitch < -89.0f) cameraPitch = -89.0f;
|
||||
}
|
||||
|
||||
void Camera::updateCameraVectors()
|
||||
void core::Camera::updateCameraVectors()
|
||||
{
|
||||
glm::vec3 front;
|
||||
front.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
|
||||
@@ -92,12 +92,12 @@ void Camera::updateCameraVectors()
|
||||
cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront));
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getViewMatrix() const
|
||||
glm::mat4 core::Camera::getViewMatrix() const
|
||||
{
|
||||
return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getProjectionMatrix() const
|
||||
glm::mat4 core::Camera::getProjectionMatrix() const
|
||||
{
|
||||
return glm::perspective(
|
||||
glm::radians(this->fov),
|
||||
@@ -105,7 +105,7 @@ glm::mat4 Camera::getProjectionMatrix() const
|
||||
100.0f);
|
||||
}
|
||||
|
||||
void Camera::setSpeed(float newSpeed)
|
||||
void core::Camera::setSpeed(float newSpeed)
|
||||
{
|
||||
if (newSpeed > 0.0f)
|
||||
{
|
||||
@@ -113,7 +113,7 @@ void Camera::setSpeed(float newSpeed)
|
||||
}
|
||||
}
|
||||
|
||||
void Camera::setCameraSensitivity(float newSensitivity)
|
||||
void core::Camera::setCameraSensitivity(float newSensitivity)
|
||||
{
|
||||
if (newSensitivity > 0.0f)
|
||||
{
|
||||
@@ -121,7 +121,7 @@ void Camera::setCameraSensitivity(float newSensitivity)
|
||||
}
|
||||
}
|
||||
|
||||
void Camera::setFov(float newFov)
|
||||
void core::Camera::setFov(float newFov)
|
||||
{
|
||||
if (newFov > 1.0f && newFov < 179.0f)
|
||||
{
|
||||
@@ -129,7 +129,7 @@ void Camera::setFov(float newFov)
|
||||
}
|
||||
}
|
||||
|
||||
void Camera::setPosition(const glm::vec3& newPosition)
|
||||
void core::Camera::setPosition(const glm::vec3& newPosition)
|
||||
{
|
||||
cameraPosition = newPosition;
|
||||
}
|
||||
93
src/cube.cpp
93
src/cube.cpp
@@ -1,65 +1,64 @@
|
||||
// #include "primitives/cube.hpp"
|
||||
// #include "cube.hpp"
|
||||
|
||||
// #include "core/ebo.hpp"
|
||||
// #include "core/vao.hpp"
|
||||
// #include "core/vbo.hpp"
|
||||
// #include "ebo.hpp"
|
||||
// #include "glm/ext/matrix_clip_space.hpp"
|
||||
// #include "glm/ext/matrix_transform.hpp"
|
||||
// #include "glm/gtc/type_ptr.hpp"
|
||||
// #include "p_cube.hpp"
|
||||
// #include "vao.hpp"
|
||||
// #include "vbo.hpp"
|
||||
// #include "primitives/p_cube.hpp"
|
||||
|
||||
// static GLsizei stride = 8 * sizeof(float);
|
||||
// constexpr static GLsizei STRIDE = 8 * sizeof(float);
|
||||
|
||||
// Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture)
|
||||
// : Shape(camera, pos, Shader{}, Texture{texture})
|
||||
// {
|
||||
// this->vao.bind();
|
||||
// this->vbo.bind();
|
||||
// this->ebo.bind();
|
||||
// // Cube::Cube(Camera &camera, glm::vec3 pos, std::string texture)
|
||||
// // : Shape(camera, pos, Shader{}, Texture{texture})
|
||||
// // {
|
||||
// // this->vao.bind();
|
||||
// // this->vbo.bind();
|
||||
// // this->ebo.bind();
|
||||
|
||||
// this->vbo.setData(P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE));
|
||||
// // this->vbo.setData(P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE));
|
||||
|
||||
// // 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)));
|
||||
// // // 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->ebo.setData(P_CUBE_INDICE, sizeof(P_CUBE_INDICE));
|
||||
|
||||
// this->shader.compile((char *)P_CUBE_VERT, (char *)P_CUBE_FRAG);
|
||||
// }
|
||||
// // this->shader.compile((char *)P_CUBE_VERT, (char *)P_CUBE_FRAG);
|
||||
// // }
|
||||
|
||||
// void Cube::render(int width, int height)
|
||||
// {
|
||||
// shader.use();
|
||||
// glActiveTexture(GL_TEXTURE0);
|
||||
// // void Cube::render(int width, int height)
|
||||
// // {
|
||||
// // shader.use();
|
||||
// // glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
// 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);
|
||||
// // 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);
|
||||
|
||||
// GLint texLoc =
|
||||
// glGetUniformLocation(shader.getShaderProgramID(), "material.diffuse");
|
||||
// glUniform1i(texLoc, 0);
|
||||
// // GLint texLoc =
|
||||
// // glGetUniformLocation(shader.getShaderProgramID(), "material.diffuse");
|
||||
// // glUniform1i(texLoc, 0);
|
||||
|
||||
// GLint projectionLoc =
|
||||
// glGetUniformLocation(shader.getShaderProgramID(), "projection");
|
||||
// glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
||||
// // GLint projectionLoc =
|
||||
// // glGetUniformLocation(shader.getShaderProgramID(), "projection");
|
||||
// // glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
||||
|
||||
// GLint viewLoc = glGetUniformLocation(shader.getShaderProgramID(), "view");
|
||||
// glUniformMatrix4fv(viewLoc, 1, GL_FALSE,
|
||||
// glm::value_ptr(camera.getViewMatrix()));
|
||||
// // GLint viewLoc = glGetUniformLocation(shader.getShaderProgramID(), "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.getShaderProgramID(), "model");
|
||||
// glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
|
||||
// // 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));
|
||||
|
||||
// vao.drawElement(GL_TRIANGLES, sizeof(P_CUBE_INDICE) / sizeof(unsigned int),
|
||||
// GL_UNSIGNED_INT, 0);
|
||||
// }
|
||||
// // vao.drawElement(GL_TRIANGLES, sizeof(P_CUBE_INDICE) / sizeof(unsigned int),
|
||||
// // GL_UNSIGNED_INT, 0);
|
||||
// // }
|
||||
58
src/main.cpp
58
src/main.cpp
@@ -1,8 +1,13 @@
|
||||
#include "core/core.hpp"
|
||||
#include "core/ebo.hpp"
|
||||
#include "core/shader.hpp"
|
||||
#include "glm/ext/matrix_clip_space.hpp"
|
||||
#include "glm/ext/matrix_transform.hpp"
|
||||
#include "glm/fwd.hpp"
|
||||
#include "glm/gtc/type_ptr.hpp"
|
||||
#include "primitives/p_cube.hpp"
|
||||
#include "primitives/p_triangle.hpp"
|
||||
|
||||
using namespace mesh;
|
||||
|
||||
int main()
|
||||
{
|
||||
// Game game{800, 600, "window"};
|
||||
@@ -57,34 +62,65 @@ int main()
|
||||
|
||||
glfwSwapInterval(1); // activate vsync
|
||||
|
||||
// glEnable(GL_DEPTH_TEST);
|
||||
// glEnable(GL_MULTISAMPLE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
// CUBE
|
||||
core::VAO vao{};
|
||||
core::VBO vbo{mesh::P_TRIANGLE_VERTICE, sizeof(mesh::P_TRIANGLE_VERTICE)};
|
||||
core::EBO ebo{mesh::P_TRIANGLE_INDICE, sizeof(mesh::P_TRIANGLE_INDICE)};
|
||||
core::Shader shader{(char *)mesh::P_TRIANGLE_VERT,
|
||||
(char *)mesh::P_TRIANGLE_FRAG};
|
||||
core::VBO vbo{P_CUBE_VERTICE, sizeof(P_CUBE_VERTICE)};
|
||||
core::EBO ebo{P_CUBE_INDICE, sizeof(P_CUBE_INDICE)};
|
||||
core::Shader shader{(char *)P_CUBE_VERT, (char *)P_CUBE_FRAG};
|
||||
|
||||
// CAMERA
|
||||
core::Camera camera(width, height, window, .1f);
|
||||
|
||||
// BINDING
|
||||
vao.bind();
|
||||
vbo.bind();
|
||||
ebo.bind();
|
||||
|
||||
vao.setAttributePointer(0, 3, GL_FLOAT, 3 * sizeof(GLfloat), (void *)(0));
|
||||
// positions
|
||||
vao.setAttributePointer(0, 3, GL_FLOAT, 8 * sizeof(GLfloat), (void *)(0));
|
||||
// normales
|
||||
vao.setAttributePointer(1, 3, GL_FLOAT, 8 * sizeof(GLfloat),
|
||||
(void *)(3 * sizeof(float)));
|
||||
// texture
|
||||
vao.setAttributePointer(2, 2, GL_FLOAT, 8 * sizeof(GLfloat),
|
||||
(void *)(6 * sizeof(float)));
|
||||
|
||||
glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -10.0f);
|
||||
|
||||
time.start();
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
glfwGetWindowSize(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);
|
||||
|
||||
camera.update(time.getDeltaTime());
|
||||
|
||||
shader.use();
|
||||
|
||||
vao.drawElement(GL_TRIANGLES, mesh::P_TRIANGLE_INDICE_LEN, GL_UNSIGNED_INT, 0);
|
||||
// projection matrix
|
||||
const glm::float32 *view = glm::value_ptr(camera.getViewMatrix());
|
||||
glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate);
|
||||
glm::mat4 projection = glm::perspective(
|
||||
glm::radians(camera.getFov()),
|
||||
static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.f);
|
||||
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader.getId(), "projection"), 1,
|
||||
GL_FALSE, glm::value_ptr(projection));
|
||||
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader.getId(), "view"), 1,
|
||||
GL_FALSE, glm::value_ptr(camera.getViewMatrix()));
|
||||
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader.getId(), "model"), 1,
|
||||
GL_FALSE, glm::value_ptr(model));
|
||||
|
||||
vao.drawElement(GL_TRIANGLES, sizeof(P_TRIANGLE_VERTICE), GL_UNSIGNED_INT,
|
||||
0);
|
||||
|
||||
glfwPollEvents();
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
Reference in New Issue
Block a user