diff --git a/Renderer/Renderer.cpp b/Renderer/Renderer.cpp index 7fe6b91..e266138 100644 --- a/Renderer/Renderer.cpp +++ b/Renderer/Renderer.cpp @@ -185,9 +185,9 @@ Renderer::Renderer(Window& win) : win(win) { }); std::vector 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 bindings = { @@ -196,7 +196,6 @@ Renderer::Renderer(Window& win) : win(win) { }; /* initialize models */ -// models.push_back(std::make_shared(phys_dev, dev, "assets/models/dragon.gltf")); Timer model_timer; models.push_back(std::make_shared(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(sz.width) / static_cast(sz.height), 0.01f, 2000.0f); - uniform_buffer->upload(UniformData{ + uniform_buffer->upload(UniformData { .mvp = p * cam.view(), .time = time, - .aspect_ratio = static_cast(sz.width)/static_cast(sz.height), + // .aspect_ratio= static_cast(sz.width) / static_cast(sz.height), + .cam_pos = cam.pos, }); diff --git a/Renderer/UniformBuffer.hpp b/Renderer/UniformBuffer.hpp index 66523a5..da2ff63 100644 --- a/Renderer/UniformBuffer.hpp +++ b/Renderer/UniformBuffer.hpp @@ -12,7 +12,7 @@ struct UniformData { glm::mat4 mvp; float time; - float aspect_ratio; + glm::vec3 cam_pos; }; struct UniformBuffer { diff --git a/UI/UI.cpp b/UI/UI.cpp index 85937fc..bf1bd76 100644 --- a/UI/UI.cpp +++ b/UI/UI.cpp @@ -11,7 +11,7 @@ #include -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"); diff --git a/UI/UI.hpp b/UI/UI.hpp index c334c91..8bd8f85 100644 --- a/UI/UI.hpp +++ b/UI/UI.hpp @@ -12,6 +12,7 @@ struct UI { struct UI_Info { float fps = 0.0; bool& flycam; + float& time; /* camera stuff */ Camera& cam; } info; diff --git a/assets/models/ball.bin b/assets/models/ball.bin new file mode 100644 index 0000000..c6f3a54 Binary files /dev/null and b/assets/models/ball.bin differ diff --git a/assets/models/ball.gltf b/assets/models/ball.gltf new file mode 100644 index 0000000..315c74b --- /dev/null +++ b/assets/models/ball.gltf @@ -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" + } + ] +} diff --git a/assets/shaders/basic.frag b/assets/shaders/basic.frag index f852fc8..dd8796f 100644 --- a/assets/shaders/basic.frag +++ b/assets/shaders/basic.frag @@ -13,4 +13,4 @@ layout (set = 0, binding = 1) uniform sampler2D tex; void main() { FragColor = vec4(norm, 1.0); -} \ No newline at end of file +} \ No newline at end of file diff --git a/assets/shaders/basic.geom b/assets/shaders/basic.geom new file mode 100644 index 0000000..6fc93be --- /dev/null +++ b/assets/shaders/basic.geom @@ -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(); +} \ No newline at end of file diff --git a/assets/shaders/basic.geom.spv b/assets/shaders/basic.geom.spv new file mode 100644 index 0000000..1234c47 Binary files /dev/null and b/assets/shaders/basic.geom.spv differ diff --git a/assets/shaders/fraglight.geom b/assets/shaders/fraglight.geom new file mode 100644 index 0000000..2a304a3 --- /dev/null +++ b/assets/shaders/fraglight.geom @@ -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(); +} \ No newline at end of file diff --git a/assets/shaders/fraglight.geom.spv b/assets/shaders/fraglight.geom.spv new file mode 100644 index 0000000..d212b9e Binary files /dev/null and b/assets/shaders/fraglight.geom.spv differ diff --git a/assets/shaders/fraglight.vert b/assets/shaders/fraglight.vert new file mode 100644 index 0000000..4e94796 --- /dev/null +++ b/assets/shaders/fraglight.vert @@ -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; +} \ No newline at end of file diff --git a/assets/shaders/fraglight.vert.spv b/assets/shaders/fraglight.vert.spv new file mode 100644 index 0000000..2b08c4a Binary files /dev/null and b/assets/shaders/fraglight.vert.spv differ diff --git a/assets/shaders/gol.geom b/assets/shaders/gol.geom new file mode 100644 index 0000000..890f2a5 --- /dev/null +++ b/assets/shaders/gol.geom @@ -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(); +} \ No newline at end of file diff --git a/assets/shaders/gol.geom.spv b/assets/shaders/gol.geom.spv new file mode 100644 index 0000000..af5672d Binary files /dev/null and b/assets/shaders/gol.geom.spv differ diff --git a/assets/shaders/gooch.frag b/assets/shaders/gooch.frag new file mode 100644 index 0000000..8a271a5 --- /dev/null +++ b/assets/shaders/gooch.frag @@ -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); +} \ No newline at end of file diff --git a/assets/shaders/gooch.frag.spv b/assets/shaders/gooch.frag.spv new file mode 100644 index 0000000..f900846 Binary files /dev/null and b/assets/shaders/gooch.frag.spv differ