Directx11教程(55) 建立球形和锥形物体
本教程中,我们新建2个model class,SphereModelClass以及CylinderModelClass,分别用来表示球形和锥形物体。
程序执行后的界面如下:

线框模式界面如下:

从线框模式可以看出,球形是由三个因素决定:半径、经度线、纬度线。
在SphereModelClass.cpp中,我们看到,初始化顶点缓冲和索引缓冲的函数为:InitializeBuffers(ID3D11Device* device, float radius, int numSlices, int numStacks),它多了三个参数,分别表示半径、经度切片的数量、纬度切面的数量。具体构建球形顶点的操作在函数buildStacks(vertices, indices)中,主要就是把经纬度切片的数目转化成球坐标系中的角度,求出球坐标系中顶点,再转化到笛卡尔坐标系中。
代码如下:
void SphereModelClass::buildStacks(VertexList& vertices, IndexList& indices)
{
float phiStep = PI/m_NumStacks;
int numRings = m_NumStacks-1;
// 对于每个纬度环,计算顶点.
for(int i = 1; i <= numRings; ++i)
{
float phi = i*phiStep;
// 环上的顶点
float thetaStep = 2.0f*PI/m_NumSlices;
for(int j = 0; j <= m_NumSlices; ++j)
{
float theta = j*thetaStep;
VertexType v;
// 球坐标到笛卡尔坐标的转化
v.position.x = m_Radius*sinf(phi)*cosf(theta);
v.position.y = m_Radius*cosf(phi);
v.position .z = m_Radius*sinf(phi)*sinf(theta);
D3DXVec3Normalize(&v.normal, &v.position);
//球的纹理坐标
v.texture.x = theta / (2.0f*PI);
v.texture.y = phi / PI;
v.Kd = D3DXVECTOR4(0.2, 0.2, 0.1,1.0);
v.Ks = D3DXVECTOR4(0.2, 0.2, 0.2,1.0);
vertices.push_back( v );
}
}
// 球的极点: 会出现纹理坐标扭曲
VertexType t1;
t1.position = D3DXVECTOR3(0.0f, -m_Radius, 0.0f);
t1.normal = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
t1.texture = D3DXVECTOR2(0.0f, 1.0f);
t1.Kd = D3DXVECTOR4(0.2, 0.2, 0.1,1.0);
t1.Ks = D3DXVECTOR4(0.2, 0.2, 0.2,1.0);
vertices.push_back( t1 );
t1.position = D3DXVECTOR3(0.0f, m_Radius, 0.0f);
t1.normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
t1.texture = D3DXVECTOR2(0.0f, 0.0f);
vertices.push_back(t1 );
int northPoleIndex = (int)vertices.size()-1;
int southPoleIndex = (int)vertices.size()-2;
int numRingVertices = m_NumSlices+1;
// 计算索引(不考虑极点)
for(int i = 0; i < m_NumStacks-2; ++i)
{
for(int j = 0; j < m_NumSlices; ++j)
{
indices.push_back(i*numRingVertices + j);
indices.push_back(i*numRingVertices + j+1);
indices.push_back((i+1)*numRingVertices + j);
indices.push_back((i+1)*numRingVertices + j);
indices.push_back(i*numRingVertices + j+1);
indices.push_back((i+1)*numRingVertices + j+1);
}
}
//北极点索引
for(int i = 0; i < m_NumSlices; ++i)
{
indices.push_back(northPoleIndex);
indices.push_back(i+1);
indices.push_back(i);
}
//南极点索引
int baseIndex = (numRings-1)*numRingVertices;
for(int i = 0; i < m_NumSlices; ++i)
{
indices.push_back(southPoleIndex);
indices.push_back(baseIndex+i);
indices.push_back(baseIndex+i+1);
}
}
在CylinderModelClass.cpp中,我们看到InitializeBuffers(ID3D11Device* device, float topRadius, float bottomRadius, float height, int numSlices, int numStacks),它多出了5个参数,分别表示锥体的顶部圆半径、底部圆半径,高度、经度切片的数量、纬度切片的数量。
具体计算顶点缓冲和索引缓冲由个函数组成,这三个函数的具体代码请参考源文件:
buildStacks(vertices, indices);
buildTopCap(vertices, indices);
buildBottomCap(vertices, indices);
完整的代码请参考:
工程文件myTutorialD3D11_50
代码下载:
http://files.cnblogs.com/mikewolf2002/d3d1150-58.zip
http://files.cnblogs.com/mikewolf2002/pictures.zip
Directx11教程(55) 建立球形和锥形物体的更多相关文章
- Directx11教程(56) 建立一个skydome
原文:Directx11教程(56) 建立一个skydome 本章建立一个skydome(天空穹),主要学习如何使用cube mapping. cube map就是把六张纹理当作 ...
- Directx11教程(60) tessellation学习(2)
原文:Directx11教程(60) tessellation学习(2) 本教程中,我们开始tessellation编程,共实现了2个程序,第一个tessellation程序,是对一个三 ...
- Directx11教程(5) 画一个简单的三角形(1)
原文:Directx11教程(5) 画一个简单的三角形(1) 在本篇教程中,我们将通过D3D11画一个简单的三角形.在D3D11中,GPU的渲染主要通过shader来操作(当然还有一些操作 ...
- Directx11教程(66) D3D11屏幕文本输出(1)
原文:Directx11教程(66) D3D11屏幕文本输出(1) 在D3D10中,通过ID3DX10Font接口对象,我们可以方便的在屏幕上输出文字信息,一个DrawText函数就能解决所 ...
- Directx11教程(59) tessellation学习(1)
原文:Directx11教程(59) tessellation学习(1) 在D3D11管线中,新增加了3个stage, Hull shader, Tessellator, Domain s ...
- Directx11教程(57) 环境映射
原文:Directx11教程(57) 环境映射 建好skydome后,如果我们想让其中的某个物体,比如那个球体来映射出周围环境的蓝天白云(不包括自己附近的物体),该怎么做呢?此时可以把这个 ...
- Directx11教程(54) 简单的基于GS的billboard实现
原文:Directx11教程(54) 简单的基于GS的billboard实现 本章我们用一个billboard的实现来学习D3D11中的GS. 在VS shader中,我们输入的是顶点 ...
- Directx11教程(52) 实例(instancing)的简单应用
原文:Directx11教程(52) 实例(instancing)的简单应用 有些时候,我们需要在场景中渲染大量的重复的物体,比如体育场中的观众,森林里面的树木等等,这些物体具有相似的形状,比如很多树 ...
- Directx11教程(49) stencil的应用-镜面反射
原文:Directx11教程(49) stencil的应用-镜面反射 本教程中,我们利用stencil来实现一个镜面反射效果. 1.首先我们要在D3DClass中增加几个成员变量及函数. I ...
随机推荐
- php连接数据库插入数据
<form action="updata.php" method="post"> 姓名:<input type="text" ...
- substring() 方法用于提取字符串中介于两个指定下标之间的字符。
substring() 方法用于提取字符串中介于两个指定下标之间的字符. 语法 stringObject.substring(start,stop) 参数 描述 start 必需.一个非负的整数,规定 ...
- jeecms添加站点
Step1:点击[站点管理],然后点击[添加站点]. Step2:按照下图填写,注意[路径]这一栏!!这里我随便写了个[aaa]. Step3:这个时候在本地部署的tomcat的模板路径:tomcat ...
- IO流13 --- 转换流实现文件复制 --- 技术搬运工(尚硅谷)
InputStreamReader 将字节输入流转换为字符输入流 OutputStreamWriter 将字符输出流转换为字节输出流 @Test public void test2() { //转换流 ...
- 手写Function.bind函数
if(!Function.prototype.bind){ Function.prototype.bind = function(oThis){ if(typeof this !=="fun ...
- tensorflow根据pb多bitch size去推导物体
with self.detection_graph.as_default(): with tf.Session(graph=self.detection_graph) as sess: # Expan ...
- 现在学习 JavaScript 的哪种技术更好:Angular、jQuery 还是 Node.js?(转)
本文选自<开发者头条>1 月 7 日最受欢迎文章 Top 3,感谢作者 @WEB资源网 分享. 欢迎分享:http://toutiao.io/contribute 这是一个发布在 Quor ...
- ES6--反引号的使用
/*动态初始退出登出框话模态框*/ /*动态的初始化退出登陆模态框 反引号ES6语法 * 为什么在使用字符串格式直接创建模态框 * 1.不能在html页面中创建模板,因为如果换一个页面就没有对应的模板 ...
- Ubuntu下安装Libpcap
Libpcap是 Unix/Linux 平台下的网络数据捕获函数包,百度百科是这么说的,唉,不管什么来头,只要帮我完成作业就行,安装过程记录如下: 还是那个套路,先在网上搜了一把,大概也就那样,被疯狂 ...
- netbeans 代码自动补全设置
编辑器-----代码完成------语言选择"JAVA"------在如图红框中输入 @ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst ...