summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c32
1 files changed, 19 insertions, 13 deletions
diff --git a/main.c b/main.c
index 04eb4ce..e4bd106 100644
--- a/main.c
+++ b/main.c
@@ -97,9 +97,11 @@ cs_src[] =
"\n"
PARTICLE_BUFFER
"\n"
+ "uniform mediump float delta;\n"
+ "\n"
"void main() {\n"
" mediump uint i = gl_GlobalInvocationID.x;\n"
- " positions[i] += 0.016 * velocities[i];\n"
+ " positions[i].xy += delta * velocities[i].xy;\n"
"\n"
" if (positions[i].x > 1.1) {\n"
" positions[i].x -= 2.2;\n"
@@ -359,16 +361,20 @@ init_particles(void) {
static void
update_particles(
const GLuint ssbo,
- const GLuint compute_prog
+ const GLuint compute_prog,
+ const GLint u_delta,
+ const float delta
) {
if (USE_COMPUTE_SHADER) {
UNUSED(ssbo);
glUseProgram(compute_prog);
+ glUniform1f(u_delta, delta);
glDispatchCompute(NUM_PARTICLES / WORKGROUP_SIZE, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
} else {
UNUSED(compute_prog);
+ UNUSED(u_delta);
// bind shader storage buffer
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
@@ -385,7 +391,7 @@ update_particles(
#pragma omp parallel for
for (int i = 0; i < NUM_PARTICLES; i++) {
// update x coordinate
- data->positions[4 * i + 0] += 0.016 * data->velocities[4 * i + 0];
+ data->positions[4 * i + 0] += delta * data->velocities[4 * i + 0];
if (data->positions[4 * i + 0] < -1.1) {
data->positions[4 * i + 0] += 2.2;
} else if (data->positions[4 * i + 0] > 1.1) {
@@ -393,7 +399,7 @@ update_particles(
}
// update y coordinate
- data->positions[4 * i + 1] += 0.016 * data->velocities[4 * i + 1];
+ data->positions[4 * i + 1] += delta * data->velocities[4 * i + 1];
if (data->positions[4 * i + 1] < -1.1) {
data->positions[4 * i + 1] += 2.2;
} else if (data->positions[4 * i + 1] > 1.1) {
@@ -535,7 +541,7 @@ ctx_init(context_t * const ctx) {
}
// init times ring buffer
- // memset(ctx->times, 0, sizeof(ctx->times));
+ memset(ctx->times, 0, sizeof(ctx->times));
ctx->times_ofs = 0;
// init flags
@@ -669,26 +675,26 @@ int main(int argc, char *argv[]) {
// unbind vao
glBindVertexArray(0);
- // link compute program
+ // link compute program, get uniform
GLuint compute_prog = link_program(1, COMPUTE_SHADERS);
- // TODO: need to pass delta in shader storage block eventually
- // (for now it's hard-coded as 16ms in shader)
- // GLint u_delta = glGetUniformLocation(compute_prog, "delta");
+ GLint u_delta = glGetUniformLocation(compute_prog, "delta");
- // link render program, get uniforms
+ // link render program, get uniform
GLuint render_prog = link_program(2, RENDER_SHADERS);
GLint u_time = glGetUniformLocation(render_prog, "time");
// main loop
while (!ctx.done) {
- // get start time
- uint32_t now = SDL_GetTicks();
+ // get start time, then calculate delta
+ const uint32_t now = SDL_GetTicks();
+ const uint32_t last_times_ofs = (ctx.times_ofs - 1) & (MAX_TIMES - 1);
+ const float delta = (now - ctx.times[last_times_ofs].draw_start) / 1000.0;
// save start time
ctx.times[ctx.times_ofs].draw_start = now;
// update particles
- update_particles(ssbo, compute_prog);
+ update_particles(ssbo, compute_prog, u_delta, delta);
// clear screen
glClearColor(0, 0, 0, 1);