Skip to content

Commit c9a78be

Browse files
committed
Working camera!
1 parent 4bfd2ed commit c9a78be

7 files changed

Lines changed: 130 additions & 7 deletions

File tree

‎src/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(SOURCE_FILES
22
"graphics/graphics.cpp"
33
"graphics/graphics.h"
4-
"types/types.h" "parser/tokenizer.h" "parser/checker.h" "parser/rpn.h" "parser/parser.h" "parser/checker.cpp" "parser/parser.cpp" "parser/rpn.cpp" "parser/tokenizer.cpp" "parser/simplifier.h" "parser/simplifier.cpp" "transformer/constant.h" "transformer/constant.cpp" "preprocessor/preprocessor.h" "preprocessor/string_builder.h" "transformer/operator.h" "transformer/operator.cpp" "transformer/transformer.h" "transformer/transformer.cpp" "preprocessor/string_builder.cpp" "preprocessor/preprocessor.cpp" "interactions/interactions.h" "interactions/interactions.cpp" "parser/validator.cpp" "parser/validator.h" "graphics/ui_init.h" "graphics/ui_init.cpp" "graphics/ui.h" "graphics/ui.cpp" "types/type_mapper.h" "types/type_mapper.cpp" "compiler/compiler_shader.h" "compiler/compiler_shader.cpp" "transformer/tree.h" "transformer/tree.cpp" "higher_order/derivative.h" "higher_order/higher_order.h" "higher_order/derivative.cpp" "higher_order/higher_order.cpp" "graphics/3d/mesh.h" "graphics/3d/mesh.cpp")
4+
"types/types.h" "parser/tokenizer.h" "parser/checker.h" "parser/rpn.h" "parser/parser.h" "parser/checker.cpp" "parser/parser.cpp" "parser/rpn.cpp" "parser/tokenizer.cpp" "parser/simplifier.h" "parser/simplifier.cpp" "transformer/constant.h" "transformer/constant.cpp" "preprocessor/preprocessor.h" "preprocessor/string_builder.h" "transformer/operator.h" "transformer/operator.cpp" "transformer/transformer.h" "transformer/transformer.cpp" "preprocessor/string_builder.cpp" "preprocessor/preprocessor.cpp" "interactions/interactions.h" "interactions/interactions.cpp" "parser/validator.cpp" "parser/validator.h" "graphics/ui_init.h" "graphics/ui_init.cpp" "graphics/ui.h" "graphics/ui.cpp" "types/type_mapper.h" "types/type_mapper.cpp" "compiler/compiler_shader.h" "compiler/compiler_shader.cpp" "transformer/tree.h" "transformer/tree.cpp" "higher_order/derivative.h" "higher_order/higher_order.h" "higher_order/derivative.cpp" "higher_order/higher_order.cpp" "graphics/3d/mesh.h" "graphics/3d/mesh.cpp" "graphics/3d/camera_state.h" "graphics/3d/camera_state.cpp")
55

66
add_executable(complex-plotter ${SOURCE_FILES} "main.cpp")
77
find_package(OpenGL REQUIRED)

