summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c85
1 files changed, 64 insertions, 21 deletions
diff --git a/main.c b/main.c
index e112bfa..cc9ed66 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,7 @@
#include <stdbool.h>
#include <stdlib.h>
+#include <openssl/rand.h>
#include <GL/glew.h>
#include <SDL2/SDL.h>
@@ -37,36 +38,35 @@ typedef struct {
static const float
verts[] = {
- -0.5, 0.5, 0,
- 0.5, 0.5, 0,
- 0.0, -0.5, 0,
+ -1, 1, 0,
+ 1, 1, 0,
+ 0.0, -1, 0,
};
static const char
vs_src[] =
"#version 320 es\n"
"\n"
- "layout (std140, binding = 0) buffer particles {\n"
- " mediump vec4 positions[" S(NUM_PARTICLES) "];\n"
- " mediump vec4 velocities[" S(NUM_PARTICLES) "];\n"
- " mediump vec4 colors[" S(NUM_PARTICLES) "];\n"
+ "layout (std430, binding = 1) buffer particles {\n"
+ " highp vec4 positions[" S(NUM_PARTICLES) "];\n"
+ " highp vec4 velocities[" S(NUM_PARTICLES) "];\n"
+ " highp vec4 colors[" S(NUM_PARTICLES) "];\n"
"};\n"
"\n"
"layout (location = 0) in mediump vec3 pos;\n"
"\n"
"void main() {\n"
- " mediump float ofs = 0.002 * float(gl_InstanceID - 500);\n"
- " gl_Position = vec4(pos.x - ofs, pos.y, pos.z, 1);\n"
+ " gl_Position = positions[gl_InstanceID] + 0.025 * vec4(pos, 1);\n"
"}\n";
static const char
fs_src[] =
"#version 320 es\n"
"\n"
- "layout (std140, binding = 0) buffer particles {\n"
- " mediump vec4 positions[" S(NUM_PARTICLES) "];\n"
- " mediump vec4 velocities[" S(NUM_PARTICLES) "];\n"
- " mediump vec4 colors[" S(NUM_PARTICLES) "];\n"
+ "layout (std430, binding = 1) buffer particles {\n"
+ " highp vec4 positions[" S(NUM_PARTICLES) "];\n"
+ " highp vec4 velocities[" S(NUM_PARTICLES) "];\n"
+ " highp vec4 colors[" S(NUM_PARTICLES) "];\n"
"};\n"
"\n"
"out mediump vec4 color;\n"
@@ -340,6 +340,56 @@ static Uint32 timer_cb(
return interval;
}
+static GLuint
+init_particles(void) {
+ // create shader storage buffer
+ GLuint ssbo;
+ glGenBuffers(1, &ssbo);
+
+ // generate seed data
+ uint8_t seeds[4 * NUM_PARTICLES];
+ if (!RAND_bytes(seeds, 4 * NUM_PARTICLES)) {
+ SDL_Log("RAND_bytes() failed");
+ exit(EXIT_FAILURE);
+ }
+
+ // populate data
+ float data[3 * 4 * NUM_PARTICLES];
+ #pragma omp parallel for
+ for (int i = 0; i < NUM_PARTICLES; i++) {
+ float x = 2.0 * seeds[4 * i + 0] / 0xFF - 1.0,
+ y = 2.0 * seeds[4 * i + 1] / 0xFF - 1.0;
+
+ // SDL_Log("%f,%f", x, y);
+
+ // populate position
+ data[4 * i + 0] = x;
+ data[4 * i + 1] = y;
+ data[4 * i + 2] = 0.0;
+ data[4 * i + 3] = 1.0;
+
+ // populate random velocity
+ data[4 * (NUM_PARTICLES + i) + 0] = 2.0 * seeds[4 * i + 2] / 0xFF - 1.0;
+ data[4 * (NUM_PARTICLES + i) + 1] = 2.0 * seeds[4 * i + 3] / 0xFF - 1.0;
+
+ // populate colors
+ data[4 * (2 * NUM_PARTICLES + i) + 0] = ((i % 3) == 0) ? 1.0 : 0.0;
+ data[4 * (2 * NUM_PARTICLES + i) + 1] = ((i % 3) == 1) ? 1.0 : 0.0;
+ data[4 * (2 * NUM_PARTICLES + i) + 2] = ((i % 3) == 2) ? 1.0 : 0.0;
+ data[4 * (2 * NUM_PARTICLES + i) + 3] = 1.0;
+ }
+
+ // populate buffer
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
+ glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
+
+ glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssbo);
+
+ // return buffer
+ return ssbo;
+}
+
int main(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
@@ -357,14 +407,7 @@ int main(int argc, char *argv[]) {
glBindVertexArray(vao);
// generate shader storage buffer
- GLuint ssbo;
- glGenBuffers(1, &ssbo);
- glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
- glBufferData(GL_SHADER_STORAGE_BUFFER, NUM_PARTICLES * 3 * 4 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
- glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
-
- // bind uniform buffer to slot 0
- // glBindBufferBase(GL_UNIFORM_BUFFER, 2, ubo);
+ GLuint ssbo = init_particles();
// generate vertex buffer
GLuint vbo;