genode/repos/gems/include/polygon_gfx/interpolate_rgba.h
Norman Feske 259b127f96 Polygon drawing and rudimentary 3D routines
This patch adds two new painters located at gems/include/polygon_gfx.
Both painters draw convex polygons with an arbirary number of points.
The shaded-polygon painter interpolates the color and alpha values
whereas the textured-polygon painter applies a texture to the polygon.
The painters are accompanied by simplistic 3D routines located at
gems/include/nano3d/ and a corresponding example (gems/run/nano3d.run).
2015-07-07 19:48:04 +02:00

71 lines
2.0 KiB
C++

/*
* \brief Interpolation functions for polygon painting
* \date 2015-06-19
* \author Norman Feske
*/
/*
* Copyright (C) 2015 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#ifndef _INCLUDE__POLYGON_GFX__INTERPOLATE_RGBA_H_
#define _INCLUDE__POLYGON_GFX__INTERPOLATE_RGBA_H_
/* Genode includes */
#include <util/color.h>
namespace Polygon {
using Genode::Color;
template <typename PT>
static inline void interpolate_rgba(Color, Color, PT *, unsigned char *,
unsigned, int, int);
}
/**
* Interpolate color values
*/
template <typename PT>
static inline void Polygon::interpolate_rgba(Polygon::Color start,
Polygon::Color end,
PT *dst,
unsigned char *dst_alpha,
unsigned num_values,
int x, int y)
{
/* sanity check */
if (num_values == 0) return;
/* use 16.16 fixpoint values for the calculation */
int const r_ascent = ((end.r - start.r)<<16) / (int)num_values,
g_ascent = ((end.g - start.g)<<16) / (int)num_values,
b_ascent = ((end.b - start.b)<<16) / (int)num_values,
a_ascent = ((end.a - start.a)<<16) / (int)num_values;
/* set start values for color components */
int r = start.r<<16,
g = start.g<<16,
b = start.b<<16,
a = start.a<<16;
for ( ; num_values--; dst++, dst_alpha++) {
/* combine current color value with existing pixel via alpha blending */
*dst = PT::mix(*dst, PT(r>>16, g>>16, b>>16), a>>16);
*dst_alpha += ((255 - *dst_alpha)*a) >> (16 + 8);
/* increment color-component values by ascent */
r += r_ascent;
g += g_ascent;
b += b_ascent;
a += a_ascent;
}
}
#endif /* _INCLUDE__POLYGON_GFX__INTERPOLATE_RGBA_H_ */