Skip to content

Commit 4bfd2ed

Browse files
committed
3D Rendering done - no camera
1 parent 614238a commit 4bfd2ed

7 files changed

Lines changed: 116 additions & 57 deletions

File tree

‎src/compiler/compiler_shader.cpp‎

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <compiler/compiler_shader.h>
22

3-
CompilerShader::CompilerShader(const std::string& vertex_path, const std::string& frag_path) {
3+
CompilerShader::CompilerShader(const std::string& vertex_path, const std::string& frag_path, const bool use_vertex) {
44
std::ifstream vfile;
55
std::ifstream ffile;
66
vfile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
@@ -22,12 +22,25 @@ CompilerShader::CompilerShader(const std::string& vertex_path, const std::string
2222
catch (const std::ifstream::failure& e) {
2323
std::cerr << "Error: failed reading contents of file '" << vertex_path << "' or '" << frag_path << "'";
2424
}
25-
clean_frag_source();
26-
find_injection_point();
25+
prepare_source(vert_source, frag_source, use_vertex);
2726
}
2827

29-
void CompilerShader::compile(const std::string glsl_expression) {
30-
shader.compile(vert_source, generate_full_source(glsl_expression));
28+
29+
void CompilerShader::prepare_source(std::string& vertex_source, const std::string& fragment_source, const bool use_vertex) {
30+
this->vert_source = vertex_source;
31+
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);
36+
}
37+
38+
void CompilerShader::compile(const std::string glsl_expression, const bool use_vertex) {
39+
if (use_vertex) {
40+
shader.compile(generate_full_source(vert_source, glsl_expression), frag_source);
41+
return;
42+
}
43+
shader.compile(vert_source, generate_full_source(frag_source,glsl_expression));
3144
}
3245

3346
size_t CompilerShader::find_end(const std::string& source, const std::string& substr, const size_t start) {
@@ -41,31 +54,32 @@ size_t CompilerShader::find_end(const std::string& source, const std::string& su
4154
return pos + substr.size();
4255
}
4356

44-
void CompilerShader::clean_frag_source() {
57+
void CompilerShader::clean_source(std::string& source) {
4558
constexpr std::array kill_these_guys = { "INTERPRETER_SPECIFIC_FUNCTIONS","INTERPRETER_ASSIGNEMENT" };
4659
size_t prev_end = 0;
4760
for (const std::string& s : kill_these_guys) {
48-
const size_t start = frag_source.find("#define " + s) - 1;
49-
const size_t end = find_end(frag_source, "#define END_" + s);
50-
const size_t end_of_line = frag_source.find('\n', end);
61+
const size_t start = source.find("#define " + s) - 1;
62+
const size_t end = find_end(source, "#define END_" + s);
63+
const size_t end_of_line = source.find('\n', end);
5164
if (start == std::string::npos || end == std::string::npos) {
5265
std::cerr << "Formatting error for tag " << s;
5366
continue;
5467
}
55-
frag_source.erase(start, end_of_line - start);
68+
source.erase(start, end_of_line - start);
5669
}
5770
}
5871

59-
void CompilerShader::find_injection_point() {
60-
injection_point = find_end(frag_source, "#define INJECTION_POINT HERE", 0);
72+
void CompilerShader::find_injection_point(std::string& source) {
73+
injection_point = find_end(source, "#define INJECTION_POINT HERE", 0);
6174
if (injection_point == std::string::npos) {
6275
std::cerr << "Injection point not found in shader";
6376
}
6477
}
6578

66-
std::string CompilerShader::generate_full_source(const std::string& expression) {
67-
size_t line_end = frag_source.find('\n', injection_point);
68-
std::string copy = frag_source;
79+
std::string CompilerShader::generate_full_source(std::string& source, const std::string& expression) {
80+
find_injection_point(source);
81+
size_t line_end = source.find('\n', injection_point);
82+
std::string copy = source;
6983
copy.insert(line_end + 1, "\n vec2 func_value = " + expression + ";\n");
7084
std::cout << copy;
7185
return copy;

‎src/compiler/compiler_shader.h‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99

1010
class CompilerShader {
1111
public:
12+
std::string frag_source;
13+
std::string vert_source;
1214
Shader shader;
13-
CompilerShader(const std::string& vertex_path, const std::string& frag_path);
14-
void compile(const std::string glsl_expression);
15+
CompilerShader(const std::string& vertex_path, const std::string& frag_path, const bool use_vertex = false);
16+
17+
CompilerShader() = default;
18+
19+
void compile(const std::string glsl_expression, const bool use_vertex);
20+
21+
void prepare_source(std::string& vertex_source, const std::string& frag_source, const bool use_vertex = false);
1522

1623
static size_t find_end(const std::string& source, const std::string& substr, const size_t start = 0);
1724

1825
private:
19-
void clean_frag_source();
20-
void find_injection_point();
21-
std::string generate_full_source(const std::string& expression);
22-
std::string frag_source;
23-
std::string vert_source;
26+
void clean_source(std::string& source);
27+
void find_injection_point(std::string& source);
28+
std::string generate_full_source(std::string& source, const std::string& expression);
2429
size_t injection_point;
2530
};

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
#include <graphics/3d/mesh.h>
22

3+
void link_mesh(Mesh& mesh, const vector<float>& vertices, const vector<unsigned int>& indices) {
4+
glGenVertexArrays(1, &mesh.VAO);
5+
glGenBuffers(1, &mesh.VBO);
6+
glGenBuffers(1, &mesh.EBO);
7+
8+
glBindVertexArray(mesh.VAO);
9+
10+
glBindBuffer(GL_ARRAY_BUFFER, mesh.VBO);
11+
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
12+
13+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.EBO);
14+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
15+
16+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
17+
glEnableVertexAttribArray(0);
18+
19+
glBindVertexArray(0);
20+
mesh.index_count = (unsigned int)indices.size();
21+
}
322
/// <summary>
423
/// Generates a mesh object of an n x n grid
524
/// </summary>
@@ -39,22 +58,3 @@ Mesh generate_grid_mesh(const int grid_size) {
3958
return mesh;
4059
}
4160

42-
void link_mesh(Mesh& mesh, const vector<float>& vertices, const vector<unsigned int>& indices) {
43-
glGenVertexArrays(1, &mesh.VAO);
44-
glGenBuffers(1, &mesh.VBO);
45-
glGenBuffers(1, &mesh.EBO);
46-
47-
glBindVertexArray(mesh.VAO);
48-
49-
glBindBuffer(GL_ARRAY_BUFFER, mesh.VBO);
50-
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
51-
52-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.EBO);
53-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
54-
55-
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
56-
glEnableVertexAttribArray(0);
57-
58-
glBindVertexArray(0);
59-
mesh.index_count = (unsigned int)indices.size();
60-
}

‎src/graphics/ui.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void compile(FunctionState& state, CompilerShader& compiler_shader) {
4646
try {
4747
vector<TokenOperator> stack = parser::parse(state.expression);
4848
const string& expression = stack_to_glsl_string(stack);
49-
compiler_shader.compile(expression);
49+
compiler_shader.compile(expression,state.is_3d);
5050
}
5151
catch (const std::runtime_error& e) {
5252
state.error_message = e.what();

‎src/graphics/ui.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typedef struct FunctionState {
1212
float last_typing_time = 0.0f;
1313
bool needs_reparse = true;
1414
bool is_interpreted = true;
15+
bool is_3d = false;
1516
} FunctionState;
1617

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

‎src/main.cpp‎

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <GLFW/glfw3.h>
44
#include <glm/glm.hpp>
55
#include <glm/common.hpp>
6+
#include <glm/gtc/matrix_transform.hpp>
67
#include <graphics/graphics.h>
78
#include <graphics/ui.h>
89
#include <parser/parser.h>
@@ -12,6 +13,7 @@
1213
#include <stdexcept>
1314
#include <iostream>
1415
#include <compiler/compiler_shader.h>
16+
#include <graphics/3d/mesh.h>
1517

1618
#include <imgui.h>
1719
#include <imgui_impl_glfw.h>
@@ -35,17 +37,26 @@ int main() {
3537
Shader shader_program("shaders/plotter.vert", "shaders/plotter.frag");
3638

3739
Shader shader_3d;
38-
const string& frag_source = shader_program.fragment_source;
40+
const string frag_source = shader_program.fragment_source;
3941
string vert_source_3d = get_source("shaders/plotter3d.vert");
4042
const string frag_source_3d = get_source("shaders/plotter3d.frag");
41-
string vert_source_3d = build_shader_string(vert_source_3d, frag_source);
43+
std::cout << vert_source_3d;
44+
try {
45+
vert_source_3d = build_shader_string(vert_source_3d, frag_source);
46+
std::cout << vert_source_3d;
47+
}
48+
catch (std::runtime_error& er) {
49+
std::cout << er.what();
50+
}
4251
shader_3d.compile(vert_source_3d, frag_source_3d);
4352

53+
CompilerShader compiled_shader_3d;
54+
compiled_shader_3d.prepare_source(vert_source_3d, frag_source_3d, true);
55+
4456
CompilerShader compiled_shader("shaders/plotter.vert", "shaders/plotter.frag");
4557

4658
unsigned int VAO;
4759
glGenVertexArrays(1, &VAO);
48-
glBindVertexArray(VAO);
4960

5061
ViewState view_state;
5162
view_state.width = WIDTH;
@@ -61,14 +72,25 @@ int main() {
6172
float last_time = 0.0f;
6273
init_imgui(window);
6374
FunctionState function_state;
75+
function_state.is_3d = true;
76+
77+
Mesh grid_mesh = generate_grid_mesh(256);
6478
render(function_state, stack_tbo_texture, constants_tbo_texture,shader_program);
6579
while (!glfwWindowShouldClose(window)) {
6680
glfwPollEvents();
67-
glClear(GL_COLOR_BUFFER_BIT);
81+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
6882
init_imgui_loop();
69-
70-
render_and_update(function_state, stack_tbo_texture, constants_tbo_texture, shader_program, compiled_shader);
71-
83+
if (function_state.is_3d) {
84+
if (function_state.is_interpreted) {
85+
function_state.current_shader = &shader_3d;
86+
}
87+
glEnable(GL_DEPTH_TEST);
88+
render_and_update(function_state, stack_tbo_texture, constants_tbo_texture, shader_3d, compiled_shader_3d);
89+
}
90+
else {
91+
glDisable(GL_DEPTH_TEST);
92+
render_and_update(function_state, stack_tbo_texture, constants_tbo_texture, shader_program, compiled_shader);
93+
}
7294
Shader* current_shader = function_state.current_shader;
7395
current_shader->use();
7496
current_shader->setFloat("time", glfwGetTime());
@@ -79,14 +101,29 @@ int main() {
79101
if (function_state.is_interpreted) {
80102
glActiveTexture(GL_TEXTURE0);
81103
glBindTexture(GL_TEXTURE_BUFFER, stack_tbo_texture);
82-
shader_program.setInt("operator_stack", 0);
104+
current_shader->setInt("operator_stack", 0);
83105

84106
glActiveTexture(GL_TEXTURE1);
85107
glBindTexture(GL_TEXTURE_BUFFER, constants_tbo_texture);
86-
shader_program.setInt("constant_stack", 1);
108+
current_shader->setInt("constant_stack", 1);
109+
}
110+
if (function_state.is_3d) {
111+
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));
115+
glm::mat4 model = glm::mat4(1.0f);
116+
117+
current_shader->setMat4("projection", projection);
118+
current_shader->setMat4("view", view);
119+
current_shader->setMat4("model", model);
120+
glBindVertexArray(grid_mesh.VAO);
121+
glDrawElements(GL_TRIANGLES, grid_mesh.index_count, GL_UNSIGNED_INT, 0);
122+
}
123+
else {
124+
glBindVertexArray(VAO);
125+
glDrawArrays(GL_TRIANGLES, 0, 3);
87126
}
88-
89-
glDrawArrays(GL_TRIANGLES, 0, 3);
90127

91128
render_imgui();
92129
glfwSwapBuffers(window);

‎src/shaders/plotter3d.vert‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#version 330 core
22

3+
#define HERE ;
4+
35
layout (location=0) in vec3 aPos;
46

57
out vec2 f_z;
@@ -27,20 +29,20 @@ vec2 run_stack(samplerBuffer operator_stack, usamplerBuffer constant_stack, vec2
2729
#define END_FUNCTION_DEFINITIONS HERE
2830

2931

30-
#define INTERRPRETER_SPECIFIC_FUNCTIONS HERE
32+
#define INTERPRETER_SPECIFIC_FUNCTIONS HERE
3133

32-
#define END_INTERRPRETER_SPECIFIC_FUNCTIONS HERE
34+
#define END_INTERPRETER_SPECIFIC_FUNCTIONS HERE
3335

3436
void main(){
35-
vec2 z = (aPos.xy * u_range) + shift;
37+
vec2 z = (vec2(aPos.x,aPos.z) * u_range) + shift;
3638

3739
#define INTERPRETER_ASSIGNEMENT HERE
3840
vec2 func_value = run_stack(operator_stack,constant_stack,z);
3941
#define END_INTERPRETER_ASSIGNEMENT HERE
4042

4143
#define INJECTION_POINT HERE
4244

43-
float height = length(z);
45+
float height = length(func_value);
4446

4547
gl_Position = projection * view * model * vec4(aPos.x,height,aPos.z,1.0f);
4648

0 commit comments

Comments
 (0)