‎src/graphics/3d/camera_state.cpp‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <graphics/3d/camera_state.h>
2+
#include <iostream>
3+
CameraState camera_state;
4+
5+
void update_camera_vectors(CameraState& state) {
6+
glm::vec3 new_front;
7+
new_front.x = cos(glm::radians(state.yaw)) * cos(glm::radians(state.pitch));
8+
new_front.y = sin(glm::radians(state.pitch));
9+
new_front.z = sin(glm::radians(state.yaw)) * cos(glm::radians(state.pitch));
10+
state.front = glm::normalize(new_front);
11+
state.right = glm::normalize(glm::cross(state.front, state.world_up));
12+
state.up = glm::normalize(glm::cross(state.right, state.front));
13+
}
14+
15+
glm::mat4 get_view_matrix(const CameraState& state) {
16+
return glm::lookAt(state.position, state.position + state.front, state.up);
17+
}
18+
19+
void process_keyboard(CameraState& state, int direction, float delta_time) {
20+
float velocity = state.movement_speed * delta_time;
21+
if (direction == 0) state.position += state.front * velocity;
22+
if (direction == 1) state.position -= state.front * velocity;
23+
if (direction == 2) state.position -= state.right * velocity;
24+
if (direction == 3) state.position += state.right * velocity;
25+
if (direction == 4) state.position += state.world_up * velocity;
26+
if (direction == 5) state.position -= state.world_up * velocity;
27+
}
28+
29+
void process_mouse_movement(CameraState& state, float x_offset, float y_offset, bool constrain_pitch) {
30+
x_offset *= state.mouse_sensitivity;
31+
y_offset *= state.mouse_sensitivity;
32+
state.yaw += x_offset;
33+
state.pitch += y_offset;
34+
35+
if (constrain_pitch) {
36+
if (state.pitch > 89.0f) state.pitch = 89.0f;
37+
if (state.pitch < -89.0f) state.pitch = -89.0f;
38+
}
39+
update_camera_vectors(state);
40+
}
41+
42+
void process_mouse_scroll(CameraState& state, float y_offset) {
43+
state.zoom -= (float)y_offset;
44+
if (state.zoom < 1.0f) state.zoom = 1.0f;
45+
if (state.zoom > 45.0f) state.zoom = 45.0f;
46+
}
47+
48+
void handle_camera_input(GLFWwindow* window, const float delta_time) {
49+
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
50+
process_keyboard(camera_state, 0, delta_time);
51+
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
52+
process_keyboard(camera_state, 1, delta_time);
53+
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
54+
process_keyboard(camera_state, 2, delta_time);
55+
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
56+
process_keyboard(camera_state, 3, delta_time);
57+
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(window,GLFW_KEY_SPACE) == GLFW_PRESS)
58+
process_keyboard(camera_state, 4, delta_time);
59+
60+
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
61+
process_keyboard(camera_state, 5, delta_time);
62+
}

‎src/graphics/3d/camera_state.h‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
#include <glad/glad.h>
3+
#include <GLFW/glfw3.h>
4+
#include <glm/glm.hpp>
5+
#include <glm/gtc/matrix_transform.hpp>
6+
7+
struct CameraState {
8+
glm::vec3 position = glm::vec3(-10.0f, 5.0f, 3.0f);
9+
glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f);
10+
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
11+
glm::vec3 right;
12+
glm::vec3 world_up = glm::vec3(0.0f, 1.0f, 0.0f);
13+
14+
float yaw = -90.0f;
15+
float pitch = 0.0f;
16+
float movement_speed = 2.5f;
17+
float mouse_sensitivity = 0.05f;
18+
float zoom = 45.0f;
19+
};
20+
21+
extern CameraState camera_state;
22+
23+
24+
void update_camera_vectors(CameraState& state);
25+
26+
glm::mat4 get_view_matrix(const CameraState& state);
27+
28+
void process_keyboard(CameraState& state, int direction, float delta_time);
29+
30+
void process_mouse_movement(CameraState& state, float x_offset, float y_offset, bool constrain_pitch = true);
31+
32+
void process_mouse_scroll(CameraState& state, float y_offset);
33+
34+
void handle_camera_input(GLFWwindow* window, const float delta_time);
35+

‎src/graphics/3d/mesh.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma once
12
#include <vector>
23
#include <glad/glad.h>
34

