Review some formatting for consistency
This commit is contained in:
parent
5f0f87ecce
commit
6589c808b5
5 changed files with 87 additions and 61 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -42,7 +42,7 @@ Thumbs.db
|
|||
*.sbr
|
||||
*.sdf
|
||||
obj/
|
||||
# [Rr]elease/
|
||||
[Rr]elease/
|
||||
[Rr]elease.win32/
|
||||
_ReSharper*/
|
||||
[Tt]est[Rr]esult*
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
*
|
||||
* raylib [textures] example - Procedural images generation
|
||||
*
|
||||
* This example has been created using raylib 1.7 (www.raylib.com)
|
||||
* This example has been created using raylib 1.8 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2O17 Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2O17 Wilhem Barbier (@nounoursheureux)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
@ -15,15 +15,16 @@
|
|||
|
||||
int main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - procedural images generation");
|
||||
SetTargetFPS(60);
|
||||
|
||||
Image verticalGradient = GenImageGradientV(screenWidth, screenHeight, RED, BLUE);
|
||||
Image horizontalGradient = GenImageGradientH(screenWidth, screenHeight, RED, BLUE);
|
||||
Image radialGradient = GenImageRadialGradient(screenWidth, screenHeight, 0.f, WHITE, BLACK);
|
||||
Image radialGradient = GenImageGradientRadial(screenWidth, screenHeight, 0.f, WHITE, BLACK);
|
||||
Image checked = GenImageChecked(screenWidth, screenHeight, 32, 32, RED, BLUE);
|
||||
Image whiteNoise = GenImageWhiteNoise(screenWidth, screenHeight, 0.5f);
|
||||
Image perlinNoise = GenImagePerlinNoise(screenWidth, screenHeight, 8.f);
|
||||
|
@ -40,19 +41,36 @@ int main()
|
|||
|
||||
int currentTexture = 0;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//---------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||
{
|
||||
currentTexture = (currentTexture + 1) % TEXTURES_NUM; // cycle between the 5 textures
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
DrawTexture(textures[currentTexture], 0, 0, WHITE);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Unload image data (CPU RAM)
|
||||
UnloadImage(verticalGradient);
|
||||
UnloadImage(horizontalGradient);
|
||||
UnloadImage(radialGradient);
|
||||
|
@ -60,10 +78,12 @@ int main()
|
|||
UnloadImage(whiteNoise);
|
||||
UnloadImage(perlinNoise);
|
||||
UnloadImage(cellular);
|
||||
for (int i = 0; i < TEXTURES_NUM; i++) // unload the textures
|
||||
{
|
||||
UnloadTexture(textures[i]);
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
// Unload textures data (GPU VRAM)
|
||||
for (int i = 0; i < TEXTURES_NUM; i++) UnloadTexture(textures[i]);
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
BIN
examples/textures/textures_image_generation.png
Normal file
BIN
examples/textures/textures_image_generation.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 217 KiB |
|
@ -876,7 +876,7 @@ RLAPI void ImageColorBrightness(Image *image, int brightness);
|
|||
// Image generation functions
|
||||
RLAPI Image GenImageGradientV(int width, int height, Color top, Color bottom); // Generate image: vertical gradient
|
||||
RLAPI Image GenImageGradientH(int width, int height, Color left, Color right); // Generate image: horizontal gradient
|
||||
RLAPI Image GenImageRadialGradient(int width, int height, float density, Color inner, Color outer); // Generate image: radial gradient
|
||||
RLAPI Image GenImageGradientRadial(int width, int height, float density, Color inner, Color outer); // Generate image: radial 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, float scale); // Generate image: perlin noise
|
||||
|
|
|
@ -1493,25 +1493,28 @@ Image GenImageGradientH(int width, int height, Color left, Color right)
|
|||
}
|
||||
|
||||
// Generate image: radial gradient
|
||||
Image GenImageRadialGradient(int width, int height, float density, Color inner, Color outer)
|
||||
Image GenImageGradientRadial(int width, int height, float density, Color inner, Color outer)
|
||||
{
|
||||
Color *pixels = (Color *)malloc(width*height*sizeof(Color));
|
||||
float radius = (width < height) ? (float)width / 2.f : (float)height / 2.f;
|
||||
float radius = (width < height) ? (float)width/2.0f : (float)height/2.0f;
|
||||
|
||||
float centerX = (float)width/2.0f;
|
||||
float centerY = (float)height/2.0f;
|
||||
|
||||
float center_x = (float)width / 2.f;
|
||||
float center_y = (float)height / 2.f;
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
float dist = hypotf((float)x - center_x, (float)y - center_y);
|
||||
float factor = (dist - radius * density) / (radius * (1.f - density));
|
||||
float dist = hypotf((float)x - centerX, (float)y - centerY);
|
||||
float factor = (dist - radius*density)/(radius*(1.0f - density));
|
||||
|
||||
factor = fmax(factor, 0.f);
|
||||
factor = fmin(factor, 1.f); // dist can be bigger than radius so we have to check
|
||||
pixels[y*width + x].r = (int)((float)outer.r * factor + (float)inner.r * (1.f - factor));
|
||||
pixels[y*width + x].g = (int)((float)outer.g * factor + (float)inner.g * (1.f - factor));
|
||||
pixels[y*width + x].b = (int)((float)outer.b * factor + (float)inner.b * (1.f - factor));
|
||||
pixels[y*width + x].a = (int)((float)outer.a * factor + (float)inner.a * (1.f - 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1548,7 +1551,7 @@ Image GenImageWhiteNoise(int width, int height, float factor)
|
|||
|
||||
for (int i = 0; i < width*height; i++)
|
||||
{
|
||||
if (GetRandomValue(0, 99) < (int)(factor * 100.f)) pixels[i] = WHITE;
|
||||
if (GetRandomValue(0, 99) < (int)(factor*100.0f)) pixels[i] = WHITE;
|
||||
else pixels[i] = BLACK;
|
||||
}
|
||||
|
||||
|
@ -1569,9 +1572,11 @@ Image GenImagePerlinNoise(int width, int height, float scale)
|
|||
{
|
||||
float nx = (float)x*scale/(float)width;
|
||||
float ny = (float)y*scale/(float)height;
|
||||
|
||||
// we need to translate the data from [-1; 1] to [0; 1]
|
||||
float p = (stb_perlin_fbm_noise3(nx, ny, 1.f, 2.f, 0.5f, 6, 0, 0, 0) + 1.f) / 2.f;
|
||||
int intensity = (int)(p * 255.f);
|
||||
float p = (stb_perlin_fbm_noise3(nx, ny, 1.0f, 2.0f, 0.5f, 6, 0, 0, 0) + 1.0f) / 2.0f;
|
||||
|
||||
int intensity = (int)(p * 255.0f);
|
||||
pixels[y*width + x] = (Color){intensity, intensity, intensity, 255};
|
||||
}
|
||||
}
|
||||
|
@ -1587,52 +1592,53 @@ Image GenImageCellular(int width, int height, int tileSize)
|
|||
{
|
||||
Color *pixels = (Color *)malloc(width*height*sizeof(Color));
|
||||
|
||||
int seeds_per_row = width / tileSize;
|
||||
int seeds_per_col = height / tileSize;
|
||||
int seeds_count = seeds_per_row * seeds_per_col;
|
||||
int seedsPerRow = width/tileSize;
|
||||
int seedsPerCol = height/tileSize;
|
||||
int seedsCount = seedsPerRow * seedsPerCol;
|
||||
|
||||
Vector2* seeds = (Vector2*)malloc(seeds_count * sizeof(Vector2));
|
||||
Vector2 *seeds = (Vector2 *)malloc(seedsCount*sizeof(Vector2));
|
||||
|
||||
for (int i = 0; i < seeds_count; i++)
|
||||
for (int i = 0; i < seedsCount; i++)
|
||||
{
|
||||
int y = (i / seeds_per_row) * tileSize + GetRandomValue(0, tileSize-1);
|
||||
int x = (i % seeds_per_row) * tileSize + GetRandomValue(0, tileSize-1);
|
||||
int y = (i/seedsPerRow)*tileSize + GetRandomValue(0, tileSize - 1);
|
||||
int x = (i%seedsPerRow)*tileSize + GetRandomValue(0, tileSize - 1);
|
||||
seeds[i] = (Vector2){x, y};
|
||||
}
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
int tile_y = y / tileSize;
|
||||
int tileY = y/tileSize;
|
||||
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
int tile_x = x / tileSize;
|
||||
int tileX = x/tileSize;
|
||||
|
||||
float min_distance = strtod("Inf", NULL);
|
||||
float minDistance = strtod("Inf", NULL);
|
||||
|
||||
// Check all adjacent tiles
|
||||
for (int i = -1; i < 2; i++)
|
||||
{
|
||||
if (tile_x + i < 0 || tile_x + i >= seeds_per_row) continue;
|
||||
if ((tileX + i < 0) || (tileX + i >= seedsPerRow)) continue;
|
||||
|
||||
for (int j = -1; j < 2; j++)
|
||||
{
|
||||
if (tile_y + j < 0 || tile_y + j >= seeds_per_col) continue;
|
||||
if ((tileY + j < 0) || (tileY + j >= seedsPerCol)) continue;
|
||||
|
||||
Vector2 neighbor_seed = seeds[(tile_y+j) * seeds_per_row + tile_x+i];
|
||||
Vector2 neighborSeed = seeds[(tileY + j)*seedsPerRow + tileX + i];
|
||||
|
||||
float dist = hypot(x - (int)neighbor_seed.x, y - (int)neighbor_seed.y);
|
||||
min_distance = fmin(min_distance, dist);
|
||||
float dist = hypot(x - (int)neighborSeed.x, y - (int)neighborSeed.y);
|
||||
minDistance = fmin(minDistance, dist);
|
||||
}
|
||||
}
|
||||
|
||||
// I made this up but it seems to give good results at all tile sizes
|
||||
int intensity = (int)(min_distance * 256.f / tileSize);
|
||||
int intensity = (int)(minDistance*256.0f/tileSize);
|
||||
if (intensity > 255) intensity = 255;
|
||||
|
||||
Color c = {intensity, intensity, intensity, 255};
|
||||
pixels[y*width + x] = c;
|
||||
pixels[y*width + x] = (Color){ intensity, intensity, intensity, 255 };
|
||||
}
|
||||
}
|
||||
|
||||
free(seeds);
|
||||
|
||||
Image image = LoadImageEx(pixels, width, height);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue