Got multiple objects working kinda

This commit is contained in:
Jack Christensen 2023-11-11 21:59:53 -05:00
parent 42b93f2a8f
commit bc03bee122
3 changed files with 96 additions and 120 deletions

View File

@ -4,7 +4,7 @@ Size=400,400
Collapsed=0 Collapsed=0
[Window][Debug Window] [Window][Debug Window]
Pos=4,3 Pos=5,7
Size=785,130 Size=791,242
Collapsed=0 Collapsed=0

View File

@ -1,4 +1,5 @@
#version 430 #version 430
#extension GL_NV_uniform_buffer_std430_layout : enable
uniform mat4 P; uniform mat4 P;
uniform mat4 V; uniform mat4 V;
@ -17,27 +18,33 @@ uniform vec3 ks;
uniform float smoothing; uniform float smoothing;
uniform vec3 sphere_center; uniform vec3 sphere_center;
layout (std430, binding=2) buffer newPrim struct GLSLPrimitive
{ {
vec3 diffuse_color; vec4 position;
vec3 specular_color; vec4 diffuse_color;
vec3 ambient_color; vec4 specular_color;
vec3 position; vec4 ambient_color;
unsigned int specular_exponent;
int specular_exponent; unsigned int type;
int shape;
float radius; float radius;
float height; float height;
float inner_radius; float inner_radius;
float outer_radius; float outer_radius;
float size float size;
} };
layout(std430, binding = 0) buffer PrimitiveBuffer {
GLSLPrimitive primitives[];
};
out vec4 FragColor; out vec4 FragColor;
struct Torus struct Torus
{ {
vec4 center;
float R; float R;
float r; float r;
}; };
@ -54,22 +61,23 @@ Sphere sphere;
void initTorus() void initTorus()
{ {
torus.R = 0.5; torus.center = primitives[1].position;
torus.r = 0.15; torus.R = primitives[1].inner_radius;
torus.r = primitives[1].height;
} }
void initSphere() void initSphere()
{ {
sphere.center = sphere_center; sphere.center = primitives[0].position.xyz;
sphere.r = 0.25; sphere.r = primitives[0].radius;
} }
float sphereSDF(vec3 p, vec3 center, float r) { float sphereSDF(vec3 p, vec3 center, float r) {
return length(p - center) - r; return length(p - center) - r;
} }
float torusSDF(vec3 p, float R, float r) { float torusSDF(vec3 p, vec4 center, float R, float r) {
vec2 q = vec2(length(p.xz) - R, p.y); vec2 q = vec2(length(p.xz - center.xz) - R, p.y - center.y);
return length(q) - r; return length(q) - r;
} }
@ -85,9 +93,9 @@ vec3 estimateNormalsSphere(vec3 p)
vec3 estimateNormalsTorus(vec3 p) vec3 estimateNormalsTorus(vec3 p)
{ {
float epsilon = 0.001; float epsilon = 0.001;
float x = torusSDF(vec3(p.x + epsilon, p.y, p.z), torus.R, torus.r) - torusSDF(vec3(p.x - epsilon, p.y, p.z), torus.R, torus.r); float x = torusSDF(vec3(p.x + epsilon, p.y, p.z), torus.center, torus.R, torus.r) - torusSDF(vec3(p.x - epsilon, p.y, p.z), torus.center, torus.R, torus.r);
float y = torusSDF(vec3(p.x, p.y + epsilon, p.z), torus.R, torus.r) - torusSDF(vec3(p.x, p.y - epsilon, p.z), torus.R, torus.r); float y = torusSDF(vec3(p.x, p.y + epsilon, p.z), torus.center, torus.R, torus.r) - torusSDF(vec3(p.x, p.y - epsilon, p.z), torus.center, torus.R, torus.r);
float z = torusSDF(vec3(p.x, p.y, p.z + epsilon), torus.R, torus.r) - torusSDF(vec3(p.x, p.y, p.z - epsilon), torus.R, torus.r); float z = torusSDF(vec3(p.x, p.y, p.z + epsilon), torus.center, torus.R, torus.r) - torusSDF(vec3(p.x, p.y, p.z - epsilon), torus.center, torus.R, torus.r);
return normalize(vec3(x, y, z)); return normalize(vec3(x, y, z));
} }
@ -105,18 +113,18 @@ float smoothMaxSDF(float d1, float d2, float k) {
vec3 estimateSmoothNormals(vec3 p, float smoothness) { vec3 estimateSmoothNormals(vec3 p, float smoothness) {
float epsilon = 0.001; float epsilon = 0.001;
float d = smoothMinSDF(torusSDF(p, torus.R, torus.r), sphereSDF(p, sphere.center, sphere.r), smoothness); float d = smoothMinSDF(torusSDF(p, torus.center, torus.R, torus.r), sphereSDF(p, sphere.center, sphere.r), smoothness);
float nx = smoothMinSDF(torusSDF(vec3(p.x + epsilon, p.y, p.z), torus.R, torus.r), sphereSDF(vec3(p.x + epsilon, p.y, p.z), sphere.center, sphere.r), smoothness) - d; float nx = smoothMinSDF(torusSDF(vec3(p.x + epsilon, p.y, p.z), torus.center, torus.R, torus.r), sphereSDF(vec3(p.x + epsilon, p.y, p.z), sphere.center, sphere.r), smoothness) - d;
float ny = smoothMinSDF(torusSDF(vec3(p.x, p.y + epsilon, p.z), torus.R, torus.r), sphereSDF(vec3(p.x, p.y + epsilon, p.z), sphere.center, sphere.r), smoothness) - d; float ny = smoothMinSDF(torusSDF(vec3(p.x, p.y + epsilon, p.z), torus.center, torus.R, torus.r), sphereSDF(vec3(p.x, p.y + epsilon, p.z), sphere.center, sphere.r), smoothness) - d;
float nz = smoothMinSDF(torusSDF(vec3(p.x, p.y, p.z + epsilon), torus.R, torus.r), sphereSDF(vec3(p.x, p.y, p.z + epsilon), sphere.center, sphere.r), smoothness) - d; float nz = smoothMinSDF(torusSDF(vec3(p.x, p.y, p.z + epsilon), torus.center, torus.R, torus.r), sphereSDF(vec3(p.x, p.y, p.z + epsilon), sphere.center, sphere.r), smoothness) - d;
return normalize(vec3(nx, ny, nz)); return normalize(vec3(nx, ny, nz));
} }
vec3 estimateNormalsMax(vec3 p, float smoothness) { vec3 estimateNormalsMax(vec3 p, float smoothness) {
float epsilon = 0.001; float epsilon = 0.001;
float d = max(torusSDF(p, torus.R, torus.r), sphereSDF(p, sphere.center, sphere.r)); float d = max(torusSDF(p, torus.center, torus.R, torus.r), sphereSDF(p, sphere.center, sphere.r));
float nx = max(torusSDF(vec3(p.x + epsilon, p.y, p.z), torus.R, torus.r), sphereSDF(vec3(p.x + epsilon, p.y, p.z), sphere.center, sphere.r)) - d; float nx = max(torusSDF(vec3(p.x + epsilon, p.y, p.z), torus.center, torus.R, torus.r), sphereSDF(vec3(p.x + epsilon, p.y, p.z), sphere.center, sphere.r)) - d;
float ny = max(torusSDF(vec3(p.x, p.y + epsilon, p.z), torus.R, torus.r), sphereSDF(vec3(p.x, p.y + epsilon, p.z), sphere.center, sphere.r)) - d; float ny = max(torusSDF(vec3(p.x, p.y + epsilon, p.z), torus.center, torus.R, torus.r), sphereSDF(vec3(p.x, p.y + epsilon, p.z), sphere.center, sphere.r)) - d;
float nz = max(torusSDF(vec3(p.x, p.y, p.z + epsilon), torus.R, torus.r), sphereSDF(vec3(p.x, p.y, p.z + epsilon), sphere.center, sphere.r)) - d; float nz = max(torusSDF(vec3(p.x, p.y, p.z + epsilon), torus.center, torus.R, torus.r), sphereSDF(vec3(p.x, p.y, p.z + epsilon), sphere.center, sphere.r)) - d;
return normalize(vec3(nx, ny, nz)); return normalize(vec3(nx, ny, nz));
} }
@ -135,8 +143,9 @@ void main(void)
int spec_exponent = 40; int spec_exponent = 40;
float k = smoothing; float k = smoothing;
initTorus();
initSphere(); initSphere();
initTorus();
vec2 ndc_pos = 2.0 * vec2(gl_FragCoord.x / window_width, gl_FragCoord.y / window_height) - 1.0; vec2 ndc_pos = 2.0 * vec2(gl_FragCoord.x / window_width, gl_FragCoord.y / window_height) - 1.0;
vec4 cam_dir = inverse(P) * vec4(ndc_pos, 1.0, 1.0); vec4 cam_dir = inverse(P) * vec4(ndc_pos, 1.0, 1.0);
@ -148,7 +157,7 @@ void main(void)
while (ray_dist < max_dist && steps < max_steps) while (ray_dist < max_dist && steps < max_steps)
{ {
steps++; steps++;
float distTorus = torusSDF(ray_pos.xyz, torus.R, torus.r); float distTorus = torusSDF(ray_pos.xyz, torus.center, torus.R, torus.r);
float distSphere = sphereSDF(ray_pos.xyz, sphere.center, sphere.r); float distSphere = sphereSDF(ray_pos.xyz, sphere.center, sphere.r);
float minDist = smoothMinSDF(distTorus, distSphere, k); float minDist = smoothMinSDF(distTorus, distSphere, k);
@ -156,8 +165,8 @@ void main(void)
{ {
vec4 ambient_color, diffuse_color; vec4 ambient_color, diffuse_color;
ambient_color = vec4(ka, 1.0); ambient_color = primitives[0].ambient_color;
diffuse_color = vec4(kd, 1.0); diffuse_color = primitives[0].diffuse_color;
vec4 ambient = vec4(la, 1.0) * ambient_color; vec4 ambient = vec4(la, 1.0) * ambient_color;
@ -174,7 +183,7 @@ void main(void)
vec3 halfwayDir = normalize(lw + vw); vec3 halfwayDir = normalize(lw + vw);
spec = pow(max(dot(halfwayDir, nw), 0), spec_exponent); spec = pow(max(dot(halfwayDir, nw), 0), spec_exponent);
vec4 specular = spec * vec4(ks * ls, 1.0); vec4 specular = spec * vec4(primitives[0].specular_color.rgb * ls, 1.0);
color = (ambient + (1.0 / (dist * dist) * (diffuse + specular))).xyz; color = (ambient + (1.0 / (dist * dist) * (diffuse + specular))).xyz;
break; break;

View File

@ -27,25 +27,20 @@
struct Primitive struct Primitive
{ {
glm::vec3 diffuse_color = glm::vec3(0.8f); glm::vec4 position = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
glm::vec3 specular_color = glm::vec3(1.0f); glm::vec4 diffuse_color = glm::vec4(0.8f, 0.8f, 0.8f, 1.0f);
glm::vec3 ambient_color = glm::vec3(0.2f); glm::vec4 specular_color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
glm::vec3 position = glm::vec3(0.0f); glm::vec4 ambient_color = glm::vec4(0.2f, 0.2f, 0.2f, 1.0f);
unsigned int specular_exponent = 40; unsigned int specular_exponent = 40;
unsigned int shape = -1; unsigned int type = -1;
unsigned int SSBO = -1;
unsigned int VBO = -1;
unsigned int VAO = -1;
float radius = -1.f; float radius = -1.f;
float height = -1.f; float height = -1.f;
float inner_radius = -1.f; float inner_radius = -1.f;
float outer_radius = -1.f; float outer_radius = -1.f;
float size = -1.f; float size = -1.f;
std::string name = "Primitive";
}; };
struct Light struct Light
@ -56,23 +51,6 @@ struct Light
glm::vec3 position = glm::vec3(0.0f); 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[] = {
-1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, -1.0f, -1.0f, 0.0f,
@ -85,7 +63,7 @@ unsigned int VAO;
namespace window namespace window
{ {
int size[] = { 800, 600 }; int size[] = { 1920, 1080};
} }
namespace scene namespace scene
@ -99,24 +77,7 @@ 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<Primitive> primitiveData;
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
@ -130,6 +91,8 @@ namespace scene
float smoothing = 0.0f; float smoothing = 0.0f;
glm::vec3 sphere_center = { 0.0, 0.0, 0.0 }; glm::vec3 sphere_center = { 0.0, 0.0, 0.0 };
unsigned int p_vbo = -1;
} }
namespace mouse namespace mouse
@ -163,27 +126,23 @@ static char* ReadShaderSource(const char* shaderFile)
void AddPrimitive(Primitive newPrim) void AddPrimitive(Primitive newPrim)
{ {
scene::p_diffuse.push_back(newPrim.diffuse_color); scene::primitiveData.push_back(newPrim);
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) void InitPrimitive()
{ {
scene::l_diffuse.push_back(newLight.diffuse_color); glGenBuffers(1, &scene::p_vbo);
scene::l_specular.push_back(newLight.specular_color); glBindBuffer(GL_SHADER_STORAGE_BUFFER, scene::p_vbo);
scene::l_ambient.push_back(newLight.ambient_color); glBufferData(GL_SHADER_STORAGE_BUFFER, scene::primitiveData.size() * sizeof(Primitive), scene::primitiveData.data(), GL_DYNAMIC_DRAW);
scene::l_position.push_back(newLight.position); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, scene::p_vbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
void UpdatePrimitive()
{
glBindBuffer(GL_SHADER_STORAGE_BUFFER, scene::p_vbo);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, scene::primitiveData.size() * sizeof(Primitive), scene::primitiveData.data());
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
} }
static unsigned int CompileShader(unsigned int type, const std::string& source) static unsigned int CompileShader(unsigned int type, const std::string& source)
@ -249,21 +208,6 @@ 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)
{ {
@ -281,7 +225,11 @@ void draw_gui(GLFWwindow* window)
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::SliderFloat("Smoothing", &scene::smoothing, 0.0f, 1.0f); ImGui::SliderFloat("Smoothing", &scene::smoothing, 0.0f, 1.0f);
ImGui::SliderFloat3("Sphere Position", glm::value_ptr(scene::sphere_center), -2.0f, 2.0f);
ImGui::SliderFloat4("Position Sphere", glm::value_ptr(scene::primitiveData[0].position), -2.0f, 2.0f);
ImGui::SliderFloat4("Position Torus", glm::value_ptr(scene::primitiveData[1].position), -2.0f, 2.0f);
ImGui::SliderFloat4("Position Cone", glm::value_ptr(scene::primitiveData[2].position), -2.0f, 2.0f);
ImGui::End(); ImGui::End();
static bool show_test = false; static bool show_test = false;
@ -352,12 +300,6 @@ 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));
@ -368,6 +310,7 @@ void display(GLFWwindow* window)
glUniform3fv(glGetUniformLocation(scene::shader, "ks"), 1, glm::value_ptr(scene::ks)); glUniform3fv(glGetUniformLocation(scene::shader, "ks"), 1, glm::value_ptr(scene::ks));
glUniform1i(glGetUniformLocation(scene::shader, "spec_exponent"), scene::specular_exponent); glUniform1i(glGetUniformLocation(scene::shader, "spec_exponent"), scene::specular_exponent);
UpdatePrimitive();
glBindVertexArray(VAO); glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(canvas) / 3); glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(canvas) / 3);
@ -477,7 +420,31 @@ void init()
scene::shader = CreateShader("shaders/parade_vs.glsl", "shaders/parade_fs.glsl"); scene::shader = CreateShader("shaders/parade_vs.glsl", "shaders/parade_fs.glsl");
glUseProgram(scene::shader); glUseProgram(scene::shader);
Primitive sphere;
//sphere.position = glm::vec4(0.0f, -1.0f, 0.0f, 1.0f);
sphere.diffuse_color = glm::vec4(0.f, 66.f / 255.f, 37.f / 255.f, 1.0f);
sphere.type = P_SPHERE;
sphere.radius = 0.25f;
Primitive torus;
torus.diffuse_color = glm::vec4(245.f / 255.f, 245.f / 255.f, 220.f / 255.f, 1.0f);
torus.type = P_TORUS;
torus.inner_radius = 0.15f;
torus.outer_radius = 0.5f;
Primitive cone;
cone.diffuse_color = glm::vec4(255.f / 255.f, 176.f / 255.f, 0.f, 1.0f);
cone.type = P_CONE;
cone.height = 0.5f;
cone.radius = 0.25f;
AddPrimitive(sphere);
AddPrimitive(torus);
AddPrimitive(cone);
//AddPrimitive(sphere);
InitCanvas(); InitCanvas();
InitPrimitive();
} }
int main() int main()