~ | Variable refactoring

This commit is contained in:
TheRedShip
2025-01-05 14:50:29 +01:00
parent 846efdd5c6
commit b9b7084a4f
6 changed files with 36 additions and 17 deletions

View File

@ -24,14 +24,16 @@ class Camera
void update(float deltaTime); void update(float deltaTime);
void process_mouse(float xoffset, float yoffset, bool constrainPitch); void processMouse(float xoffset, float yoffset, bool constrainPitch);
void process_keyboard(bool forward, bool backward, bool left, bool right, bool up, bool down); void processKeyboard(bool forward, bool backward, bool left, bool right, bool up, bool down);
glm::mat4 get_view_matrix(); glm::mat4 getViewMatrix();
glm::vec3 get_position(); glm::vec3 getPosition();
void setPosition(glm::vec3 position);
private: private:
void update_camera_vectors(); void updateCameraVectors();
glm::vec3 _position; glm::vec3 _position;
glm::vec3 _forward; glm::vec3 _forward;

View File

@ -24,7 +24,7 @@ class SceneParser
private: private:
void parseMaterial(std::stringstream &line); void parseMaterial(std::stringstream &line);
void parseCamera(std::stringstream &line);
Scene *_scene; Scene *_scene;

View File

@ -49,8 +49,8 @@ int main(int argc, char **argv)
shader.set_int("u_objectsNum", gpu_data.size()); shader.set_int("u_objectsNum", gpu_data.size());
shader.set_float("u_time", (float)(glfwGetTime())); shader.set_float("u_time", (float)(glfwGetTime()));
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT)); shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
shader.set_vec3("u_cameraPosition", scene.getCamera()->get_position()); shader.set_vec3("u_cameraPosition", scene.getCamera()->getPosition());
shader.set_mat4("u_viewMatrix", scene.getCamera()->get_view_matrix()); shader.set_mat4("u_viewMatrix", scene.getCamera()->getViewMatrix());
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1); glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

View File

@ -16,14 +16,14 @@ Camera::Camera(glm::vec3 start_pos, glm::vec3 start_up, float start_yaw, float s
: _position(start_pos), _forward(glm::vec3(0.0f, 0.0f, -1.0f)), _up(start_up), _pitch(start_pitch), _yaw(start_yaw), : _position(start_pos), _forward(glm::vec3(0.0f, 0.0f, -1.0f)), _up(start_up), _pitch(start_pitch), _yaw(start_yaw),
_velocity(0.0f), _acceleration(0.0f) _velocity(0.0f), _acceleration(0.0f)
{ {
update_camera_vectors(); updateCameraVectors();
} }
Camera::~Camera(void) Camera::~Camera(void)
{ {
} }
void Camera::update_camera_vectors() void Camera::updateCameraVectors()
{ {
glm::vec3 frontTemp; glm::vec3 frontTemp;
@ -54,7 +54,7 @@ void Camera::update(float delta_time)
} }
void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch = true) void Camera::processMouse(float xoffset, float yoffset, bool constraint_pitch = true)
{ {
_yaw += xoffset * _sensitivity; _yaw += xoffset * _sensitivity;
_pitch += yoffset * _sensitivity; _pitch += yoffset * _sensitivity;
@ -65,10 +65,10 @@ void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch
if (_pitch < -89.0f) _pitch = -89.0f; if (_pitch < -89.0f) _pitch = -89.0f;
} }
update_camera_vectors(); updateCameraVectors();
} }
void Camera::process_keyboard(bool forward, bool backward, bool left, bool right, bool up, bool down) void Camera::processKeyboard(bool forward, bool backward, bool left, bool right, bool up, bool down)
{ {
glm::vec3 acceleration(0.0f); glm::vec3 acceleration(0.0f);
@ -85,12 +85,17 @@ void Camera::process_keyboard(bool forward, bool backward, bool left, bool righ
_acceleration = acceleration; _acceleration = acceleration;
} }
glm::mat4 Camera::get_view_matrix() glm::mat4 Camera::getViewMatrix()
{ {
return (glm::lookAt(_position, _position + _forward, _up)); return (glm::lookAt(_position, _position + _forward, _up));
} }
glm::vec3 Camera::get_position() glm::vec3 Camera::getPosition()
{ {
return (_position); return (_position);
} }
void Camera::setPosition(glm::vec3 position)
{
_position = position;
}

View File

@ -54,6 +54,16 @@ void SceneParser::parseMaterial(std::stringstream &line)
_scene->addMaterial(mat); _scene->addMaterial(mat);
} }
void SceneParser::parseCamera(std::stringstream &line)
{
float x,y,z;
if (!(line >> x >> y >> z))
throw std::runtime_error("Camera: Missing camera properties");
_scene->getCamera()->setPosition(glm::vec3(x, y, z));
}
bool SceneParser::parseLine(const std::string &line) bool SceneParser::parseLine(const std::string &line)
{ {
if (line.empty() || line[0] == '#') if (line.empty() || line[0] == '#')
@ -77,6 +87,8 @@ bool SceneParser::parseLine(const std::string &line)
if (identifier == "MAT") if (identifier == "MAT")
this->parseMaterial(ss); this->parseMaterial(ss);
else if (identifier == "CAM")
this->parseCamera(ss);
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {

View File

@ -67,7 +67,7 @@ void Window::process_input()
if (forward || backward || left || right || up || down) if (forward || backward || left || right || up || down)
_frameCount = 0; _frameCount = 0;
_scene->getCamera()->process_keyboard(forward, backward, left, right, up, down); _scene->getCamera()->processKeyboard(forward, backward, left, right, up, down);
} }
void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos) void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
@ -89,7 +89,7 @@ void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{ {
win->_scene->getCamera()->process_mouse(xoffset, yoffset, true); win->_scene->getCamera()->processMouse(xoffset, yoffset, true);
win->_frameCount = 0; win->_frameCount = 0;
} }