Skip to content

Commit 5893ae0

Browse files
committed
Inspector done!
1 parent ba3f299 commit 5893ae0

11 files changed

Lines changed: 241 additions & 23 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" "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" "graphics/picker.cpp")
55
set(SHADERS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/shaders")
66
set(HEADER_BUILD "${CMAKE_CURRENT_BINARY_DIR}/shaders/embedded_shaders.h")
77
set(HEADER_SRC "${CMAKE_CURRENT_SOURCE_DIR}/shaders/embedded_shaders.h")

‎src/graphics/picker.cpp‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <graphics/picker.h>
2+
3+
PickerResult get_hover_value(double raw_x, double raw_y, ViewState& view_state, unsigned int stack_tex, unsigned int const_tex, Shader& picker_shader, unsigned int picker_fbo) {
4+
glBindFramebuffer(GL_FRAMEBUFFER, picker_fbo);
5+
glViewport(0, 0, 1, 1);
6+
picker_shader.use();
7+
8+
float gl_x = static_cast<float>(raw_x);
9+
float gl_y = view_state.height - static_cast<float>(raw_y);
10+
11+
picker_shader.setVec2("u_mouse", glm::vec2(gl_x, gl_y));
12+
picker_shader.setVec2("u_resolution", glm::vec2(view_state.width, view_state.height));
13+
picker_shader.setFloat("u_range", view_state.range);
14+
picker_shader.setVec2("shift", view_state.shift);
15+
picker_shader.setFloat("time", glfwGetTime());
16+
17+
glActiveTexture(GL_TEXTURE0);
18+
glBindTexture(GL_TEXTURE_BUFFER, stack_tex);
19+
picker_shader.setInt("operator_stack", 0);
20+
21+
glActiveTexture(GL_TEXTURE1);
22+
glBindTexture(GL_TEXTURE_BUFFER, const_tex);
23+
picker_shader.setInt("constant_stack", 1);
24+
25+
glDrawArrays(GL_TRIANGLES, 0, 3);
26+
27+
float data[4];
28+
glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, &data);
29+
30+
glBindFramebuffer(GL_FRAMEBUFFER, 0);
31+
glViewport(0, 0, view_state.width, view_state.height);
32+
33+
PickerResult result;
34+
result.value = glm::vec2(data[0], data[1]);
35+
result.coordinate = glm::vec2(data[2], data[3]);
36+
result.valid = true;
37+
38+
return result;
39+
}
40+
41+
void init_picker(unsigned int& picker_tex, unsigned int& picker_fbo) {
42+
glGenFramebuffers(1, &picker_fbo);
43+
glBindFramebuffer(GL_FRAMEBUFFER, picker_fbo);
44+
glGenTextures(1, &picker_tex);
45+
glBindTexture(GL_TEXTURE_2D, picker_tex);
46+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 1, 1, 0, GL_RGBA, GL_FLOAT, NULL);
47+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
48+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
49+
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, picker_tex, 0);
50+
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
51+
std::cout << "ERROR::FRAMEBUFFER:: Picker Framebuffer is not complete!" << std::endl;
52+
}
53+
glBindFramebuffer(GL_FRAMEBUFFER, 0);
54+
}

‎src/graphics/picker.h‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include <graphics/ui.h>
3+
#include <shader/shader.h>
4+
#include <GLFW/glfw3.h>
5+
6+
struct PickerResult {
7+
glm::vec2 value;
8+
glm::vec2 coordinate;
9+
bool valid;
10+
};
11+
12+
PickerResult get_hover_value(
13+
double raw_x,
14+
double raw_y,
15+
ViewState& view_state,
16+
unsigned int stack_tex,
17+
unsigned int const_tex,
18+
Shader& picker_shader,
19+
unsigned int picker_fbo
20+
);
21+
22+
void init_picker(unsigned int& picker_tex, unsigned int& picker_fbo);

‎src/graphics/ui.cpp‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77
#include <iostream>
88

9+
910
using string = std::string;
1011
using glm::vec2;
1112

@@ -43,9 +44,15 @@ void render(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex,
4344
state.is_interpreted = true;
4445
}
4546

