G-Buffer

更新时间:2023-10-03 18:41

G-buffering是一种在Video Post中基于图象过滤和图层事件中可使用的物体蒙板的一种着色技术。用户可以通过标记物体ID或材质ID来得到专用的图象通道。

交互式渲染中的G-Buffer

指Geometry Buffer,亦即“几何缓冲”。区别于普通的仅将颜色渲染到纹理中,G-Buffer指包含颜色、法线、世界空间坐标的缓冲区,亦即指包含颜色、法线、世界空间坐标的纹理。

由于G-Buffer需要的向量长度超出通常纹理能包含的向量的长度,通常在游戏开发中,使用多渲染目标技术来生成G-Buffer,即在一次绘制中将颜色、法线、世界空间坐标分别渲染到三张浮点纹理中。

如在DirectX平台下:

//OnCreateDevice中:

//先创建三张纹理及其相应渲染表面,分别用于存法线(Normal)、颜色(Color)、空间坐标(Position)

V_RETURN(pd3dDevice->CreateTexture(pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &g_pNormalMap, NULL));

V_RETURN(g_pNormalMap->GetSurfaceLevel(0, &g_pNormalSurface));

V_RETURN(pd3dDevice->CreateTexture(pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &g_pColorMap, NULL));

V_RETURN(g_pColorMap->GetSurfaceLevel(0, &g_pColorSurface));

V_RETURN(pd3dDevice->CreateTexture(pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &g_pPositionMap, NULL));

V_RETURN(g_pPositionMap->GetSurfaceLevel(0, &g_pPositionSurface));

//开启多渲染目标

pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA);

pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE1, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA);

pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE2, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA);

//OnFrameRender中:

//保存屏幕表面,设置渲染目标为0,1,2号

LPDIRECT3DSURFACE9 pRTScreen;

V( pd3dDevice->GetRenderTarget(0, &pRTScreen) );

V( pd3dDevice->SetRenderTarget(0, g_pNormalSurface));

V( pd3dDevice->SetRenderTarget(1, g_pColorSurface));

V( pd3dDevice->SetRenderTarget(2, g_pPositionSurface));

/*……渲染场景……*/

//恢复渲染目标

V( pd3dDevice->SetRenderTarget(0, pRTScreen));

V( pd3dDevice->SetRenderTarget(1, NULL));

V( pd3dDevice->SetRenderTarget(2, NULL));

//Shader中,定义像素格式为COLOR[n],注意在输出时需要把法线和空间坐标映射到0~1范围

struct PS_OUTPUT

{

float4 Normal : COLOR0; // Normal

float4 RGBColor : COLOR1; // Pixel color

float4 Position : COLOR2; // Position

};

免责声明
隐私政策
用户协议
目录 22
0{{catalogNumber[index]}}. {{item.title}}
{{item.title}}