SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
cell.h
Go to the documentation of this file.
1#ifndef CELL_H
2#define CELL_H
3
4#include <array>
5
6#include <jGL/OpenGL/gl.h>
7#include <jGL/OpenGL/Shader/glShader.h>
8
9#include <glUtils.h>
10
16class Cell
17{
18
19public:
20
28 Cell(glm::vec3 a, glm::vec3 b, glm::vec3 c)
29 : a(a), b(b), c(c)
30 {
31 generateFaces();
32 shader = std::make_unique<jGL::GL::glShader>(vertexShader, fragmentShader);
33 shader->use();
34 shader->setUniform<glm::vec4>("colour", {0.0,1.0,0.0,0.5});
35 glGenVertexArrays(1, &vao);
36 glGenBuffers(1, &a_vertices);
37 glGenBuffers(1, &a_coords);
38
39 glBindVertexArray(vao);
40
42 (
43 a_vertices,
44 cube.data(),
45 cube.size(),
46 GL_DYNAMIC_DRAW,
47 0,
48 3,
49 0
50 );
51
53 (
54 a_coords,
55 textureCoords.data(),
56 textureCoords.size(),
57 GL_STATIC_DRAW,
58 1,
59 2,
60 0
61 );
62
63 glBindVertexArray(0);
64 }
65
67 {
68 glDeleteBuffers(1, &a_vertices);
69 glDeleteBuffers(1, &a_coords);
70 glDeleteVertexArrays(1, &vao);
71 }
72
78 void setProjectionView(glm::mat4 pv)
79 {
80 shader->use();
81 shader->setUniform<glm::mat4>("proj", pv);
82 }
83
91 void setVectors(glm::vec3 a, glm::vec3 b, glm::vec3 c)
92 {
93 this->a = a; this->b = b; this->c = c;
94 generateFaces();
95 glBindVertexArray(vao);
96
98 (
99 a_vertices,
100 cube.data(),
101 cube.size()
102 );
103
104 glBindVertexArray(0);
105 }
106
111 void draw()
112 {
113 glEnable(GL_BLEND);
114 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
115 glEnable(GL_DEPTH_TEST);
116 glEnable(GL_CULL_FACE);
117 glCullFace(GL_BACK);
118
119 shader->use();
120 glBindVertexArray(vao);
121 glDrawArrays(GL_TRIANGLES, 0, 12*3);
122 glBindVertexArray(0);
123 }
124
125private:
126
127 glm::vec3 a, b, c;
128
129 std::unique_ptr<jGL::GL::glShader> shader;
130
131 std::array<float, 12*3*3> cube;
132
133 const std::array<float, 12*3*2> textureCoords =
134 {
135 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
136 0.0, 1.0, 1.0, 0.0, 1.0, 1.0,
137
138 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
139 0.0, 1.0, 1.0, 1.0, 1.0, 0.0,
140
141 0.0, 0.0, 1.0, 0.0, 1.0, 1.0,
142 0.0, 0.0, 1.0, 1.0, 0.0, 1.0,
143
144 0.0, 0.0, 1.0, 0.0, 1.0, 1.0,
145 0.0, 0.0, 1.0, 1.0, 0.0, 1.0,
146
147 0.0, 0.0, 1.0, 0.0, 1.0, 1.0,
148 0.0, 0.0, 1.0, 1.0, 0.0, 1.0,
149
150 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
151 1.0, 0.0, 0.0, 0.0, 0.0, 1.0
152 };
153
154 GLuint vao, a_vertices, a_coords;
155
156 const char * vertexShader =
157 "#version " GLSL_VERSION "\n"
158 "precision lowp float; precision lowp int;\n"
159 "layout(location=0) in vec3 a_vertices;\n"
160 "layout(location=1) in vec2 a_coords;\n"
161 "uniform mat4 proj;\n"
162 "out vec2 coords;\n"
163 "void main()\n"
164 "{\n"
165 " gl_Position = proj*vec4(a_vertices.xyz, 1.0);\n"
166 " coords = a_coords;\n"
167 "}";
168
169 const char * fragmentShader =
170 "#version " GLSL_VERSION "\n"
171 "precision lowp float; precision lowp int;\n"
172 "uniform vec4 colour;\n"
173 "in vec2 coords;\n"
174 "out vec4 o_colour;\n"
175 "void main()\n"
176 "{\n"
177 " float d = 1.0-2.0*min(min(coords.x, 1.0-coords.x), min(coords.y, 1.0-coords.y));\n"
178 " if (d < 0.98) { d = smoothstep(0.97, 0.98, d); }\n"
179 " else { d = 1.0; }\n"
180 " o_colour = vec4(1.0, 0.0, 0.0, d);\n"
181 "}";
182
183 void generateFaces()
184 {
185 // Front vertices.
186 glm::vec3 f0 = {0.0, 0.0, 0.0};
187 glm::vec3 f1 = f0 + a;
188 glm::vec3 f2 = f0 + b;
189 glm::vec3 f3 = f0 + a + b;
190 // Back vertices.
191 glm::vec3 b0 = f0 + c;
192 glm::vec3 b1 = b0 + a;
193 glm::vec3 b2 = b0 + b;
194 glm::vec3 b3 = b0 + a + b;
195
196 std::array<glm::vec3, 12*3> vertices =
197 {
198 f0, f2, f1,
199 f1, f2, f3,
200
201 b0, b1, b2,
202 b1, b3, b2,
203
204 f1, f3, b3,
205 f1, b3, b1,
206
207 f0, b0, b2,
208 f0, b2, f2,
209
210 f0, f1, b1,
211 f0, b1, b0,
212
213 f2, b3, f3,
214 f2, b2, b3
215 };
216
217 glm::vec3 com = {0.0, 0.0, 0.0};
218 for (const auto & v : vertices) { com += v; }
219 com /= float(vertices.size());
220
221 for (uint8_t i = 0; i < vertices.size(); i++)
222 {
223 vertices[i] -= com;
224 for (uint8_t j = 0; j < 3; j++)
225 {
226 cube[i*3+j] = vertices[i][j];
227 }
228 }
229 }
230};
231
232#endif /* CELL_H */
Draw the simulation cell (a box).
Definition cell.h:17
void setVectors(glm::vec3 a, glm::vec3 b, glm::vec3 c)
Set the cell vectors.
Definition cell.h:91
void draw()
Draw the cell box by its edges.
Definition cell.h:111
~Cell()
Definition cell.h:66
Cell(glm::vec3 a, glm::vec3 b, glm::vec3 c)
Construct a new Cell from cell vectors.
Definition cell.h:28
void setProjectionView(glm::mat4 pv)
Set the Projection*View matrix.
Definition cell.h:78
void subFullBuffer(GLuint &buffer, float *data, GLuint size)
Substitute a GL_ARRAY_BUFFER fully.
Definition glUtils.h:15
void createBuffer(GLuint &buffer, const float *data, GLuint dataSize, int drawType, GLuint attribute, GLuint size, GLuint divisor)
Create a GL_ARRAY_BUFFER from data.
Definition glUtils.h:71