void OsnNoise::set_seed(int seed) { if (seed == _seed) return; // This is not fair... if (_context) open_simplex_noise_free(_context); open_simplex_noise(seed, &_context); _seed = seed; }
heman_image* heman_ops_emboss(heman_image* img, int mode) { int seed = 1; int octaves = 4; struct osn_context* ctx; open_simplex_noise(seed, &ctx); int width = img->width; int height = img->height; assert(img->nbands == 1); heman_image* result = heman_image_create(width, height, 1); HEMAN_FLOAT invw = 1.0 / width; HEMAN_FLOAT invh = 1.0 / height; HEMAN_FLOAT inv = MIN(invw, invh); float gain = 0.6; float lacunarity = 2.0; float land_amplitude = 0.0005; float land_frequency = 256.0; float ocean_amplitude = 0.5; float ocean_frequency = 1.0; #pragma omp parallel for for (int y = 0; y < height; y++) { HEMAN_FLOAT* dst = result->data + y * width; for (int x = 0; x < width; x++) { HEMAN_FLOAT z = *heman_image_texel(img, x, y); if (z > 0 && mode == 1) { float s = x * inv; float t = y * inv; float a = land_amplitude; float f = land_frequency; for (int i = 0; i < octaves; i++) { z += NOISEX(s, t, a, f); a *= gain; f *= lacunarity; } } else if (z <= 0 && mode == -1) { z = MAX(z, -0.1); float soften = fabsf(z); float s = x * inv; float t = y * inv; float a = ocean_amplitude; float f = ocean_frequency; for (int i = 0; i < octaves; i++) { z += soften * NOISEX(s, t, a, f); a *= gain; f *= lacunarity; } } *dst++ = z; } } open_simplex_noise_free(ctx); return result; }
int init_engine(SDL_GLContext *context, SDL_Window *window) { int img_flags = IMG_INIT_PNG; if (!(IMG_Init(img_flags) & img_flags)) { printf("SD_image could not initialize! SDL_image Error: %s\n", IMG_GetError()); return -1; } if (init_gl(context, window)) return -1; if (init_glew()) return -1; // if (init_effects(shader_programs, shader_infos, LENGTH(shader_programs))) { // printf("Something went wrong with shader program initialization!\n"); // return -1; // } load_effects( effects.all, LENGTH(effects.all), shader_file_paths, LENGTH(shader_file_paths), attribute_strings, LENGTH(attribute_strings), uniform_strings, LENGTH(uniform_strings)); open_simplex_noise(open_simplex_noise_seed, &osnctx); SDL_GameControllerEventState(SDL_ENABLE); /* Open the first available controller. */ SDL_GameController *controller = NULL; for (int i = 0; i < SDL_NumJoysticks(); ++i) { printf("Testing controller %i\n", i); if (SDL_IsGameController(i)) { controller = SDL_GameControllerOpen(i); if (controller) { printf("Successfully opened controller %i\n", i); break; } else { printf("Could not open gamecontroller %i: %s\n", i, SDL_GetError()); } } else { printf("Controller %i is not a controller?\n", i); } } init_keyboard(); init_render(); //Located in render.c if (signal(SIGUSR1, reload_signal_handler) == SIG_ERR) { printf("An error occurred while setting a signal handler.\n"); } return 0; }
// Initializes a newly created Generator object instance. VALUE OSimplex_Generator_init (const int argc, const VALUE *argv, const VALUE self) { SELF2GENERATOR(); VALUE seed = UINT2NUM(0); VALUE scale = rb_float_new(1.0); VALUE alpha = rb_float_new(1.0); VALUE beta = rb_float_new(0.0); rb_scan_args(argc, argv, "04", &seed, &scale, &alpha, &beta); open_simplex_noise(NUM2UINT(seed), &gen->context); gen->scale = NUM2DBL( scale ); gen->alpha = NUM2DBL( alpha ); gen->beta = NUM2DBL( beta ); return self; }
int main(int argc, char *argv[]) { float min, max; setlocale(LC_ALL, "C"); process_options(argc, argv); snis_srand((unsigned int) random_seed); open_simplex_noise(random_seed, &ctx); printf("Loading %s\n", heightfile); sampledata = load_image(heightfile, &samplew, &sampleh, &samplea, &sample_bytes_per_row); printf("Loading %s\n", landfile); land = load_image(landfile, &landw, &landh, &landa, &landbpr); printf("Loading %s\n", waterfile); water = load_image(waterfile, &waterw, &waterh, &watera, &waterbpr); printf("Allocating output images.\n"); allocate_output_images(); printf("Initializing vertices.\n"); initialize_vertices(); find_min_max_height(&min, &max); add_bumps(initial_bumps); add_craters(ncraters); printf("Rendering %d total bumps\n", totalbumps); render_all_bumps(); render_all_craters(min, max); find_min_max_height(&min, &max); printf("min h = %f, max h = %f\n", min, max); paint_height_maps(min, max); calculate_normals(); paint_normal_and_height_maps(min, max); printf("painting terrain colors\n"); paint_terrain_colors(min, max); printf("Writing color textures\n"); save_output_images(); printf("Writing normal maps\n"); save_normal_maps(); printf("Writing height maps\n"); save_height_maps(); open_simplex_noise_free(ctx); printf("Done.\n"); return 0; }
void SimplexNoise::_init_seeds() { for (int i = 0; i < 6; ++i) { open_simplex_noise(seed + i * 2, &(contexts[i])); } }
static int simplex(double* buffer, const scil_dims_t* dims, double mn, double mx, double arg, double arg2, int seed){ const int frequencyCount = (int) arg2; double highFrequency = (double) arg; if( scilU_double_equal(mn, mx) || frequencyCount <= 0 ){ return SCIL_EINVAL; } struct osn_context *ctx; open_simplex_noise(seed, &ctx); int64_t max_potenz = 1<<frequencyCount; const size_t* len = dims->length; switch(dims->dims){ case (1):{ size_t count = scil_dims_get_count(dims); for (size_t i=0; i < count; i++){ buffer[i] = 0; int64_t potenz = max_potenz; int64_t divisor = 1; for(int f = 1 ; f <= frequencyCount; f++){ buffer[i] += potenz * open_simplex_noise2(ctx, 1.0, (double) i / count * divisor * highFrequency); potenz /= 2; divisor *= 2; } } break; }case (2):{ for (size_t y=0; y < len[1]; y++){ for (size_t x=0; x < len[0]; x++){ double var = 0; int64_t potenz = max_potenz; int64_t divisor = 1; for(int f = 1 ; f <= frequencyCount; f++){ var += potenz * open_simplex_noise2(ctx, (double) x / len[0] * divisor*highFrequency, (double) y / len[1] * divisor*highFrequency); potenz /= 2; divisor *= 2; } buffer[x+y*len[0]] = var; } } break; }case (3):{ for (size_t z=0; z < len[2]; z++){ for (size_t y=0; y < len[1]; y++){ for (size_t x=0; x < len[0]; x++){ double var = 0; int64_t potenz = max_potenz; int64_t divisor = 1; for(int f = 1 ; f <= frequencyCount; f++){ var += potenz * open_simplex_noise3(ctx, (double) x / len[0]*divisor*highFrequency, (double) y / len[1]*divisor*highFrequency, (double) z / len[2]*divisor*highFrequency); potenz /= 2; divisor *= 2; } buffer[x+y*len[0]+z*(len[0]*len[1])] = var; } } } break; }case (4):{ for (size_t w=0; w < len[3]; w++){ for (size_t z=0; z < len[2]; z++){ for (size_t y=0; y < len[1]; y++){ for (size_t x=0; x < len[0]; x++){ double var = 0; int64_t potenz = max_potenz; int64_t divisor = 1; for(int f = 1 ; f <= frequencyCount; f++){ var += potenz * open_simplex_noise4(ctx, (double) x / len[0]*divisor*highFrequency, (double) y / len[1]*divisor*highFrequency, (double) z / len[2]*divisor*highFrequency, (double) w / len[3]*divisor*highFrequency); potenz /= 2; divisor *= 2; } buffer[x+len[0]*(y+len[1]*(z+len[2]*w))] = var; } } } } break; }default: assert(0 && "Not supported"); } // fix min + max, first identify min/max scilP_change_data_scale(buffer, dims, mn, mx); open_simplex_noise_free(ctx); return SCIL_NO_ERR; }
heman_image* heman_ops_warp_core(heman_image* img, heman_image* secondary, int seed, int octaves) { struct osn_context* ctx; open_simplex_noise(seed, &ctx); int width = img->width; int height = img->height; int nbands = img->nbands; heman_image* result = heman_image_create(width, height, nbands); heman_image* result2 = secondary ? heman_image_create(width, height, secondary->nbands) : 0; HEMAN_FLOAT invw = 1.0 / width; HEMAN_FLOAT invh = 1.0 / height; HEMAN_FLOAT inv = MIN(invw, invh); HEMAN_FLOAT aspect = (float) width / height; float gain = 0.6; float lacunarity = 2.0; float initial_amplitude = 0.05; float initial_frequency = 8.0; #pragma omp parallel for for (int y = 0; y < height; y++) { HEMAN_FLOAT* dst = result->data + y * width * nbands; for (int x = 0; x < width; x++) { float a = initial_amplitude; float f = initial_frequency; HEMAN_FLOAT* src; // This is a little hack that modulates noise according to // elevation, to prevent "swimming" at high elevations. if (nbands == 4) { src = heman_image_texel(img, x, y); HEMAN_FLOAT elev = 1 - src[3]; a *= pow(elev, 4); } float s = x * inv; float t = y * inv; float u = x * invw; float v = y * invh; for (int i = 0; i < octaves; i++) { u += NOISEX(s, t, a, f); v += aspect * NOISEY(s, t, a, f); a *= gain; f *= lacunarity; } int i = CLAMP(u * width, 0, width - 1); int j = CLAMP(v * height, 0, height - 1); src = heman_image_texel(img, i, j); for (int n = 0; n < nbands; n++) { *dst++ = *src++; } if (secondary) { src = heman_image_texel(secondary, x, y); HEMAN_FLOAT* dst2 = heman_image_texel(result2, i, j); for (int n = 0; n < secondary->nbands; n++) { *dst2++ = *src++; } } } } open_simplex_noise_free(ctx); if (secondary) { free(secondary->data); secondary->data = result2->data; free(result2); } return result; }
OsnNoise::OsnNoise() : Reference(), _context(NULL), _seed(0) { open_simplex_noise(_seed, &_context); }