Skip to content

Commit 7923ee3

Browse files
committed
File conversion to strings at compile-time
1 parent c9a78be commit 7923ee3

11 files changed

Lines changed: 102 additions & 38 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vs/
22
out/
33
build/
4-
CMakeSettings.json
4+
CMakeSettings.json
5+
src/shaders/embedded_shaders.h

‎devlog.md‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
We can take derivatives of functions now!
1+
**We can do things in 3D now!**
22

3-
For those not familiar with the term, a derivative is a **higher-order function** (meaning it takes a function as a parameter, and outputs another function). This is not a feature natively supported by our shader's language (GLSL), so we have to do all the higher-order logic ourselves in the parser. Which makes sense: if `d(z^2) = 2z`, and the user types `d(z^2)`, we can just send `2z` to the shader.
4-
Our stack is great for evaluating functions numerically, but not optimal for symbolic manipulation. An **Abstract Syntax Tree**, or AST for short, works far better for this, because it simulates the "recursive" nature of expressions. So, we had to convert our RPN queue to an AST, and then back to RPN. Some time put into that!
3+
This might make you ask *what* are we plotting in this additional dimension, and the answer is simple: nothing that we weren't showing already
54

6-
Now, there is a generic way to calculate any derivative, but it would require us to work with infinitesimally small quantities, which just do not exist in computers. So, if we tried to use it, we'd get a rough approximation at best, and a very innacurate result at worst. This, however, can be fixed with a derivative table, falling back to this numeric method whenever needed
5+
In our plotter, we use brightness to represent magnitude (how large a number is). Black corresponds to zero, and white corresponds to a very high number, making roots of functions look like "sinks". It turns out that it's far easier to visualize these "sinks", "valleys" and "peaks" with the help of a third dimension (akin to looking at a satellite picture of a mountain range vs being there).
76

8-
All this higher-order tree manipulation is of intermediate difficult when using raw pointers (`int*`) but quickly becomes _hell_ when using C++'s smart pointers (`std::unique_ptr`). Safe to say, this difficulty is what caused most of these dev hours.
7+
There's many ways to render functions in 3D, but the one I chose is by laying out a large 256x256 grid of "mini-planes" that are molded to look like the function (there are methods with 'infinite' precision but they are horribly slow and, if we want more precision, we can just add more segments, anyway!). This required me to create a `Mesh` struct and functions to handle their creation and use.
8+
Also, since all this logic operates on vertices, we have to move the 3D code into a vertex shader. This required massive refactoring of pretty much *all* of our preprocessing functions, since they were all rigged to work with the fragment shader. We also had to do more dependency injection trickery to make sure the 2D and 3D shaders remain synchronized (of course).
9+
Finally, we had to create a custom camera to actually move around our 3D plot, and update our state variables to see if the plot is 3D.
910

