summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/render.c151
-rw-r--r--src/shader.frag1
-rw-r--r--src/shader.vert4
-rw-r--r--src/state.h5
4 files changed, 136 insertions, 25 deletions
diff --git a/src/render.c b/src/render.c
index 50f686e..32ffb7c 100644
--- a/src/render.c
+++ b/src/render.c
@@ -3,6 +3,7 @@
#include <time.h>
#include <shaderc/shaderc.h>
+#include <vulkan/vulkan_core.h>
#define SDL_MAIN_HANDLED
#define VK_USE_PLATFORM_XCB_KHR
@@ -18,6 +19,9 @@
#define STB_IMAGE_IMPLEMENTATION
#include "../lib/stb_image.h"
+/* #define CIMGUI_DEFINE_ENUMS_AND_STRUCTS */
+/* #include "../lib/cimgui/cimgui.h" */
+
//#include "cplusplus.h"
#include "vkutil.h"
#include "state.h"
@@ -89,14 +93,30 @@ Vertex vertices[] = {
(Vertex) { (V3) {0.5f, -0.5f, -0.5f}, (V3) {0.0f, 1.0f, 0.0f}, (V2) {1.0f, 0.0f}},
(Vertex) { (V3) {0.5f, 0.5f, -0.5f}, (V3) {0.0f, 0.0f, 1.0f}, (V2) {1.0f, 1.0f}},
(Vertex) { (V3) {-0.5f, 0.5f, -0.5f}, (V3) {1.0f, 1.0f, 1.0f}, (V2) {0.0f, 1.0f}},
+ (Vertex) { (V3) {0.0f, 0.0f, 0.5f}, (V3) {1.0f, 1.0f, 1.0f}, (V2) {0.0f, 0.0f}},
};
const int VERTICES_SIZE = VK_ARRAY_LEN(vertices);
const uint16_t indices[] = {
- 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4
+ 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 8, 5, 4
};
const int INDICES_SIZE = VK_ARRAY_LEN(indices);
+/* Vertex vertices[] = { */
+/* (Vertex) { (V3) {0}, (V3) {0}, (V2) {0}}, */
+/* (Vertex) { (V3) {1}, (V3) {0}, (V2) {0}}, */
+/* (Vertex) { (V3) {2}, (V3) {0}, (V2) {0}}, */
+/* (Vertex) { (V3) {3}, (V3) {0}, (V2) {0}}, */
+/* (Vertex) { (V3) {4}, (V3) {0}, (V2) {0}}, */
+/* (Vertex) { (V3) {5}, (V3) {0}, (V2) {0}}, */
+/* }; */
+/* const int VERTICES_SIZE = VK_ARRAY_LEN(vertices); */
+
+/* const uint16_t indices[] = { */
+/* 0,0,0,0,0,0 */
+/* }; */
+/* const int INDICES_SIZE = VK_ARRAY_LEN(indices); */
+
static int resizing_event_watcher(void* data, SDL_Event* event) {
if (event->type == SDL_WINDOWEVENT &&
event->window.event == SDL_WINDOWEVENT_RESIZED) {
@@ -105,6 +125,23 @@ static int resizing_event_watcher(void* data, SDL_Event* event) {
return 0;
}
+void move_towards(vec3 position, vec3 front, float step) {
+ // Calculate the direction vector from position to front
+ float direction[3] = {
+ front[0] - position[0],
+ front[1] - position[1],
+ front[2] - position[2]
+ };
+
+ // Normalize the direction vector
+ glm_normalize(direction);
+
+ // Move position along the direction vector by the step amount
+ position[0] += direction[0] * step;
+ position[1] += direction[1] * step;
+ position[2] += direction[2] * step;
+}
+
bool
init()
{
@@ -539,17 +576,55 @@ vulkan_create_logical_device()
vkGetDeviceQueue(s.vk_device, indices.presentFamily, 0, &s.vk_present_queue);
}
+void move_relative(vec3 position, vec3 front, float step, int x) {
+ // Calculate the direction vector from position to front
+ float direction_vec[3] = {
+ front[0] - position[0],
+ front[1] - position[1],
+ front[2] - position[2]
+ };
+
+ // Normalize the direction vector
+ glm_normalize(direction_vec);
+
+ // Calculate the right vector
+ vec3 right;
+ glm_vec3_cross(GLM_ZUP, direction_vec, right);
+ glm_normalize(right);
+
+ // Calculate the up vector
+ vec3 up;
+ glm_vec3_cross(right, direction_vec, up);
+ glm_normalize(up);
+
+ // Move front according to the specified direction
+ if (x) {
+ front[0] += right[0] * step;
+ front[1] += right[1] * step;
+ front[2] += right[2] * step;
+ } else {
+ front[0] += up[0] * step;
+ front[1] += up[1] * step;
+ front[2] += up[2] * step;
+ }
+}
+
void
update_camera(float xoffset, float yoffset)
{
s.camera.yaw += xoffset;
s.camera.pitch += yoffset;
+ vk_log(VK_WARN, "yaw: %f pitch: %f\n", s.camera.yaw, s.camera.pitch);
+
// Make sure that when pitch is out of bounds, the screen doesn't get flipped
- if (s.camera.pitch > 89.0f)
- s.camera.pitch = 89.0f;
- if (s.camera.pitch < -89.0f)
- s.camera.pitch = -89.0f;
+ /* if (s.camera.pitch > 89.0f) */
+ /* s.camera.pitch = 89.0f; */
+ /* if (s.camera.pitch < -89.0f) */
+ /* s.camera.pitch = -89.0f; */
+
+ move_relative(s.camera.pos, s.camera.front, xoffset / 100.0, 1 /* x axis */);
+ move_relative(s.camera.pos, s.camera.front, yoffset / 100.0, 0 /* y axis */);
/* vec3 front; */
/* front[0] = -sin(glm_rad(s.camera.yaw)) * cos(glm_rad(s.camera.pitch)); */
@@ -930,7 +1005,7 @@ vulkan_create_depth_resources()
}
void
-vulkan_create_graphics_pipeline()
+vulkan_create_graphics_pipeline(VkPolygonMode polygon_mode)
{
shaderc_compilation_result_t vert_result = load_compile_shader_data("src/shader.vert", shaderc_vertex_shader);
if (!vert_result) {
@@ -1044,7 +1119,7 @@ vulkan_create_graphics_pipeline()
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.depthClampEnable = VK_FALSE;
rasterizer.rasterizerDiscardEnable = VK_FALSE;
- rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
+ rasterizer.polygonMode = polygon_mode;
rasterizer.lineWidth = 1.0f;
rasterizer.cullMode = VK_CULL_MODE_NONE;
//rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
@@ -1072,13 +1147,6 @@ vulkan_create_graphics_pipeline()
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
- /* colorBlendAttachment.blendEnable = VK_FALSE; */
- /* colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; // Optional */
- /* colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional */
- /* colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; // Optional */
- /* colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; // Optional */
- /* colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional */
- /* colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; // Optional */
VkPipelineColorBlendStateCreateInfo colorBlending = {0};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
@@ -1523,6 +1591,19 @@ recreateSwapChain()
}
void
+recreate_graphics_pipeline()
+{
+ recreateSwapChain();
+ vkDestroyPipeline(s.vk_device, s.vk_graphics_pipeline, NULL);
+ vkDestroyPipelineLayout(s.vk_device, s.vk_pipeline_layout, NULL);
+ vulkan_create_graphics_pipeline(s.polygon_mode);
+}
+
+int polygon_mode_n = 3;
+VkPolygonMode polygon_modes[3] = {VK_POLYGON_MODE_FILL, VK_POLYGON_MODE_LINE,
+ VK_POLYGON_MODE_POINT};
+
+void
handle_input(bool * quit)
{
SDL_Event e;
@@ -1536,18 +1617,38 @@ handle_input(bool * quit)
else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_w:
- s.camera.pos[0] += 0.1f;
+ // s.camera.pos[0] += 0.1f;
+ move_towards(s.camera.front, s.camera.pos, -0.1f);
+ move_towards(s.camera.pos, s.camera.front, 0.1f);
break;
case SDLK_s:
- s.camera.pos[0] -= 0.1f;
+ //s.camera.pos[0] -= 0.1f;
+ move_towards(s.camera.front, s.camera.pos, 0.1f);
+ move_towards(s.camera.pos, s.camera.front, -0.1f);
+ break;
+ case SDLK_a:
+ move_relative(s.camera.front, s.camera.pos, -0.1f, 1);
+ move_relative(s.camera.pos, s.camera.front, 0.1f, 1);
break;
- case SDLK_LEFT:
+ case SDLK_d:
+ move_relative(s.camera.front, s.camera.pos, 0.1f, 1);
+ move_relative(s.camera.pos, s.camera.front, -0.1f, 1);
+ break;
+ case SDLK_q:
+ move_relative(s.camera.front, s.camera.pos, 0.1f, 0);
+ move_relative(s.camera.pos, s.camera.front, 0.1f, 0);
+ break;
+ case SDLK_e:
+ move_relative(s.camera.front, s.camera.pos, -0.1f, 0);
+ move_relative(s.camera.pos, s.camera.front, -0.1f, 0);
break;
case SDLK_g: // reload shaders
- recreateSwapChain();
- vkDestroyPipeline(s.vk_device, s.vk_graphics_pipeline, NULL);
- vkDestroyPipelineLayout(s.vk_device, s.vk_pipeline_layout, NULL);
- vulkan_create_graphics_pipeline();
+ recreate_graphics_pipeline();
+ break;
+ case SDLK_z: // toggle polygon mode
+ polygon_mode_n = (polygon_mode_n + 1) % 3;
+ s.polygon_mode = polygon_modes[polygon_mode_n];
+ recreate_graphics_pipeline();
break;
case SDLK_l:
s.rotate = s.rotate ? 0 : 1;
@@ -1718,7 +1819,7 @@ init_vulkan()
vulkan_create_swap_chain();
vulkan_create_image_views();
vulkan_create_descriptor_set_layout();
- vulkan_create_graphics_pipeline();
+ vulkan_create_graphics_pipeline(s.polygon_mode);
vulkan_create_command_pool();
vulkan_create_depth_resources();
vulkan_create_texture_image();
@@ -1814,8 +1915,8 @@ updateUniformBuffer(uint32_t currentImage)
glm_mat4_identity(ubo.model);
- if (s.rotate)
- glm_rotate(ubo.model, glm_rad(50 * time * glm_rad(90.0f)), GLM_ZUP);
+ if (!s.rotate)
+ glm_rotate(ubo.model, glm_rad(5 * time * glm_rad(90.0f)), GLM_ZUP);
vec3 eye = GLM_VEC3_ONE_INIT;
vec3 center = GLM_VEC3_ZERO_INIT;
@@ -1827,7 +1928,7 @@ updateUniformBuffer(uint32_t currentImage)
/* glm_lookat(eye, center, GLM_ZUP, ubo.view); */
float aspect = s.vk_swap_chain_extent.width / (float)s.vk_swap_chain_extent.height;
- glm_perspective(glm_rad(45.0f ), aspect, 1.0f, 10.0f, ubo.proj);
+ glm_perspective(glm_rad(45.0f ), aspect, 0.1f, 100.0f, ubo.proj);
// Inverting the Y axis for Vulkan
ubo.proj[1][1] *= -1;
diff --git a/src/shader.frag b/src/shader.frag
index 203bf3b..3f76290 100644
--- a/src/shader.frag
+++ b/src/shader.frag
@@ -17,4 +17,5 @@ layout(binding = 0) uniform UniformBufferObject {
void main() {
//outColor = vec4(fragColor * texture(texSampler, fragTexCoord).rgb, 1.0);
outColor = texture(texSampler, fragTexCoord);
+ //outColor = vec4(fragColor ,1);
}
diff --git a/src/shader.vert b/src/shader.vert
index e36a5ea..6c050ac 100644
--- a/src/shader.vert
+++ b/src/shader.vert
@@ -17,6 +17,10 @@ layout(location = 1) out vec2 fragTexCoord;
void main() {
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
//gl_Position = vec4(inPosition * 2, 1.0);
+ gl_PointSize = 10.0f;
+
fragColor = inColor;
fragTexCoord = inTexCoord;
}
+
+
diff --git a/src/state.h b/src/state.h
index 2158b5b..b56544a 100644
--- a/src/state.h
+++ b/src/state.h
@@ -1,6 +1,7 @@
#include <SDL2/SDL.h>
#include <vulkan/vulkan.h>
#include <shaderc/shaderc.h>
+#include <vulkan/vulkan_core.h>
#define CGLM_FORCE_DEPTH_ZERO_TO_ONE
@@ -109,6 +110,8 @@ typedef struct state {
VkImage vk_depth_image;
VkDeviceMemory vk_depth_image_memory;
VkImageView vk_depth_image_view;
+
+ VkPolygonMode polygon_mode;
} state_t ;
static void
@@ -158,4 +161,6 @@ init_state(state_t * s)
s->prev_vert_result = NULL;
s->prev_frag_result = NULL;
+
+ s->polygon_mode = VK_POLYGON_MODE_FILL;
}