Fix GenImageGradientSquare and add to textures_image_generation example

This commit is contained in:
dane_madsen 2023-05-22 14:28:00 +10:00
parent 534adbe7ce
commit 42156ffd76
6 changed files with 363 additions and 288 deletions

View File

@ -13,7 +13,7 @@
#include "raylib.h" #include "raylib.h"
#define NUM_TEXTURES 7 // Currently we have 7 generation algorithms #define NUM_TEXTURES 10 // Currently we have 8 generation algorithms but some are have multiple purposes (Linear and Square Gradients)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
@ -31,8 +31,11 @@ int main(void)
Image horizontalGradient = GenImageGradientLinear(screenWidth, screenHeight, 90, RED, BLUE); Image horizontalGradient = GenImageGradientLinear(screenWidth, screenHeight, 90, RED, BLUE);
Image diagonalGradient = GenImageGradientLinear(screenWidth, screenHeight, 45, RED, BLUE); Image diagonalGradient = GenImageGradientLinear(screenWidth, screenHeight, 45, RED, BLUE);
Image radialGradient = GenImageGradientRadial(screenWidth, screenHeight, 0.0f, WHITE, BLACK); Image radialGradient = GenImageGradientRadial(screenWidth, screenHeight, 0.0f, WHITE, BLACK);
Image squareGradient = GenImageGradientSquare(screenWidth, screenHeight, screenHeight*0.9, screenHeight*0.9, 0.0f, WHITE, BLACK);
Image rectangularGradient = GenImageGradientSquare(screenWidth, screenHeight, screenWidth, screenHeight, 0.0f, WHITE, BLACK);
Image checked = GenImageChecked(screenWidth, screenHeight, 32, 32, RED, BLUE); Image checked = GenImageChecked(screenWidth, screenHeight, 32, 32, RED, BLUE);
Image whiteNoise = GenImageWhiteNoise(screenWidth, screenHeight, 0.5f); Image whiteNoise = GenImageWhiteNoise(screenWidth, screenHeight, 0.5f);
Image perlinNoise = GenImagePerlinNoise(screenWidth, screenHeight, 50, 50, 4.0f);
Image cellular = GenImageCellular(screenWidth, screenHeight, 32); Image cellular = GenImageCellular(screenWidth, screenHeight, 32);
Texture2D textures[NUM_TEXTURES] = { 0 }; Texture2D textures[NUM_TEXTURES] = { 0 };
@ -41,16 +44,22 @@ int main(void)
textures[1] = LoadTextureFromImage(horizontalGradient); textures[1] = LoadTextureFromImage(horizontalGradient);
textures[2] = LoadTextureFromImage(diagonalGradient); textures[2] = LoadTextureFromImage(diagonalGradient);
textures[3] = LoadTextureFromImage(radialGradient); textures[3] = LoadTextureFromImage(radialGradient);
textures[4] = LoadTextureFromImage(checked); textures[4] = LoadTextureFromImage(squareGradient);
textures[5] = LoadTextureFromImage(whiteNoise); textures[5] = LoadTextureFromImage(rectangularGradient);
textures[6] = LoadTextureFromImage(cellular); textures[6] = LoadTextureFromImage(checked);
textures[7] = LoadTextureFromImage(whiteNoise);
textures[8] = LoadTextureFromImage(perlinNoise);
textures[9] = LoadTextureFromImage(cellular);
// Unload image data (CPU RAM) // Unload image data (CPU RAM)
UnloadImage(verticalGradient); UnloadImage(verticalGradient);
UnloadImage(horizontalGradient); UnloadImage(horizontalGradient);
UnloadImage(diagonalGradient);
UnloadImage(radialGradient); UnloadImage(radialGradient);
UnloadImage(squareGradient);
UnloadImage(checked); UnloadImage(checked);
UnloadImage(whiteNoise); UnloadImage(whiteNoise);
UnloadImage(perlinNoise);
UnloadImage(cellular); UnloadImage(cellular);
int currentTexture = 0; int currentTexture = 0;
@ -87,9 +96,12 @@ int main(void)
case 1: DrawText("HORIZONTAL GRADIENT", 540, 10, 20, RAYWHITE); break; case 1: DrawText("HORIZONTAL GRADIENT", 540, 10, 20, RAYWHITE); break;
case 2: DrawText("DIAGONAL GRADIENT", 540, 10, 20, RAYWHITE); break; case 2: DrawText("DIAGONAL GRADIENT", 540, 10, 20, RAYWHITE); break;
case 3: DrawText("RADIAL GRADIENT", 580, 10, 20, LIGHTGRAY); break; case 3: DrawText("RADIAL GRADIENT", 580, 10, 20, LIGHTGRAY); break;
case 4: DrawText("CHECKED", 680, 10, 20, RAYWHITE); break; case 4: DrawText("SQUARE GRADIENT", 580, 10, 20, LIGHTGRAY); break;
case 5: DrawText("WHITE NOISE", 640, 10, 20, RED); break; case 5: DrawText("RECTANGULAR GRADIENT", 530, 10, 20, LIGHTGRAY); break;
case 6: DrawText("CELLULAR", 670, 10, 20, RAYWHITE); break; case 6: DrawText("CHECKED", 680, 10, 20, RAYWHITE); break;
case 7: DrawText("WHITE NOISE", 640, 10, 20, RED); break;
case 8: DrawText("PERLIN NOISE", 640, 10, 20, RED); break;
case 9: DrawText("CELLULAR", 670, 10, 20, RAYWHITE); break;
default: break; default: break;
} }

