added Blend.h and supported it.

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-07-11 18:48:55 +08:00
parent ca74184d39
commit 713a9f98b3
3 changed files with 530 additions and 431 deletions

View File

@ -5,6 +5,8 @@
#include<hgl/math/HalfFloat.h>
#include<iterator>
namespace hgl
{
namespace bitmap
{
template<typename T>
static void FillPixels(T *p,const T &color,const int length)
@ -107,5 +109,6 @@ namespace hgl
delete[] temp;
}
};//template<typename T> class Bitmap
}//namespace bitmap
}//namespace hgl
#endif//HGL_2D_BITMAP_INCLUDE

57
inc/hgl/2d/Blend.h Normal file
View File

@ -0,0 +1,57 @@
#pragma once
#include<hgl/type/DataType.h>
namespace hgl
{
namespace bitmap
{
enum class BlendMode
{
NoBlend, ///<不混合
Alpha, ///<Alpha混合
Add, ///<加法混合
Sub, ///<减法混合
};//enum class BlendMode
template<typename T> struct BlendColor
{
BlendMode mode=BlendMode::NoBlend;
public:
virtual const T &BlendNoBlend(const T &src,const T &dst,const float alpha)
{
return src;
}
virtual const T &BlendAlpha(const T &src,const T &dst,const float alpha)
{
return src*alpha+dst*(1-alpha);
}
virtual const T &BlendAdd(const T &src,const T &dst,const float alpha)
{
return src*alpha+dst;
}
virtual const T &BlendSub(const T &src,const T &dst,const float alpha)
{
return src*alpha-dst;
}
virtual const T &Blend(const T &src,const T &dst,const float alpha)
{
switch(mode)
{
case BlendMode::NoBlend: return BlendNoBlend(src,dst,alpha);
case BlendMode::Alpha: return BlendAlpha(src,dst,alpha);
case BlendMode::Add: return BlendAdd(src,dst,alpha);
case BlendMode::Sub: return BlendSub(src,dst,alpha);
}
return src;
}
};//template<typename T> class BlendColor
}//namespace bitmap
}//namespace hgl

View File

@ -1,9 +1,12 @@
#pragma once
#include<hgl/2d/Bitmap.h>
#include<hgl/2d/blend.h>
#include<hgl/math/FastTriangle.h>
namespace hgl
{
namespace bitmap
{
template<typename T> class DrawGeometry
{
@ -16,12 +19,17 @@ namespace hgl
FormatBitmap *bitmap;
T draw_color;
float alpha;
BlendColor<T> *blend;
public:
DrawGeometry(FormatBitmap *fb)
{
bitmap=fb;
draw_color=0;
alpha=1;
}
virtual ~DrawGeometry()=default;
@ -31,6 +39,26 @@ namespace hgl
draw_color=color;
}
void SetBlend(BlendColor<T> *bc)
{
blend=bc;
}
virtual void SetBlend(const BlendMode &mode)
{
blend->mode=mode;
}
void CloseBlend()
{
blend->mode=BlendMode.NoBlend;
}
void SetAlpha(const float &a)
{
alpha=a;
}
bool GetPixel(int x,int y,T &color)
{
if(!data)return(false);
@ -52,7 +80,7 @@ namespace hgl
if(!p)return(false);
*p=draw_color;
*p=blend->Blend(draw_color,*p,alpha);
return(true);
}
@ -71,7 +99,10 @@ namespace hgl
if(length<=0)return(false);
FillPixels<T>(bitmap->GetData(x,y),draw_color,length);
T *p=bitmap->GetData(x,y);
for(int i=0;i<length;i++)
*p++=blend->Blend(draw_color,*p,alpha);
return(true);
}
@ -93,8 +124,15 @@ namespace hgl
if(w<=0||h<=0)return(false);
T *p=bitmap->GetData(x,y);
for(int y=t;y<t+h;y++)
FillPixels<T>(bitmap->GetData(l,y),draw_color,w);
{
for(int i=0;i<length;i++)
*p++=blend->Blend(draw_color,*p,alpha);
p+=width-w;
}
return(true);
}
@ -118,7 +156,7 @@ namespace hgl
for(int i=0;i<length;i++)
{
*p=draw_color;
*p=blend->Blend(draw_color,*p,alpha);
p+=line_bytes;
}
@ -406,4 +444,5 @@ namespace hgl
free(xy);
}
};//template<typename T,uint CHANNELS> class DrawGeometry
}//namespace bitmap
}//namespace hgl