Skip to content

Commit

Permalink
Complete demoscene-style palsma effect
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Oct 3, 2023
1 parent 5643500 commit 39dd401
Showing 1 changed file with 18 additions and 64 deletions.
82 changes: 18 additions & 64 deletions ncc/examples/fire.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ u32 palette[256];
// Greyscale fire values
int fire[FRAME_HEIGHT][FRAME_WIDTH];





//
// Converts a HUE to r, g or b.
// returns float in the set [0, 1].
Expand All @@ -35,86 +31,40 @@ float hue2rgb(float p, float q, float t)
t = t + 1;
if (t > 1)
t = t - 1;
if (t < 1.0f/6)
if (t < 1.0f / 6)
return p + (q - p) * 6 * t;
if (t < 1.0f/2)
if (t < 1.0f / 2)
return q;
if (t < 2.0f/3)
return p + (q - p) * (2.0f/3 - t) * 6;
if (t < 2.0f / 3)
return p + (q - p) * (2.0f / 3 - t) * 6;

return p;
}

//
// Converts an HSL color value to RGB. Conversion formula
// adapted from http://en.wikipedia.org/wiki/HSL_color_space.
// Assumes h, s, and l are contained in [0, 1] and
// returns RGB in [0, 255].
//
/*
u32 hsl_to_rgb(float h, float s, float l)
{
if (s == 0)
{
// Achromatic
result.r = result.g = result.b = l;
int c = (int)(255 * l);
return rgb32(c, c, c);
}
else
{
float q = l < 0.5 ? l * (1 + s) : l + s - l * s;
float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
float p = 2 * l - q;
result.r = hue2rgb(p, q, h + 1./3) * 255;
result.g = hue2rgb(p, q, h) * 255;
result.b = hue2rgb(p, q, h - 1./3) * 255;
}
return result;
}
*/





// TODO
// Convert a color from HSL format to RGB format
u32 hsl_to_rgb(float h, float s, float v)
{
if (s < 0.01f)
{
// simple gray conversion
int c = (int)(v * 255.0f);
return rgb32(c, c, c);
}

// convert hue from [0, 360( to range [0,6)
h = h / 60.0f;
if (h >= 6.0f)
h = h - 6.0f;

// break "h" down into integer and fractional parts.
int i = (int)h;
float f = h - (float)i;

// Compute the permuted RGB values
int vi = (int)(f * 255.0f);
int p = (int)((v * (1.0f - s)) * 255.0f);
int q = (int)((v * (1.0f - (s * f))) * 255.0f);
int t = (int)((v * (1.0f - (s * (1.0f - f)))) * 255.0f);

// map v, p, q, and t into red, green, and blue values
if (i == 0)
return rgb32(vi, t, p);
if (i == 1)
return rgb32(q, vi, p);
if (i == 2)
return rgb32(p, vi, t);
if (i == 3)
return rgb32(p, q, vi);
if (i == 4)
return rgb32(t, p, vi);

return rgb32(vi, p, q);
return rgb32(
(int)(hue2rgb(p, q, h + 1.0f/3) * 255),
(int)(hue2rgb(p, q, h) * 255),
(int)(hue2rgb(p, q, h - 1.0f/3) * 255)
);
}
}

void anim_callback()
Expand Down Expand Up @@ -177,7 +127,11 @@ void main()
{
// Vary the hue through the palette
// Hue should be between orange and red
palette[i] = hsl_to_rgb(360.0f / 256.0f * (float)i, 1.0f, 1.0f);
float x = (float)i / 255;
float l = x + 0.3f;
l = l * l;
if (l > 1.0f) l = 1.0f;
palette[i] = hsl_to_rgb(x * 0.33f, 1.0f, l);
}

window_create(FRAME_WIDTH, FRAME_HEIGHT, "Demoscene Fire Effect", 0);
Expand Down

0 comments on commit 39dd401

Please sign in to comment.