Skip to content

Commit c848f9c

Browse files
committed
Camera
1 parent 9cf2375 commit c848f9c

1 file changed

Lines changed: 83 additions & 4 deletions

File tree

src/main.cpp

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,32 @@ VkDescriptorPool modelDescriptorPool;
4343
VkDescriptorSet modelDescriptorSets[FRAMES_IN_FLIGHT];
4444
VulkanBuffer modelUniformBuffers[FRAMES_IN_FLIGHT];
4545

46+
struct Camera {
47+
glm::vec3 cameraPosition;
48+
glm::vec3 cameraDirection;
49+
glm::vec3 up;
50+
float yaw;
51+
float pitch;
52+
glm::mat4 viewProj;
53+
glm::mat4 view;
54+
glm::mat4 proj;
55+
} camera;
56+
4657
bool handleMessage() {
4758
SDL_Event event;
4859
while (SDL_PollEvent(&event)) {
4960
switch (event.type) {
5061
case SDL_QUIT:
5162
return false;
63+
case SDL_KEYDOWN:
64+
if(event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
65+
SDL_SetRelativeMouseMode(SDL_FALSE);
66+
}
67+
case SDL_MOUSEBUTTONDOWN:
68+
if(event.button.button == SDL_BUTTON_LEFT) {
69+
SDL_SetRelativeMouseMode(SDL_TRUE);
70+
}
71+
break;
5272
}
5373
}
5474
return true;
@@ -304,6 +324,14 @@ void initApplication(SDL_Window* window) {
304324
uploadDataToBuffer(context, &spriteIndexBuffer, indexData, sizeof(indexData));
305325

306326
model = createModel(context, "../data/models/monkey.glb");
327+
328+
{ // Init camera
329+
camera.cameraPosition = glm::vec3(0.0f);
330+
camera.cameraDirection = glm::vec3(0.0f, 0.0f, 1.0f);
331+
camera.up = glm::vec3(0.0f, 1.0f, 0.0f);
332+
camera.yaw = 0.0f;
333+
camera.pitch = 0.0f;
334+
}
307335
}
308336

309337
void recreateSwapchain() {
@@ -390,14 +418,12 @@ void renderApplication() {
390418
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, spritePipeline.pipelineLayout, 0, 1, &spriteDescriptorSet, 0, 0);
391419
vkCmdDrawIndexed(commandBuffer, ARRAY_COUNT(indexData), 1, 0, 0, 0);
392420
#else
393-
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 2.0f));
421+
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 3.0f));
394422
glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f));
395423
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), -time, glm::vec3(0.0f, 1.0f, 0.0f));
396424
glm::mat4 modelMatrix = translationMatrix * scaleMatrix * rotationMatrix;
397425

398-
//glm::mat4 projection = glm::ortho(0.0f, (float)swapchain.width, 0.0f, (float)swapchain.height, 0.0f, 1000.0f);
399-
glm::mat4 projection = getProjectionInverseZ(glm::radians(80.0f), swapchain.width, swapchain.height, 0.01f);
400-
glm::mat4 modelViewProj = projection * modelMatrix;
426+
glm::mat4 modelViewProj = camera.viewProj * modelMatrix;
401427

402428
void* mapped;
403429
VK(vkMapMemory(context->device, modelUniformBuffers[frameIndex].memory, 0, sizeof(glm::mat4), 0, &mapped));
@@ -489,6 +515,50 @@ void shutdownApplication() {
489515
exitVulkan(context);
490516
}
491517

518+
void updateApplication(float delta) {
519+
const uint8_t* keys = SDL_GetKeyboardState(0);
520+
int mouseX, mouseY;
521+
uint32_t mouseButtons = SDL_GetRelativeMouseState(&mouseX, &mouseY);
522+
523+
if(SDL_GetRelativeMouseMode()) {
524+
float cameraSpeed = 5.0f;
525+
float mouseSensitivity = 0.27f;
526+
527+
if(keys[SDL_SCANCODE_W]) {
528+
camera.cameraPosition += glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f)*camera.cameraDirection) * delta * cameraSpeed;
529+
}
530+
if(keys[SDL_SCANCODE_S]) {
531+
camera.cameraPosition -= glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f)*camera.cameraDirection) * delta * cameraSpeed;
532+
}
533+
if(keys[SDL_SCANCODE_A]) {
534+
camera.cameraPosition += glm::normalize(glm::cross(camera.cameraDirection, camera.up)) * delta * cameraSpeed;
535+
}
536+
if(keys[SDL_SCANCODE_D]) {
537+
camera.cameraPosition -= glm::normalize(glm::cross(camera.cameraDirection, camera.up)) * delta * cameraSpeed;
538+
}
539+
if(keys[SDL_SCANCODE_SPACE]) {
540+
camera.cameraPosition += glm::normalize(camera.up) * delta * cameraSpeed;
541+
}
542+
if(keys[SDL_SCANCODE_LSHIFT]) {
543+
camera.cameraPosition -= glm::normalize(camera.up) * delta * cameraSpeed;
544+
}
545+
546+
camera.yaw += mouseX * mouseSensitivity;
547+
camera.pitch -= mouseY * mouseSensitivity;
548+
}
549+
550+
if(camera.pitch > 89.0f) camera.pitch = 89.0f;
551+
if(camera.pitch < -89.0f) camera.pitch = -89.0f;
552+
glm::vec3 front;
553+
front.x = cos(glm::radians(camera.pitch)) * sin(glm::radians(camera.yaw));
554+
front.y = sin(glm::radians(camera.pitch));
555+
front.z = cos(glm::radians(camera.pitch)) * cos(glm::radians(camera.yaw));
556+
camera.cameraDirection = glm::normalize(front);
557+
camera.proj = getProjectionInverseZ(glm::radians(45.0f), swapchain.width, swapchain.height, 0.01f);
558+
camera.view = glm::lookAtLH(camera.cameraPosition, camera.cameraPosition + camera.cameraDirection, camera.up);
559+
camera.viewProj = camera.proj * camera.view;
560+
}
561+
492562
int main() {
493563
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
494564
LOG_ERROR("Error initializing SDL: ", SDL_GetError());
@@ -503,8 +573,17 @@ int main() {
503573

504574
initApplication(window);
505575

576+
float delta = 0.0f;
577+
uint64_t perfCounterFrequency = SDL_GetPerformanceFrequency();
578+
uint64_t lastCounter = SDL_GetPerformanceCounter();
506579
while (handleMessage()) {
580+
updateApplication(delta);
507581
renderApplication();
582+
583+
uint64_t endCounter = SDL_GetPerformanceCounter();
584+
uint64_t counterElapsed = endCounter - lastCounter;
585+
delta = ((float)counterElapsed) / (float) perfCounterFrequency;
586+
lastCounter = endCounter;
508587
}
509588

510589
shutdownApplication();

0 commit comments

Comments
 (0)