Skip to content

Commit

Permalink
shadows, utils
Browse files Browse the repository at this point in the history
  • Loading branch information
desatur committed Sep 1, 2024
1 parent c0a1a6e commit fdbddc0
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 128 deletions.
2 changes: 2 additions & 0 deletions Future/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ add_library(Future SHARED
src/Rendering/OpenGL/Textures/Texture.hpp
src/Rendering/OpenGL/Camera.cpp
src/Rendering/OpenGL/Camera.hpp
src/Rendering/OpenGL/Mesh.cpp
src/Rendering/OpenGL/Mesh.hpp
)

target_include_directories(Future PUBLIC
Expand Down
4 changes: 2 additions & 2 deletions Future/src/Rendering/OpenGL/Buffers/EBO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Future
{
EBO::EBO(GLuint* indices, GLsizeiptr size)
EBO::EBO(std::vector<GLuint>& indices)
{
glGenBuffers(1, &ID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW);
}

void EBO::Bind()
Expand Down
3 changes: 2 additions & 1 deletion Future/src/Rendering/OpenGL/Buffers/EBO.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef EBO_HPP
#define EBO_HPP
#include <vector>
#include <glad/glad.h>

namespace Future
Expand All @@ -8,7 +9,7 @@ namespace Future
{
public:
GLuint ID;
EBO(GLuint* indices, GLsizeiptr size);
EBO(std::vector<GLuint>& indices);

void Bind();
void Unbind();
Expand Down
4 changes: 2 additions & 2 deletions Future/src/Rendering/OpenGL/Buffers/VBO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Future
{
VBO::VBO(GLfloat* vertices, GLsizeiptr size)
VBO::VBO(std::vector<Vertex>& vertices)
{
glGenBuffers(1, &ID);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
}

void VBO::Bind()
Expand Down
13 changes: 12 additions & 1 deletion Future/src/Rendering/OpenGL/Buffers/VBO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
#define VBO_HPP
#include <glad/glad.h>

#include "vec2.hpp"
#include "vec3.hpp"

struct Vertex
{
glm::vec3 Position;
glm::vec3 Normal;
glm::vec3 color;
glm::vec2 TexCoords;
};

namespace Future
{
class VBO
Expand All @@ -10,7 +21,7 @@ namespace Future
// Reference ID of the Vertex Buffer Object
GLuint ID;
// Constructor that generates a Vertex Buffer Object and links it to vertices
VBO(GLfloat* vertices, GLsizeiptr size);
VBO(std::vector<Vertex>& vertices);

void Bind();
void Unbind();
Expand Down
56 changes: 56 additions & 0 deletions Future/src/Rendering/OpenGL/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "Mesh.hpp"


namespace Future
{
Mesh::Mesh(std::vector <Vertex>& vertices, std::vector <GLuint>& indices, std::vector<Texture>& textures)
{
Mesh::vertices = vertices;
Mesh::indices = indices;
Mesh::textures = textures;

VAO.Bind(); // Bind VAO

VBO VBO (vertices);
EBO EBO (indices);

VAO.LinkAttrib(VBO, 0, 3, GL_FLOAT, sizeof(Vertex), (void*)0); // Position
VAO.LinkAttrib(VBO, 1, 3, GL_FLOAT, sizeof(Vertex), (void*)(3 * sizeof(float))); // Color
VAO.LinkAttrib(VBO, 2, 2, GL_FLOAT, sizeof(Vertex), (void*)(6 * sizeof(float))); // Texture Coordinate
VAO.LinkAttrib(VBO, 3, 2, GL_FLOAT, sizeof(Vertex), (void*)(9 * sizeof(float))); // Normals

VAO.Unbind();
VBO.Unbind();
EBO.Unbind();
}

void Mesh::Draw(Shaders &shaders, Camera &camera)
{
shaders.Activate();
VAO.Bind();

unsigned int numDiffuse = 0;
unsigned int numSpecular = 0;

for (unsigned int i = 0; i < textures.size(); i++)
{
std::string num;
std::string type = textures[i].type;
if (type == "diffuse")
{
num = std::to_string(numDiffuse++);
}
else if (type == "specular")
{
num = std::to_string(numSpecular++);
}
textures[i].texUnit(shaders, (type + num).c_str(), i);
textures[i].Bind();
}
glUniform3f(glGetUniformLocation(shaders.ID, "camPos"), camera.Position.x, camera.Position.y, camera.Position.z);
camera.Matrix(shaders, "camMatrix");

glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
}

} // Future
27 changes: 27 additions & 0 deletions Future/src/Rendering/OpenGL/Mesh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef MESH_HPP
#define MESH_HPP

#include <string>
#include "Buffers/VAO.hpp"
#include "Buffers/EBO.hpp""
#include "Camera.hpp"
#include "Textures/Texture.hpp"
namespace Future
{
class Mesh
{
public:
std::vector<Vertex> vertices;
std::vector<GLuint> indices;
std::vector<Texture> textures;
VAO VAO;
Mesh(std::vector <Vertex>& vertices, std::vector <GLuint>& indices, std::vector<Texture>& textures);
void Draw(Shaders& shaders, Camera& camera);
};
}
#endif //MESH_HPP
144 changes: 57 additions & 87 deletions Future/src/Rendering/OpenGL/OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Buffers/VAO.hpp"
#include "Buffers/VBO.hpp"
#include "Textures/Texture.hpp"
#include "Mesh.hpp"

namespace Future
{
Expand All @@ -33,13 +34,12 @@ namespace Future

bool OpenGL::Init()
{
// Vertices coordinates
GLfloat vertices[] =
{ // COORDINATES / COLORS / TexCoord / NORMALS //
-1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f
Vertex vertices[] =
{ // COORDINATES / COLORS / NORMALS / TEXTURE COORDINATES //
Vertex{glm::vec3(-1.0f, 0.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec2(0.0f, 0.0f)},
Vertex{glm::vec3(-1.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec2(0.0f, 1.0f)},
Vertex{glm::vec3( 1.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec2(1.0f, 1.0f)},
Vertex{glm::vec3( 1.0f, 0.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec2(1.0f, 0.0f)}
};

// Indices for vertices order
Expand All @@ -49,16 +49,16 @@ namespace Future
0, 2, 3
};

GLfloat lightVertices[] =
Vertex lightVertices[] =
{ // COORDINATES //
-0.1f, -0.1f, 0.1f,
-0.1f, -0.1f, -0.1f,
0.1f, -0.1f, -0.1f,
0.1f, -0.1f, 0.1f,
-0.1f, 0.1f, 0.1f,
-0.1f, 0.1f, -0.1f,
0.1f, 0.1f, -0.1f,
0.1f, 0.1f, 0.1f
Vertex{glm::vec3(-0.1f, -0.1f, 0.1f)},
Vertex{glm::vec3(-0.1f, -0.1f, -0.1f)},
Vertex{glm::vec3(0.1f, -0.1f, -0.1f)},
Vertex{glm::vec3(0.1f, -0.1f, 0.1f)},
Vertex{glm::vec3(-0.1f, 0.1f, 0.1f)},
Vertex{glm::vec3(-0.1f, 0.1f, -0.1f)},
Vertex{glm::vec3(0.1f, 0.1f, -0.1f)},
Vertex{glm::vec3(0.1f, 0.1f, 0.1f)}
};

GLuint lightIndices[] =
Expand All @@ -83,111 +83,81 @@ namespace Future
gladLoadGL();
glViewport(0, 0, 1920, 1080);

Shaders shaderProgram("Shaders/default.vert", "Shaders/default.frag");
Texture textures[]
{
Texture("planks.png", "diffuse", 0, GL_RGBA, GL_UNSIGNED_BYTE),
Texture("planksSpec.png", "specular", 1, GL_RED, GL_UNSIGNED_BYTE)
};

// Generates Shader object using shaders default.vert and default.frag
Shaders shaderProgram("default.vert", "default.frag");
// Store mesh data in vectors for the mesh
std::vector <Vertex> verts(vertices, vertices + sizeof(vertices) / sizeof(Vertex));
std::vector <GLuint> ind(indices, indices + sizeof(indices) / sizeof(GLuint));
std::vector <Texture> tex(textures, textures + sizeof(textures) / sizeof(Texture));
// Create floor mesh
Mesh floor(verts, ind, tex);

VAO gl_VAO;
gl_VAO.Bind();

VBO gl_VBO (vertices, sizeof(vertices));
EBO gl_EBO (indices, sizeof(indices));
// Shader for light cube
Shaders lightShader("light.vert", "light.frag");
// Store mesh data in vectors for the mesh
std::vector <Vertex> lightVerts(lightVertices, lightVertices + sizeof(lightVertices) / sizeof(Vertex));
std::vector <GLuint> lightInd(lightIndices, lightIndices + sizeof(lightIndices) / sizeof(GLuint));
// Create light mesh
Mesh light(lightVerts, lightInd, tex);


gl_VAO.LinkAttrib(gl_VBO, 0, 3, GL_FLOAT, 11 * sizeof(float), (void*)0); // Position
gl_VAO.LinkAttrib(gl_VBO, 1, 3, GL_FLOAT, 11 * sizeof(float), (void*)(3 * sizeof(float))); // Color
gl_VAO.LinkAttrib(gl_VBO, 2, 2, GL_FLOAT, 11 * sizeof(float), (void*)(6 * sizeof(float))); // Texture Coordinate
gl_VAO.LinkAttrib(gl_VBO, 3, 3, GL_FLOAT, 11 * sizeof(float), (void*)(8 * sizeof(float))); // Normals

gl_VAO.Unbind();
gl_VBO.Unbind();
gl_EBO.Unbind();
// ^ Unbind so we won't overwrite it

// Shader for light cube
Shaders lightShader("Shaders/light.vert", "Shaders/light.frag");
// Generates Vertex Array Object and binds it
VAO lightVAO;
lightVAO.Bind();
// Generates Vertex Buffer Object and links it to vertices
VBO lightVBO(lightVertices, sizeof(lightVertices));
// Generates Element Buffer Object and links it to indices
EBO lightEBO(lightIndices, sizeof(lightIndices));
// Links VBO attributes such as coordinates and colors to VAO
lightVAO.LinkAttrib(lightVBO, 0, 3, GL_FLOAT, 3 * sizeof(float), (void*)0);
// Unbind all to prevent accidentally modifying them
lightVAO.Unbind();
lightVBO.Unbind();
lightEBO.Unbind();

glm::vec4 lightColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
glm::vec3 lightPos = glm::vec3(0.5f, 0.5f, 0.5f);
glm::mat4 lightModel = glm::mat4(1.0f);
lightModel = glm::translate(lightModel, lightPos);

glm::vec3 pyramidPos = glm::vec3(0.0f, 0.0f, 0.0f);
glm::mat4 pyramidModel = glm::mat4(1.0f);
pyramidModel = glm::translate(pyramidModel, pyramidPos);
glm::vec3 objectPos = glm::vec3(0.0f, 0.0f, 0.0f);
glm::mat4 objectModel = glm::mat4(1.0f);
objectModel = glm::translate(objectModel, objectPos);


lightShader.Activate();
glUniformMatrix4fv(glGetUniformLocation(lightShader.ID, "model"), 1, GL_FALSE, glm::value_ptr(lightModel));
glUniform4f(glGetUniformLocation(lightShader.ID, "lightColor"), lightColor.x, lightColor.y, lightColor.z, lightColor.w);
shaderProgram.Activate();
glUniformMatrix4fv(glGetUniformLocation(shaderProgram.ID, "model"), 1, GL_FALSE, glm::value_ptr(pyramidModel));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram.ID, "model"), 1, GL_FALSE, glm::value_ptr(objectModel));
glUniform4f(glGetUniformLocation(shaderProgram.ID, "lightColor"), lightColor.x, lightColor.y, lightColor.z, lightColor.w);
glUniform3f(glGetUniformLocation(shaderProgram.ID, "lightPos"), lightPos.x, lightPos.y, lightPos.z);

Texture testTex("testTexture.png", GL_TEXTURE_2D, GL_TEXTURE0, GL_RGB, GL_UNSIGNED_BYTE);
Texture testTexSpecular("testTextureSpecular.png", GL_TEXTURE_2D, 1, GL_RGB, GL_UNSIGNED_BYTE);
testTex.texUnit(shaderProgram, "tex0", 0);
testTexSpecular.texUnit(shaderProgram, "tex1", 1);

glEnable(GL_DEPTH_TEST); // Enable depth buffer
// Enables the Depth Buffer
glEnable(GL_DEPTH_TEST);

Camera camera(1920, 1080, glm::vec3(0.0f, 0.0f, 3.0f));
// Creates camera object
Camera camera(1920, 1080, glm::vec3(0.0f, 0.0f, 2.0f));

while (true)
{
// Specify the color of the background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Clean the back buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear screen with color (black)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers

// Handles camera inputs
camera.Inputs(glfwWindow);
// Updates and exports the camera matrix to the Vertex Shader
camera.UpdateMatrix(45.0f, 0.1f, 100.0f);

// Tells OpenGL which Shader Program we want to use
shaderProgram.Activate();
// Exports the camera Position to the Fragment Shader for specular lighting
glUniform3f(glGetUniformLocation(shaderProgram.ID, "camPos"), camera.Position.x, camera.Position.y, camera.Position.z);
// Export the camMatrix to the Vertex Shader of the pyramid
camera.Matrix(shaderProgram, "camMatrix");
// Binds texture so that is appears in rendering
testTex.Bind();
testTexSpecular.Bind();
// Bind the VAO so OpenGL knows to use it
gl_VAO.Bind();
// Draw primitives, number of indices, datatype of indices, index of indices
glDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(int), GL_UNSIGNED_INT, 0);


// Tells OpenGL which Shader Program we want to use
lightShader.Activate();
// Export the camMatrix to the Vertex Shader of the light cube
camera.Matrix(lightShader, "camMatrix");
// Bind the VAO so OpenGL knows to use it
lightVAO.Bind();
// Draw primitives, number of indices, datatype of indices, index of indices
glDrawElements(GL_TRIANGLES, sizeof(lightIndices) / sizeof(int), GL_UNSIGNED_INT, 0);
camera.UpdateMatrix(45.0f, 0.01f, 1000.0f);

// Draws different meshes
floor.Draw(shaderProgram, camera);
light.Draw(lightShader, camera);


// Swap the back buffer with the front buffer
glfwSwapBuffers(glfwWindow);
// Take care of all GLFW events
glfwPollEvents();
}

gl_VAO.Delete();
gl_VBO.Delete();
gl_EBO.Delete();
shaderProgram.Delete();
lightShader.Delete();

glfwDestroyWindow(glfwWindow);
return true;
Expand Down
Loading

0 comments on commit fdbddc0

Please sign in to comment.