diff --git a/.gitignore b/.gitignore index 6dac35d..2df812a 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,4 @@ cmake-build-debug-coverage build extern imgui.ini +bd diff --git a/core/audio/Bindings.cpp b/core/audio/Bindings.cpp deleted file mode 100644 index e9c9697..0000000 --- a/core/audio/Bindings.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "Bindings.hpp" - -char *getBinding(char *key) { return key; } \ No newline at end of file diff --git a/core/include/Cube.hpp b/core/include/Cube.hpp index 946d153..43813d2 100644 --- a/core/include/Cube.hpp +++ b/core/include/Cube.hpp @@ -18,7 +18,7 @@ const uint32_t WinWidth = 800; const uint32_t WinHeight = 800; -const uint8_t verticesSize = 192; +const uint16_t verticesSize = 264; const uint8_t indicesSize = 36; class Cube { @@ -29,6 +29,7 @@ class Cube { void setScaleUniform(); void setTexUinform(); void setMatUniform(); + void setColorUniform(); void scale(float s); void rotate(float r); @@ -37,6 +38,8 @@ class Cube { void activateShader(); void draw(); + void toggleView(); + void setPrespective(bool view); private: Shader shader; @@ -49,6 +52,8 @@ class Cube { VBO vbo; EBO ebo; + bool isPerspective; + // Model Matrix glm::mat4 model; @@ -58,6 +63,12 @@ class Cube { // Projection Matrix glm::mat4 proj; + // Light Color + glm::vec4 lightColor; + + // Light Position + glm::vec3 lightPosition; + GLuint scaleUniID; float rotation = 0.0f; diff --git a/core/include/Cubes.hpp b/core/include/Cubes.hpp index 5bc0b2d..fcfaed2 100644 --- a/core/include/Cubes.hpp +++ b/core/include/Cubes.hpp @@ -19,8 +19,12 @@ class Cubes { void activateShader(); void clean(); + void setPerspective(bool view); + void setPerVal(bool view); + private: LinkedList *list; + bool isPerspective; }; #endif // VOXELOOP_CUBES_HPP diff --git a/core/include/LightSource.hpp b/core/include/LightSource.hpp deleted file mode 100644 index 5280083..0000000 --- a/core/include/LightSource.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef VOXELOOP_LIGHTSOURCE_HPP -#define VOXELOOP_LIGHTSOURCE_HPP - -// clang-format off -#include -#include -// clang-format on -#include -#include -#include - -#include "EBO.hpp" -#include "Shader.hpp" -#include "VAO.hpp" -#include "VBO.hpp" - -const uint32_t ls_verticesSize = 5; -const uint32_t ls_indicesSize = 5; - -class LightSource { -public: - LightSource(); - ~LightSource(); - void setMatUniform(); - - void activateShader(); - void draw(); - -private: - Shader shader; - GLfloat _vertices[ls_verticesSize]; - GLuint _indices[ls_indicesSize]; - - VAO vao; - VBO vbo; - EBO ebo; - - // Model Matrix - glm::mat4 model; - - // View Matrix - glm::mat4 view; - - // Projection Matrix - glm::mat4 proj; -}; - -#endif // VOXELOOP_LIGHTSOURCE_HPP diff --git a/core/include/Voxeloop.hpp b/core/include/Voxeloop.hpp index 38ad4cd..cbf5d35 100644 --- a/core/include/Voxeloop.hpp +++ b/core/include/Voxeloop.hpp @@ -8,6 +8,7 @@ #include "Audio.hpp" #include "Common.hpp" #include "Cubes.hpp" +// #include "LightSource.hpp" #include "LinkedList.hpp" #include "WindowManager.hpp" @@ -27,6 +28,7 @@ class Voxeloop { Cubes *m_cubes; Audio *m_audio; + // LightSource *m_lightsource; float m_mvmt; float m_scale; diff --git a/core/shaders/default.frag b/core/shaders/default.frag index d0196b3..f472932 100644 --- a/core/shaders/default.frag +++ b/core/shaders/default.frag @@ -3,9 +3,31 @@ out vec4 FragColor; in vec3 color; in vec2 texCoord; +in vec3 normal; +in vec3 currentPos; uniform sampler2D tex0; +uniform vec4 lightColor; +uniform vec3 lightPos; void main() { - FragColor = texture(tex0, texCoord); + // Ambient lighting + float ambient = 0.2f; + + // Diffused lighting + vec3 normalizedNormal = normalize(normal); + vec3 lightDirection = normalize(lightPos - currentPos); + float diffuse = max(dot(normal, lightDirection), 0.0f); + + // Specular lighting + float specularLight = 0.70f; + vec3 camPos = vec3(0.3f, 0.3f, -2.0f); + vec3 viewDirection = normalize(camPos - currentPos); + vec3 reflectionDirection = reflect(-lightDirection, normal); + float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 8); + float specular = specAmount * specularLight; + + vec4 light = lightColor * (ambient + diffuse + specular); + + FragColor = texture(tex0, texCoord) * light; } \ No newline at end of file diff --git a/core/shaders/default.vert b/core/shaders/default.vert index 34f4e82..698ea8d 100644 --- a/core/shaders/default.vert +++ b/core/shaders/default.vert @@ -2,9 +2,13 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; layout (location = 2) in vec2 aTex; +layout (location = 3) in vec3 aNormal; out vec3 color; out vec2 texCoord; +out vec3 normal; + +out vec3 currentPos; uniform float scale; @@ -12,9 +16,10 @@ uniform mat4 model; uniform mat4 view; uniform mat4 proj; - void main() { - gl_Position = proj * view * model * vec4(aPos.x * scale, aPos.y * scale, aPos.z * scale, 1.0); + currentPos = vec3(model * vec4(aPos.x * scale, aPos.y * scale, aPos.z * scale, 1.0)); + gl_Position = proj * view * vec4(currentPos, 1.0); color = aColor; - texCoord = aTex; + texCoord = aTex; + normal = aNormal; } \ No newline at end of file diff --git a/core/src/Cube.cpp b/core/src/Cube.cpp index 10efa82..e0d929f 100644 --- a/core/src/Cube.cpp +++ b/core/src/Cube.cpp @@ -2,45 +2,45 @@ Cube::Cube() : shader{"../core/shaders/default.vert", "../core/shaders/default.frag"}, - _vertices{ + _vertices{ // clang-format off - // POSITIONS / COLORS / TEXTURE COORDS + // POSITIONS | COLORS | TEX COORDS | NORMALS // Face 1 - -0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Upper left corner - f - 0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Upper right corner - f - 0.125f, -0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Lower right corner - f - -0.125f,-0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Lower left corner - f + -0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, // Upper left corner - f + 0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, // Upper right corner - f + 0.125f, -0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f, // Lower right corner - f + -0.125f, -0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, // Lower left corner - f // Face 2 - -0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Upper left corner - b - 0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Upper right corner - b - 0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Lower right corner - b - -0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Lower left corner - b + -0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Upper left corner - b + 0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Upper right corner - b + 0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Lower right corner - b + -0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Lower left corner - b // Face 3 - -0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Upper left corner - f - -0.125f,-0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Lower left corner - f - -0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Lower left corner - b - -0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Upper left corner - b + -0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, // Upper left corner - f + -0.125f, -0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, // Lower left corner - f + -0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, // Lower left corner - b + -0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // Upper left corner - b // Face 4 - 0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Upper right corner - f - 0.125f, -0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Lower right corner - f - 0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Lower right corner - b - 0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Upper right corner - b + 0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Upper right corner - f + 0.125f, -0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Lower right corner - f + 0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Lower right corner - b + 0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Upper right corner - b // Face 5 - -0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Upper left corner - f - 0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Upper right corner - f - 0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Upper right corner - b - -0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Upper left corner - b + -0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Upper left corner - f + 0.125f, 0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Upper right corner - f + 0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Upper right corner - b + -0.125f, 0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Upper left corner - b // Face 6 - 0.125f, -0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Lower right corner - f - -0.125f,-0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Lower left corner - f - -0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Lower left corner - b - 0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f // Lower right corner - b + 0.125f, -0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, // Lower right corner - f + -0.125f, -0.125f, 0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, // Lower left corner - f + -0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f, // Lower left corner - b + 0.125f, -0.125f, -0.125f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f // Lower right corner - b // clang-format on }, @@ -53,18 +53,7 @@ Cube::Cube() // face 2 4, 5, 6, 6, 7, 4, - // face 3 -// 0, 4, 7, -// 7, 3, 0, -// // face 4 -// 1, 5, 6, -// 6, 2, 1, -// // face 5 -// 0, 4, 5, -// 5, 1, 0, -// // face 6 -// 3, 7, 6, -// 6, 2, 3 + //face 3 8, 9, 10, 10, 11, 8, @@ -89,11 +78,13 @@ Cube::Cube() vbo.setup(_vertices, sizeof(_vertices)); ebo.setup(_indices, sizeof(_indices)); - vao.linkAttrib(vbo, 0, 3, GL_FLOAT, 8 * sizeof(float), (void *)0); - vao.linkAttrib(vbo, 1, 3, GL_FLOAT, 8 * sizeof(float), + vao.linkAttrib(vbo, 0, 3, GL_FLOAT, 11 * sizeof(float), (void *)0); + vao.linkAttrib(vbo, 1, 3, GL_FLOAT, 11 * sizeof(float), (void *)(3 * sizeof(float))); - vao.linkAttrib(vbo, 2, 2, GL_FLOAT, 8 * sizeof(float), + vao.linkAttrib(vbo, 2, 2, GL_FLOAT, 11 * sizeof(float), (void *)(6 * sizeof(float))); + vao.linkAttrib(vbo, 3, 3, GL_FLOAT, 11 * sizeof(float), + (void *)(8 * sizeof(float))); vao.unbind(); vbo.unbind(); @@ -101,6 +92,11 @@ Cube::Cube() xpos = random(); ypos = random(); + + lightColor = glm::vec4(0.0f, 0.28f, 0.49f, 1.0f); + lightPosition = glm::vec3(0.0f, 0.0f, -1.0f); + + isPerspective = true; } Cube::~Cube() { @@ -129,6 +125,8 @@ void Cube::setMatUniform() { // Uniform for projection matrix int projLoc = glGetUniformLocation(shader.ID, "proj"); glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj)); + + } void Cube::scale(float s) { glUniform1f(scaleUniID, s); } @@ -137,6 +135,18 @@ void Cube::rotate(float r) { rotation += r; } void Cube::activateShader() { shader.activate(); } +void Cube::toggleView() { isPerspective = !isPerspective; } + +void Cube::setColorUniform() { + int colorLoc = glGetUniformLocation(shader.ID, "lightColor"); + glUniform4f(colorLoc, lightColor.x, lightColor.y, lightColor.z, lightColor.w); + + int lightPosLoc = glGetUniformLocation(shader.ID, "lightPos"); + glUniform3f(lightPosLoc, lightPosition.x, lightPosition.y, lightPosition.z); +} + +void Cube::setPrespective(bool view) {isPerspective = view;} + void Cube::draw() { // Model Matrix model = glm::mat4(1.0f); @@ -148,14 +158,20 @@ void Cube::draw() { proj = glm::mat4(1.0f); model = - glm::rotate(model, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f)); + glm::rotate(model, glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); view = glm::translate(view, glm::vec3(xpos, ypos, -2.0f + zmove)); - proj = glm::perspective(glm::radians(45.0f), (float)(WinWidth / WinHeight), + if (isPerspective) { + proj = glm::perspective(glm::radians(30.0f), (float)(WinWidth / WinHeight), 0.1f, 25.0f); + } else { + proj = glm::rotate(glm::ortho(-1.0f, 1.0f, 1.0f, -1.0f, -1000.0f, 1000.0f), + glm::radians(225.0f), glm::vec3(-1.0f, 0.0f, 0.0f)); + } setMatUniform(); + setColorUniform(); texture.bind(); vao.bind(); diff --git a/core/src/Cubes.cpp b/core/src/Cubes.cpp index e283340..6231b1f 100644 --- a/core/src/Cubes.cpp +++ b/core/src/Cubes.cpp @@ -1,7 +1,8 @@ #include "Cubes.hpp" -Cubes::Cubes() { list = new LinkedList; } +Cubes::Cubes() { list = new LinkedList; +isPerspective = true;} Cubes::~Cubes() { delete list; } @@ -43,6 +44,7 @@ void Cubes::scale(float s) { } void Cubes::draw() { + setPerspective(isPerspective); if (list->head == nullptr) return; struct Node *thead = list->head; @@ -62,4 +64,18 @@ void Cubes::activateShader() { } } +void Cubes::setPerspective(bool view) { + if (list->head == nullptr) + return; + struct Node *thead = list->head; + while (thead != nullptr) { + thead->data->setPrespective(view); + thead = thead->next; + } +} + +void Cubes::setPerVal(bool view) { + isPerspective = view; +} + void Cubes::clean() {} diff --git a/core/src/LightSource.cpp b/core/src/LightSource.cpp deleted file mode 100644 index 726d152..0000000 --- a/core/src/LightSource.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "LightSource.hpp" - -LightSource::LightSource() : - shader{" ", " "}, - _vertices{ - -0.125f, 0.125f, 0.125f, - }, - _indices{ - }{ - -} - -LightSource::~LightSource() {} - -void LightSource::setMatUniform() {} - -void LightSource::activateShader() {} - -void LightSource::draw() {} diff --git a/core/src/Voxeloop.cpp b/core/src/Voxeloop.cpp index 57953dc..431fd6a 100644 --- a/core/src/Voxeloop.cpp +++ b/core/src/Voxeloop.cpp @@ -22,6 +22,7 @@ void Voxeloop::init(GLFWwindow *window) { void Voxeloop::cleanup() { delete m_cubes; delete m_audio; + // delete m_lightsource; std::cout << "[INFO] Voxeloop cleaned\n"; } @@ -125,6 +126,14 @@ void Voxeloop::key_callback(GLFWwindow *window, int key, int scancode, cubes->addCube(); audio->addAudio(';', *mvmt); } + + if (key == GLFW_KEY_HOME) { + cubes->setPerVal(true); + } + + if (key == GLFW_KEY_END) { + cubes->setPerVal(false); + } } if (key == GLFW_KEY_UP && action == GLFW_PRESS) {