Simple2D-24 Sprite 渲染树
如果要开发游戏,单单使用 Painter 绘制图片会变得十分复杂。如果使用 Sprite 对象进行显示,可以简单地实现图片的位移、旋转和缩放,结合 Action 对象可以实现复杂的动画效果。最重要的是,Sprite 对象间可以存在父子关系,子 Sprite 的仿变换回受到父 Sprite 的影响。

实现
RectTransform 是节点对象,RectTransform 间可以通过 SetParent( ) 函数来组成结点树,使用 Visit( ) 函数来递归遍历子节点,再通过 Draw( ) 函数来执行渲染的操作,从而实现整颗渲染树的渲染。
void RectTransform::Visit(GraphicsContext* gc)
{
if ( !bVisible ) return; this->UpdateTransform();
this->Draw(gc); for ( auto& ele : vChildList ) {
ele->Visit(gc);
}
} void RectTransform::Draw(GraphicsContext* gc)
{
// Nothing to do
}
其中,RectTransform 有一个十分重要的属性:
Matrix4 mMatrix;
这是一个 4 x 4 的变换矩阵,用于实现精灵的位移、旋转和缩放变换。为什么不使用 3 个不同的属性来实现精灵的位移、旋转和缩放呢?因为精灵存在父子关系,如动图所示,小的那个精灵对象的位移、旋转和缩放是建立在大精灵的变换之上。使用矩阵就可以简单地将父精灵变换传递给子精灵:
void RectTransform::UpdateTransform()
{
mMatrix = Matrix4::makeTransform(vPosition, Vec3(vScale.x, vScale.y, 1.0), fDegrees);
if ( pParent ) {
mMatrix = pParent->mMatrix * mMatrix;
}
}
在执行绘制操作前,会进行更新变换的操作,这样确保变换在渲染树中层层传递。
Sprite 的实现
Sprite 继承于 RectTransform,Sprite 只需重写 Draw( ) 函数即可。但由于还存在按钮精灵 ButtonSprite,所以将他们相同的渲染操作放到父类 RenderSprite 中,RenderSprite 只提供渲染操作:
void RenderSprite::RenderTexture(Texture2D* texture, Vec2& size, Matrix4& mat, Color& color, Anchor& anchor, GraphicsContext* gc)
{
static Vec2 blPos;
float w0 = , h0 = , w1 = size.w, h1 = size.h;
float w2 = w1 * 0.5f, h2 = h1 * 0.5f; switch ( anchor ) {
case Anchor::Anchor_TopLeft: blPos.set( w0, -h1); break;
case Anchor::Anchot_TopCenter: blPos.set(-w2, -h1); break;
case Anchor::Anchor_TopRight: blPos.set(-w1, -h1); break;
case Anchor::Anchor_CenterLeft: blPos.set( w0, -h2); break;
case Anchor::Anchor_Center: blPos.set(-w2, -h2); break;
case Anchor::Anchor_CenterRight: blPos.set(-w1, -h2); break;
case Anchor::Anchor_BottomLeft: blPos.set( w0, h0); break;
case Anchor::Anchor_BottomCenter: blPos.set(-w2, h0); break;
case Anchor::Anchor_BottomRight: blPos.set(-w1, h0); break;
} if ( pProgramEffect ) {
/* 设置顶点数据 */
vCustomTexrureVertices[].position = mat * Vec3(blPos.x + w0, blPos.y + h0, );
vCustomTexrureVertices[].position = mat * Vec3(blPos.x + w0, blPos.y + h1, );
vCustomTexrureVertices[].position = mat * Vec3(blPos.x + w1, blPos.y + h1, );
vCustomTexrureVertices[].position = mat * Vec3(blPos.x + w1, blPos.y + h0, ); vCustomTexrureVertices[].texcoord = texture->uv[];
vCustomTexrureVertices[].texcoord = texture->uv[];
vCustomTexrureVertices[].texcoord = texture->uv[];
vCustomTexrureVertices[].texcoord = texture->uv[]; vCustomTexrureVertices[].color = color;
vCustomTexrureVertices[].color = color;
vCustomTexrureVertices[].color = color;
vCustomTexrureVertices[].color = color; pProgramEffect->GetUniform("Texture0")->SetTexture(texture->textureUnique->id); gc->SetProgramEffect(pProgramEffect);
gc->SetBlendMode(SBM_AlphaTexture);
gc->GetRenderer()->AppendRenderData(vCustomTexrureVertices, , vTextureIndices, , PrimType::PT_Triangles);
}
else {
/* 设置顶点数据 */
vTextureVertices[].position = mat * Vec3(blPos.x + w0, blPos.y + h0, );
vTextureVertices[].position = mat * Vec3(blPos.x + w0, blPos.y + h1, );
vTextureVertices[].position = mat * Vec3(blPos.x + w1, blPos.y + h1, );
vTextureVertices[].position = mat * Vec3(blPos.x + w1, blPos.y + h0, ); vTextureVertices[].texcoord = texture->uv[];
vTextureVertices[].texcoord = texture->uv[];
vTextureVertices[].texcoord = texture->uv[];
vTextureVertices[].texcoord = texture->uv[]; vTextureVertices[].color = color;
vTextureVertices[].color = color;
vTextureVertices[].color = color;
vTextureVertices[].color = color; gc->SetProgram(SPT_Sprite);
gc->SetBlendMode(SBM_AlphaTexture);
gc->GetRenderer()->AppendRenderData(vTextureVertices, , vTextureIndices, , texture->textureUnique->id, PrimType::PT_Triangles);
}
}
计算出图片 4 个顶点,设置着色程序和混合,再将渲染的顶点数据传递给 Renderer 即可。Sprite 的 Draw( ) 函数只需简单的调用 RenderTexture( ) 函数即可:
void Sprite::Draw(GraphicsContext* gc)
{
if ( pTexture == nullptr ) return;
this->RenderTexture(pTexture, vSize, mMatrix, color, anchor, gc);
}
ButtonSprite
除了普通的精灵,还需要一些控件精灵。如 ButtonSprite,它能实现按钮的功能,并添加到精灵渲染树中,实现游戏中的交互操作。ButtonSprite 继承于鼠标事件监听器 MouseEventListener,从而接受鼠标事件。但 ButtonSprite 接受到一个点击事件,它先判断点击坐标是否落在 ButtonSprite 的区域内。
思路是将光标坐标变换到坐标原点,在判断坐标是否在 ButtonSprite 大小的区域内:
bool RectTransform::AccectEvent(int x, int y)
{
Vec3 pos(x, y, ); pos = mMatrix.inverse() * pos;
float hw = vSize.w * 0.5f;
float hh = vSize.h * 0.5f; return (pos.x >= -hw && pos.x <= hw && pos.y >= -hh && pos.y <= hh);
}
将坐标乘以变换矩阵的逆矩阵得到新的坐标,执行左边变换。如果按钮被点击了,通过信号对象传递点击信息:
Signal<void()> SigButtonClicked;
void ButtonSprite::MouseReleased(const MouseEvent& event)
{
if ( bMousePressed ) {
bMousePressed = false; /* 信号:按钮点击 */
SigButtonClicked();
}
}
Signal-Slot 在前面的文章讲过,通过使用 Signal-Slot 可以实现 ButtonSprite 和其他对象的解耦。
源码下载:Simple2D-20.rar
Simple2D-24 Sprite 渲染树的更多相关文章
- 【浏览器渲染原理】渲染树构建之渲染树和DOM树的关系(转载 学习中。。。)
在DOM树构建的同时,浏览器会构建渲染树(render tree).渲染树的节点(渲染器),在Gecko中称为frame,而在webkit中称为renderer.渲染器是在文档解析和创建DOM节点后创 ...
- 探索未知种族之osg类生物---状态树与渲染树以及节点树之间的关系
节点树 首先我们来看一个场景构建的实例,并通过它来了解一下“状态节点”StateGraph 和“渲染叶”RenderLeaf 所构成的状态树,“渲染台”RenderStage 和“渲染元”Render ...
- RenderTree渲染树
RenderTree渲染树对类中的静态成员有很重要的关系,这个和多态是有很重要的关系,举个简单的例子,在游戏中,马里奥需要渲染,蘑菇也需要渲染,怪兽也需要渲染,其是串在一个树上的,但是不同的类型怎么将 ...
- [js] 渲染树构建、布局及绘制
渲染树构建.布局及绘制
- 渲染树render tree
CSSOM树和DOM树连接在一起形成一个render tree,渲染树用来计算可见元素的布局并且作为将像素渲染到屏幕上的过程的输入. DOM树和CSSOM树连接在一起形成render tree . r ...
- 学习opengl第一步
有两个地址一个是学习opengl基础知识的网站, 一个是博客园大牛分享的特别好的文章. 记录一下希望向坚持做俯卧撑一样坚持下去. 学习网站:http://learnopengl-cn.readthed ...
- css加载是否会阻塞dom树渲染
这里说的是头部引入css的情况 首先,我们都知道:css是由单独的下载线程异步下载的. 咱们先分析下css加载会影响什么,刚才的问题太笼统了,咱们需要细化一下. 会影响什么呢? 一个就是DOM树解析, ...
- 页面渲染机制(一、DOM和CSSOM树的构建)
1.HTML的加载 HTML是一个网页的基础,下载完成后解析 2.其他静态资源加载 解析HTML时,发现其中有其他外部资源链接比如CSS.JS.图片等,会立即启用别的线程下载. 但当外部资源是JS时, ...
- CoreAnimation1-图层树、寄宿图以及图层几何学
(一)图层的树状结构 Core Animation其实是一个令人误解的命名.你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只 ...
随机推荐
- Collection与Collections,Array与Arrays的区别
Collection 和 Collections的区别 Collection 在Java.util下的一个接口,它是各种集合结构的父接口.继承与他的接口主要有Set 和List. Collection ...
- Centos7安装killall,fuser, killall,pstree和pstree.x11
centos7精简安装后,使用中发现没有killall命令. 经查找,可以通过以下命令解决: yum -y install psmisc 简单介绍一下 psmisc : Psmisc软件包包含三个帮助 ...
- FineUI 3升级4.1.1时,SingleClickExpand属性改什么了? (树控件单击展开)
private Tree InitTreeMenu(List<Menu> menus) { Tree treeMenu = new Tree(); treeMenu.ID = " ...
- Java static 使用
1. 静态代码块 class H { static{ Sysout.out.println("static block"); } } 静态代码块先与构造函数执行 静态代码块: 静态 ...
- Angular 4 表单校验1
1.html <form [formGroup]="formModel" (submit)="submit()"> <div> 用户名: ...
- 如何在ASP.NET页面中使用异步任务(PageAsyncTask)
在页面加载期间,可能有些操作是要比较耗用时间的(例如调用外部资源,要长时间等待其返回),正常情况下,这个操作将一直占用线程.而大家知道,ASP.NET在服务端线程池中的线程数是有限的,如果一直占用的话 ...
- linux $* $@ 特定位置参数
举例说:脚本名称叫test.sh 入参三个: 1 2 3运行test.sh 1 2 3后$*为"1 2 3"(一起被引号包住)$@为"1" "2&qu ...
- [转]关闭WIN7“程序兼容性助理”
转载自 http://www.flighty.cn/html/tutorial/20140717_244.html WIN7程序兼容性助理其实是一个非常鸡肋的功能,对于我们基本上可以说没有什么用处,反 ...
- shell 13文件包含
同其他语言一样,shell也可以包含外部脚本.执行引用脚本可以使用source或 . 示例1 test.sh #shell #!/bin/sh echo "test.sh开始执行" ...
- 关于模板该不该用css强制编辑器文本开头空两格
关于模板该不该用css强制编辑器文本开头空两格这个问题,我很早之前就想说了,写惯了qq日志的童鞋都知道,qq空间的编辑器没有任何css控制,行头空两格是由用户自己控制,我写起日志又像流水账,长长的一篇 ...