summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2021-10-03 11:56:52 -0400
committerpommicket <pommicket@gmail.com>2021-10-03 11:56:52 -0400
commit45e36030a20e77c109bd746e7be4a64a7c9a2fbd (patch)
treef9983770d889bd7d475b53382783c140f68d1f78
parent2afbc90e16e58ae8ca42a13573308758ac398492 (diff)
README, more examples
-rw-r--r--README.md47
-rw-r--r--example.pngbin0 -> 568808 bytes
-rw-r--r--main.c14
-rw-r--r--make.bat5
-rw-r--r--sandboxes/00.txt2
-rw-r--r--sandboxes/01.txt6
-rw-r--r--sandboxes/02.txt5
-rw-r--r--sandboxes/03.txt3
-rw-r--r--sandboxes/04.txt5
-rw-r--r--sandboxes/EXAMPLE.txt (renamed from sandboxes/EXAMPLE_circulation.txt)8
10 files changed, 81 insertions, 14 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6d5c9a3
--- /dev/null
+++ b/README.md
@@ -0,0 +1,47 @@
+# Function sandbox
+
+![A screenshot of function sandbox, specifically from sandbox 02](example.png)
+
+## About
+
+Function sandbox is a program which displays a very large number of "grains of sand", where
+each one is pushed around by some "wind", with the velocity of the wind at each point being determined
+by a function of the point's coordinates.
+It does all its computations on your GPU!
+
+## Instructions
+
+You can go to https://pommicket.itch.io/FunctionSandbox to get executables
+for Windows/Linux.
+
+On Linux you might need to install SDL2 (there's a very good chance you already have it installed). On Debian/Ubuntu,
+you can do this with `sudo apt install 'libsdl2-2*'`.
+
+Use W/A/S/D/Q/E or arrow keys/page up/page down to move.
+You can check out the pre-installed sandboxes. Keep in mind that some of them use a very large number
+of grains, so if your GPU isn't very good, it might not be able to handle some of them.
+If you want to make your own sandboxes, check out `sandboxes/EXAMPLE.txt` for a well-documented
+example.
+I really recommend to just play around with functions and see what you get.
+All the pre-installed sandboxes were just made with trial and error.
+
+## Compiling it yourself
+
+First, install SDL2. On Linux, just run
+
+```
+sudo apt install libsdl2-dev
+```
+
+or equivalent. On Windows, download Visual Studio, [the VC development libraries for SDL2](https://libsdl.org/release/SDL2-devel-2.0.16-VC.zip),
+add `vcvarsall.bat` to your PATH, then run `make.bat`.
+
+Alternatively, you can just compile `main.c` with any C compiler, making sure that `SDL.h` is in your include path (and you're linking with SDL2).
+
+## Bugs
+
+You can report a bug to `pommicket at pommicket.com`
+
+## License
+
+The code for function sandbox and all the example sandboxes are in the public domain.
diff --git a/example.png b/example.png
new file mode 100644
index 0000000..6f62ce9
--- /dev/null
+++ b/example.png
Binary files differ
diff --git a/main.c b/main.c
index f471548..9cd83df 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,3 @@
-// @TODO:
-// - readme
-// - more examples
/*
Anyone is free to modify/distribute/use/sell/etc
this software for any purpose by any means.
@@ -71,7 +68,7 @@ static bool function_init(Function *f, const char *config_filename, int config_l
strbuf_print(fshader_code,
"uniform sampler2D u_tex;\n"
"uniform float u_dt;\n"
- "uniform int u_new_grains_per_second;\n"
+ "uniform float u_new_grains_per_second;\n"
"uniform float u_rand_seed;\n"
"uniform float u_grain_gen_radius;\n"
"vec3 rand(vec3 co) {\n"
@@ -86,10 +83,11 @@ static bool function_init(Function *f, const char *config_filename, int config_l
"void main() {\n"
" ivec2 pixel = ivec2(gl_FragCoord.xy);\n"
" int pixel_idx = (pixel.y << 10) | pixel.x;\n"
- " if (pixel_idx < u_new_grains_per_second) {\n"
+ " int nnew = int(u_new_grains_per_second * u_dt);\n"
+ " if (pixel_idx < nnew) {\n"
" gl_FragColor = vec4(u_grain_gen_radius * rand(vec3(gl_FragCoord.xy * 0.001, u_rand_seed)), 0.0);\n"
" } else {\n"
- " int src_pixel_idx = pixel_idx - u_new_grains_per_second;\n"
+ " int src_pixel_idx = pixel_idx - nnew;\n"
" ivec2 src_pixel = ivec2(src_pixel_idx & 1023, src_pixel_idx >> 10);\n"
" vec3 p = texelFetch(u_tex, src_pixel, 0).xyz;\n"
" float x = p.x, y = p.y, z = p.z;\n"
@@ -264,7 +262,7 @@ static Function *sandbox_create(const char *config_filename, Player *player) {
if (fp) {
int line_number = 0;
uint32_t ngrains = 100000;
- float grain_refresh_rate = 0.01f;
+ float grain_refresh_rate = 0.05f;
float grain_gen_radius = 2;
vec4 color1 = Vec4(1.0f,0.8f,.6f,1);
vec4 color2 = Vec4(1.0f,0,0,1);
@@ -550,7 +548,7 @@ int main(int argc, char **argv) {
gl.ActiveTexture(GL_TEXTURE0);
gl.BindTexture(GL_TEXTURE_2D, f->grains_tex1);
gl_uniform1i(&f->update_program, "u_tex", 0);
- gl_uniform1i(&f->update_program, "u_new_grains_per_second", (GLint)f->new_grains_per_second);
+ gl_uniform1f(&f->update_program, "u_new_grains_per_second", (float)f->new_grains_per_second);
gl_uniform1f(&f->update_program, "u_dt", dt);
gl_uniform1f(&f->update_program, "u_rand_seed", randf());
gl_uniform1f(&f->update_program, "u_grain_gen_radius", f->grain_gen_radius);
diff --git a/make.bat b/make.bat
index 4d2e68a..91a4280 100644
--- a/make.bat
+++ b/make.bat
@@ -1,10 +1,7 @@
@echo off
if "%_VCVARS%" == "" (
set "_VCVARS=1"
- set "PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\Hostx64\x64\;C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64\;%PATH%"
- set "INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\ATLMFC\include;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include;C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared;C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt;%INCLUDE%"
- set "LIB=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\ATLMFC\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64;%LIB%"
- set "LIBPATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\ATLMFC\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\store\references;C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.18362.0;C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;%LIBPATH%"
+ vcvarsall x64
)
if "%1" == "" (
cl /nologo /W4 /wd4706 /wd4996 /wd4100 /Zi /Od /DEBUG /DDEBUG=1 main.c SDL2\lib\x64\SDL2.lib SDL2\lib\x64\SDL2main.lib /Fe:sandbox shell32.lib
diff --git a/sandboxes/00.txt b/sandboxes/00.txt
new file mode 100644
index 0000000..cb780c3
--- /dev/null
+++ b/sandboxes/00.txt
@@ -0,0 +1,2 @@
+color_scale 0.1
+add vec3(y * y * tan(z), z * z * cos(x), -x * x * tan(y))
diff --git a/sandboxes/01.txt b/sandboxes/01.txt
new file mode 100644
index 0000000..0ab5eca
--- /dev/null
+++ b/sandboxes/01.txt
@@ -0,0 +1,6 @@
+grains 500000
+grain_refresh_rate 0.03
+color_scale 1
+color #7700aa
+color2 #aa0077
+add 0.1*vec3(-y/tan(z), x*x, exp(cos(y)))
diff --git a/sandboxes/02.txt b/sandboxes/02.txt
new file mode 100644
index 0000000..313a931
--- /dev/null
+++ b/sandboxes/02.txt
@@ -0,0 +1,5 @@
+grains 1000000
+color_scale 1
+color #ffffff
+color2 #7f7f7f
+add vec3(-tan(y)*x, cos(z)*y, sin(z)*x)
diff --git a/sandboxes/03.txt b/sandboxes/03.txt
new file mode 100644
index 0000000..12e7621
--- /dev/null
+++ b/sandboxes/03.txt
@@ -0,0 +1,3 @@
+grains 500000
+color_scale 1
+add 0.1*cross(p, sin(p*20))
diff --git a/sandboxes/04.txt b/sandboxes/04.txt
new file mode 100644
index 0000000..a257fa7
--- /dev/null
+++ b/sandboxes/04.txt
@@ -0,0 +1,5 @@
+grains 500000
+color_scale 1
+color2 #0000ff
+color #ffffff
+add 0.1*vec3(1.0/z, x*z*tan(y), y*x*tan(z))
diff --git a/sandboxes/EXAMPLE_circulation.txt b/sandboxes/EXAMPLE.txt
index 8d31e84..49de071 100644
--- a/sandboxes/EXAMPLE_circulation.txt
+++ b/sandboxes/EXAMPLE.txt
@@ -1,12 +1,16 @@
# This file controls the behavior of function sandbox.
# Here I'll explain what all the settings do, so you can make your own sandboxes.
+# To create a new sandbox, just add a new .txt file in this folder. It'll automatically appear in the sandbox selection menu.
# Any empty line or line beginning with the '#' character (like these first three) is ignored
+# All the settings below are optional and have defaults. So you could create a sandbox with just
+# one line, e.g. 'add vec3(-z,0,x)'.
# 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
+# regenerated (replaced by new ones in the starting area) every second.
+# In other words, (1 / this) gives you the "lifetime" of each grain in seconds.
+grain_refresh_rate 0.1
# 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)