OpenGL接口的基本实现2[转]

OpenGL接口的基本实现2[转]

目录

OpenGL接口的基本实现2[转]

绘制矩形

矩形渐变颜色(插值)

绘制矩形

  • 注意使用两个矩形相交去判断坐标是否超出范围
void    drawFilleRect(int startX,int startY,int w,int h)
{
    int left    =   tmax<int>(startX,0);
    int top     =   tmax<int>(startY,0);

    int right   =   tmin<int>(startX + w,_width);
    int bottom  =   tmin<int>(startY + h,_height);

    for (int x = left ; x <  right ; ++ x)
    {
        for (int y = top ; y <  bottom ; ++ y)
        {
            setPixelEx(x,y,_color);
        }
    }
}

inline  void    setPixelEx(unsigned x,unsigned y,Rgba color)
{
    _buffer[y * _width + x]   =   color;
}

矩形渐变颜色(插值)

void    drawRect(const int2* points,const Rgba* colors)
{
    int left    =   tmax<int>(points[0].x,0);
    int top     =   tmax<int>(points[0].y,0);

    int right   =   tmin<int>(points[2].x,_width);
    int bottom  =   tmin<int>(points[2].y,_height);

    float   w   =   right - left;
    float   h   =   bottom - top;

    for (int x = left ; x <  right ; ++ x)
    {
        Rgba    color1  =   colorLerp(colors[0],colors[1],(x - left)/w);
        Rgba    color2  =   colorLerp(colors[3],colors[2],(x - left)/w);

        for (int y = top ; y <  bottom ; ++ y)
        {
            Rgba    color  =   colorLerp(color1,color2,(y - top)/h);
            setPixelEx(x,y,color);
        }
    }
}

inline  void    setPixelEx(unsigned x,unsigned y,Rgba color)
{
    _buffer[y * _width + x]   =   color;
}

inline  Rgba4Byte   colorLerp(const Rgba4Byte& c1, const Rgba4Byte& c2, float s)
{
    Rgba4Byte   color;

    color._r = (unsigned char)(c1._r + s * (c2._r - c1._r));
    color._g = (unsigned char)(c1._g + s * (c2._g - c1._g));
    color._b = (unsigned char)(c1._b + s * (c2._b - c1._b));
    color._a = (unsigned char)(c1._a + s * (c2._a - c1._a));
    return color;
}
  • 调用:
CELL::int2  pt[] =
{
	CELL::int2(10,10),
	CELL::int2(110,10),
	CELL::int2(110,110),
	CELL::int2(10,110),
};

CELL::Rgba  colors[] =
{
	CELL::Rgba(255,0,0),
	CELL::Rgba(0,255,0),
	CELL::Rgba(0,0,255),
	CELL::Rgba(255,255,255),

};

raster.drawRect(pt,colors);

Span(int xStart,int xEnd,int y)
{
    if (xStart < xEnd)
    {
        _xStart =   xStart;
        _xEnd   =   xEnd;
        _y      =   y;
    }
    else
    {
        _xStart =   _xEnd;
        _xEnd   =   _xStart;
        _y      =   y;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值