Skip to content

Commit 614238a

Browse files
committed
Created mesh grid
1 parent 0a17f71 commit 614238a

7 files changed

Lines changed: 90 additions & 2 deletions

File tree

‎dependencies/learnopengl/include/shader/shader.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Shader
99
{
1010
public:
1111
unsigned int ID;
12+
std::string vertex_source, fragment_source;
1213
// constructor generates the shader on the fly
1314
// ------------------------------------------------------------------------
1415
// constructor generates the shader on the fly

‎dependencies/learnopengl/src/shader.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath, const char* geo
5656
{
5757
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;
5858
}
59+
fragment_source = fragmentCode;
60+
vertex_source = vertexCode;
5961
const char* vShaderCode = vertexCode.c_str();
6062
const char* fShaderCode = fragmentCode.c_str();
6163
// 2. compile shaders

‎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")
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")
55

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

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <graphics/3d/mesh.h>
2+
3+
/// <summary>
4+
/// Generates a mesh object of an n x n grid
5+
/// </summary>
6+
/// <param name="grid_size">The size of the grid (used for grid_size * grid_size)</param>
7+
/// <returns>A grid mesh</returns>
8+
Mesh generate_grid_mesh(const int grid_size) {
9+
vector<float> vertices;
10+
vector<unsigned int> indices;
11+
12+
for (unsigned int z = 0; z <= grid_size; ++z) {
13+
for (unsigned int x = 0; x <= grid_size; ++x) {
14+
float u = static_cast<float>(x) / grid_size;
15+
float v = static_cast<float>(z) / grid_size;
16+
17+
vertices.push_back(u * 2.0f - 1.0f);
18+
// y is modified at runtime
19+
vertices.push_back(0.0f);
20+
vertices.push_back(v * 2.0f - 1.0f);
21+
}
22+
}
23+
for (unsigned int z = 0; z < grid_size; ++z) {
24+
for (int x = 0; x < grid_size; ++x) {
25+
int row1 = z * (grid_size + 1);
26+
int row2 = (z + 1) * (grid_size + 1);
27+
28+
indices.push_back(row1 + x);
29+
indices.push_back(row2 + x);
30+
indices.push_back(row1 + x + 1);
31+
32+
indices.push_back(row1 + x + 1);
33+
indices.push_back(row2 + x);
34+
indices.push_back(row2 + x + 1);
35+
}
36+
}
37+
Mesh mesh;
38+
link_mesh(mesh, vertices, indices);
39+
return mesh;
40+
}
41+
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/3d/mesh.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <vector>
2+
#include <glad/glad.h>
3+
4+
struct Mesh {
5+
unsigned int VAO, VBO, EBO;
6+
unsigned int index_count;
7+
};
8+
9+
using std::vector;
10+
11+
/// <summary>
12+
/// Generates a mesh object of an n x n grid
13+
/// </summary>
14+
/// <param name="grid_size">The size of the grid (used for grid_size * grid_size)</param>
15+
/// <returns>A grid mesh</returns>
16+
Mesh generate_grid_mesh(const int grid_size);
17+

‎src/main.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ int main() {
3333
preprocess("shaders/plotter.frag", operators);
3434

3535
Shader shader_program("shaders/plotter.vert", "shaders/plotter.frag");
36+
37+
Shader shader_3d;
38+
const string& frag_source = shader_program.fragment_source;
39+
string vert_source_3d = get_source("shaders/plotter3d.vert");
40+
const string frag_source_3d = get_source("shaders/plotter3d.frag");
41+
string vert_source_3d = build_shader_string(vert_source_3d, frag_source);
42+
shader_3d.compile(vert_source_3d, frag_source_3d);
43+
3644
CompilerShader compiled_shader("shaders/plotter.vert", "shaders/plotter.frag");
3745

3846
unsigned int VAO;

‎src/preprocessor/preprocessor.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ string get_source(const string& filename);
88

99
void preprocess(const string& filename, const vector<TokenOperator>& operators);
1010

11-
string build_shader_string(string& new_shader, const string& origin);
11+
string build_shader_string(const string& new_shader, const string& origin);
1212

1313
string get_block(const string& source, const string& tag);
1414

0 commit comments

Comments
 (0)