Added rounded cube + minor changes

This commit is contained in:
Jack Christensen 2023-11-13 00:10:20 -05:00
parent 667d4d7935
commit f0f343d136
3 changed files with 67 additions and 79 deletions

View File

@ -10,13 +10,13 @@ Collapsed=0
[Window][Primitives]
Pos=0,19
Size=3440,103
Size=3440,113
Collapsed=0
DockId=0x00000001,0
[Window][Object Controls]
Pos=3046,124
Size=394,1263
Pos=3046,134
Size=394,1253
Collapsed=0
DockId=0x00000004,0
@ -36,8 +36,8 @@ Collapsed=0
[Docking][Data]
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,19 Size=3440,1368 Split=Y
DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=1904,103 Selected=0x8CCBC963
DockNode ID=0x00000002 Parent=0x3BC79352 SizeRef=1904,956 Split=X
DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=1904,113 Selected=0x8CCBC963
DockNode ID=0x00000002 Parent=0x3BC79352 SizeRef=1904,946 Split=X
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=1508,975 CentralNode=1
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=394,975 Selected=0x3B5216D2

View File

@ -11,9 +11,6 @@ uniform int window_height;
uniform vec3 la;
uniform vec3 ld;
uniform vec3 ls;
uniform vec3 ka;
uniform vec3 kd;
uniform vec3 ks;
uniform float smoothing;
uniform vec3 sphere_center;
@ -22,6 +19,7 @@ const uint P_SPHERE = 0x00;
const uint P_TORUS = 0x01;
const uint P_CONE = 0x02;
const uint P_CUBE = 0x03;
const uint P_ROUNDED_CUBE = 0x04;
struct GLSLPrimitive
{
@ -30,7 +28,7 @@ struct GLSLPrimitive
vec4 specular_color;
vec4 ambient_color;
uint specular_exponent;
int specular_exponent;
uint type;
@ -91,6 +89,10 @@ float cubeSDF(vec3 p, vec3 c, float s)
return d;
}
float roundedCubeSDF(vec3 p, vec3 center, float s, float r) {
return length(max(abs(p - center) - vec3(s), vec3(0))) - r;
}
float sdf(vec3 pos, GLSLPrimitive prim)
{
float d;
@ -116,6 +118,11 @@ float sdf(vec3 pos, GLSLPrimitive prim)
d = cubeSDF(pos, prim.position.xyz, prim.size);
break;
}
case P_ROUNDED_CUBE:
{
d = roundedCubeSDF(pos, prim.position.xyz, prim.size, prim.radius);
break;
}
default:
{
d = 0.0;
@ -165,16 +172,12 @@ void estimateSmoothNormals(vec3 p, float smoothness, float epsilon, inout vec3 n
nx = smoothMinSDF(nx, sdf(vec3(p.x + epsilon, p.y, p.z), primitives[i]), smoothness);
ny = smoothMinSDF(ny, sdf(vec3(p.x, p.y + epsilon, p.z), primitives[i]), smoothness);
nz = smoothMinSDF(nz, sdf(vec3(p.x, p.y, p.z + epsilon), primitives[i]), smoothness);
/*blendFactor = clamp(0.5 + 0.5 * (sdf(p, primitives[i-1]) - sdf(p, primitives[i])) / smoothness, 0.0, 1.0);
blendedColor = mix(primitives[i-1].diffuse_color, primitives[i].diffuse_color, blendFactor);*/
}
nx -= d;
ny -= d;
nz -= d;
normals = normalize(vec3(nx, ny, nz));
//color = blendedColor4;
}
vec3 estimateNormals(vec3 p, GLSLPrimitive prim, float epsilon)
@ -199,17 +202,15 @@ vec4 blendColors(vec4 color1, vec4 color2, float blendFactor)
return mix(color1, color2, blendFactor);
}
float weightFunction(float distance, float k, float epsilon) {
//// Ensure 'k' is positive to avoid division by zero.
//k = max(k, 0.0001);
//// Sigmoid-like smooth transition function.
//return (k*k*k)/distance;
if (k <= 0.0) {
float weightFunction(float distance, float k, float epsilon)
{
if (k <= 0.0)
{
// When k is zero, return 1 if the distance is zero (object is closest), else return 0.
return distance <= 0.001 ? 1.0 : 0.0;
}
else {
else
{
// As k increases, the transition becomes smoother.
return (k * k * k) / distance;
}
@ -236,13 +237,15 @@ void main(void)
vec4 ray_dir = normalize(inverse(V) * cam_dir);
vec4 accumulatedColor = vec4(0.0);
vec4 accumulatedDiffuse = vec4(0.0);
vec4 accumulatedSpecular = vec4(0.0);
vec4 accumulatedAmbient = vec4(0.0);
float accumulatedSpecExp = 0.0;
float totalWeight = 0.0;
int closestIndex = -1;
int secClosestIndex = -1;
while (ray_dist < max_dist && steps < max_steps)
while (ray_dist < max_dist && steps < max_steps && primitives.length() != 0)
{
steps++;
float minDist = 999999.0;
@ -253,16 +256,14 @@ void main(void)
float weight = weightFunction(dist, smoothing, epsilon);
if (dist < sdf(ray_pos.xyz, primitives[closestIndex]))
{
secClosestIndex = closestIndex;
closestIndex = i;
}
/*else if (dist < sdf(ray_pos.xyz, primitives[secClosestIndex]))
{
secClosestIndex = i;
}*/
minDist = smoothMinSDF(minDist, dist, k);
accumulatedColor += primitives[i].diffuse_color * weight;
accumulatedDiffuse += primitives[i].diffuse_color * weight;
accumulatedSpecular += primitives[i].specular_color * weight;
accumulatedAmbient += primitives[i].ambient_color * weight;
accumulatedSpecExp += primitives[i].specular_exponent * weight;
totalWeight += weight;
}
@ -271,16 +272,16 @@ void main(void)
if (minDist <= epsilon)
{
if (totalWeight > 0.0) {
accumulatedColor /= totalWeight;
accumulatedDiffuse /= totalWeight;
accumulatedSpecular /= totalWeight;
accumulatedAmbient /= totalWeight;
accumulatedSpecExp /= totalWeight;
}
vec4 ambient_color, diffuse_color;
float blendFactor = clamp(0.5 + 0.5 * (sdf(ray_pos.xyz, primitives[closestIndex]) - sdf(ray_pos.xyz, primitives[secClosestIndex])) / k, 0.0, 1.0);
vec4 blendedColor = mix(primitives[closestIndex].diffuse_color, primitives[secClosestIndex].diffuse_color, blendFactor);
ambient_color = primitives[closestIndex].ambient_color;
diffuse_color = vec4(accumulatedColor.rgb, 1.0);
ambient_color = vec4(accumulatedAmbient.rgb, 1.0);
diffuse_color = vec4(accumulatedDiffuse.rgb, 1.0);
vec4 ambient = vec4(la, 1.0) * ambient_color;
@ -296,9 +297,9 @@ void main(void)
vec3 vw = normalize(cam_pos - ray_pos.xyz);
vec3 halfwayDir = normalize(lw + vw);
spec = pow(max(dot(halfwayDir, nw), 0), spec_exponent);
spec = pow(max(dot(halfwayDir, nw), 0), int(accumulatedSpecExp));
vec4 specular = spec * vec4(primitives[closestIndex].specular_color.rgb * ls, 1.0);
vec4 specular = spec * vec4(accumulatedSpecular.rgb * ls, 1.0);
color = (ambient + (1.0 / (dist * dist) * (diffuse + specular))).xyz;
break;

View File

@ -32,7 +32,7 @@ struct Primitive
glm::vec4 specular_color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); // 16 bytes
glm::vec4 ambient_color = glm::vec4(0.2f, 0.2f, 0.2f, 1.0f); // 16 bytes
unsigned int specular_exponent = 40; // 4 bytes
int specular_exponent = 40; // 4 bytes
unsigned int type = -1; // 4 bytes
@ -87,9 +87,6 @@ namespace scene
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 ka = { 0.25f, 0.25f, 0.25f }; // Object ambient color
glm::vec3 kd = { 193.0 / 255.0, 41.0 / 255.0, 46.0 / 255.0 }; // Object diffuse color
glm::vec3 ks = { 1.0f, 1.0f, 1.0f }; // Object specular color
int specular_exponent = 10;
float smoothing = 0.0f;
@ -242,6 +239,11 @@ std::string GetName(Primitive prim)
name = "Cube";
break;
}
case 0x04:
{
name = "Rounded_Cube";
break;
}
default:
{
name = "Primitive";
@ -261,7 +263,7 @@ void PrimGUI(unsigned int obj)
switch (scene::primitiveData[obj].type)
{
case 0x00:
{
{
ImGui::SliderFloat("Radius", &scene::primitiveData[obj].radius, 0.f, 5.0f);
break;
}
@ -280,6 +282,12 @@ void PrimGUI(unsigned int obj)
ImGui::SliderFloat("Size", &scene::primitiveData[obj].size, 0.f, 5.0f);
break;
}
case 0x04:
{
ImGui::SliderFloat("Size", &scene::primitiveData[obj].size, 0.f, 5.0f);
ImGui::SliderFloat("Radius", &scene::primitiveData[obj].radius, 0.f, 2.0f);
break;
}
default:
{
break;
@ -290,6 +298,7 @@ void PrimGUI(unsigned int obj)
ImGui::ColorEdit3("Diffuse", glm::value_ptr(scene::primitiveData[obj].diffuse_color));
ImGui::ColorEdit3("Specular", glm::value_ptr(scene::primitiveData[obj].specular_color));
ImGui::ColorEdit3("Ambient", glm::value_ptr(scene::primitiveData[obj].ambient_color));
ImGui::SliderInt("Specular Exponent", &scene::primitiveData[obj].specular_exponent, 1, 500);
}
void AddSphere()
@ -314,6 +323,14 @@ void AddCube()
c.size = 0.5f;
AddPrimitive(c);
}
void AddRoundedCube()
{
Primitive rc;
rc.type = P_ROUNDED_CUBE;
rc.size = 0.25f;
rc.radius = 0.025f;
AddPrimitive(rc);
}
//Draw the ImGui user interface
void draw_gui(GLFWwindow* window)
@ -402,6 +419,11 @@ void draw_gui(GLFWwindow* window)
{
AddCube();
InitPrimitive();
}ImGui::SameLine();
if (ImGui::Button("Rounded Cube", ImVec2(ImGui::GetWindowSize().y * 0.5f, ImGui::GetWindowSize().y * 0.5f)))
{
AddRoundedCube();
InitPrimitive();
}
ImGui::End();
@ -472,11 +494,6 @@ void display(GLFWwindow* window)
glUniform3fv(glGetUniformLocation(scene::shader, "ld"), 1, glm::value_ptr(scene::Ld));
glUniform3fv(glGetUniformLocation(scene::shader, "ls"), 1, glm::value_ptr(scene::Ls));
glUniform3fv(glGetUniformLocation(scene::shader, "ka"), 1, glm::value_ptr(scene::ka));
glUniform3fv(glGetUniformLocation(scene::shader, "kd"), 1, glm::value_ptr(scene::kd));
glUniform3fv(glGetUniformLocation(scene::shader, "ks"), 1, glm::value_ptr(scene::ks));
glUniform1i(glGetUniformLocation(scene::shader, "spec_exponent"), scene::specular_exponent);
UpdatePrimitive();
glBindVertexArray(VAO);
@ -587,37 +604,7 @@ void init()
scene::shader = CreateShader("shaders/parade_vs.glsl", "shaders/parade_fs.glsl");
glUseProgram(scene::shader);
Primitive sphere;
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;
Primitive cube;
cube.diffuse_color = glm::vec4(255.f / 255.f, 176.f / 255.f, 0.f, 1.0f);
cube.type = P_CUBE;
cube.size = 0.5f;
Primitive sphere2;
sphere2.diffuse_color = glm::vec4(1.f, 0.f, 1.f, 1.0f);
sphere2.type = P_SPHERE;
sphere2.radius = 0.25f;
AddPrimitive(sphere);
AddPrimitive(torus);
AddPrimitive(cube);
AddPrimitive(sphere2);
AddRoundedCube();
InitCanvas();
InitPrimitive();