diff options
author | pommicket <pommicket@gmail.com> | 2021-09-30 10:36:23 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2021-09-30 10:36:23 -0400 |
commit | 3b6daa4906d6895157a0ed6dcc1749c34b9e997b (patch) | |
tree | 84b4159afbed8d0926b333fbd1943bf72de92d5f /main.c | |
parent | 9be232043127b484a87847c74e039963759f3655 (diff) |
cleanup
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 67 |
1 files changed, 43 insertions, 24 deletions
@@ -1,6 +1,7 @@ // @TODO: // - run grain computations on GPU // - config file +// - color coding based on speed #define V_GL 1 #define V_WINDOWED 1 #include "vlib.h" @@ -12,8 +13,12 @@ static mat4 g_camera; static bool g_wireframe; typedef struct { - vec3 *grains; // dynamic array in order of creation -} World; + vec3 *grains; + uint32_t ngrains; + uint32_t new_grains_per_second; + float grain_gen_radius; + vec4 color; +} Function; typedef struct { vec3 pos; // position of player's feet @@ -39,7 +44,7 @@ int main(int argc, char **argv) { } Player player_data = {0}, *player = &player_data; - World world_data = {0}, *world = &world_data; + Function f_data = {0}, *f = &f_data; program_grain = gl_program_new("grainv.glsl", "grainf.glsl"); typedef struct { @@ -50,14 +55,14 @@ int main(int argc, char **argv) { GLVAO grain_vao = gl_vao_new(program_grain, "grain"); { float h = 0.005f; - float HS3 = 0.866025403784f; // sqrt(3)/2 + float S3 = 0.57735026919f; // sqrt(1/3) UnlitVertex vertices[] = { {0,h*.5f,0}, - {-h*HS3,-h*.5f,0}, - {+h*HS3,-h*.5f,0}, + {-h*S3,-h*.5f,0}, + {+h*S3,-h*.5f,0}, {0,h*.5f,0}, - {0,-h*.5f,-h*HS3}, - {0,-h*.5f,+h*HS3}, + {0,-h*.5f,-h*S3}, + {0,-h*.5f,+h*S3}, }; gl_vbo_set_static_data(&grain_vbo, vertices, static_arr_len(vertices)); gl_vao_add_data3f(&grain_vao, grain_vbo, "v_pos", UnlitVertex, pos); @@ -67,23 +72,33 @@ int main(int argc, char **argv) { window_set_relative_mouse(1); - float grain_gen_radius = 2; - for (int i = 0; i < 100000; ++i) { - arr_add(world->grains, scale3(addc3(rand_vec3(), -.5f), grain_gen_radius * 2)); - } + f->ngrains = 100000; + f->new_grains_per_second = 1000; + f->grain_gen_radius = 2; + const uint32_t tex_width = 1024; uint32_t tex_chunk_size = tex_width * 4; // height must be a multiple of 4 - uint32_t tex_area = ((arr_len(world->grains) + tex_chunk_size-1) / tex_chunk_size) * tex_chunk_size; // width * height of grain pos texture + uint32_t tex_area = ((f->ngrains + tex_chunk_size-1) / tex_chunk_size) * tex_chunk_size; // width * height of grain pos texture if (tex_area < tex_chunk_size) tex_area = tex_chunk_size; uint32_t tex_height = tex_area / tex_width; - arr_reserve(world->grains, tex_area); - arr_foreachp(world->grains, vec3, g) { - g->x = g->y = g->z = NAN; + { + size_t n_grains_alloc = (size_t)((f->ngrains + tex_chunk_size - 1) / tex_chunk_size) * tex_chunk_size; + f->grains = calloc(n_grains_alloc, sizeof *f->grains); + } + if (!f->grains) { + window_message_box_error("Out of memory", "Not enough memory for grains. Try reducing the max number of grains."); + return -1; } + for (int i = 0; i < f->ngrains; ++i) { + f->grains[i] = Vec3(NAN, NAN, NAN); + } + float leftover_time = 0; + bool fullscreen = false; + while (1) { SDL_Event event = {0}; @@ -99,6 +114,10 @@ int main(int argc, char **argv) { case SDLK_z: g_wireframe = !g_wireframe; break; + case SDLK_F11: + fullscreen = !fullscreen; + window_set_fullscreen(fullscreen); + break; } break; case SDL_MOUSEMOTION: { @@ -132,20 +151,20 @@ int main(int argc, char **argv) { gl.Enable(GL_DEPTH_TEST); { - uint n_new_grains = 100; - uint32_t ngrains = arr_len(world->grains); vec3 *p, *end; - memmove(world->grains, world->grains + n_new_grains, (ngrains - n_new_grains) * sizeof *world->grains); - end = world->grains + ngrains; + uint32_t n_new_grains = (uint32_t)((float)f->new_grains_per_second * dt); + memmove(f->grains, f->grains + n_new_grains, (f->ngrains - n_new_grains) * sizeof *f->grains); + end = f->grains + f->ngrains; for (p = end - n_new_grains; p < end; ++p) { - *p = scale3(addc3(rand_vec3(), -.5f), grain_gen_radius * 2); + *p = scale3(addc3(rand_vec3(), -.5f), f->grain_gen_radius * 2); } } { // fixed time step (for consistency) float t = dt + leftover_time; while (t >= timestep) { - arr_foreachp(world->grains, vec3, g) { + vec3 *g, *end; + for (g = f->grains, end = g + f->ngrains; g < end; ++g) { float x = g->x, y = g->y, z = g->z; (void)x; (void)y; (void)z; vec3 wind = Vec3(-tanf(y)*x, cosf(z)*y, sinf(z)*x); @@ -165,7 +184,7 @@ int main(int argc, char **argv) { gl_uniformM4(program_grain, "u_transform", &g_camera); gl_uniform4f(program_grain, "u_color", Vec4(1.0f,0.8f,.6f,1)); gl.BindTexture(GL_TEXTURE_2D, grains_texture); - gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, (GLsizei)tex_width, (GLsizei)tex_height, 0, GL_RGB, GL_FLOAT, world->grains); + gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, (GLsizei)tex_width, (GLsizei)tex_height, 0, GL_RGB, GL_FLOAT, f->grains); gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -177,7 +196,7 @@ int main(int argc, char **argv) { gl.BindVertexArray(grain_vao.id); - gl.DrawArraysInstanced(GL_TRIANGLES, 0, 6, (GLsizei)arr_len(world->grains)); + gl.DrawArraysInstanced(GL_TRIANGLES, 0, 6, (GLsizei)f->ngrains); } |