Calculate view_direction from camera rotation

This commit is contained in:
Abdelrahman Said 2024-09-28 21:21:26 +01:00
parent 6721044b6d
commit 4967b33479

View File

@ -49,6 +49,9 @@ struct Camera {
// The up vector in world coordinates // The up vector in world coordinates
glm::vec3 up; glm::vec3 up;
// pitch, yaw
glm::vec2 rotation;
}; };
struct App { struct App {
@ -101,7 +104,7 @@ struct App {
int init (App &app); int init (App &app);
void create_vertex_spec (App &app); void create_vertex_spec (App &app);
void create_graphics_pipeline(App &app); void create_graphics_pipeline (App &app);
void run_main_loop (App &app); void run_main_loop (App &app);
void cleanup (App &app); void cleanup (App &app);
GLuint create_shader_program (const std::string &vertex_shader_source, const std::string &fragment_shader_source); GLuint create_shader_program (const std::string &vertex_shader_source, const std::string &fragment_shader_source);
@ -109,6 +112,7 @@ GLuint compile_shader (GLuint type, const std::string &source);
std::string load_shader (const std::string &filepath); std::string load_shader (const std::string &filepath);
void handle_object_movement (App &app); void handle_object_movement (App &app);
void handle_camera_movement (App &app); void handle_camera_movement (App &app);
glm::vec3 rotation_to_view_direction(const Camera &camera);
int main() { int main() {
App app = {}; App app = {};
@ -176,8 +180,9 @@ int init(App &app) {
app.translation = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 1.5f)); app.translation = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 1.5f));
app.rotation = glm::mat4(1.0f); app.rotation = glm::mat4(1.0f);
app.scale = glm::mat4(1.0f); app.scale = glm::mat4(1.0f);
app.camera.rotation = glm::vec2(0.0f, -90.0f);
app.camera.position = glm::vec3(0.0f, 0.0f, 3.0f); app.camera.position = glm::vec3(0.0f, 0.0f, 3.0f);
app.camera.view_direction = glm::vec3(0.0f, 0.0f, -1.0f); app.camera.view_direction = rotation_to_view_direction(app.camera);
app.camera.up = glm::vec3(0.0f, 1.0f, 0.0f); app.camera.up = glm::vec3(0.0f, 1.0f, 0.0f);
app.model_mat = app.translation * app.rotation * app.scale; app.model_mat = app.translation * app.rotation * app.scale;
app.view_mat = glm::lookAt(app.camera.position, app.camera.position + app.camera.view_direction, app.camera.up); app.view_mat = glm::lookAt(app.camera.position, app.camera.position + app.camera.view_direction, app.camera.up);
@ -446,9 +451,11 @@ void handle_object_movement(App &app) {
void handle_camera_movement(App &app) { void handle_camera_movement(App &app) {
if (app.event.type == SDL_MOUSEMOTION) { if (app.event.type == SDL_MOUSEMOTION) {
glm::vec2 offset = glm::vec2(app.event.motion.xrel, 0) * 0.1f; glm::vec2 offset = glm::vec2(-app.event.motion.yrel, app.event.motion.xrel) * 0.1f;
app.prev_mouse += offset; app.prev_mouse += offset;
app.camera.view_direction = glm::rotate(app.camera.view_direction, glm::radians(offset.x), app.camera.up); app.camera.rotation += offset;
app.camera.rotation.x = glm::clamp(app.camera.rotation.x, -89.0f, 89.0f);
app.camera.view_direction = rotation_to_view_direction(app.camera);
} else { } else {
switch (app.event.key.keysym.sym) { switch (app.event.key.keysym.sym) {
case SDLK_w: case SDLK_w:
@ -470,3 +477,12 @@ void handle_camera_movement(App &app) {
app.view_mat = glm::lookAt(app.camera.position, app.camera.position + app.camera.view_direction, app.camera.up); app.view_mat = glm::lookAt(app.camera.position, app.camera.position + app.camera.view_direction, app.camera.up);
} }
glm::vec3 rotation_to_view_direction(const Camera &camera) {
glm::vec3 direction;
direction.x = cos(glm::radians(camera.rotation.y)) * cos(glm::radians(camera.rotation.x));
direction.y = sin(glm::radians(camera.rotation.x));
direction.z = sin(glm::radians(camera.rotation.y)) * cos(glm::radians(camera.rotation.x));
return glm::normalize(direction);
}