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>
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);
0 commit comments