Verified Commit b1225a59 authored by Sofus Albert Høgsbro Rose's avatar Sofus Albert Høgsbro Rose
Browse files

Zoom in and out!

parent 9cab2174
...@@ -4,6 +4,8 @@ DATE = $(shell date) ...@@ -4,6 +4,8 @@ DATE = $(shell date)
VULKAN_SDK_PATH = /home/sofus/subhome/binaries/vulkan_sdk/VulkanSDK/1.0.61.1/x86_64 VULKAN_SDK_PATH = /home/sofus/subhome/binaries/vulkan_sdk/VulkanSDK/1.0.61.1/x86_64
#Project Dirs #Project Dirs
SRCDIR = src SRCDIR = src
INCDIR = inc INCDIR = inc
...@@ -11,10 +13,14 @@ OBJDIR = obj ...@@ -11,10 +13,14 @@ OBJDIR = obj
BINDIR = bin BINDIR = bin
SHABIN = $(BINDIR)/shaders SHABIN = $(BINDIR)/shaders
SHADIR = shaders SHADIR = shaders
RECBIN = $(BINDIR)/resources
RECDIR = resources
#Flags #Flags
TARGET = learn TARGET = learn
LIBS = -L$(VULKAN_SDK_PATH)/lib `pkg-config --static --libs glfw3` -lvulkan LIBS = -L$(VULKAN_SDK_PATH)/lib `pkg-config --static --libs glfw3` -lvulkan -lOpenImageIO
CXXFLAGS = -Wall -I$(VULKAN_SDK_PATH)/include -std=c++14 -O3 CXXFLAGS = -Wall -I$(VULKAN_SDK_PATH)/include -std=c++14 -O3
CXX = g++ CXX = g++
...@@ -22,6 +28,8 @@ CXX = g++ ...@@ -22,6 +28,8 @@ CXX = g++
LINKER = g++ LINKER = g++
LFLAGS = -Wall -I$(INCDIR) $(LIBS) LFLAGS = -Wall -I$(INCDIR) $(LIBS)
#Shader Compilation - output to SPIR-V bytecode #Shader Compilation - output to SPIR-V bytecode
GL = $(VULKAN_SDK_PATH)/bin/glslangValidator GL = $(VULKAN_SDK_PATH)/bin/glslangValidator
GLFLAGS = -V GLFLAGS = -V
...@@ -32,12 +40,15 @@ F_SHAD = $(SHADIR)/shader.frag ...@@ -32,12 +40,15 @@ F_SHAD = $(SHADIR)/shader.frag
V_OUT = $(SHABIN)/vert.spv V_OUT = $(SHABIN)/vert.spv
F_OUT = $(SHABIN)/frag.spv F_OUT = $(SHABIN)/frag.spv
#Internal Vars
#Internal Vars
SOURCES := $(wildcard $(SRCDIR)/*.cpp) SOURCES := $(wildcard $(SRCDIR)/*.cpp)
INCLUDES := $(wildcard $(SRCDIR)/*.h) INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o) OBJECTS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
#Main Rules #Main Rules
.PHONY: all debug release .PHONY: all debug release
...@@ -45,13 +56,30 @@ all: debug ...@@ -45,13 +56,30 @@ all: debug
# Debug needs to add an extra CXX flag. # Debug needs to add an extra CXX flag.
debug: CXXFLAGS += -DNDEBUG debug: CXXFLAGS += -DNDEBUG
debug: $(BINDIR)/$(TARGET) debug: $(BINDIR)/$(TARGET) resources shaders
release: $(BINDIR)/$(TARGET) release: $(BINDIR)/$(TARGET) resources shaders
#Worker Rules
$(BINDIR)/$(TARGET): $(OBJECTS) $(V_OUT) $(F_OUT)
#Helper Rules
.PHONY: clean shaders resources dir-setup
clean:
-rm -rf $(OBJDIR)/* $(BINDIR)/*
shaders: $(V_OUT) $(F_OUT)
resources:
-cp -r $(RECDIR) $(RECBIN)
dir-setup:
-mkdir -p $(SHABIN)
#Worker Rules
$(BINDIR)/$(TARGET): $(OBJECTS)
@$(LINKER) $(OBJECTS) $(LFLAGS) -o $@ @$(LINKER) $(OBJECTS) $(LFLAGS) -o $@
@echo "Successfully linked "$@" at "$(shell date) @echo "Successfully linked "$@" at "$(shell date)
...@@ -59,20 +87,11 @@ $(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp ...@@ -59,20 +87,11 @@ $(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp
@echo $(OBJECTS) @echo $(OBJECTS)
@$(CXX) $(CXXFLAGS) -c $< -o $@ @$(CXX) $(CXXFLAGS) -c $< -o $@
#Shader Rules #Shader Rules
$(V_OUT): dir-setup $(V_OUT): dir-setup
@$(GL) $(GLFLAGS) $(V_SHAD) -o $@ @$(GL) $(GLFLAGS) $(V_SHAD) -o $@
$(F_OUT): dir-setup $(F_OUT): dir-setup
@$(GL) $(GLFLAGS) $(F_SHAD) -o $@ @$(GL) $(GLFLAGS) $(F_SHAD) -o $@
.PHONY: clean shaders dir-setup
clean:
-rm -rf obj/* bin/*
shaders: $(V_OUT) $(F_OUT)
dir-setup:
-mkdir -p $(SHABIN)
...@@ -212,8 +212,21 @@ class HelloTriangleApplication { ...@@ -212,8 +212,21 @@ class HelloTriangleApplication {
} }
private : private :
//Application Properties
float speedZoom = 1.0f; //Units per second.
//Application State Flags
bool quitApp = false; //Set to true to quit gracefully after drawing the next frame. bool quitApp = false; //Set to true to quit gracefully after drawing the next frame.
//Position Flags.
float currentDist = 2.0f; //Current Zoom.
//Input Flags.
bool movingZoomIn = false;
bool movingZoomOut = false;
//~ bool w_press_flag = false; //A flag triggered when w is pressed.
GLFWwindow* window; //Pointer to the GLFW window. GLFWwindow* window; //Pointer to the GLFW window.
VkInstance instance; //The Vulkan instance itself. VkInstance instance; //The Vulkan instance itself.
VkDebugReportCallbackEXT callback; //Even the debug callback is managed with a created/destroyed handle... VkDebugReportCallbackEXT callback; //Even the debug callback is managed with a created/destroyed handle...
...@@ -273,6 +286,26 @@ class HelloTriangleApplication { ...@@ -273,6 +286,26 @@ class HelloTriangleApplication {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { //Exit the app when we press ESC! if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { //Exit the app when we press ESC!
app->quitApp = true; app->quitApp = true;
} }
if (key == GLFW_KEY_W && action == GLFW_PRESS) {
app->movingZoomIn = true;
std::cout << "Zoom In Start!" << std::endl;
}
if (key == GLFW_KEY_W && action == GLFW_RELEASE) {
app->movingZoomIn = false;
std::cout << "Zoom In Stop!" << std::endl;
}
if (key == GLFW_KEY_S && action == GLFW_PRESS) {
app->movingZoomOut = true;
std::cout << "Zoom Out Start!" << std::endl;
}
if (key == GLFW_KEY_S && action == GLFW_RELEASE) {
app->movingZoomOut = false;
std::cout << "Zoom Out Stop!" << std::endl;
}
} }
void initWindow() { void initWindow() {
...@@ -1723,6 +1756,7 @@ class HelloTriangleApplication { ...@@ -1723,6 +1756,7 @@ class HelloTriangleApplication {
createGraphicsPipeline(); //The graphics pipeline is completely immutable - we have to build it from scratch, for performance. createGraphicsPipeline(); //The graphics pipeline is completely immutable - we have to build it from scratch, for performance.
createFramebuffers(); //Make the framebuffers for all the images in the swap chain - and use the one that corresponds to the retrieved image at draw time. createFramebuffers(); //Make the framebuffers for all the images in the swap chain - and use the one that corresponds to the retrieved image at draw time.
createCommandPool(); //Commands, like draw/memory transfer, are executed by command buffer objects. All the hard setup can be done in multiple threads, then! createCommandPool(); //Commands, like draw/memory transfer, are executed by command buffer objects. All the hard setup can be done in multiple threads, then!
//~ createTextureImage(); //This is where we'll load an image, then upload it to a Vulkan image object.
createVertexBuffer(); //Vertex buffers store vertex data to be read by the GPU. createVertexBuffer(); //Vertex buffers store vertex data to be read by the GPU.
createIndexBuffer(); //Index buffers store indices to the vertex buffer, to reduce redundancy. createIndexBuffer(); //Index buffers store indices to the vertex buffer, to reduce redundancy.
createUniformBuffer(); //Uniform buffers store data accessible from the shaders. createUniformBuffer(); //Uniform buffers store data accessible from the shaders.
...@@ -1812,14 +1846,9 @@ class HelloTriangleApplication { ...@@ -1812,14 +1846,9 @@ class HelloTriangleApplication {
vkQueueWaitIdle(presentQueue); vkQueueWaitIdle(presentQueue);
} }
void updateUniformBuffer() { void updateUniformBuffer(float time) {
//Updates the projection matrix to spin the geometry. //Updates the projection matrix to spin the geometry.
// We want 90 degrees per second regardless of framerate! // We want 90 degrees per second regardless of framerate! Thus, the parameter time.
static auto startTime = std::chrono::high_resolution_clock::now(); //Calculate a precise time.
auto currentTime = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - startTime).count() / 1e6f;
UniformBufferObject ubo = {}; UniformBufferObject ubo = {};
...@@ -1829,7 +1858,7 @@ class HelloTriangleApplication { ...@@ -1829,7 +1858,7 @@ class HelloTriangleApplication {
// Our existing transform is an identity matrix, the angle is time * 90 degrees (corresponding to framerate), then the Z axis. // Our existing transform is an identity matrix, the angle is time * 90 degrees (corresponding to framerate), then the Z axis.
//The view (our position and direction) transform has us looking at the geometry from above, at a 45 degree angle. //The view (our position and direction) transform has us looking at the geometry from above, at a 45 degree angle.
ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); ubo.view = glm::lookAt(glm::vec3(currentDist, currentDist, currentDist), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
// glm::lookAt(eye position, center (lookat) position, "up" axis. // glm::lookAt(eye position, center (lookat) position, "up" axis.
// The projection is a perspective projection with a 45 degree vertical field of view. // The projection is a perspective projection with a 45 degree vertical field of view.
...@@ -1850,16 +1879,41 @@ class HelloTriangleApplication { ...@@ -1850,16 +1879,41 @@ class HelloTriangleApplication {
void mainLoop() { void mainLoop() {
//Time to bring it all together :D //Time to bring it all together :D
auto currentTime = std::chrono::high_resolution_clock::now();
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
static auto startTime = std::chrono::high_resolution_clock::now(); //Won't change - the time that we first start rendering
glfwPollEvents(); //Loops and checks for events until the window "should close". glfwPollEvents(); //Loops and checks for events until the window "should close".
//Time Calculations
//Hacky FPS Indicator
//~ std::cout << "\r" << dTime * 1000 << "ms, " << 1 / dTime << " FPS ";
auto lastTime = currentTime;
currentTime = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - startTime).count() / 1e6f; //Time since we started.
float dTime = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - lastTime).count() / 1e6f; //Time since last frame.
//Handle any new state as a result of triggered events. //Handle any new state as a result of triggered events.
if (quitApp) { if (quitApp) {
break; break;
} }
if (movingZoomIn) {
currentDist -= dTime * speedZoom;
}
if (movingZoomOut) {
currentDist += dTime * speedZoom;
}
//Draw the next frame. //Draw the next frame.
updateUniformBuffer(); updateUniformBuffer(time);
drawFrame(); //Draw frames as fast as possible :P. drawFrame(); //Draw frames as fast as possible :P.
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment