Skip to content

Commit 43157b2

Browse files
author
Tymofiy Sompura
committed
draw cube and fix camera
1 parent 1a50443 commit 43157b2

12 files changed

Lines changed: 357 additions & 136 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ coverage*
1616
*.log
1717
*.profraw
1818
.cache
19-
shaders/slang.spv
19+
shaders/*.spv
2020

2121
# Test assets
2222
assets/models/

CMakeLists.txt

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,8 @@ target_sources(${EXEC_NAME} PRIVATE
5050
src/app/Scene.cpp
5151
)
5252

53-
set (SHADERS_DIR ${CMAKE_CURRENT_LIST_DIR}/shaders)
54-
55-
function (add_slang_shader_target TARGET)
56-
cmake_parse_arguments ("SHADER" "" "" "SOURCES" ${ARGN})
57-
set (ENTRY_POINTS -entry vertMain -entry fragMain)
58-
add_custom_command (
59-
OUTPUT ${SHADERS_DIR}
60-
COMMAND ${CMAKE_COMMAND} -E make_directory ${SHADERS_DIR}
61-
)
62-
add_custom_command (
63-
OUTPUT ${SHADERS_DIR}/slang.spv
64-
COMMAND slangc ${SHADER_SOURCES} -verbose-paths -target spirv -profile spirv_1_4 -emit-spirv-directly -fvk-use-entrypoint-name ${ENTRY_POINTS} -o slang.spv
65-
WORKING_DIRECTORY ${SHADERS_DIR}
66-
DEPENDS ${SHADERS_DIR} ${SHADER_SOURCES}
67-
COMMENT "Compiling Slang Shaders"
68-
VERBATIM
69-
)
70-
add_custom_target (${TARGET} DEPENDS ${SHADERS_DIR}/slang.spv)
71-
endfunction()
72-
73-
find_program(SLANGC_EXECUTABLE slangc)
74-
if(SLANGC_EXECUTABLE)
75-
message(STATUS "Found slangc: ${SLANGC_EXECUTABLE}")
76-
add_slang_shader_target(shadersTarget SOURCES ${SHADERS_DIR}/shader.slang)
77-
add_dependencies(${EXEC_NAME} shadersTarget)
78-
else()
79-
message(WARNING "slangc not found - skipping shader compilation. Install from: https://github.com/shader-slang/slang/releases")
80-
# Create empty shader target so build doesn't fail
81-
add_custom_target(shadersTarget)
82-
endif()
53+
add_subdirectory(${PROJECT_SOURCE_DIR}/shaders)
54+
add_dependencies(${EXEC_NAME} shaders)
8355

8456
add_subdirectory(${PROJECT_SOURCE_DIR}/src/launcher)
8557

CMakePresets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": 2,
2+
"version": 3,
33
"configurePresets": [
44
{
55
"name": "vcpkg",

include/app/Camera.hpp

Lines changed: 80 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace sauce {
88
struct CameraCreateInfo {
99
float scrWidth;
1010
float scrHeight;
11-
glm::vec3 pos = { 0, 0, 1 };
12-
glm::vec3 worldUp = { 0, 1, 0 };
11+
glm::vec3 pos = { 2.0f, 2.0f, 2.0f };
12+
glm::vec3 worldUp = { 0.0f, 0.0f, 1.0f };
1313
float yaw = YAW_DEFAULT;
1414
float pitch = PITCH_DEFAULT;
1515
float fov = FOV_DEFAULT;
@@ -44,55 +44,59 @@ class Camera {
4444
movementSpeed(createInfo.movementSpeed), mouseSensitivity(createInfo.mouseSensitivity),
4545
scrWidth(createInfo.scrWidth), scrHeight(createInfo.scrHeight)
4646
{
47-
updateView();
47+
updateView();
4848
}
4949

50-
/**
51-
* Sets camera position
52-
*
53-
* @param pos - position to move the camera to
54-
*/
55-
void setPos(glm::vec3 pos) {
56-
this->pos=pos;
57-
updateView();
58-
}
59-
60-
/**
61-
* Translates the camera position by offset
62-
*
63-
* @param offs - offset by which to translate the camera
64-
*/
65-
void translate(glm::vec3 offs) {
66-
translate(offs.x, offs.y, offs.z);
67-
}
68-
69-
/**
70-
* Translates the camera position by (x, y, z)
71-
*/
72-
void translate(float x, float y, float z) {
50+
/**
51+
* Sets camera position
52+
*
53+
* @param pos - position to move the camera to
54+
*/
55+
void setPos(glm::vec3 pos) {
56+
this->pos=pos;
57+
updateView();
58+
}
59+
60+
glm::vec3 getPos() const {
61+
return this->pos;
62+
}
63+
64+
/**
65+
* Translates the camera position by offset
66+
*
67+
* @param offs - offset by which to translate the camera
68+
*/
69+
void translate(glm::vec3 offs) {
70+
translate(offs.x, offs.y, offs.z);
71+
}
72+
73+
/**
74+
* Translates the camera position by (x, y, z)
75+
*/
76+
void translate(float x, float y, float z) {
7377
this->pos.x+=x;
7478
this->pos.y+=y;
7579
this->pos.z+=z;
7680
updateView();
77-
}
78-
79-
/**
80-
* Get current FOV
81-
*/
82-
float getFOV() const { return fov; }
83-
84-
/**
85-
* Set camera FOV
86-
*/
87-
void setFOV(float fov) { this->fov=fov; }
88-
89-
/**
90-
* Get view matrix from the current view vectors
91-
*
92-
* @return the view matrix for this camera
93-
*/
94-
glm::mat4 getViewMatrix() const {
95-
return glm::lookAt(this->pos, this->front, this->up);
81+
}
82+
83+
/**
84+
* Get current FOV
85+
*/
86+
float getFOV() const { return fov; }
87+
88+
/**
89+
* Set camera FOV
90+
*/
91+
void setFOV(float fov) { this->fov=fov; }
92+
93+
/**
94+
* Get view matrix from the current view vectors
95+
*
96+
* @return the view matrix for this camera
97+
*/
98+
glm::mat4 getViewMatrix() const {
99+
return glm::lookAt(this->pos, this->pos + this->front, this->worldUp);
96100
}
97101

98102
/**
@@ -101,7 +105,7 @@ class Camera {
101105
* @return projection matrix for this camera
102106
*/
103107
glm::mat4 getProjectionMatrix() const {
104-
return glm::perspective(this->fov, scrWidth/scrHeight, 0.1f, 100.f);
108+
return glm::perspective(glm::radians(this->fov), scrWidth/scrHeight, 0.1f, 100.f);
105109
}
106110

107111
/**
@@ -140,44 +144,48 @@ class Camera {
140144
* @param constrainPitch - whether or not to clamp pitch when out of bounds
141145
*/
142146
void processMouseMovement(float xoffset, float yoffset, bool constrainPitch = true) {
143-
xoffset=glm::radians(xoffset);
144-
yoffset=glm::radians(yoffset);
147+
yaw += xoffset;
148+
pitch += yoffset;
149+
145150
if (constrainPitch) {
146-
yoffset=(yoffset>CameraCreateInfo::PITCH_MAX)? CameraCreateInfo::PITCH_MAX : ((yoffset<CameraCreateInfo::PITCH_MIN)? CameraCreateInfo::PITCH_MIN : yoffset);
151+
pitch = std::max(std::min(pitch, CameraCreateInfo::PITCH_MAX), CameraCreateInfo::PITCH_MIN);
147152
}
148153

149-
yaw+=glm::radians(xoffset);
150-
pitch+=glm::radians(yoffset);
154+
updateView();
151155
}
152156

153157
private:
154-
glm::vec3 pos;
158+
glm::vec3 pos;
155159

156-
/* view orientation vectors */
157-
glm::vec3 front, up, right;
160+
/* view orientation vectors */
161+
glm::vec3 front, up, right;
158162

159-
/* Used to get the right vector from front vector */
160-
glm::vec3 worldUp;
163+
/* Used to get the right vector from front vector */
164+
glm::vec3 worldUp;
161165

162-
float movementSpeed, mouseSensitivity;
166+
float movementSpeed, mouseSensitivity;
163167

164-
// euler Angles
165-
float yaw, pitch;
168+
// euler Angles
169+
float yaw, pitch;
166170

167-
float fov;
168-
float scrWidth, scrHeight;
171+
float fov;
172+
float scrWidth, scrHeight;
169173

170-
/**
171-
* Sets the front, right, and up vectors using the current values for yaw and pitch.
172-
* This should be called any time the camera position or angle is changed.
173-
*/
174-
void updateView() {
175-
front.x=cos(pitch)*cos(yaw);
176-
front.y=sin(pitch);
177-
front.z=cos(pitch)*sin(yaw);
178-
right=glm::normalize(glm::cross(worldUp, front));
179-
up=glm::normalize(glm::cross(front, right));
180-
}
174+
/**
175+
* Sets the front, right, and up vectors using the current values for yaw and pitch.
176+
* This should be called any time the camera position or angle is changed.
177+
*/
178+
void updateView() {
179+
float pitchRad = glm::radians(pitch);
180+
float yawRad = glm::radians(yaw);
181+
182+
front.x = cos(pitchRad) * sin(yawRad);
183+
front.y = cos(pitchRad) * cos(yawRad);
184+
front.z = sin(pitchRad);
185+
186+
right=glm::normalize(glm::cross(front, worldUp));
187+
up=glm::normalize(glm::cross(right, front));
188+
}
181189
};
182190

