花了不少时间总算是做出来了,先看看效果:
![](http://www.cppblog.com/images/cppblog_com/vertexer/ao.JPG)
虽然离D3D SDK里的PRT的Demo尚有一段距离,但是相比经典的简单漫反射着色模型要强了不少。
具体的算法可以参考GPU Gems卷1第17章,我就不多做介绍,贴一点代码实现供新手们参考,老手们指正~
今天先贴预处理部分吧,整个算法写成了一个函数,调用及其简单。
不过代码风格不太好,整个算法只写了两个函数,十分冗长,阅读起来可能比较困难,效率也不太高,仅作为一种实现方式参考吧~
算法需要计算的数据有两个,一个是可到达度,即1-被遮蔽度,另一个是未被遮挡方向的平均方向。
算法中先针对模型的每个面片计算上述两值,然后加权到它的三个顶点,再由顶点取平均值,
这样在渲染的时候从顶点读出数据再经过插值就得到了各个像素的上述参数了。
1
//-----------------------------------------------------------------
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**////GenRandRays 用于生成面片朝向半球内的随机向量
3
///pRas:用于接受生成向量的数组
4
///nNum:生成射线的数量
5
///vcNormal:面片的法线向量 6
//-----------------------------------------------------------------
7
void GenRandRays(D3DXVECTOR3* pRays, int nNum, D3DXVECTOR3 &vcNormal)
8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
if(pRays ==NULL)
10
return;
11
int n = 0;
12
D3DXVECTOR3 vcRay;
13
while(n<nNum)
14![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
vcRay.x = float(rand())/12.34567f;
16
vcRay.y = float(rand())/12.34567f;
17
vcRay.z = float(rand())/12.34567f; //生成随机向量
18
vcRay.x -= int(vcRay.x);
19
vcRay.y -= int(vcRay.y);
20
vcRay.z -= int(vcRay.z); //取小数部分
21
vcRay.x -= 0.5f;
22
vcRay.y -= 0.5f;
23
vcRay.z -= 0.5f;
24
vcRay*=2.0f; //归一化
25
srand(int(rand()+vcRay.x*1000+vcRay.y));
26
if(vcRay.x*vcRay.x + vcRay.y*vcRay.y +vcRay.z*vcRay.z >1)
27
continue;
28
if(D3DXVec3Dot(&vcRay, &vcNormal) < 0)
29
continue;//向量指向面片反面的半球
30
D3DXVec3Normalize(&pRays[n++], &vcRay);
31
}
32
}
1
//用于存储顶点遮蔽信息的结构体
2
struct VERTEXINFO
3![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4
D3DXVECTOR3 vcAvgUnoccluded;
5
//未被遮蔽方向的向量
6
float fAccessibility;
7
//光线可到达度(即1-遮蔽度)
8
int nFaces;
9
//顶点所在的面片数,用于计算上述两项的平均值
10
};
1![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
2
//---------------------------------------------------------------
3![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**////GenOccInfo 用于生成模型所有顶点的遮挡信息
4
///pMesh:待处理的X格式模型
5
///nNumRays:计算未遮蔽方向平均向量时使用的随机向量数目
6
///szSave:用于保存遮蔽信息的文件名及路径
7
///pProgressHandler:回调函数指针,用于接受和处理进度信息,参数为0-1 8
//---------------------------------------------------------------
9![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
10
bool GenOccInfo(LPD3DXMESH pMesh, int nNumRays, LPCTSTR szSave, void (*pProgressHandler)(float*)=NULL)
11![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12
if(pMesh == NULL || szSave == NULL)
13
return false;
14
LPDIRECT3DINDEXBUFFER9 pIB = NULL;
15
LPDIRECT3DVERTEXBUFFER9 pVB = NULL;
16
BYTE* pVertices = NULL;
17
WORD* pIndices = NULL;
18
DWORD dwNumFaces = 0, dwNumVertices = 0, dwBytesPerVert=0;
19![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
20
pMesh->GetIndexBuffer(&pIB);
21
pMesh->GetVertexBuffer(&pVB);
22![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
23
dwNumFaces = pMesh->GetNumFaces();
24
dwBytesPerVert= pMesh->GetNumBytesPerVertex();
25
dwNumVertices = pMesh->GetNumVertices();
26![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
27
pIB->Lock(0, 0, (void**)&pIndices, 0);
28
pVB->Lock(0, 0, (void**)&pVertices, 0);
29![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
30
VERTEXINFO* pVerticesInfo = new VERTEXINFO[dwNumVertices];
31
ZeroMemory(pVerticesInfo, sizeof(VERTEXINFO)*dwNumVertices);
32
D3DXVECTOR3* pRays = new D3DXVECTOR3[nNumRays];
33![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
34![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
D3DXVECTOR3* pVertex[3] =
{NULL, NULL, NULL};
35![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
D3DXVECTOR3* pFaceVert[3] =
{NULL, NULL, NULL};
36
D3DXVECTOR3 vcCenter, vcNormal;
37![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
38![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
39
for(DWORD dwFace=0; dwFace<dwNumFaces; dwFace++)
40![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//遍历所有面片,求中点及法线
41
pVertex[0] = (D3DXVECTOR3*)(pVertices + pIndices[dwFace*3]*dwBytesPerVert);
42
pVertex[1] = (D3DXVECTOR3*)(pVertices + pIndices[dwFace*3+1]*dwBytesPerVert);
43
pVertex[2] = (D3DXVECTOR3*)(pVertices + pIndices[dwFace*3+2]*dwBytesPerVert);
44![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
45
vcCenter = (*pVertex[0]+*pVertex[1]+*pVertex[2])/3;
46![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
47
D3DXVec3Cross(&vcNormal, &(*(pVertex[0]) - vcCenter), &(*(pVertex[1]) - vcCenter));
48
::D3DXVec3Normalize(&vcNormal,&vcNormal);
49![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
50
//为当前面片生成指向正面半球内的随机射线
51
GenRandRays(pRays, nNumRays, vcNormal);
52![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
53
D3DXVECTOR3 vcAvgUnoccluded(0.0f, 0.0f, 0.0f);
54
DWORD dwNumUnocc = 0;
55
for(int i=0; i<nNumRays; i++)
56![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//为每根射线对模型做碰撞检测
57
bool bOccluded = false;
58
for(DWORD k=0; k<dwNumFaces; k++)
59![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//与模型的每个面片做碰撞检测
60
if(k==dwFace)
61
continue;
62
pFaceVert[0] = (D3DXVECTOR3*)(pVertices + pIndices[k*3]*dwBytesPerVert);
63
pFaceVert[1] = (D3DXVECTOR3*)(pVertices + pIndices[k*3+1]*dwBytesPerVert);
64
pFaceVert[2] = (D3DXVECTOR3*)(pVertices + pIndices[k*3+2]*dwBytesPerVert);
65
float x,y,z;
66
if(D3DXIntersectTri(pFaceVert[0], pFaceVert[1], pFaceVert[2], &vcCenter, &pRays[i], &x, &y, &z))
67![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
bOccluded = true;
69
break;
70
}
71
}
72
if(!bOccluded)
73![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//所有碰撞检测失败,即射线未被遮挡
74
vcAvgUnoccluded += pRays[i];
75
dwNumUnocc++;
76
}
77
}
78
//计算当前面的平均未遮挡方向及可到达度
79
D3DXVec3Normalize(&vcAvgUnoccluded, &vcAvgUnoccluded);
80
float fAccessibility = float(dwNumUnocc)/float(nNumRays);
81
82
for(int j=0; j<3; j++)
83![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//累积到当前面片的三个顶点
84
pVerticesInfo[pIndices[dwFace*3+j]].vcAvgUnoccluded += vcAvgUnoccluded;
85
pVerticesInfo[pIndices[dwFace*3+j]].fAccessibility += fAccessibility;
86
pVerticesInfo[pIndices[dwFace*3+j]].nFaces++;
87
}
88![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
89
if(pProgressHandler!=NULL)
90![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//处理进度信息
91
float fProg = float(dwFace)/float(dwNumFaces);
92
(*pProgressHandler)(&fProg);
93
}
94
}
95![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
96
for(DWORD dwVert = 0; dwVert<dwNumVertices; dwVert++)
97![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//统计所有顶点的平均值
98
pVerticesInfo[dwVert].fAccessibility/=float(pVerticesInfo[dwVert].nFaces);
99
D3DXVec3Normalize(&pVerticesInfo[dwVert].vcAvgUnoccluded, &pVerticesInfo[dwVert].vcAvgUnoccluded);
100
}
101![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
102
pVB->Unlock();
103
pIB->Unlock();
104![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
105
CFile file;
106
file.Open(szSave, CFile::modeCreate|CFile::modeWrite);
107
char buff[100];
108
for(DWORD dwVert = 0; dwVert<dwNumVertices; dwVert++)
109![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//写入到文件
110
sprintf(buff, "%f,%f,%f,%f\n", pVerticesInfo[dwVert].vcAvgUnoccluded.x, pVerticesInfo[dwVert].vcAvgUnoccluded.y, pVerticesInfo[dwVert].vcAvgUnoccluded.z, pVerticesInfo[dwVert].fAccessibility);
111
file.Write(buff, strlen(buff));
112
}
113
file.Flush();
114
file.Close();
115![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
116
delete [] pVerticesInfo;
117
delete [] pRays;
118![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
119
CString strMsg;
120
strMsg.Format(L"Processed:\n\t%d - faces\n\t%d - vertices.", dwNumFaces, dwNumVertices);
121
MessageBox(NULL, strMsg, L"Done", 0);
122![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
123
return true;
124
}
渲染部分的代码改天再贴。
使用上面的代码为一个2000面左右的Mesh生成遮挡信息在P4 3.0GHz的机器上需要大约几分钟的时间,效率比较低下,另外有个问题是,完全对称的模型,生成的遮挡信息居然不对称,从上面的图也看出来了,不知道是什么原因,还望高手指教~
posted on 2009-08-27 20:47
Vertexer 阅读(939)
评论(1) 编辑 收藏 引用