Skip to content
Snippets Groups Projects
Commit f9be0060 authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

[#35]Hacked camera into push constants

parent 2a64c0a1
No related branches found
No related tags found
1 merge request!26Resolve "Kamera - Erstellung und Handling"
Pipeline #25040 failed
......@@ -164,7 +164,7 @@ namespace vkcv
* @brief render a beautiful triangle
*/
void renderTriangle(const PassHandle renderpassHandle, const PipelineHandle pipelineHandle,
const int width, const int height);
const int width, const int height, const size_t pushConstantSize, const void* pushConstantData);
/**
* @brief end recording and present image
......
......@@ -8,15 +8,22 @@ namespace vkcv {
class Camera {
protected:
glm::mat4 m_view, m_projection;
int m_width, m_height;
float m_oldX, m_oldY,
m_near, m_far,
m_fov, m_ratio;
glm::vec3 m_position, m_direction, m_up;
glm::mat4 m_view;
glm::mat4 m_projection;
int m_width;
int m_height;
float m_oldX;
float m_oldY;
float m_near;
float m_far;
float m_fov;
float m_ratio;
glm::vec3 m_position;
glm::vec3 m_direction;
glm::vec3 m_up;
public:
Camera();
......
......@@ -3,11 +3,15 @@
layout(location = 0) out vec3 fragColor;
layout( push_constant ) uniform constants{
mat4 mvp;
};
void main() {
vec3 positions[3] = {
vec3(-0.5, 0.5, 0),
vec3( 0.5, 0.5, 0),
vec3(0, -0.5, 0)
vec3(-0.5, 0.5, -1),
vec3( 0.5, 0.5, -1),
vec3(0, -0.5, -1)
};
vec3 colors[3] = {
......@@ -16,6 +20,6 @@ void main() {
vec3(0, 0, 1)
};
gl_Position = vec4(positions[gl_VertexIndex], 1.0);
gl_Position = mvp * vec4(positions[gl_VertexIndex], 1.0);
fragColor = colors[gl_VertexIndex];
}
\ No newline at end of file
No preview for this file type
......@@ -24,7 +24,7 @@ int main(int argc, const char** argv) {
std::shared_ptr<vkcv::TrackballCamera> trackball;
camera.setPerspective( glm::radians(60.0f), windowWidth / (float)windowHeight, 0.1f, 10.f);
glm::vec3 up(0.0f, 1.0f, 0.0f);
glm::vec3 position(1.0f, 0.0f, 0.0f);
glm::vec3 position(0.0f, 0.0f, 0.0f);
glm::vec3 front(0.0f, 0.0f, -1.0f);
glm::vec3 center = position + front;
camera.lookAt(position, center, up);
......@@ -41,7 +41,7 @@ int main(int argc, const char** argv) {
// showing basic usage lambda events of window
window.e_mouseMove.add([&](double x, double y) {
std::cout << "movement: " << x << " , " << y << std::endl;
//std::cout << "movement: " << x << " , " << y << std::endl;
if (firstMouse) {
lastX = x;
......@@ -77,7 +77,7 @@ int main(int argc, const char** argv) {
center = position + front;
camera.lookAt(position, center, up);
std::cout << "New center: " << center.x << ", " << center.y << ", " << center.z << std::endl;
//std::cout << "New center: " << center.x << ", " << center.y << ", " << center.z << std::endl;
});
window.e_mouseScroll.add([&](double xoffset, double yoffset) {
......@@ -90,37 +90,37 @@ int main(int argc, const char** argv) {
fov = 45.0f;
}
camera.setFov(fov);
std::cout << "New FOV: " << fov << std::endl;
//std::cout << "New FOV: " << fov << std::endl;
});
window.e_key.add([&](int key, int scancode, int action, int mods) {
switch (key) {
case GLFW_KEY_W:
std::cout << "Move forward" << std::endl;
//std::cout << "Move forward" << std::endl;
position += cameraSpeed * front;
center = position + front;
camera.lookAt(position, center, up);
break;
case GLFW_KEY_S:
std::cout << "Move left" << std::endl;
//std::cout << "Move left" << std::endl;
position -= cameraSpeed * front;
center = position + front;
camera.lookAt(position, center, up);
break;
case GLFW_KEY_A:
std::cout << "Move backward" << std::endl;
//std::cout << "Move backward" << std::endl;
position -= glm::normalize(glm::cross(front, up)) * cameraSpeed;
center = position + front;
camera.lookAt(position, center, up);
break;
case GLFW_KEY_D:
std::cout << "Move right" << std::endl;
//std::cout << "Move right" << std::endl;
position += glm::normalize(glm::cross(front, up)) * cameraSpeed;
center = position + front;
camera.lookAt(position, center, up);
break;
default:
std::cout << "this key is not supported yet: " << std::endl;
__nop;//std::cout << "this key is not supported yet: " << std::endl;
}
});
......@@ -211,7 +211,10 @@ int main(int argc, const char** argv) {
while (window.isWindowOpen())
{
core.beginFrame();
core.renderTriangle(trianglePass, trianglePipeline, windowWidth, windowHeight);
const glm::mat4 mvp = camera.getProjection() * camera.getView();
core.renderTriangle(trianglePass, trianglePipeline, windowWidth, windowHeight, sizeof(mvp), &mvp);
core.endFrame();
}
return 0;
......
......@@ -167,7 +167,8 @@ namespace vkcv
}
void Core::renderTriangle(const PassHandle renderpassHandle, const PipelineHandle pipelineHandle,
const int width, const int height) {
const int width, const int height, const size_t pushConstantSize, const void *pushConstantData) {
if (m_currentSwapchainImageIndex == std::numeric_limits<uint32_t>::max()) {
return;
}
......@@ -184,7 +185,9 @@ namespace vkcv
m_CommandResources.commandBuffer.beginRenderPass(beginInfo, subpassContents, {});
const vk::Pipeline pipeline = m_PipelineManager->getVkPipeline(pipelineHandle);
const vk::PipelineLayout pipelineLayout = m_PipelineManager->getVkPipelineLayout(pipelineHandle);
m_CommandResources.commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline, {});
m_CommandResources.commandBuffer.pushConstants(pipelineLayout, vk::ShaderStageFlagBits::eAll, 0, pushConstantSize, pushConstantData);
m_CommandResources.commandBuffer.draw(3, 1, 0, 0, {});
m_CommandResources.commandBuffer.endRenderPass();
}
......
......@@ -140,14 +140,14 @@ namespace vkcv
{ 1.f,1.f,1.f,1.f }
);
const size_t matrixPushConstantSize = 4 * 4 * sizeof(float);
const vk::PushConstantRange pushConstantRange(vk::ShaderStageFlagBits::eAll, 0, matrixPushConstantSize);
// pipeline layout
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo(
{},
0,
{},
0,
{}
);
{},
{},
(pushConstantRange));
vk::PipelineLayout vkPipelineLayout{};
if (m_Device.createPipelineLayout(&pipelineLayoutCreateInfo, nullptr, &vkPipelineLayout) != vk::Result::eSuccess)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment