From 02281e2a151634f8d57c98e77ab4bb09fafab4fd Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 3 Oct 2021 10:52:14 -0400 Subject: nice example with comments, also nice errors --- main.c | 38 +++++++++++++++++++++++++++++----- sandboxes/EXAMPLE_circulation.txt | 43 +++++++++++++++++++++++++++++++++++++++ sandboxes/test.txt | 11 ---------- sandboxes/test2.txt | 1 - 4 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 sandboxes/EXAMPLE_circulation.txt delete mode 100644 sandboxes/test.txt delete mode 100644 sandboxes/test2.txt diff --git a/main.c b/main.c index 5fd77b9..d40625e 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,6 @@ // @TODO: -// - nice examples with comments // - readme +// - more examples /* Anyone is free to modify/distribute/use/sell/etc this software for any purpose by any means. @@ -56,7 +56,7 @@ static void APIENTRY gl_message_callback(GLenum source, GLenum type, unsigned in static GLuint g_grain_vshader, g_update_vshader; -static void function_init(Function *f, const char *wind_formula, uint32_t ngrains) { +static void function_init(Function *f, const char *config_filename, int config_line_number, const char *wind_formula, uint32_t ngrains) { memset(f, 0, sizeof *f); { GLuint tex[2] = {0}; @@ -101,8 +101,35 @@ static void function_init(Function *f, const char *wind_formula, uint32_t ngrain wind_formula ); - GLuint fshader = V_gl_shader_compile_code("updatef.glsl", NULL, fshader_code, GL_FRAGMENT_SHADER); + GLuint fshader = gl.CreateShader(GL_FRAGMENT_SHADER); + const GLchar *sources[2]; GLint status = 0; + + char header[256]; + strbuf_print(header, + "#version %d\n" + "#define GLSL_VERSION %d\n" + "#define PI 3.14159265\n" + "#define pi PI\n" + "#line 1\n", V_glsl_version(), V_glsl_version()); + sources[0] = header; + sources[1] = fshader_code; + + gl.ShaderSource(fshader, 2, sources, NULL); + gl.CompileShader(fshader); + { + gl.GetShaderiv(fshader, GL_COMPILE_STATUS, &status); + if (status == 1) { + // all good! + } else { + char log[256]; + strbuf_print(log, "Your formula on line %d of %s has errors. Details:\n", config_line_number, config_filename); + gl.GetShaderInfoLog(fshader, (GLsizei)(sizeof log - strlen(log)), NULL, log + strlen(log)); + window_message_box_error("Formula error", log); + exit(-1); + } + } + GLuint prog = gl.CreateProgram(); gl.AttachShader(prog, g_update_vshader); gl.AttachShader(prog, fshader); @@ -118,6 +145,7 @@ static void function_init(Function *f, const char *wind_formula, uint32_t ngrain { static char fshader_code[65536]; strbuf_print(fshader_code, + "#define pi PI\n" "uniform vec4 u_color1, u_color2;\n" "uniform float u_color_scale;\n" "varying vec3 pos;\n" @@ -242,7 +270,7 @@ static Function *sandbox_create(const char *config_filename, Player *player) { } while (0) line[strcspn(line, "\r\n")] = '\0'; - if (line[0] == '#' || line[0] == '\0') break; + if (line[0] == '#' || line[0] == '\0') continue; char *command = line; char *args = strchr(command, ' '); @@ -254,7 +282,7 @@ static Function *sandbox_create(const char *config_filename, Player *player) { } if (strcmp(command, "add") == 0) { Function *f = arr_addp(functions); - function_init(f, args, ngrains); + function_init(f, config_filename, line_number, args, ngrains); uint32_t nnew = (uint32_t)(grain_refresh_rate * (float)ngrains); f->new_grains_per_second = nnew == 0 ? 1 : nnew; f->grain_gen_radius = grain_gen_radius; diff --git a/sandboxes/EXAMPLE_circulation.txt b/sandboxes/EXAMPLE_circulation.txt new file mode 100644 index 0000000..1293dad --- /dev/null +++ b/sandboxes/EXAMPLE_circulation.txt @@ -0,0 +1,43 @@ +# This file controls the behavior of function sandbox. +# Here I'll explain what all the settings do, so you can make your own sandboxes. +# Any empty line or line beginning with the '#' character (like these first three) is ignored + +# This sets the number of grains used in the simulation to 20,000 +grains 20000 +# The grain "refresh rate" is the proportion of the grains which are +# regenerated (replaced by new ones in the starting area) every second +grain_refresh_rate 0.01 +# This controls the size of the starting area grains are added to +start_radius 3 +# Set the color of the grains (you can look up "color picker" to find tools for finding color codes) +color #ffaabb +# Let's add a function now! +# The functions are written in the GLSL language (check out https://www.khronos.org/opengles/sdk/docs/manglsl/docbook4/). +# You can use all sorts of functions like sin, cos, sqrt, +# etc. For multiplication you use * and to take a to the power of b you need to use pow(a, b). +# You can use x,y,z to get the coordinates of the grain, or p to get them as a vector. +add vec3(z, 0, -sin(y)*x) + +# We can add more functions in this same file. We can change settings before adding our next function +# to make them only apply to it. +# We'll use more grains for this one. +grains 30000 +# For this function, let's color-code grains based on speed. First, we can set the two colors, +# for "slow" grains and "fast" grains. +color #0000ff +color2 #ff0000 +# And now we need to specify the "scaling", i.e. how much change in speed corresponds to +# change in color. +color_scale 1 +# Now let's add the function. +add vec3(y, -x*cos(x), 0) + +# We can also control some settings which don't apply to any particular function. You can put these anywhere. +# This controls your movement speed. +move_speed 5.0 +# Now we get to some technical settings, which you can just leave set to the defaults +# Field of view in degrees +fov 50 +# Near and far clipping planes, respectively +clipping_planes 1 100 + diff --git a/sandboxes/test.txt b/sandboxes/test.txt deleted file mode 100644 index 32195f5..0000000 --- a/sandboxes/test.txt +++ /dev/null @@ -1,11 +0,0 @@ -clipping_planes 0.1 10 -move_speed 0 -fov 45 -grains 100000 -grain_refresh_rate 0.01 -color #0000ff -color2 #ff00ff -color_scale 0.5 -add vec3(-z, 0.0, x) -color2 #ffff00 -add vec3(-y, x, 0.0) diff --git a/sandboxes/test2.txt b/sandboxes/test2.txt deleted file mode 100644 index d30da32..0000000 --- a/sandboxes/test2.txt +++ /dev/null @@ -1 +0,0 @@ -add vec3(-z, 0, x) -- cgit v1.2.3