183191
}

include/app/GraphicsPipeline.hpp

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ namespace sauce {
1515

1616
struct GraphicsPipeline {
1717

18-
GraphicsPipeline(const sauce::LogicalDevice& logicalDevice, const vk::raii::DescriptorSetLayout& descriptorSetLayout, const sauce::SwapChain& swapChain) {
19-
vk::raii::ShaderModule shaderModule = createShaderModule(logicalDevice, readBinaryFile("shaders/slang.spv"));
18+
GraphicsPipeline(
19+
const sauce::PhysicalDevice& physicalDevice,
20+
const sauce::LogicalDevice& logicalDevice,
21+
const vk::raii::DescriptorSetLayout& descriptorSetLayout,
22+
const sauce::SwapChain& swapChain
23+
) {
24+
vk::raii::ShaderModule shaderModule = createShaderModule(logicalDevice, readBinaryFile("shaders/shader_lights.spv"));
2025
vk::PipelineShaderStageCreateInfo vertShaderCreateInfo {
2126
.stage = vk::ShaderStageFlagBits::eVertex,
2227
.module = shaderModule,
@@ -66,6 +71,15 @@ struct GraphicsPipeline {
6671
.sampleShadingEnable = vk::False,
6772
};
6873

74+
75+
vk::PipelineDepthStencilStateCreateInfo depthStencil {
76+
.depthTestEnable = vk::True,
77+
.depthWriteEnable = vk::True,
78+
.depthCompareOp = vk::CompareOp::eLess,
79+
.depthBoundsTestEnable = vk::False,
80+
.stencilTestEnable = vk::False,
81+
};
82+
6983
vk::PipelineColorBlendAttachmentState colorBlendAttachment {
7084
.blendEnable = vk::False,
7185
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA,
@@ -96,9 +110,13 @@ struct GraphicsPipeline {
96110

97111
layout = vk::raii::PipelineLayout { *logicalDevice, pipelineLayoutInfo };
98112

113+
114+
vk::Format depthFormat = findDepthFormat(physicalDevice);
115+
99116
vk::PipelineRenderingCreateInfo renderingCreateInfo {
100117
.colorAttachmentCount = 1,
101118
.pColorAttachmentFormats = &swapChain.getSurfaceFormat().format,
119+
.depthAttachmentFormat = depthFormat,
102120
};
103121

104122
vk::GraphicsPipelineCreateInfo pipelineInfo {
@@ -110,6 +128,7 @@ struct GraphicsPipeline {
110128
.pViewportState = &viewportStateInfo,
111129
.pRasterizationState = &rasterizerInfo,
112130
.pMultisampleState = &multisamplingInfo,
131+
.pDepthStencilState = &depthStencil,
113132
.pColorBlendState = &colorBlendInfo,
114133
.pDynamicState = &dynamicStateInfo,
115134
.layout = layout,
@@ -130,6 +149,15 @@ struct GraphicsPipeline {
130149
const vk::raii::PipelineLayout& getLayout() const noexcept {
131150
return layout;
132151
}
152+
153+
static vk::Format findDepthFormat(const sauce::PhysicalDevice& physicalDevice) {
154+
return findSupportedFormat(
155+
physicalDevice,
156+
{vk::Format::eD32Sfloat, vk::Format::eD32SfloatS8Uint, vk::Format::eD24UnormS8Uint},
157+
vk::ImageTiling::eOptimal,
158+
vk::FormatFeatureFlagBits::eDepthStencilAttachment
159+
);
160+
}
133161

134162
private:
135163
vk::raii::PipelineLayout layout = nullptr;
@@ -161,6 +189,25 @@ struct GraphicsPipeline {
161189
return { *logicalDevice, shaderModuleCreateInfo };
162190
}
163191

192+
193+
static vk::Format findSupportedFormat(const sauce::PhysicalDevice& physicalDevice, const std::vector<vk::Format>& candidates, vk::ImageTiling tiling, vk::FormatFeatureFlags features) {
194+
for (const auto format: candidates) {
195+
vk::FormatProperties props = physicalDevice->getFormatProperties(format);
196+
if (tiling == vk::ImageTiling::eLinear && (props.linearTilingFeatures & features) == features){
197+
return format;
198+
}
199+
if (tiling == vk::ImageTiling::eOptimal && (props.optimalTilingFeatures & features) == features) {
200+
return format;
201+
}
202+
}
203+
204+
throw std::runtime_error("Failed to find supported format!");
205+
}
206+
207+
bool hasStencilComponent(vk::Format format) {
208+
return format == vk::Format::eD32SfloatS8Uint || format == vk::Format::eD24UnormS8Uint;
209+
}
210+
164211
};
165212

166213
}

0 commit comments

Comments
 (0)