Browse Source

test: add simplex noise test program.

undefined
Sam Hocevar 10 years ago
parent
commit
88603c1ecf
3 changed files with 80 additions and 1 deletions
  1. +1
    -0
      .gitignore
  2. +6
    -1
      demos/test/Makefile.am
  3. +73
    -0
      demos/test/simplex.cpp

+ 1
- 0
.gitignore View File

@@ -82,6 +82,7 @@ demos/test/sandbox/sample
demos/test/math/pi
demos/test/math/poly
demos/test/nacl_phystest
demos/test/simplex
demos/tutorial/01_triangle
demos/tutorial/02_cube
demos/tutorial/03_noise


+ 6
- 1
demos/test/Makefile.am View File

@@ -6,7 +6,8 @@ SUBDIRS = math sandbox
bench: benchsuite$(EXEEXT)
./benchsuite$(EXEEXT)

noinst_PROGRAMS = benchsuite btphystest nacl_phystest meshviewer
noinst_PROGRAMS = benchsuite btphystest nacl_phystest meshviewer \
simplex

benchsuite_SOURCES = benchsuite.cpp \
benchmark/vector.cpp benchmark/half.cpp benchmark/trig.cpp \
@@ -53,3 +54,7 @@ if USE_EMSCRIPTEN
meshviewer_LDFLAGS += -s EXPORTED_FUNCTIONS="['_main', '_C_Send']"
endif

simplex_SOURCES = simplex.cpp
simplex_CPPFLAGS = $(AM_CPPFLAGS)
simplex_DEPENDENCIES = @LOL_DEPS@


+ 73
- 0
demos/test/simplex.cpp View File

@@ -0,0 +1,73 @@
//
// Simplex Noise Test Program
//
// Copyright (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
// (c) 2013-2014 Guillaume Bittoun <guillaume.bittoun@gmail.com>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
//

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <lol/engine.h>

using namespace lol;

int main(int argc, char **argv)
{
UNUSED(argc, argv);

int const period = 2;
float const zoom = 0.03f;

/* Create some gradients */
array2d<vec2> gradients(vec_t<ptrdiff_t, 2>({period, period}));
for (int i = 0 ; i < period ; ++i)
for (int j = 0 ; j < period ; ++j)
gradients[i][j] = normalize(vec2(rand(-1.f, 1.f), rand(-1.f, 1.f)));
simplex_interpolator<2> s;
s.SetGradients(gradients);

/* Create an image */
ivec2 const size(800, 600);
Image img(size);
array2d<vec4> &data = img.Lock2D<PixelFormat::RGBA_F32>();

/* Fill image with simplex noise */
for (int j = 0; j < size.y; ++j)
for (int i = 0; i < size.x; ++i)
{
float p = 0.5f + 0.5f * s.Interp(zoom * vec2(i, j));
#if 0
for (int k = 2; k < 32; k *= 2)
p += 0.5f / k * s.Interp(zoom * k * vec2(i, j));
#endif
data[i][j] = vec4(saturate(p), 0.f, 0.f, 1.f);
}

/* Mark simplex vertices */
vec2 dx(1.f, 0.f);
vec2 dy = mat2::rotate(60.f) * dx;
for (int j = -100; j < 100; ++j)
for (int i = -100; i < 100; ++i)
{
ivec2 coord = ivec2(i / zoom * dx + j / zoom * dy);
if (coord.x >= 0 && coord.x < size.x - 1
&& coord.y >= 0 && coord.y < size.y - 1)
{
data[coord.x][coord.y] = vec4(1.f, 0.f, 1.f, 1.f);
data[coord.x + 1][coord.y] = vec4(1.f, 0.f, 1.f, 1.f);
data[coord.x][coord.y + 1] = vec4(1.f, 0.f, 1.f, 1.f);
data[coord.x + 1][coord.y + 1] = vec4(1.f, 0.f, 1.f, 1.f);
}
}

/* Save image */
img.Unlock2D(data);
img.Save("simplex.png");
}


Loading…
Cancel
Save