Add GenImageGradientSquare (#3077)

* Add GenImageGradientSquare to allow square gradients

* Fix GenImageGradientSquare and add to textures_image_generation example

* Remove params from GenImageGradientSquare
This commit is contained in:
Dane Madsen 2023-05-22 23:20:28 +10:00 committed by GitHub
parent 84ae26cdc0
commit a4a6d4da8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 407 additions and 282 deletions

View File

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

View File

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

View File

@ -4997,7 +4997,7 @@ return {
},
{
name = "GenImageGradientLinear",
description = "Generate image: linear gradient",
description = "Generate image: linear gradient, direction in degrees [0..360], 0=Vertical gradient",
returnType = "Image",
params = {
{type = "int", name = "width"},
@ -5019,6 +5019,18 @@ return {
{type = "Color", name = "outer"}
}
},
{
name = "GenImageGradientSquare",
description = "Generate image: square gradient",
returnType = "Image",
params = {
{type = "int", name = "width"},
{type = "int", name = "height"},
{type = "float", name = "density"},
{type = "Color", name = "inner"},
{type = "Color", name = "outer"}
}
},
{
name = "GenImageChecked",
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="" />
</Callback>
</Callbacks>
<Functions count="516">
<Functions count="517">
<Function name="InitWindow" retType="void" paramCount="3" desc="Initialize window and OpenGL context">
<Param type="int" name="width" desc="" />
<Param type="int" name="height" desc="" />
@ -1549,7 +1549,7 @@
<Param type="int" name="height" desc="" />
<Param type="Color" name="color" desc="" />
</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="height" desc="" />
<Param type="int" name="direction" desc="" />
@ -1563,6 +1563,13 @@
<Param type="Color" name="inner" desc="" />
<Param type="Color" name="outer" desc="" />
</Function>
<Function name="GenImageGradientSquare" retType="Image" paramCount="5" desc="Generate image: square gradient">
<Param type="int" name="width" desc="" />
<Param type="int" name="height" desc="" />
<Param type="float" name="density" desc="" />
<Param type="Color" name="inner" desc="" />
<Param type="Color" name="outer" desc="" />
</Function>
<Function name="GenImageChecked" retType="Image" paramCount="6" desc="Generate image: checked">
<Param type="int" name="width" desc="" />
<Param type="int" name="height" desc="" />

View File

@ -213,6 +213,7 @@ ImageColorReplace|void|(Image *image, Color color, Color replace);|
GenImageColor|Image|(int width, int height, Color color);|
GenImageGradientLinear|Image|(int width, int height, int direction, Color start, Color end);|
GenImageGradientRadial|Image|(int width, int height, float density, Color inner, Color outer);|
GenImageGradientSquare|Image|(int width, int height, float density, Color inner, Color outer);|
GenImageChecked|Image|(int width, int height, int checksX, int checksY, Color col1, Color col2);|
GenImageWhiteNoise|Image|(int width, int height, float factor);|
GenImagePerlinNoise|Image|(int width, int height, int offsetX, int offsetY, float scale);|

View File

@ -1396,6 +1396,15 @@
<Param name="Color outer" />
</Overload>
</KeyWord>
<KeyWord name="GenImageGradientSquare" func="yes">
<Overload retVal="Image" descr="Generate image: square gradient">
<Param name="int width" />
<Param name="int height" />
<Param name="float density" />
<Param name="Color inner" />
<Param name="Color outer" />
</Overload>
</KeyWord>
<KeyWord name="GenImageChecked" func="yes">
<Overload retVal="Image" descr="Generate image: checked">
<Param name="int width" />

View File

@ -319,6 +319,7 @@ RLAPI bool ExportImageAsCode(Image image, const char *fileName);
RLAPI Image GenImageColor(int width, int height, Color color); // Generate image: plain color
RLAPI Image GenImageGradientLinear(int width, int height, int direction, Color start, Color end); // Generate image: linear gradient
RLAPI Image GenImageGradientRadial(int width, int height, float density, Color inner, Color outer); // Generate image: radial gradient
RLAPI Image GenImageGradientSquare(int width, int height, float density, Color inner, Color outer); // Generate image: square gradient
RLAPI Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2); // Generate image: checked
RLAPI Image GenImageWhiteNoise(int width, int height, float factor); // Generate image: white noise
RLAPI Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float scale); // Generate image: perlin noise

View File

@ -1241,6 +1241,7 @@ RLAPI bool ExportImageAsCode(Image image, const char *fileName);
RLAPI Image GenImageColor(int width, int height, Color color); // Generate image: plain color
RLAPI Image GenImageGradientLinear(int width, int height, int direction, Color start, Color end); // Generate image: linear gradient, direction in degrees [0..360], 0=Vertical gradient
RLAPI Image GenImageGradientRadial(int width, int height, float density, Color inner, Color outer); // Generate image: radial gradient
RLAPI Image GenImageGradientSquare(int width, int height, float density, Color inner, Color outer); // Generate image: square gradient
RLAPI Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2); // Generate image: checked
RLAPI Image GenImageWhiteNoise(int width, int height, float factor); // Generate image: white noise
RLAPI Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float scale); // Generate image: perlin noise

View File

@ -764,6 +764,55 @@ Image GenImageGradientRadial(int width, int height, float density, Color inner,
return image;
}
// Generate image: square gradient
Image GenImageGradientSquare(int width, int height, float density, Color inner, Color outer)
{
Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color));
float centerX = (float)width/2.0f;
float centerY = (float)height/2.0f;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// Calculate the Manhattan distance from the center
float distX = fabsf(x - centerX);
float distY = fabsf(y - centerY);
// Normalize the distances by the dimensions of the gradient rectangle
float normalizedDistX = distX / centerX;
float normalizedDistY = distY / centerY;
// Calculate the total normalized Manhattan distance
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
factor = fminf(fmaxf(factor, 0.f), 1.f);
// 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].g = (int)((float)outer.g*factor + (float)inner.g*(1.0f - factor));
pixels[y*width + x].b = (int)((float)outer.b*factor + (float)inner.b*(1.0f - factor));
pixels[y*width + x].a = (int)((float)outer.a*factor + (float)inner.a*(1.0f - factor));
}
}
Image image = {
.data = pixels,
.width = width,
.height = height,
.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
.mipmaps = 1
};
return image;
}
// Generate image: checked
Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2)
{