![](http://home.qlsc.sdu.edu.cn/images/smilies/yumen.gif)
自己几何也太差劲了,时间都花在计算坐标位置上了
图片附件: Sierpinski.JPG (2007-3-29 00:56, 39.68 K)
![](http://images.blogcn.com/2007/3/29/1/xoyojank,2007032911352.jpg)
附件: D3DStudy.exe (2007-3-29 00:56, 64 K)
主要算法,就是个递归:
/**/
/***************************************
*三角形绘制
*vertex[3]
***************************************/
inline
void
DrawTriangle(CustomVertexvertex[])
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
//拷贝到顶点缓存
memcpy(pVertices,vertex,3*sizeof(CustomVertex));
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
/****************************************
*Sierpinski镂垫绘制
*deepth:递归深度
****************************************/
void
DrawSierpinski(CustomVertexvertex[],
int
deepth)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if(deepth==0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DrawTriangle(vertex);
return;
}else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//计算中点
CustomVertexmidVertex[]=
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{(vertex[1].x+vertex[2].x)/2,(vertex[1].y+vertex[2].y)/2,(vertex[1].z+vertex[2].z)/2,vertex[0].color},
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{(vertex[0].x+vertex[2].x)/2,(vertex[0].y+vertex[2].y)/2,(vertex[0].z+vertex[2].z)/2,vertex[1].color},
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{(vertex[1].x+vertex[0].x)/2,(vertex[1].y+vertex[0].y)/2,(vertex[1].z+vertex[0].z)/2,vertex[2].color}
};
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
CustomVertextempVertex[3];
//分三部分进行递归绘制
tempVertex[0]=vertex[0];
tempVertex[1]=midVertex[2];
tempVertex[2]=midVertex[1];
DrawSierpinski(tempVertex,deepth-1);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
tempVertex[0]=midVertex[2];
tempVertex[1]=vertex[1];
tempVertex[2]=midVertex[0];
DrawSierpinski(tempVertex,deepth-1);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
tempVertex[0]=midVertex[1];
tempVertex[1]=midVertex[0];
tempVertex[2]=vertex[2];
DrawSierpinski(tempVertex,deepth-1);
}
}