CSharpGL(42)借助帧缓存实现渲染到纹理(RenderToTexture)
CSharpGL(42)借助帧缓存实现渲染到纹理(RenderToTexture)
渲染到纹理(Render To Texture)是实现很多OpenGL高级效果的一个基础。本文记录了如何用CSharpGL实现RTT。
下载
CSharpGL已在GitHub开源,欢迎对OpenGL有兴趣的同学加入(https://github.com/bitzhuwei/CSharpGL)
开始



如图所示,我们把teapot贴到一个矩形上了。这是借助RTT实现的。
RTT步骤如下:
- 创建Framebuffer,这Framebuffer里包含一个Texture,这个Texture就是RTT里的Texture。
- 绑定Framebuffer,然后正常渲染,然后解绑Framebuffer。(RTT结束)
- 此时Framebuffer里包含的Texture已经有了想要的内容,我们可以像使用其他普通Texture一样使用此Texture。
创建Framebuffer
Framebuffer里包含若干renderbuffer和一个depthbuffer,就像VertexArrayObject里包含若干VertexBuffer和一个IndexBuffer。
创建Framebuffer过程概括如下:
private Framebuffer CreateFramebuffer(int width, int height)
{
var texture = new Texture(TextureTarget.Texture2D,
new NullImageFiller(width, height, GL.GL_RGBA, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE),
new SamplerParameters(
TextureWrapping.Repeat,
TextureWrapping.Repeat,
TextureWrapping.Repeat,
TextureFilter.Linear,
TextureFilter.Linear));
texture.Initialize();
this.BindingTexture = texture;
Renderbuffer colorBuffer = Renderbuffer.CreateColorbuffer(width, height, GL.GL_RGBA);
Renderbuffer depthBuffer = Renderbuffer.CreateDepthbuffer(width, height, DepthComponentType.DepthComponent24);
var framebuffer = new Framebuffer();
framebuffer.Bind();
framebuffer.Attach(colorBuffer);//
framebuffer.Attach(texture);//
framebuffer.Attach(depthBuffer);// special
framebuffer.SetDrawBuffers(GL.GL_COLOR_ATTACHMENT0 + );// as in 1 in framebuffer.Attach(texture);//
framebuffer.CheckCompleteness();
framebuffer.Unbind();
return framebuffer;
}
使用Framebuffer
创建Framebuffer后,先绑定Framebuffer,再像平时一样渲染,再解绑Framebuffer。
public void RenderBeforeChildren(RenderEventArgs arg)
{
if (this.Width <= || this.Height <= ) { return; } var viewport = new int[];
GL.Instance.GetIntegerv((uint)GetTarget.Viewport, viewport); this.framebuffer = this.helper.GetFramebuffer(this.Width, this.Height);
framebuffer.Bind();
GL.Instance.Viewport(, , this.Width, this.Height);
{
int[] value = new int[];
GL.Instance.GetIntegerv((uint)GetTarget.ColorClearValue, value);
{
vec3 color = this.BackgroundColor.ToVec3();
GL.Instance.ClearColor(color.x, color.y, color.z, 0.0f); // 0.0f for alpha channel, in case that transparent background is needed.
GL.Instance.Clear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT);
}
{
var args = new RenderEventArgs(this.Camera);
foreach (var item in this.Children)
{
RenderAction.Render(item, args);
}
}
{
GL.Instance.ClearColor(value[], value[], value[], value[]);// recover clear color.
}
}
GL.Instance.Viewport(viewport[], viewport[], viewport[], viewport[]);// recover viewport.
this.framebuffer.Unbind();
}
使用RTT
此时Framebuffer里包含的Texture已经有了想要的内容,我们可以像使用其他普通Texture一样使用此Texture。例如本文开始处将Texture贴到一个矩形上。

