From 42b93f2a8f2446b479044357df9eddb32372dfd9 Mon Sep 17 00:00:00 2001 From: Jack Murphy Christensen Date: Thu, 9 Nov 2023 20:09:35 -0500 Subject: [PATCH] Switched up some stuff with Primitive struct --- include/Primitive.h | 16 +++--- shaders/parade_fs.glsl | 19 +++++- shaders/parade_vs.glsl | 2 +- src/Application.cpp | 127 ++++++++++++++++++++++++++++++++++++++--- src/Primitive.cpp | 42 -------------- 5 files changed, 145 insertions(+), 61 deletions(-) diff --git a/include/Primitive.h b/include/Primitive.h index 8674acd..540037c 100644 --- a/include/Primitive.h +++ b/include/Primitive.h @@ -56,8 +56,8 @@ private: float m_size; public: - float& radius() { return m_size; } - const float& radius() const { return m_size; } + float& size() { return m_size; } + const float& size() const { return m_size; } }; struct RoundedCube : Primitive @@ -67,8 +67,8 @@ private: float m_radius; public: - float& radius() { return m_size; } - const float& radius() const { return m_size; } + float& size() { return m_size; } + const float& size() const { return m_size; } float& radius() { return m_radius; } const float& radius() const { return m_radius; } @@ -81,8 +81,8 @@ private: float m_radius; public: - float& radius() { return m_height; } - const float& radius() const { return m_height; } + float& height() { return m_height; } + const float& height() const { return m_height; } float& radius() { return m_radius; } const float& radius() const { return m_radius; } @@ -95,8 +95,8 @@ private: float m_radius; public: - float& radius() { return m_height; } - const float& radius() const { return m_height; } + float& height() { return m_height; } + const float& height() const { return m_height; } float& radius() { return m_radius; } const float& radius() const { return m_radius; } diff --git a/shaders/parade_fs.glsl b/shaders/parade_fs.glsl index c71cfd5..cf876c5 100644 --- a/shaders/parade_fs.glsl +++ b/shaders/parade_fs.glsl @@ -1,4 +1,4 @@ -#version 400 +#version 430 uniform mat4 P; uniform mat4 V; @@ -17,6 +17,23 @@ uniform vec3 ks; uniform float smoothing; uniform vec3 sphere_center; +layout (std430, binding=2) buffer newPrim +{ + vec3 diffuse_color; + vec3 specular_color; + vec3 ambient_color; + vec3 position; + + int specular_exponent; + int shape; + + float radius; + float height; + float inner_radius; + float outer_radius; + float size +} + out vec4 FragColor; struct Torus diff --git a/shaders/parade_vs.glsl b/shaders/parade_vs.glsl index f4651a3..813ae2e 100644 --- a/shaders/parade_vs.glsl +++ b/shaders/parade_vs.glsl @@ -1,4 +1,4 @@ -#version 400 +#version 430 layout(location = 0) in vec3 pos_attrib; diff --git a/src/Application.cpp b/src/Application.cpp index 6c30901..aaa57f9 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -16,12 +16,61 @@ #include #include -#include +//#include -float vertices[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f +#define P_SPHERE 0x00; +#define P_TORUS 0x01; +#define P_CONE 0x02; +#define P_CUBE 0x03; +#define P_ROUNDED_CUBE 0x04; +#define P_CYLINDER 0x05; + +struct Primitive +{ + glm::vec3 diffuse_color = glm::vec3(0.8f); + glm::vec3 specular_color = glm::vec3(1.0f); + glm::vec3 ambient_color = glm::vec3(0.2f); + glm::vec3 position = glm::vec3(0.0f); + + unsigned int specular_exponent = 40; + + unsigned int shape = -1; + unsigned int SSBO = -1; + unsigned int VBO = -1; + unsigned int VAO = -1; + + float radius = -1.f; + float height = -1.f; + float inner_radius = -1.f; + float outer_radius = -1.f; + float size = -1.f; + + std::string name = "Primitive"; +}; + +struct Light +{ + glm::vec3 diffuse_color = glm::vec3(0.8f); + glm::vec3 specular_color = glm::vec3(1.0f); + glm::vec3 ambient_color = glm::vec3(0.2f); + glm::vec3 position = glm::vec3(0.0f); +}; + +struct ScenePrimitives +{ + std::vector p_diffuse; + std::vector p_specular; + std::vector p_ambient; + std::vector p_position; + + std::vector p_spec_exp; + std::vector p_shape; + + std::vector p_radius; + std::vector p_height; + std::vector p_inner_radius; + std::vector p_outer_radius; + std::vector p_size; }; float canvas[] = { @@ -41,7 +90,7 @@ namespace window namespace scene { - std::vector prims; + //std::vector prims; Camera camera; unsigned int shader = -1; @@ -50,6 +99,25 @@ namespace scene float yaw = -90.f; float pitch = 0.f; + std::vector p_diffuse; + std::vector p_specular; + std::vector p_ambient; + std::vector p_position; + std::vector p_spec_exp; + std::vector p_shape; + std::vector p_VBO; + std::vector p_VAO; + std::vector p_radius; + std::vector p_height; + std::vector p_inner_radius; + std::vector p_outer_radius; + std::vector p_size; + + std::vector l_diffuse; + std::vector l_specular; + std::vector l_ambient; + std::vector l_position; + glm::vec3 Lp = { 0.f, 1.f, 0.f }; // Light position glm::vec3 La = { 124.0 / 255.0, 114.0 / 255.0, 160.0 / 255.0 }; // Light ambient color glm::vec3 Ld = { 1.f, 1.f, 1.f }; // Light diffuse color @@ -93,9 +161,29 @@ static char* ReadShaderSource(const char* shaderFile) return NULL; } -void addPrim(std::vector* p, Primitive newPrim) +void AddPrimitive(Primitive newPrim) { - p->push_back(newPrim); + scene::p_diffuse.push_back(newPrim.diffuse_color); + scene::p_specular.push_back(newPrim.specular_color); + scene::p_ambient.push_back(newPrim.ambient_color); + scene::p_position.push_back(newPrim.position); + scene::p_spec_exp.push_back(newPrim.specular_exponent); + scene::p_shape.push_back(newPrim.shape); + scene::p_VBO.push_back(newPrim.VBO); + scene::p_VAO.push_back(newPrim.VAO); + scene::p_radius.push_back(newPrim.radius); + scene::p_height.push_back(newPrim.height); + scene::p_inner_radius.push_back(newPrim.inner_radius); + scene::p_outer_radius.push_back(newPrim.outer_radius); + scene::p_size.push_back(newPrim.size); +} + +void AddLight(Light newLight) +{ + scene::l_diffuse.push_back(newLight.diffuse_color); + scene::l_specular.push_back(newLight.specular_color); + scene::l_ambient.push_back(newLight.ambient_color); + scene::l_position.push_back(newLight.position); } static unsigned int CompileShader(unsigned int type, const std::string& source) @@ -161,6 +249,21 @@ void InitCanvas() } +unsigned int InitPrimitive(Primitive newPrim) +{ + glGenBuffers(1, &newPrim.SSBO); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, newPrim.SSBO); + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(newPrim), &newPrim, GL_DYNAMIC_COPY); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); + + glBindBuffer(GL_SHADER_STORAGE_BUFFER, newPrim.SSBO); + GLvoid* p = glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY); + memcpy(p, &newPrim, sizeof(newPrim)); + glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); + + return newPrim.SSBO; +} + //Draw the ImGui user interface void draw_gui(GLFWwindow* window) { @@ -249,6 +352,12 @@ void display(GLFWwindow* window) glUniform3fv(sphere_center_loc, 1, glm::value_ptr(scene::sphere_center)); } + int p_diffuse_loc = glGetUniformLocation(scene::shader, "p_diffuse"); + if (p_diffuse_loc != -1) + { + glUniform3fv(p_diffuse_loc, size(scene::p_diffuse), glm::value_ptr(scene::p_diffuse[0])); + } + glUniform3fv(glGetUniformLocation(scene::shader, "light_pos"), 1, glm::value_ptr(scene::Lp)); glUniform3fv(glGetUniformLocation(scene::shader, "la"), 1, glm::value_ptr(scene::La)); glUniform3fv(glGetUniformLocation(scene::shader, "ld"), 1, glm::value_ptr(scene::Ld)); @@ -261,7 +370,7 @@ void display(GLFWwindow* window) glBindVertexArray(VAO); - glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(vertices) / 3); + glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(canvas) / 3); draw_gui(window); diff --git a/src/Primitive.cpp b/src/Primitive.cpp index 4360037..82440d8 100644 --- a/src/Primitive.cpp +++ b/src/Primitive.cpp @@ -1,43 +1 @@ #include "Primitive.h" - -struct Primitive { - glm::vec3 m_ambientColor; - glm::vec3 m_diffuseColor; - glm::vec3 m_specularColor; - - glm::vec3 m_position; -}; - -struct Sphere : public Primitive -{ - float m_radius; -}; - -struct Torus : public Primitive -{ - float m_outerRadius; - float m_innerRadius; -}; - -struct Cube : public Primitive -{ - float m_size; -}; - -struct RoundedCube : public Primitive -{ - float m_size; - float m_radius; -}; - -struct Cylinder : public Primitive -{ - float m_height; - float m_radius; -}; - -struct Cone : public Primitive -{ - float m_height; - float m_radius; -}; \ No newline at end of file