How to fill a matrix with Perlin noise values using stb_perlin_noise3 in C

1 Answer

0 votes
#define STB_PERLIN_IMPLEMENTATION
#include "stb_perlin.h"

#include <stdio.h>

int main() {
    int width = 16;
    int height = 16;
    float scale = 0.1f;
    float noise[16][16] = { {0},{0} };

    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            float fx = x * scale;
            float fy = y * scale;
            noise[y][x] = stb_perlin_noise3(fx, fy, 0.0f, 0, 0, 0);
        }
    }

    // Print the noise values
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            printf("%f ", noise[y][x]);
        }
        printf("\n");
    }

    return 0;
}



/*
run:

-0.106848 -0.099210 -0.060909 0.006331 0.081986 0.141012 0.163839 0.142079 0.079955 -0.008553 -0.106848 -0.203512 -0.282563 -0.324415 -0.316221 -0.257704
-0.234752 -0.227494 -0.191100 -0.127208 -0.055320 0.000768 0.022458 0.001782 -0.057249 -0.141350 -0.234752 -0.325374 -0.393113 -0.416444 -0.383373 -0.296336
-0.365232 -0.358784 -0.326452 -0.269693 -0.205829 -0.156002 -0.136733 -0.155101 -0.207543 -0.282256 -0.365232 -0.445166 -0.501393 -0.512088 -0.465389 -0.364156
-0.463488 -0.458230 -0.431861 -0.385570 -0.333485 -0.292848 -0.277133 -0.292114 -0.334883 -0.395816 -0.463488 -0.528931 -0.575175 -0.582051 -0.536967 -0.440464
-0.500000 -0.496148 -0.476832 -0.442922 -0.404768 -0.375000 -0.363488 -0.374462 -0.405792 -0.450428 -0.500000 -0.549144 -0.588416 -0.601076 -0.573024 -0.500000
-0.463488 -0.461042 -0.448779 -0.427250 -0.403027 -0.384128 -0.376819 -0.383786 -0.403677 -0.432016 -0.463488 -0.496958 -0.532863 -0.558986 -0.559238 -0.523024
-0.365232 -0.363976 -0.357676 -0.346615 -0.334171 -0.324462 -0.320707 -0.324287 -0.334505 -0.349064 -0.365232 -0.385894 -0.421515 -0.464484 -0.496685 -0.501076
-0.234752 -0.234306 -0.232068 -0.228140 -0.223720 -0.220272 -0.218938 -0.220210 -0.223839 -0.229009 -0.234752 -0.246959 -0.283950 -0.341725 -0.400580 -0.438416
-0.106848 -0.106782 -0.106451 -0.105871 -0.105218 -0.104708 -0.104511 -0.104699 -0.105235 -0.105999 -0.106848 -0.115203 -0.153508 -0.219663 -0.293128 -0.349144
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 -0.007704 -0.046336 -0.114156 -0.190464 -0.250000
0.106848 0.107565 0.107618 0.106764 0.105109 0.102996 0.100883 0.099228 0.098374 0.098427 0.099144 0.092289 0.054421 -0.012863 -0.088983 -0.148716
0.234752 0.239652 0.240298 0.235126 0.224825 0.211584 0.198343 0.188042 0.182871 0.183516 0.188416 0.186454 0.152993 0.088802 0.013766 -0.047104
0.365232 0.379167 0.381791 0.368944 0.342458 0.308154 0.273850 0.247364 0.234518 0.237141 0.251076 0.259540 0.235467 0.177865 0.105137 0.041846
0.463488 0.490884 0.497558 0.475891 0.429235 0.368256 0.307277 0.260621 0.238954 0.245628 0.273024 0.296792 0.286499 0.238569 0.169229 0.102384
0.500000 0.543580 0.556560 0.527690 0.461920 0.375000 0.288080 0.222310 0.193440 0.206420 0.250000 0.291868 0.297872 0.261382 0.196048 0.125000

*/

 



answered Jan 26, 2025 by avibootz

Related questions

1 answer 289 views
1 answer 164 views
1 answer 298 views
298 views asked Feb 17, 2024 by avibootz
1 answer 194 views
...