46-
void compile(FunctionState& state, CompilerShader& compiler_shader) {
47+
void compile(FunctionState& state, CompilerShader& compiler_shader, unsigned int& op_tex, unsigned int& const_tex) {
4748
try {
4849
vector<TokenOperator> stack = parser::parse(state.expression);
50+
vector<unsigned char> operator_stack;
51+
vector<vec2> constant_stack;
52+
get_stacks(stack, operator_stack, constant_stack);
53+
unsigned int op_tbo_buf, const_tbo_buf;
54+
populate_texture(op_tbo_buf, op_tex, operator_stack);
55+
populate_texture(const_tbo_buf, const_tex, constant_stack);
4956
const string& expression = stack_to_glsl_string(stack);
5057
compiler_shader.compile(expression,state.is_3d);
5158
}
@@ -57,13 +64,56 @@ void compile(FunctionState& state, CompilerShader& compiler_shader) {
5764
state.is_interpreted = false;
5865
}
5966

60-
void change_3d_mode(ViewState& view_state) {
6167

68+
void render_inspector_overlay(const PickerResult& hover, ViewState& view_state) {
69+
const float padding = 10.0f;
70+
const ImVec2 offset = ImVec2(15.0f, 15.0f);
71+
const ImGuiViewport* viewport = ImGui::GetMainViewport();
72+
ImVec2 work_pos = viewport->WorkPos;
73+
ImVec2 work_size = viewport->WorkSize;
74+
75+
if (view_state.inspector_follows_mouse) {
76+
const ImVec2 mouse_pos = ImGui::GetMousePos();
77+
ImGui::SetNextWindowPos(ImVec2(offset.x + mouse_pos.x, offset.y + mouse_pos.y), ImGuiCond_Always);
78+
}
79+
else {
80+
ImVec2 window_pos, window_pos_pivot;
81+
window_pos.x = work_pos.x + work_size.x - padding;
82+
window_pos.y = work_pos.y + work_size.y - padding;
83+
window_pos_pivot = ImVec2(1.0f, 1.0f);
84+
ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot);
85+
}
86+
ImGui::SetNextWindowBgAlpha(0.65f);
87+
88+
ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration |
89+
ImGuiWindowFlags_AlwaysAutoResize |
90+
ImGuiWindowFlags_NoSavedSettings |
91+
ImGuiWindowFlags_NoFocusOnAppearing |
92+
ImGuiWindowFlags_NoNav |
93+
ImGuiWindowFlags_NoInputs;
94+
if (ImGui::Begin("Inspector", nullptr, window_flags)) {
95+
ImGui::Text("Coordinate (z)");
96+
ImGui::Separator();
97+
ImGui::Text("%.3f %s %.3fi",
98+
hover.coordinate.x,
99+
(hover.coordinate.y >= 0 ? "+" : "-"),
100+
std::abs(hover.coordinate.y));
101+
102+
ImGui::Spacing();
103+
104+
ImGui::Text("Value f(z)");
105+
ImGui::Separator();
106+
ImGui::Text("%.3f %s %.3fi",
107+
hover.value.x,
108+
(hover.value.y >= 0 ? "+" : "-"),
109+
std::abs(hover.value.y));
110+
}
111+
ImGui::End();
62112
}
63113

64114

65-
void render_and_update(FunctionState& state, ViewState& view_state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader, bool& should_switch) {
66-
ImGui::Begin("Function Control", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
115+
void render_and_update(FunctionState& state, ViewState& view_state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader) {
116+
ImGui::Begin("Function Editor", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
67117
ImGui::SameLine();
68118
if (state.is_interpreted) {
69119
ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.0f, 1.0f), "[ INTERPRETED ]");
@@ -104,7 +154,7 @@ void render_and_update(FunctionState& state, ViewState& view_state, unsigned int
104154
const bool debounced = time_dif > DEBOUNCE_DELAY;
105155

106156
if (pressed_enter) {
107-
compile(state, compiler_shader);
157+
compile(state, compiler_shader, op_tex, const_tex);
108158
}
109159
if (state.needs_reparse && debounced && !pressed_enter) {
110160
render(state, op_tex, const_tex, interpreter_shader);
@@ -131,6 +181,13 @@ void render_and_update(FunctionState& state, ViewState& view_state, unsigned int
131181
if (ImGui::RadioButton("3D Surface", state.is_3d)) {
132182
view_state.is_3d = true;
133183
}
184+
ImGui::Checkbox("Show Value Inspector", &view_state.show_inspector);
185+
186+
if (view_state.show_inspector) {
187+
ImGui::Indent();
188+
ImGui::Checkbox("Follow Mouse", &view_state.inspector_follows_mouse);
189+
ImGui::Unindent();
190+
}
134191
}
135192

136193
if (ImGui::CollapsingHeader("3D Keybinds")) {

‎src/graphics/ui.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <graphics/ui_init.h>
66
#include <string>
77
#include <interactions/interactions.h>
8+
#include <graphics/picker.h>
9+
10+
struct PickerResult;
811

912
typedef struct FunctionState {
1013
Shader* current_shader = nullptr;
@@ -18,6 +21,9 @@ typedef struct FunctionState {
1821

1922
void render(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader);
2023

21-
void compile(FunctionState& state, CompilerShader& compiler_shader);
24+
void compile(FunctionState& state, CompilerShader& compiler_shader, unsigned int& op_tex, unsigned int& const_tex);
25+
26+
void render_and_update(FunctionState& state,ViewState& view_state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader);
27+
28+
void render_inspector_overlay(const PickerResult& hover, ViewState& view_state);
2229

23-
void render_and_update(FunctionState& state,ViewState& view_state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader, bool& should_switch);

‎src/interactions/interactions.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ typedef struct ViewState {
1111
float height = 600.0f;
1212
bool is_dragging = false;
1313
bool is_3d = false;
14+
bool show_inspector = true;
15+
bool inspector_follows_mouse = true;
1416
} ViewState;
1517

1618

‎src/main.cpp‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <compiler/compiler_shader.h>
1616
#include <graphics/3d/mesh.h>
1717
#include <graphics/3d/camera_state.h>
18+
#include <shaders/embedded_shaders.h>
1819

1920
#include <imgui.h>
2021
#include <imgui_impl_glfw.h>
@@ -38,15 +39,20 @@ unsigned int stack_tbo_buffer, stack_tbo_texture;
3839
unsigned int constants_tbo_buffer, constants_tbo_texture;
3940

4041

42+
4143
int main() {
4244
GLFWwindow* window = initalize_window(WIDTH, HEIGHT, "Domain Coloring");
4345
preprocess_string("shaders/plotter.frag", operators);
4446

4547
Shader shader_program;
4648
build_shader_path(shader_program, "shaders/plotter.vert", "shaders/plotter.frag");
47-
48-
std::cout << get_source("shaders/plotter.frag");
4949

50+
Shader picker;
51+
string picker_frag = SRC_PICKER_FRAG;
52+
inject_at(picker_frag, "FUNCTION_DEFINITIONS HERE", get_block(shader_program.fragment_source, "FUNCTION_DEFINITIONS"));
53+
inject_at(picker_frag, "INTERPRETER_SPECIFIC_FUNCTIONS", get_block(shader_program.fragment_source, "INTERPRETER_SPECIFIC_FUNCTIONS"));
54+
inject_at(picker_frag, "CONSTANT_DEFINITIONS HERE", get_block(shader_program.fragment_source, "CONSTANT_DEFINITIONS"));
55+
picker.compile(shader_program.vertex_source, picker_frag);
5056

5157
Shader shader_3d;
5258
const string frag_source = shader_program.fragment_source;
@@ -90,9 +96,10 @@ int main() {
9096
render(function_state, stack_tbo_texture, constants_tbo_texture,shader_program);
9197
update_camera_vectors(camera_state);
9298
bool pressing_t = false;
93-
bool should_switch = false;
9499

100+
unsigned int picker_tex, picker_fbo;
95101

102+
init_picker(picker_tex,picker_fbo);
96103
while (!glfwWindowShouldClose(window)) {
97104
glfwPollEvents();
98105
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -121,11 +128,11 @@ int main() {
121128
function_state.current_shader = &shader_3d;
122129
}
123130
glEnable(GL_DEPTH_TEST);
124-
render_and_update(function_state,view_state, stack_tbo_texture, constants_tbo_texture, shader_3d, compiled_shader_3d, should_switch);
131+
render_and_update(function_state,view_state, stack_tbo_texture, constants_tbo_texture, shader_3d, compiled_shader_3d);
125132
}
126133
else {
127134
glDisable(GL_DEPTH_TEST);
128-
render_and_update(function_state,view_state, stack_tbo_texture, constants_tbo_texture, shader_program, compiled_shader, should_switch);
135+
render_and_update(function_state,view_state, stack_tbo_texture, constants_tbo_texture, shader_program, compiled_shader);
129136
}
130137
Shader* current_shader = function_state.current_shader;
131138
current_shader->use();
@@ -169,6 +176,15 @@ int main() {
169176
std::cout << "Switched to " << (view_state.is_3d ? "3D" : "2D") << std::endl;
170177
}
171178

179+
180+
if (view_state.show_inspector) {
181+
double xpos, ypos;
182+
glfwGetCursorPos(window, &xpos, &ypos);
183+
184+
PickerResult hover = get_hover_value(xpos, ypos, view_state, stack_tbo_texture, constants_tbo_texture, picker, picker_fbo);
185+
render_inspector_overlay(hover, view_state);
186+
}
187+
172188
render_imgui();
173189
glfwSwapBuffers(window);
174190
}

‎src/preprocessor/preprocessor.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ string get_block(const string& source, const string& tag) {
9696
size_t start, length;
9797
if (!found_bounds(source, tag, start, length)) return "";
9898

99-
string start_str = "#define " + tag;
100-
size_t content_start = start + start_str.length();
101-
99+
size_t newline_pos = source.find('\n', start);
100+
if (newline_pos == string::npos) return "";
101+
size_t content_start = newline_pos + 1;
102102
string end_str = "#define END_" + tag;
103-
size_t content_end = source.find(end_str, start);
104-
103+
size_t content_end = source.find(end_str, content_start);
104+
if (content_end == string::npos) return "";
105105
return source.substr(content_start, content_end - content_start);
106106
}
107107

‎src/scripts/embed_shaders.cmake‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ foreach(FILE ${SHADER_FILES})
1313

1414
file(READ ${FILE} SHADER_TEXT)
1515

16-
string(APPEND FILE_CONTENT "std::string SRC_${VAR_NAME} = R\"(${SHADER_TEXT})\";\n\n")
16+
string(APPEND FILE_CONTENT "inline std::string SRC_${VAR_NAME} = R\"(${SHADER_TEXT})\";\n\n")
1717
endforeach()
1818

1919
file(WRITE "${DEST_BUILD}" "${FILE_CONTENT}")

‎src/shaders/embedded_shaders.h‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
#pragma once
22
#include <string>
33

4-
std::string SRC_PLOTTER_FRAG = R"(#version 330
4+
inline std::string SRC_PICKER_FRAG = R"(#version 330 core
5+
layout (location = 0) out vec4 FragData;
6+
7+
uniform float u_range;
8+
uniform vec2 shift;
9+
uniform vec2 u_resolution;
10+
uniform vec2 u_mouse;
11+
uniform float time;
12+
13+
#define CONSTANT_DEFINITIONS HERE
14+
15+
#define END_CONSTANT_DEFINITIONS HERE
16+
17+
#define FUNCTION_DEFINITIONS HERE
18+
19+
#define END_FUNCTION_DEFINITIONS HERE
20+
21+
#define INTERPRETER_SPECIFIC_FUNCTIONS HERE
22+
23+
#define END_INTERPRETER_SPECIFIC_FUNCTIONS HERE
24+
25+
void main(){
26+
vec2 z = (u_mouse/u_resolution.y) * u_range;
27+
z -= (u_resolution / u_resolution.y) * 0.5 * u_range;
28+
z += shift;
29+
30+
vec2 val = run_stack(operator_stack,constant_stack,z);
31+
32+
FragData = vec4(val.x,val.y,z.x,z.y);
33+
})";
34+
35+
inline std::string SRC_PLOTTER_FRAG = R"(#version 330
536
637
// Other useful constants
738
@@ -262,7 +293,7 @@ void main(){
262293
}
263294
)";
264295

265-
std::string SRC_PLOTTER_VERT = R"(#version 330
296+
inline std::string SRC_PLOTTER_VERT = R"(#version 330
266297
267298
out vec2 pos;
268299
@@ -273,7 +304,7 @@ void main(){
273304
gl_Position = vec4(x,y,0.0f,1.0f);
274305
})";
275306

276-
std::string SRC_PLOTTER3D_FRAG = R"(#version 330 core
307+
inline std::string SRC_PLOTTER3D_FRAG = R"(#version 330 core
277308
278309
in vec2 f_z;
279310
out vec4 FragColor;
@@ -298,7 +329,7 @@ void main(){
298329
FragColor = vec4(hsl2rgb(hsl),1.0f);
299330
})";
300331

301-
std::string SRC_PLOTTER3D_VERT = R"(#version 330 core
332+
inline std::string SRC_PLOTTER3D_VERT = R"(#version 330 core
302333
303334
#define HERE ;
304335

0 commit comments

Comments
 (0)