How to generate basic Perlin noise with Raylib and stb_perlin in C

2 Answers

0 votes
#include "raylib.h"

// #define STB_PERLIN_IMPLEMENTATION
#include "stb_perlin.h"

int main(void)
{
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "Raylib Perlin Noise");

    int currentTexture = 0;

    SetTargetFPS(60);

    // Main game loop
    while (!WindowShouldClose())
    {
        BeginDrawing();

        ClearBackground(RAYWHITE);

        // void DrawText(const char *text, int posX, int posY, int fontSize, Color color);   
        DrawText("PERLIN NOISE", 640, 10, 20, RED);

        // Generate and display Perlin noise
        for (int y = 0; y < screenHeight; y++) {
            for (int x = 0; x < screenWidth; x++) {

                // Scale coordinates for Perlin noise function
                float nx = x / (float)screenWidth, ny = y / (float)screenHeight;

                // float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)
                float noiseValue = stb_perlin_noise3(nx * 10, ny * 10, 0, 0, 0, 0);

                /*
                    typedef struct Color {
                        unsigned char r;        // Color red value
                        unsigned char g;        // Color green value
                        unsigned char b;        // Color blue value
                        unsigned char a;        // Color alpha value
                    } Color;
                */

                // Map noise value to grayscale color
                int color = (unsigned char)((noiseValue + 1) * 127);

                Color c = { color, color, color, 255 };

                DrawPixel(x, y, c);
            }
        }

        EndDrawing();
    }

    CloseWindow(); // Close window and OpenGL context

    return 0;
}



/*
run:


*/

 



answered Jan 25 by avibootz
edited Jan 26 by avibootz
0 votes
#include "raylib.h"

// #define STB_PERLIN_IMPLEMENTATION
#include "stb_perlin.h"

#define WIDTH 800
#define HEIGHT 600

int main(void)
{
    InitWindow(WIDTH, HEIGHT, "Raylib Perlin Noise");

    float scale = 0.1f;

    SetTargetFPS(60);

    // Main game loop
    while (!WindowShouldClose())
    {
        BeginDrawing();

        ClearBackground(RAYWHITE);

        for (int y = 0; y < HEIGHT; y++) {
            for (int x = 0; x < WIDTH; x++) {

                // float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)
                float noiseValue = stb_perlin_noise3(x * scale, y * scale, 0.0f, 0, 0, 0);

                /*
                    typedef struct Color {
                        unsigned char r;        // Color red value
                        unsigned char g;        // Color green value
                        unsigned char b;        // Color blue value
                        unsigned char a;        // Color alpha value
                    } Color;
                */

                // Map noise value to color
                int color = (unsigned char)(noiseValue * 255);

                Color c = { color, color, color, 255 };

                DrawPixel(x, y, c);
            }
        }

        DrawText("PERLIN NOISE", 640, 10, 20, RED);

        EndDrawing();
    }

    CloseWindow(); // Close window and OpenGL context

    return 0;
}



/*
run:


*/

 



answered Jan 26 by avibootz
edited Jan 26 by avibootz
...