Arbitrary num of lights started

This commit is contained in:
Jack Christensen 2023-11-13 11:29:48 -05:00
parent f0f343d136
commit 5bf1345de9
3 changed files with 104 additions and 45 deletions

View File

@ -10,13 +10,13 @@ Collapsed=0
[Window][Primitives] [Window][Primitives]
Pos=0,19 Pos=0,19
Size=3440,113 Size=1920,113
Collapsed=0 Collapsed=0
DockId=0x00000001,0 DockId=0x00000001,0
[Window][Object Controls] [Window][Object Controls]
Pos=3046,134 Pos=1526,134
Size=394,1253 Size=394,927
Collapsed=0 Collapsed=0
DockId=0x00000004,0 DockId=0x00000004,0
@ -26,7 +26,7 @@ Collapsed=0
[Window][DockSpace Demo] [Window][DockSpace Demo]
Pos=0,0 Pos=0,0
Size=3440,1387 Size=1920,1061
Collapsed=0 Collapsed=0
[Window][Dear ImGui Demo] [Window][Dear ImGui Demo]
@ -35,7 +35,7 @@ Size=1553,966
Collapsed=0 Collapsed=0
[Docking][Data] [Docking][Data]
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,19 Size=3440,1368 Split=Y DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,19 Size=1920,1042 Split=Y
DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=1904,113 Selected=0x8CCBC963 DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=1904,113 Selected=0x8CCBC963
DockNode ID=0x00000002 Parent=0x3BC79352 SizeRef=1904,946 Split=X DockNode ID=0x00000002 Parent=0x3BC79352 SizeRef=1904,946 Split=X
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=1508,975 CentralNode=1 DockNode ID=0x00000003 Parent=0x00000002 SizeRef=1508,975 CentralNode=1

View File

@ -8,12 +8,11 @@ uniform vec3 cam_pos;
uniform int window_width; uniform int window_width;
uniform int window_height; uniform int window_height;
uniform vec3 la; //uniform vec3 la;
uniform vec3 ld; //uniform vec3 ld;
uniform vec3 ls; //uniform vec3 ls;
uniform float smoothing; uniform float smoothing;
uniform vec3 sphere_center;
const uint P_SPHERE = 0x00; const uint P_SPHERE = 0x00;
const uint P_TORUS = 0x01; const uint P_TORUS = 0x01;
@ -40,11 +39,24 @@ struct GLSLPrimitive
uint dummy; uint dummy;
}; };
struct GLSLLight
{
vec4 position;
vec4 diffuse_color;
vec4 specular_color;
vec4 ambient_color;
};
layout(std430, binding = 0) buffer PrimitiveBuffer layout(std430, binding = 0) buffer PrimitiveBuffer
{ {
GLSLPrimitive primitives[]; GLSLPrimitive primitives[];
}; };
layout(std430, binding = 1) buffer LightBuffer
{
GLSLLight lights[];
};
out vec4 FragColor; out vec4 FragColor;
struct NormalEstimationData struct NormalEstimationData
@ -219,15 +231,14 @@ float weightFunction(float distance, float k, float epsilon)
void main(void) void main(void)
{ {
vec3 normals; vec3 normals;
vec3 color = la; vec3 color = lights[0].ambient_color.rgb;
vec4 ray_pos = vec4(cam_pos, 1.0); vec4 ray_pos = vec4(cam_pos, 1.0);
float ray_dist = 0.0; float ray_dist = 0.0;
float max_dist = 100.0; float max_dist = 100.0;
float epsilon = 0.0001; float epsilon = 0.001;
int steps = 0; int steps = 0;
int max_steps = 1000; int max_steps = 1000;
vec3 light_pos = vec3(0.0, 1.0, 0.0); vec3 light_pos = vec3(0.0, 1.0, 0.0);
int spec_exponent = 40;
float k = smoothing; float k = smoothing;
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;
@ -279,29 +290,37 @@ void main(void)
} }
vec4 ambient_color, diffuse_color; vec4 ambient_color, diffuse_color;
vec4 ambient;
vec4 diffuse;
vec4 specular;
float distLight;
vec3 lw;
ambient_color = vec4(accumulatedAmbient.rgb, 1.0); ambient_color = vec4(accumulatedAmbient.rgb, 1.0);
diffuse_color = vec4(accumulatedDiffuse.rgb, 1.0); diffuse_color = vec4(accumulatedDiffuse.rgb, 1.0);
vec4 ambient = vec4(la, 1.0) * ambient_color; for (int i = 0; i < lights.length(); i++)
{
ambient += lights[i].ambient_color * ambient_color;
//normals = estimateNormals(ray_pos.xyz, primitives[closestIndex], epsilon); //normals = estimateNormals(ray_pos.xyz, primitives[closestIndex], epsilon);
estimateSmoothNormals(ray_pos.xyz, k, epsilon, normals, diffuse_color); estimateSmoothNormals(ray_pos.xyz, k, epsilon, normals, diffuse_color);
vec3 nw = normalize(normals); vec3 nw = normalize(normals);
vec3 lw = normalize(light_pos - ray_pos.xyz); lw = normalize(lights[i].position.xyz - ray_pos.xyz);
float dist = length(vec4(ray_pos.xyz - light_pos, 1.0)); distLight = length(vec4(ray_pos.xyz - lights[i].position.xyz, 1.0));
vec4 diffuse = max(dot(nw, lw), 0) * diffuse_color * vec4(ld, 1.0); diffuse += max(dot(nw, lw), 0) * diffuse_color * lights[i].diffuse_color;
float spec; float spec;
vec3 vw = normalize(cam_pos - ray_pos.xyz); vec3 vw = normalize(cam_pos - ray_pos.xyz);
vec3 halfwayDir = normalize(lw + vw); vec3 halfwayDir = normalize(lw + vw);
spec = pow(max(dot(halfwayDir, nw), 0), int(accumulatedSpecExp)); spec = pow(max(dot(halfwayDir, nw), 0), int(accumulatedSpecExp));
vec4 specular = spec * vec4(accumulatedSpecular.rgb * ls, 1.0); specular += spec * accumulatedSpecular * lights[i].specular_color;
color = (ambient + (1.0 / (distLight * distLight) * (diffuse + specular))).xyz;
}
color = (ambient + (1.0 / (dist * dist) * (diffuse + specular))).xyz;
break; break;
} }
ray_pos += ray_dir * minDist; ray_pos += ray_dir * minDist;