View File

@ -6242,7 +6242,7 @@
}, },
{ {
"name": "GenImageGradientLinear", "name": "GenImageGradientLinear",
"description": "Generate image: linear gradient", "description": "Generate image: linear gradient, direction in degrees [0..360], 0=Vertical gradient",
"returnType": "Image", "returnType": "Image",
"params": [ "params": [
{ {
@ -6294,6 +6294,41 @@
} }
] ]
}, },
{
"name": "GenImageGradientSquare",
"description": "Generate image: square gradient",
"returnType": "Image",
"params": [
{
"type": "int",
"name": "width"
},
{
"type": "int",
"name": "height"
},
{
"type": "int",
"name": "gradientWidth"
},
{
"type": "int",
"name": "gradientHeight"
},
{
"type": "float",
"name": "density"
},
{
"type": "Color",
"name": "inner"
},
{
"type": "Color",
"name": "outer"
}
]
},
{ {
"name": "GenImageChecked", "name": "GenImageChecked",
"description": "Generate image: checked", "description": "Generate image: checked",

View File

@ -4997,7 +4997,7 @@ return {
}, },
{ {
name = "GenImageGradientLinear", name = "GenImageGradientLinear",
description = "Generate image: linear gradient", description = "Generate image: linear gradient, direction in degrees [0..360], 0=Vertical gradient",
returnType = "Image", returnType = "Image",
params = { params = {
{type = "int", name = "width"}, {type = "int", name = "width"},
@ -5019,6 +5019,20 @@ return {
{type = "Color", name = "outer"} {type = "Color", name = "outer"}
} }
}, },
{
name = "GenImageGradientSquare",
description = "Generate image: square gradient",
returnType = "Image",
params = {
{type = "int", name = "width"},
{type = "int", name = "height"},
{type = "int", name = "gradientWidth"},
{type = "int", name = "gradientHeight"},
{type = "float", name = "density"},
{type = "Color", name = "inner"},
{type = "Color", name = "outer"}
}
},
{ {
name = "GenImageChecked", name = "GenImageChecked",
description = "Generate image: checked", description = "Generate image: checked",

File diff suppressed because it is too large Load Diff

View File

@ -656,7 +656,7 @@
<Param type="unsigned int" name="frames" desc="" /> <Param type="unsigned int" name="frames" desc="" />
</Callback> </Callback>
</Callbacks> </Callbacks>
<Functions count="516"> <Functions count="517">
<Function name="InitWindow" retType="void" paramCount="3" desc="Initialize window and OpenGL context"> <Function name="InitWindow" retType="void" paramCount="3" desc="Initialize window and OpenGL context">
<Param type="int" name="width" desc="" /> <Param type="int" name="width" desc="" />
<Param type="int" name="height" desc="" /> <Param type="int" name="height" desc="" />
@ -1549,7 +1549,7 @@
<Param type="int" name="height" desc="" /> <Param type="int" name="height" desc="" />
<Param type="Color" name="color" desc="" /> <Param type="Color" name="color" desc="" />
</Function> </Function>
<Function name="GenImageGradientLinear" retType="Image" paramCount="5" desc="Generate image: linear gradient"> <Function name="GenImageGradientLinear" retType="Image" paramCount="5" desc="Generate image: linear gradient, direction in degrees [0..360], 0=Vertical gradient">
<Param type="int" name="width" desc="" /> <Param type="int" name="width" desc="" />
<Param type="int" name="height" desc="" /> <Param type="int" name="height" desc="" />
<Param type="int" name="direction" desc="" /> <Param type="int" name="direction" desc="" />
@ -1566,8 +1566,8 @@
<Function name="GenImageGradientSquare" retType="Image" paramCount="7" desc="Generate image: square gradient"> <Function name="GenImageGradientSquare" retType="Image" paramCount="7" desc="Generate image: square gradient">
<Param type="int" name="width" desc="" /> <Param type="int" name="width" desc="" />
<Param type="int" name="height" desc="" /> <Param type="int" name="height" desc="" />
<Param type="int" name="int gradiantWidth" desc="" /> <Param type="int" name="gradientWidth" desc="" />
<Param type="int" name="int gradientHeight" desc="" /> <Param type="int" name="gradientHeight" desc="" />
<Param type="float" name="density" desc="" /> <Param type="float" name="density" desc="" />
<Param type="Color" name="inner" desc="" /> <Param type="Color" name="inner" desc="" />
<Param type="Color" name="outer" desc="" /> <Param type="Color" name="outer" desc="" />

View File

@ -765,7 +765,7 @@ Image GenImageGradientRadial(int width, int height, float density, Color inner,
} }
// Generate image: square gradient // Generate image: square gradient
Image GenImageGradientSquare(int width, int weight, int gradientWidth, int gradientHeight, float density, Color inner, Color outer) Image GenImageGradientSquare(int width, int height, int gradientWidth, int gradientHeight, float density, Color inner, Color outer)
{ {
Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color)); Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color));
@ -788,10 +788,14 @@ Image GenImageGradientSquare(int width, int weight, int gradientWidth, int gradi
float normalizedDistY = distY / gradientCenterY; float normalizedDistY = distY / gradientCenterY;
// Calculate the total normalized Manhattan distance // Calculate the total normalized Manhattan distance
float manhattanDist = fmax(normalizedDistX, normalizedDistY) * density; float manhattanDist = fmax(normalizedDistX, normalizedDistY);
// Subtract the density from the manhattanDist, then divide by (1 - density)
// This makes the gradient start from the center when density is 0, and from the edge when density is 1
float factor = (manhattanDist - density) / (1.0f - density);
// Clamp the factor between 0 and 1 // Clamp the factor between 0 and 1
float factor = fminf(fmaxf(manhattanDist, 0.f), 1.f); factor = fminf(fmaxf(factor, 0.f), 1.f);
// Blend the colors based on the calculated factor // Blend the colors based on the calculated factor
pixels[y*width + x].r = (int)((float)outer.r*factor + (float)inner.r*(1.0f - factor)); pixels[y*width + x].r = (int)((float)outer.r*factor + (float)inner.r*(1.0f - factor));
@ -812,7 +816,6 @@ Image GenImageGradientSquare(int width, int weight, int gradientWidth, int gradi
return image; return image;
} }
// Generate image: checked // Generate image: checked
Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2) Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2)
{ {