10-
**Related Issues**:
11-
[Issue #8](https://github.com/Sekqies/complex-plotter/issues/8): Computing higher order functions [And it's sub-issues:]
12-
* Issue #9: Stack into syntax tree
13-
* Issue #10: Syntax tree into RPN stack
14-
* Issue #11: Differentiation
15-
* Issue #12: Operator rules
16-
* Issue #13: Numerical differentiation
17-
* Issue #14: Analytic differentiation
11+
Attached, the third dimension! :O
12+
13+
**Related Issues:**
14+
[Issue #29](https://url.jam06452.uk/ou8zyn): 3D Plotting
1815

1916
**Related Commits:**
20-
[Commit b56b9fa](https://url.jam06452.uk/fyai8z): ast -> stack, stack -> ast
21-
[Commit f410dba](https://url.jam06452.uk/utr804): Finished implementing derivatives
17+
[Commit 4a0ab12](https://url.jam06452.uk/dv60mh): Fragment and vertex shader for 3D
18+
[Commit 0a17f71](https://url.jam06452.uk/zsu6bw): Refactor of preprocessor logic
19+
[Commit 614238a](https://url.jam06452.uk/u896ze): Created mesh grid
20+
[Commit 4bfd2ed](https://url.jam06452.uk/fo0yqb): 3D Rendering done
21+
[Commit 0a17f71](https://url.jam06452.uk/a8oszc): Working camera!

‎src/CMakeLists.txt‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
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" "graphics/3d/camera_state.h" "graphics/3d/camera_state.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" "shaders/embedded_shaders.h")
5+
set(SHADERS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/shaders")
6+
set(HEADER_BUILD "${CMAKE_CURRENT_BINARY_DIR}/shaders/embedded_shaders.h")
7+
set(HEADER_SRC "${CMAKE_CURRENT_SOURCE_DIR}/shaders/embedded_shaders.h")
58

6-
add_executable(complex-plotter ${SOURCE_FILES} "main.cpp")
9+
file(GLOB SHADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/shaders/*")
10+
11+
add_custom_command(
12+
OUTPUT ${HEADER_BUILD} ${HEADER_SRC}
13+
14+
COMMAND ${CMAKE_COMMAND}
15+
-DSHADER_SOURCE_DIR="${SHADERS_DIR}"
16+
-DDEST_BUILD="${HEADER_BUILD}"
17+
-DDEST_SRC="${HEADER_SRC}"
18+
-P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/embed_shaders.cmake"
19+
20+
DEPENDS ${SHADERS_DIR}
21+
COMMENT "[Custom] Generating embedded_shaders.h in build and src folders..."
22+
)
23+
24+
25+
add_executable(complex-plotter ${SOURCE_FILES} ${HEADER_SRC} "main.cpp")
726
find_package(OpenGL REQUIRED)
827

928
target_link_libraries(complex-plotter PRIVATE
@@ -18,7 +37,7 @@ add_custom_target(copy-additional-folders ALL
1837
COMMAND ${CMAKE_COMMAND} -E copy_directory
1938
"${CMAKE_CURRENT_SOURCE_DIR}/shaders"
2039
"${CMAKE_CURRENT_BINARY_DIR}/shaders"
21-
COMMENT "Copying additional folders to build directory..."
40+
COMMENT "[Custom] Copying additional folders to build directory..."
2241
)
2342

2443
add_dependencies(complex-plotter copy-additional-folders)

‎src/compiler/compiler_shader.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ CompilerShader::CompilerShader(const std::string& vertex_path, const std::string
2929
void CompilerShader::prepare_source(std::string& vertex_source, const std::string& fragment_source, const bool use_vertex) {
3030
this->vert_source = vertex_source;
3131
this->frag_source = fragment_source;
32-
std::string& source = frag_source;
33-
if (use_vertex) source = vert_source;
34-
clean_source(source);
35-
find_injection_point(source);
32+
std::string* source = &frag_source;
33+
if (use_vertex) source = &vert_source;
34+
clean_source(*source);
35+
find_injection_point(*source);
3636
}
3737

3838
void CompilerShader::compile(const std::string glsl_expression, const bool use_vertex) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <graphics/3d/camera_state.h>
2-
#include <iostream>
32
CameraState camera_state;
43

54
void update_camera_vectors(CameraState& state) {
@@ -56,7 +55,6 @@ void handle_camera_input(GLFWwindow* window, const float delta_time) {
5655
process_keyboard(camera_state, 3, delta_time);
5756
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(window,GLFW_KEY_SPACE) == GLFW_PRESS)
5857
process_keyboard(camera_state, 4, delta_time);
59-
6058
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
6159
process_keyboard(camera_state, 5, delta_time);
6260
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55
#include <glm/gtc/matrix_transform.hpp>
66

77
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);
8+
glm::vec3 position = glm::vec3(8.0f, 8.0f, 8.0f);
9+
glm::vec3 front;
1010
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
1111
glm::vec3 right;
1212
glm::vec3 world_up = glm::vec3(0.0f, 1.0f, 0.0f);
1313

14-
float yaw = -90.0f;
15-
float pitch = 0.0f;
14+
float yaw;
15+
float pitch;
1616
float movement_speed = 2.5f;
1717
float mouse_sensitivity = 0.05f;
1818
float zoom = 45.0f;
19+
20+
CameraState() {
21+
glm::vec3 direction = glm::normalize(-position);
22+
front = direction;
23+
pitch = glm::degrees(asin(direction.y));
24+
yaw = glm::degrees(atan2(direction.z, direction.x));
25+
right = glm::normalize(glm::cross(front, world_up));
26+
up = glm::normalize(glm::cross(right, front));
27+
}
1928
};
2029

2130
extern CameraState camera_state;

‎src/graphics/ui.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void render_and_update(FunctionState& state, unsigned int& op_tex, unsigned int&
9595
ImGui::BulletText("Pan: Left Click + Drag");
9696
ImGui::BulletText("Zoom: Scroll Wheel");
9797
ImGui::BulletText("Enter: Compile (increases performance)");
98+
ImGui::BulletText("T: Toggle 3D mode");
9899

99100
ImGui::End();
100101
}

‎src/interactions/interactions.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ void window_size_callback(GLFWwindow* window, const int width, const int height)
7777

7878
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
7979
{
80+
ViewState* state = get_state(window);
8081
if (action != GLFW_PRESS) return;
8182
if (key == GLFW_KEY_ESCAPE) {
8283
glfwSetWindowShouldClose(window, true);
8384
}
85+
if (key == GLFW_KEY_T) {
86+
state->is_3d = !state->is_3d;
87+
}
8488
if (key == GLFW_KEY_SPACE) {
8589

8690
}

‎src/main.cpp‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,37 @@ int main() {
8080
float last_time = 0.0f;
8181
init_imgui(window);
8282
FunctionState function_state;
83-
function_state.is_3d = true;
8483

8584
Mesh grid_mesh = generate_grid_mesh(256);
85+
8686
render(function_state, stack_tbo_texture, constants_tbo_texture,shader_program);
8787
update_camera_vectors(camera_state);
88+
bool pressing_t = false;
8889
while (!glfwWindowShouldClose(window)) {
8990
glfwPollEvents();
9091
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
9192
float current_frame = (float)glfwGetTime();
9293
delta_time = current_frame - last_frame;
9394
last_frame = current_frame;
94-
view_state.is_3d = function_state.is_3d;
95+
96+
if (!pressing_t && !ImGui::GetIO().WantCaptureKeyboard && glfwGetKey(window, GLFW_KEY_T) == GLFW_PRESS) {
97+
std::cout << "pressed t";
98+
pressing_t = true;
99+
}
100+
101+
if (pressing_t && !ImGui::GetIO().WantCaptureKeyboard && glfwGetKey(window, GLFW_KEY_T) == GLFW_RELEASE) {
102+
pressing_t = false;
103+
if (view_state.is_3d) {
104+
function_state.current_shader = &shader_program;
105+
}
106+
view_state.is_3d = !view_state.is_3d;
107+
}
108+
109+
function_state.is_3d = view_state.is_3d;
95110
init_imgui_loop();
96111
if (function_state.is_3d) {
97-
handle_camera_input(window,delta_time);
112+
if(!ImGui::GetIO().WantCaptureKeyboard)
113+
handle_camera_input(window,delta_time);
98114
if (function_state.is_interpreted) {
99115
function_state.current_shader = &shader_3d;
100116
}

‎src/scripts/embed_shaders.cmake‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
file(GLOB SHADER_FILES "${SHADER_SOURCE_DIR}/*")
2+
3+
set(FILE_CONTENT "#pragma once\n#include <string>\n\n")
4+
5+
foreach(FILE ${SHADER_FILES})
6+
get_filename_component(FILENAME ${FILE} NAME)
7+
string(REPLACE "." "_" VAR_NAME ${FILENAME})
8+
string(TOUPPER ${VAR_NAME} VAR_NAME)
9+
10+
file(READ ${FILE} SHADER_TEXT)
11+
12+
string(APPEND FILE_CONTENT "const std::string SRC_${VAR_NAME} = R\"(${SHADER_TEXT})\";\n\n")
13+
endforeach()
14+
15+
file(WRITE "${DEST_BUILD}" "${FILE_CONTENT}")
16+
file(WRITE "${DEST_SRC}" "${FILE_CONTENT}")

0 commit comments

Comments
 (0)