当然也可以结合Billboard的需求,把Texture贴到Billboard上。
总结
最近几个月我在尝试修整CSharpGL,感觉好多了。
CSharpGL(42)借助帧缓存实现渲染到纹理(RenderToTexture)的更多相关文章
- OpenGL帧缓存对象(FBO:Frame Buffer Object)(转载)
原文地址http://www.songho.ca/opengl/gl_fbo.html 但有改动. OpenGL Frame BufferObject(FBO) Overview: 在OpenGL渲染 ...
- OpenGL帧缓存对象(FBO:Frame Buffer Object)
http://blog.csdn.net/dreamcs/article/details/7691690 转http://blog.csdn.net/xiajun07061225/article/de ...
- OpenGL帧缓存对象(FBO:Frame Buffer Object) 【转】
http://blog.csdn.net/dreamcs/article/details/7691690 原文地址http://www.songho.ca/opengl/gl_fbo.html 但有改 ...
- 【OpenGL】OpenGL帧缓存对象(FBO:Frame Buffer Object) 【转】
http://blog.csdn.net/xiajun07061225/article/details/7283929/ OpenGL Frame BufferObject(FBO) Overview ...
- WebGL简易教程(十三):帧缓存对象(离屏渲染)
目录 1. 概述 2. 示例 2.1. 着色器部分 2.2. 初始化/准备工作 2.2.1. 着色器切换 2.2.2. 帧缓冲区 2.3. 绘制函数 2.3.1. 初始化顶点数组 2.3.2. 传递非 ...
- OpenGL读取帧缓存数据
https://blog.csdn.net/niu2212035673/article/details/80251949 简述有些时候我们可能需要获取渲染后的图像数据,比较常用的函数是glReadPi ...
- [译]Vulkan教程(17)帧缓存
[译]Vulkan教程(17)帧缓存 Framebuffers 帧缓存 We've talked a lot about framebuffers in the past few chapters a ...
- 【Cocos2d-x 3.x】 精灵帧缓存和纹理缓存
转自泰然网(Cocos2d-x 3.x官方文档):精灵帧缓存:http://www.tairan.com/archives/6378/ 纹理缓存: http://www.tairan.com/ar ...
- SpriteFrameCache 精灵帧缓存
//获取精灵帧缓存的单例对象 auto spriteFrameCache = SpriteFrameCache::getInstance(); //从plist文件添加多个精灵帧 spriteFra ...
随机推荐
- N维法向量与N维超平面的关系的简单证明(日志二)
虽然按照上面的方式证明出来,但感觉之中,似乎依旧是不严密的, 如下: 如果直线是2x+2y+1=0 那么(-1,1)也是其平行向量 ,.那么(1,1)依旧是法向量 此时,直线经过(0,-1/2)这个点 ...
- NSA Fuzzbunch分析与利用案例
Shadow Brokers泄露出一份震惊世界的机密文档,其中包含了多个 Windows 远程漏洞利用工具.本文主要介绍了其中一款工具Fuzzbunch的分析与利用案例 1 整体目录介绍 解压EQGR ...
- 全景智慧城市——NOW!!!VRSHOPPING颠覆你的购物认知!
互联网+时代,人们对现有的网络资源已经不再感冒,一般的图片.文字信息已经无法满足人们对互联网的需求,虚拟现实.身临其境的体验是不可小觑的发展趋势,尤其是VR逛街.购物,更会深入人心,再次改变人们的生活 ...
- ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据的传递
前言 最近公司项目进行架构调整,由原来的三层架构改进升级到微服务架构(准确的说是服务化,还没完全做到微的程度,颗粒度没那么细),遵循RESTFull规范,使前后端完全分离,实现大前端思想.由于是初次尝 ...
- MyBatis3入门
这里对mybatis的入门介绍以官方最新MyBatis3.4.1为准,具体文档及jar包请访问:https://github.com/mybatis/mybatis-3/releases. 以前经常都 ...
- MyBatis之级联——一对一关系
在学数据库概论的时候会听到这么几个词:数据库的关系分为一对一.一对多.多对多.对于在学校里学的知识已经忘得差不多了,在这里简单的提一下数据库的关系.此篇是介绍MyBatis是如何实现数据库中一对一关系 ...
- maven 热部署至tomcat
1.配置tomcat的界面访问账号和权限./tomcat/conf目录下tomcat-users.xml添加 这里是根据自己的需求添加的一个角色权限 <role rolename="a ...
- 在Linux下的找不同-打补丁
Q:为什么要找不同,为什么要打补丁? A: 在Linux应用中,作为DBA,我们知道MySQL跑在Linux系统之上,数据库最重要的追求就是性能,"稳"是重中之重,所以不能动不动就 ...
- C语言之运算符和条件结构
表达式:是有操作数和运算符组成的. 操作数:常量.变量.子表达式 X=(x+2)*(y-2); 运算符: 赋值运算符:= .其作用是做赋值运算,将等号后边的值赋值给等号前边的. 复合赋值运算符: += ...
- Unity遮挡透明渐变
遮挡透明若没有渐变实现方法: 1.透明中物体存在list中 2.每过一段时间(可以每帧,但是流畅性会降低)摄像机发送一条射线向玩家,out hitInfo 3.list与hitInfo比对,将在lis ...