~ | Better camera managment

This commit is contained in:
TheRedShip
2025-01-09 11:48:32 +01:00
parent dbdd08f3ec
commit e474e451e6
11 changed files with 144 additions and 50 deletions

View File

@ -28,6 +28,12 @@ int main(int argc, char **argv)
GLuint materialSSBO;
glGenBuffers(1, &materialSSBO);
GLuint cameraUBO;
glGenBuffers(1, &cameraUBO);
glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO);
glBufferData(GL_UNIFORM_BUFFER, sizeof(GPUCamera), nullptr, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, cameraUBO);
shader.attach();
Vertex vertices[3] = {{{-1.0f, -1.0f}, {0.0f, 0.0f}},{{3.0f, -1.0f}, {2.0f, 0.0f}},{{-1.0f, 3.0f}, {0.0f, 2.0f}}};
@ -56,12 +62,14 @@ int main(int argc, char **argv)
glBufferData(GL_SHADER_STORAGE_BUFFER, material_data.size() * sizeof(GPUMaterial), material_data.data(), GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, materialSSBO);
GPUCamera camera_data = scene.getCamera()->getGPUData();
glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GPUCamera), &camera_data);
shader.set_int("u_frameCount", window.getFrameCount());
shader.set_int("u_objectsNum", object_data.size());
shader.set_float("u_time", (float)(glfwGetTime()));
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
shader.set_vec3("u_cameraPosition", scene.getCamera()->getPosition());
shader.set_mat4("u_viewMatrix", scene.getCamera()->getViewMatrix());
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

View File

@ -95,7 +95,41 @@ glm::vec3 Camera::getPosition()
return (_position);
}
glm::vec2 Camera::getDirection()
{
return (glm::vec2(_pitch, _yaw));
}
glm::vec2 Camera::getDOV()
{
return (glm::vec2(_aperture_size, _focus_distance));
}
GPUCamera Camera::getGPUData()
{
GPUCamera data;
data.aperture_size = _aperture_size;
data.focus_distance = _focus_distance;
data.camera_position = _position;
data.view_matrix = getViewMatrix();
return (data);
}
void Camera::setPosition(glm::vec3 position)
{
_position = position;
}
void Camera::setDirection(float pitch, float yaw)
{
_pitch = pitch;
_yaw = yaw;
}
void Camera::setDOV(float aperture, float focus)
{
_aperture_size = aperture;
_focus_distance = focus;
}

View File

@ -91,8 +91,8 @@ void Scene::updateGPUData()
else if (obj->getType() == Object::Type::QUAD)
{
auto quad = static_cast<Quad *>(obj);
gpu_obj.vertex1 = quad->getEdge1();
gpu_obj.vertex2 = quad->getEdge2();
gpu_obj.vertex1 = quad->getUp();
gpu_obj.vertex2 = quad->getRight();
}
else if (obj->getType() == Object::Type::TRIANGLE)
{
@ -110,8 +110,8 @@ void Scene::updateGPUData()
else if (obj->getType() == Object::Type::PORTAL)
{
auto portal = static_cast<Portal *>(obj);
gpu_obj.vertex1 = portal->getEdge1();
gpu_obj.vertex2 = portal->getEdge2();
gpu_obj.vertex1 = portal->getUp();
gpu_obj.vertex2 = portal->getRight();
gpu_obj.normal = portal->getNormal();
gpu_obj.transform = glm::mat4(portal->getTransform());

View File

@ -85,12 +85,30 @@ void SceneParser::parseMaterial(std::stringstream &line)
void SceneParser::parseCamera(std::stringstream &line)
{
float x,y,z;
float x,y,z;
float yaw, pitch;
float aperture, focus;
if (!(line >> x >> y >> z))
throw std::runtime_error("Camera: Missing camera properties");
if (!(line >> yaw >> pitch))
{
yaw = 0;
pitch = -90;
}
if (!(line >> aperture >> focus))
{
aperture = 0.0;
focus = 1.0;
}
_scene->getCamera()->setPosition(glm::vec3(x, y, z));
_scene->getCamera()->setDirection(yaw, pitch);
_scene->getCamera()->setDOV(aperture, focus);
_scene->getCamera()->updateCameraVectors();
}
bool SceneParser::parseLine(const std::string &line)

View File

@ -110,6 +110,18 @@ void Window::keyCallback(GLFWwindow *window, int key, int scancode, int action,
{
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
(void) win; (void) key; (void) scancode; (void) action; (void) mods;
if (key == 67 && action == GLFW_PRESS)
{
glm::vec3 pos = win->_scene->getCamera()->getPosition();
glm::vec2 dir = win->_scene->getCamera()->getDirection();
glm::vec2 dov = win->_scene->getCamera()->getDOV();
std::cout << "\nCAM\t" << pos.x << " " << pos.y << " " << pos.z << "\t"
<< dir.x << " " << dir.y << " " << "\t"
<< dov.x << " " << dov.y << " " << "\t"
<< std::endl;
}
}
void Window::display()