View File

@ -18,12 +18,12 @@
#include <Camera.h> #include <Camera.h>
//#include <Primitive.h> //#include <Primitive.h>
#define P_SPHERE 0x00; constexpr auto P_SPHERE = 0x00;
#define P_TORUS 0x01; constexpr auto P_TORUS = 0x01;
#define P_CONE 0x02; constexpr auto P_CONE = 0x02;
#define P_CUBE 0x03; constexpr auto P_CUBE = 0x03;
#define P_ROUNDED_CUBE 0x04; constexpr auto P_ROUNDED_CUBE = 0x04;
#define P_CYLINDER 0x05; constexpr auto P_CYLINDER = 0x05;
struct Primitive struct Primitive
{ {
@ -48,10 +48,10 @@ struct Primitive
struct Light struct Light
{ {
glm::vec3 diffuse_color = glm::vec3(0.8f); glm::vec4 position = glm::vec4(0.f, 0.f, 0.f, 1.f);
glm::vec3 specular_color = glm::vec3(1.0f); glm::vec4 diffuse_color = glm::vec4(0.8f, 0.8f, 0.8f, 1.f);
glm::vec3 ambient_color = glm::vec3(0.2f); glm::vec4 specular_color = glm::vec4(1.f, 1.f, 1.f, 1.f);
glm::vec3 position = glm::vec3(0.0f); glm::vec4 ambient_color = glm::vec4(.2f, 0.2f, 0.2f, 1.f);
}; };
float canvas[] = { float canvas[] = {
@ -71,7 +71,6 @@ namespace window
namespace scene namespace scene
{ {
//std::vector<Primitive> prims;
Camera camera; Camera camera;
unsigned int shader = -1; unsigned int shader = -1;
@ -81,17 +80,16 @@ namespace scene
float pitch = 0.f; float pitch = 0.f;
std::vector<Primitive> primitiveData; std::vector<Primitive> primitiveData;
std::vector<Light> lightData;
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
glm::vec3 Ls = { 1.f, 1.f, 1.f }; // Light specular color glm::vec3 Ls = { 1.f, 1.f, 1.f }; // Light specular color
int specular_exponent = 10;
float smoothing = 0.0f; float smoothing = 0.0f;
glm::vec3 sphere_center = { 0.0, 0.0, 0.0 };
unsigned int l_vbo = -1;
unsigned int p_vbo = -1; unsigned int p_vbo = -1;
int object = 0; int object = 0;
} }
@ -143,6 +141,15 @@ void InitPrimitive()
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
} }
void InitLights()
{
glGenBuffers(1, &scene::l_vbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, scene::l_vbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, scene::lightData.size() * sizeof(Light), scene::lightData.data(), GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, scene::l_vbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
void UpdatePrimitive() void UpdatePrimitive()
{ {
glBindBuffer(GL_SHADER_STORAGE_BUFFER, scene::p_vbo); glBindBuffer(GL_SHADER_STORAGE_BUFFER, scene::p_vbo);
@ -288,6 +295,12 @@ void PrimGUI(unsigned int obj)
ImGui::SliderFloat("Radius", &scene::primitiveData[obj].radius, 0.f, 2.0f); ImGui::SliderFloat("Radius", &scene::primitiveData[obj].radius, 0.f, 2.0f);
break; break;
} }
//case 0xFF:
//{
// ImGui::SliderFloat("Size", &scene::primitiveData[obj].size, 0.f, 5.0f);
// ImGui::SliderFloat("Radius", &scene::primitiveData[obj].radius, 0.f, 2.0f);
// break;
//}
default: default:
{ {
break; break;
@ -332,6 +345,12 @@ void AddRoundedCube()
AddPrimitive(rc); AddPrimitive(rc);
} }
void AddPointLight()
{
Primitive pl;
AddPrimitive(pl);
}
//Draw the ImGui user interface //Draw the ImGui user interface
void draw_gui(GLFWwindow* window) void draw_gui(GLFWwindow* window)
{ {
@ -400,6 +419,7 @@ void draw_gui(GLFWwindow* window)
{ {
ImGui::RadioButton(GetName(scene::primitiveData[i]).c_str(), &scene::object, i); ImGui::RadioButton(GetName(scene::primitiveData[i]).c_str(), &scene::object, i);
} }
ImGui::Separator();
PrimGUI(scene::object); PrimGUI(scene::object);
ImGui::End(); ImGui::End();
@ -408,21 +428,31 @@ void draw_gui(GLFWwindow* window)
if (ImGui::Button("Sphere", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f))) if (ImGui::Button("Sphere", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f)))
{ {
AddSphere(); AddSphere();
scene::object = scene::primitiveData.size() - 1;
InitPrimitive(); InitPrimitive();
}ImGui::SameLine(); }ImGui::SameLine();
if (ImGui::Button("Torus", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f))) if (ImGui::Button("Torus", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f)))
{ {
AddTorus(); AddTorus();
scene::object = scene::primitiveData.size() - 1;
InitPrimitive(); InitPrimitive();
}ImGui::SameLine(); }ImGui::SameLine();
if (ImGui::Button("Cube", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f))) if (ImGui::Button("Cube", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f)))
{ {
AddCube(); AddCube();
scene::object = scene::primitiveData.size() - 1;
InitPrimitive(); InitPrimitive();
}ImGui::SameLine(); }ImGui::SameLine();
if (ImGui::Button("Rounded Cube", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f))) if (ImGui::Button("Rounded Cube", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f)))
{ {
AddRoundedCube(); AddRoundedCube();
scene::object = scene::primitiveData.size() - 1;
InitPrimitive();
}ImGui::SameLine();
if (ImGui::Button("Point Light", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f)))
{
AddPointLight();
scene::object = scene::primitiveData.size() - 1;
InitPrimitive(); InitPrimitive();
} }
ImGui::End(); ImGui::End();
@ -483,11 +513,6 @@ void display(GLFWwindow* window)
{ {
glUniform1f(smoothing_loc, scene::smoothing); glUniform1f(smoothing_loc, scene::smoothing);
} }
int sphere_center_loc = glGetUniformLocation(scene::shader, "sphere_center");
if (sphere_center_loc != -1)
{
glUniform3fv(sphere_center_loc, 1, glm::value_ptr(scene::sphere_center));
}
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));
@ -606,8 +631,23 @@ void init()
AddRoundedCube(); AddRoundedCube();
Light l;
l.position = glm::vec4{ -sqrt(3.f) / 3.f, sqrt(3.f) / 3.f, -sqrt(3.f) / 3.f, 1.f };
l.diffuse_color = glm::vec4{ 142.f / 255.f, 202.f / 255.f, 230.f / 255.f, 1.f };
scene::lightData.push_back(l);
Light l2;
l2.position = glm::vec4{ sqrt(3.f) / 3.f, sqrt(3.f) / 3.f, sqrt(3.f) / 3.f, 1.f};
l2.diffuse_color = glm::vec4(255.f / 255.f, 183.f / 255.f, 3.f / 255.f, 1.f);
scene::lightData.push_back(l2);
Light l3;
l3.position = glm::vec4{ 0.f, -1.f, 0.f, 1.f };
l3.diffuse_color *= 0.5f;
scene::lightData.push_back(l3);
InitCanvas(); InitCanvas();
InitPrimitive(); InitPrimitive();
InitLights();
} }
int main() int main()