Switched up some stuff with Primitive struct

This commit is contained in:
Jack Murphy Christensen 2023-11-09 20:09:35 -05:00
parent 378bffbbfc
commit 42b93f2a8f
5 changed files with 145 additions and 61 deletions

View File

@ -56,8 +56,8 @@ private:
float m_size; float m_size;
public: public:
float& radius() { return m_size; } float& size() { return m_size; }
const float& radius() const { return m_size; } const float& size() const { return m_size; }
}; };
struct RoundedCube : Primitive struct RoundedCube : Primitive
@ -67,8 +67,8 @@ private:
float m_radius; float m_radius;
public: public:
float& radius() { return m_size; } float& size() { return m_size; }
const float& radius() const { return m_size; } const float& size() const { return m_size; }
float& radius() { return m_radius; } float& radius() { return m_radius; }
const float& radius() const { return m_radius; } const float& radius() const { return m_radius; }
@ -81,8 +81,8 @@ private:
float m_radius; float m_radius;
public: public:
float& radius() { return m_height; } float& height() { return m_height; }
const float& radius() const { return m_height; } const float& height() const { return m_height; }
float& radius() { return m_radius; } float& radius() { return m_radius; }
const float& radius() const { return m_radius; } const float& radius() const { return m_radius; }
@ -95,8 +95,8 @@ private:
float m_radius; float m_radius;
public: public:
float& radius() { return m_height; } float& height() { return m_height; }
const float& radius() const { return m_height; } const float& height() const { return m_height; }
float& radius() { return m_radius; } float& radius() { return m_radius; }
const float& radius() const { return m_radius; } const float& radius() const { return m_radius; }

View File

@ -1,4 +1,4 @@
#version 400 #version 430
uniform mat4 P; uniform mat4 P;
uniform mat4 V; uniform mat4 V;
@ -17,6 +17,23 @@ uniform vec3 ks;
uniform float smoothing; uniform float smoothing;
uniform vec3 sphere_center; 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; out vec4 FragColor;
struct Torus struct Torus

View File

@ -1,4 +1,4 @@
#version 400 #version 430
layout(location = 0) in vec3 pos_attrib; layout(location = 0) in vec3 pos_attrib;

View File

@ -16,12 +16,61 @@
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <Camera.h> #include <Camera.h>
#include <Primitive.h> //#include <Primitive.h>
float vertices[] = { #define P_SPHERE 0x00;
-0.5f, -0.5f, 0.0f, #define P_TORUS 0x01;
0.5f, -0.5f, 0.0f, #define P_CONE 0x02;
0.0f, 0.5f, 0.0f #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<glm::vec3> p_diffuse;
std::vector<glm::vec3> p_specular;
std::vector<glm::vec3> p_ambient;
std::vector<glm::vec3> p_position;
std::vector<unsigned int> p_spec_exp;
std::vector<unsigned int> p_shape;
std::vector<float> p_radius;
std::vector<float> p_height;
std::vector<float> p_inner_radius;
std::vector<float> p_outer_radius;
std::vector<float> p_size;
}; };
float canvas[] = { float canvas[] = {
@ -41,7 +90,7 @@ namespace window
namespace scene namespace scene
{ {
std::vector<Primitive> prims; //std::vector<Primitive> prims;
Camera camera; Camera camera;
unsigned int shader = -1; unsigned int shader = -1;
@ -50,6 +99,25 @@ namespace scene
float yaw = -90.f; float yaw = -90.f;
float pitch = 0.f; float pitch = 0.f;
std::vector<glm::vec3> p_diffuse;
std::vector<glm::vec3> p_specular;
std::vector<glm::vec3> p_ambient;
std::vector<glm::vec3> p_position;
std::vector<unsigned int> p_spec_exp;
std::vector<unsigned int> p_shape;
std::vector<unsigned int> p_VBO;
std::vector<unsigned int> p_VAO;
std::vector<float> p_radius;
std::vector<float> p_height;
std::vector<float> p_inner_radius;
std::vector<float> p_outer_radius;
std::vector<float> p_size;
std::vector<glm::vec3> l_diffuse;
std::vector<glm::vec3> l_specular;
std::vector<glm::vec3> l_ambient;
std::vector<glm::vec3> l_position;
glm::vec3 Lp = { 0.f, 1.f, 0.f }; // Light 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 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 glm::vec3 Ld = { 1.f, 1.f, 1.f }; // Light diffuse color
@ -93,9 +161,29 @@ static char* ReadShaderSource(const char* shaderFile)
return NULL; return NULL;
} }
void addPrim(std::vector<Primitive>* 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) 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 //Draw the ImGui user interface
void draw_gui(GLFWwindow* window) void draw_gui(GLFWwindow* window)
{ {
@ -249,6 +352,12 @@ void display(GLFWwindow* window)
glUniform3fv(sphere_center_loc, 1, glm::value_ptr(scene::sphere_center)); 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, "light_pos"), 1, glm::value_ptr(scene::Lp));
glUniform3fv(glGetUniformLocation(scene::shader, "la"), 1, glm::value_ptr(scene::La)); glUniform3fv(glGetUniformLocation(scene::shader, "la"), 1, glm::value_ptr(scene::La));
glUniform3fv(glGetUniformLocation(scene::shader, "ld"), 1, glm::value_ptr(scene::Ld)); glUniform3fv(glGetUniformLocation(scene::shader, "ld"), 1, glm::value_ptr(scene::Ld));
@ -261,7 +370,7 @@ void display(GLFWwindow* window)
glBindVertexArray(VAO); glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(vertices) / 3); glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(canvas) / 3);
draw_gui(window); draw_gui(window);

View File

@ -1,43 +1 @@
#include "Primitive.h" #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;
};