@@ -8,6 +9,8 @@ struct Mesh {
89

910
using std::vector;
1011

12+
13+
1114
/// <summary>
1215
/// Generates a mesh object of an n x n grid
1316
/// </summary>

‎src/interactions/interactions.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <interactions/interactions.h>
22
#include <graphics/ui.h>
3+
#include <graphics/3d/camera_state.h>
34
#include <imgui.h>
45
ViewState* get_state(GLFWwindow* window) {
56
return (ViewState*)glfwGetWindowUserPointer(window);
@@ -29,17 +30,22 @@ void scroll_callback(GLFWwindow* window, const double xoffset, const double yoff
2930
void mouse_button_callback(GLFWwindow* window, const int button, int action, int mods) {
3031
ViewState* state = get_state(window);
3132
if (!state) return;
33+
3234
if (ImGui::GetIO().WantCaptureMouse) return;
33-
if (button != GLFW_MOUSE_BUTTON_LEFT) return;
35+
if (button != GLFW_MOUSE_BUTTON_LEFT && button != GLFW_MOUSE_BUTTON_RIGHT) return;
3436
if (action == GLFW_RELEASE) {
3537
state->is_dragging = false;
3638
return;
3739
}
3840
double x, y;
3941
glfwGetCursorPos(window, &x, &y);
42+
43+
4044
// this is casting doubles down to float.
4145
state->last_mouse_pos = glm::vec2(x, y);
4246
state->is_dragging = true;
47+
48+
4349
}
4450

4551
void cursor_position_callback(GLFWwindow* window, const double xpos, const double ypos) {
@@ -49,11 +55,16 @@ void cursor_position_callback(GLFWwindow* window, const double xpos, const doubl
4955
//downcasting again
5056
const glm::vec2 current_pos(xpos, ypos);
5157
const glm::vec2 delta = current_pos - state->last_mouse_pos;
58+
state->last_mouse_pos = current_pos;
59+
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) {
60+
process_mouse_movement(camera_state, delta.x, -delta.y);
61+
return;
62+
}
63+
5264
const float scale = state->range / state->height;
5365

5466
state->shift.x -= delta.x * scale;
5567
state->shift.y += delta.y * scale;
56-
state->last_mouse_pos = current_pos;
5768
}
5869

5970
void window_size_callback(GLFWwindow* window, const int width, const int height) {

‎src/interactions/interactions.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct ViewState {
1010
float width = 800.0f;
1111
float height = 600.0f;
1212
bool is_dragging = false;
13+
bool is_3d = false;
1314
} ViewState;
1415

1516

‎src/main.cpp‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <iostream>
1515
#include <compiler/compiler_shader.h>
1616
#include <graphics/3d/mesh.h>
17+
#include <graphics/3d/camera_state.h>
1718

1819
#include <imgui.h>
1920
#include <imgui_impl_glfw.h>
@@ -26,6 +27,13 @@ using vec2 = glm::vec2;
2627
constexpr float WIDTH = 800.00f;
2728
constexpr float HEIGHT = 600.00f;
2829

30+
float last_x = 800.0f / 2.0f;
31+
float last_y = 600.0f / 2.0f;
32+
bool first_mouse = true;
33+
float last_frame = 0.0f;
34+
float delta_time = 0.0f;
35+
36+
2937
unsigned int stack_tbo_buffer, stack_tbo_texture;
3038
unsigned int constants_tbo_buffer, constants_tbo_texture;
3139

@@ -76,11 +84,17 @@ int main() {
7684

7785
Mesh grid_mesh = generate_grid_mesh(256);
7886
render(function_state, stack_tbo_texture, constants_tbo_texture,shader_program);
87+
update_camera_vectors(camera_state);
7988
while (!glfwWindowShouldClose(window)) {
8089
glfwPollEvents();
8190
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
91+
float current_frame = (float)glfwGetTime();
92+
delta_time = current_frame - last_frame;
93+
last_frame = current_frame;
94+
view_state.is_3d = function_state.is_3d;
8295
init_imgui_loop();
8396
if (function_state.is_3d) {
97+
handle_camera_input(window,delta_time);
8498
if (function_state.is_interpreted) {
8599
function_state.current_shader = &shader_3d;
86100
}
@@ -109,11 +123,8 @@ int main() {
109123
}
110124
if (function_state.is_3d) {
111125
glm::mat4 projection = glm::perspective(glm::radians(45.0f), WIDTH / HEIGHT, 0.1f, 100.0f);
112-
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 4.0f, 4.0f),
113-
glm::vec3(0.0f, 0.0f, 0.0f),
114-
glm::vec3(0.0f, 1.0f, 0.0f));
126+
glm::mat4 view = get_view_matrix(camera_state);
115127
glm::mat4 model = glm::mat4(1.0f);
116-
117128
current_shader->setMat4("projection", projection);
118129
current_shader->setMat4("view", view);
119130
current_shader->setMat4("model", model);

0 commit comments

Comments
 (0)