Started experimenting with lighting and pipelines for per-fragment lighting.

This commit is contained in:
connellpaxton 2024-02-03 13:59:36 -05:00
parent 48a3749bd5
commit 8ebae9b362
17 changed files with 274 additions and 11 deletions

View File

@ -185,9 +185,9 @@ Renderer::Renderer(Window& win) : win(win) {
});
std::vector<Shader> shaders = {
{ dev, "assets/shaders/basic.vert.spv", vk::ShaderStageFlagBits::eVertex },
{ dev, "assets/shaders/explode.geom.spv", vk::ShaderStageFlagBits::eGeometry },
{ dev, "assets/shaders/basic.frag.spv", vk::ShaderStageFlagBits::eFragment },
{ dev, "assets/shaders/fraglight.vert.spv", vk::ShaderStageFlagBits::eVertex },
{ dev, "assets/shaders/fraglight.geom.spv", vk::ShaderStageFlagBits::eGeometry },
{ dev, "assets/shaders/gooch.frag.spv", vk::ShaderStageFlagBits::eFragment },
};
std::vector<vk::DescriptorSetLayoutBinding> bindings = {
@ -196,7 +196,6 @@ Renderer::Renderer(Window& win) : win(win) {
};
/* initialize models */
// models.push_back(std::make_shared<Model>(phys_dev, dev, "assets/models/dragon.gltf"));
Timer model_timer;
models.push_back(std::make_shared<Model>(phys_dev, dev, "assets/models/dragon.gltf"));
auto t = model_timer.stop();
@ -271,7 +270,7 @@ void Renderer::draw() {
command_buffer->begin();
vk::ClearValue clear_values[] = {
vk::ClearColorValue(1.0f, 1.0f, 1.0f, 1.0f),
vk::ClearColorValue(0.0f, 0.0f, 0.0f, 0.0f),
vk::ClearDepthStencilValue {.depth = 1.0f}
};
@ -321,10 +320,11 @@ void Renderer::draw() {
const auto p = glm::perspective(glm::radians(90.0f), static_cast<float>(sz.width) / static_cast<float>(sz.height), 0.01f, 2000.0f);
uniform_buffer->upload(UniformData{
uniform_buffer->upload(UniformData {
.mvp = p * cam.view(),
.time = time,
.aspect_ratio = static_cast<float>(sz.width)/static_cast<float>(sz.height),
// .aspect_ratio= static_cast<float>(sz.width) / static_cast<float>(sz.height),
.cam_pos = cam.pos,
});

View File

@ -12,7 +12,7 @@
struct UniformData {
glm::mat4 mvp;
float time;
float aspect_ratio;
glm::vec3 cam_pos;
};
struct UniformBuffer {

View File

@ -11,7 +11,7 @@
#include <Scene/Camera.hpp>
UI::UI(Renderer* ren) : info{ .flycam = ren->flycam, .cam = ren->cam }, dev(ren->dev) {
UI::UI(Renderer* ren) : info{ .flycam = ren->flycam, .time = ren->time, .cam = ren->cam, }, dev(ren->dev) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
@ -86,10 +86,11 @@ void UI::newFrame() {
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::SetNextWindowBgAlpha(0.8f);
ImGui::SetNextWindowBgAlpha(0.5f);
ImGui::Begin("Rendering Info", nullptr);
ImGui::Text("FPS: %f", info.fps);
ImGui::Text("Time: %f", info.time);
ImGui::Checkbox("Fly Camera", &info.flycam);
ImGui::SliderAngle("Theta", &info.cam.theta, 0.01, 179.9);
ImGui::SliderAngle("Phi", &info.cam.phi, 0.0, 360.0, "%.0f def");

View File

@ -12,6 +12,7 @@ struct UI {
struct UI_Info {
float fps = 0.0;
bool& flycam;
float& time;
/* camera stuff */
Camera& cam;
} info;

BIN
assets/models/ball.bin Normal file

Binary file not shown.

108
assets/models/ball.gltf Normal file
View File

@ -0,0 +1,108 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.0.44",
"version":"2.0"
},
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
0,
1,
2,
3
]
}
],
"nodes":[
{
"name":"Mball"
},
{
"name":"Circle",
"translation":[
0,
0,
-2.97784423828125
]
},
{
"name":"Circle.001"
},
{
"mesh":0,
"name":"Sphere"
}
],
"meshes":[
{
"name":"Sphere",
"primitives":[
{
"attributes":{
"POSITION":0,
"NORMAL":1
},
"indices":2
}
]
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":30729,
"max":[
0.9991506934165955,
0.9976890087127686,
0.9973824620246887
],
"min":[
-0.9990072846412659,
-0.9995680451393127,
-0.9988008141517639
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":30729,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5123,
"count":56796,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":368748,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":368748,
"byteOffset":368748,
"target":34962
},
{
"buffer":0,
"byteLength":113592,
"byteOffset":737496,
"target":34963
}
],
"buffers":[
{
"byteLength":851088,
"uri":"ball.bin"
}
]
}

27
assets/shaders/basic.geom Normal file
View File

@ -0,0 +1,27 @@
#version 450 core
layout (triangles) in;
layout (triangle_strip) out;
layout (max_vertices = 3) out;
layout (location = 0) in vec3 norm[];
layout (location = 1) in vec2 texCoord[];
layout (location = 0) out vec3 _norm;
layout (location = 1) out vec2 _texCoord;
layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp;
float time;
};
void main(void) {
for(int i = 0; i < gl_in.length(); i++) {
gl_Position = mvp * gl_in[i].gl_Position;
_norm = norm[i];
_texCoord = texCoord[i];
EmitVertex();
}
EndPrimitive();
}

Binary file not shown.

View File

@ -0,0 +1,30 @@
#version 450 core
layout (triangles) in;
layout (triangle_strip) out;
layout (max_vertices = 3) out;
layout (location = 0) in vec3 norm[];
layout (location = 1) in vec2 texCoord[];
layout (location = 2) in vec3 pos[];
layout (location = 0) out vec3 _norm;
layout (location = 1) out vec2 _texCoord;
layout (location = 2) out vec3 _pos;
layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp;
float time;
};
void main(void) {
for(int i = 0; i < gl_in.length(); i++) {
gl_Position = mvp * gl_in[i].gl_Position;
_norm = norm[i];
_texCoord = texCoord[i];
_pos = pos[i];
EmitVertex();
}
EndPrimitive();
}

Binary file not shown.

View File

@ -0,0 +1,20 @@
#version 460 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNorm;
layout (location = 2) in vec2 aTexCoord;
layout (location = 0) out vec3 norm;
layout (location = 1) out vec2 texCoord;
layout (location = 2) out vec3 pos;
layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp;
float time;
};
void main() {
pos = (aPos + (vec3(10.0) * gl_InstanceIndex));
gl_Position = vec4(pos, 1.0);
texCoord = aTexCoord;
norm = aNorm;
}

Binary file not shown.

44
assets/shaders/gol.geom Normal file
View File

@ -0,0 +1,44 @@
#version 450 core
layout (triangles) in;
layout (triangle_strip) out;
layout (max_vertices = 6) out;
layout (location = 0) in vec3 norm[];
layout (location = 1) in vec2 texCoord[];
layout (location = 0) out vec3 _norm;
layout (location = 1) out vec2 _texCoord;
layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp;
float time;
};
vec4 explode(vec4 pos, vec3 n) {
float mag = 2.0;
vec3 dir = n * (sin(time/10.0)-3.0)/10.0 * mag;
return pos + vec4(dir, 0.0);
}
void main(void) {
if (time < 3.0) {
for(int i = 0; i < gl_in.length(); i++) {
gl_Position = mvp * gl_in[i].gl_Position;
_norm = norm[i];
_texCoord = texCoord[i];
EmitVertex();
}
}
vec3 n = norm[0] + norm[1] + norm[2];
n/=3;
for(int i = 0; i < gl_in.length(); i++) {
gl_Position = mvp * explode(gl_in[i].gl_Position*abs(cos(time)), n);
_texCoord = texCoord[i];
_norm = n;
EmitVertex();
}
EndPrimitive();
}

BIN
assets/shaders/gol.geom.spv Normal file

Binary file not shown.

32
assets/shaders/gooch.frag Normal file
View File

@ -0,0 +1,32 @@
#version 460 core
layout (location = 0) in vec3 norm;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in vec3 pos;
layout (location = 0) out vec4 FragColor;
layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp;
float time;
vec3 cam_pos;
};
layout (set = 0, binding = 1) uniform sampler2D tex;
void main() {
vec3 light_pos = normalize(vec3(cos(time), sin(time), 0.0))*10.0;
/* calculate highlight using angle between fragment and viewer */
vec3 I = normalize(cam_pos-pos);
vec3 L = normalize(light_pos-pos);
vec3 cool = vec3(0.0, 0.0, 0.55);
vec3 warm = vec3(0.3, 0.3, 0);
float t = (dot(norm, L)+1.0)/2.0;
vec3 r = 2.0*(dot(norm,L)*norm)-L;
float s = 100.0*(dot(r,I))-97.0;
if(s < 0.0)
s = 0.0;
else if(s > 1.0)
s = 1.0;
FragColor = vec4((1.0-s)*(t*warm + (vec3(0.5)-t)*cool) + s*vec3(1.0), 1.0);
}

Binary file not shown.