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 ...
随机推荐
- 通过media媒体查询设置ie7/8样式、使用media判断各机型、手淘flexible.js
@media all and (min-width:1280px){ /* 所有设备宽度大于1280干嘛干嘛嘞... */ body{ background:#f00; } } @media (min ...
- 深入浅出 Java Concurrency (1) : J.U.C的整体认识[转]
去年年底有一个Guice的研究计划,可惜由于工作“繁忙”加上实际工作中没有用上导致“无疾而终”,最终只是完成了Guice的初步学习教程,深入的研究没有继续进行下去. 最近一直用的比较多的就是java. ...
- 499 单词计数 (Map Reduce版本)
原题网址:https://www.lintcode.com/problem/word-count-map-reduce/description 描述 使用 map reduce 来计算单词频率http ...
- 常用 docker 容器 使用
mongo: 单点 docker run -idt --name=mongo --restart=always -p : -v /home/hylas/opt/mongo/data:/data/db ...
- Jquery选择器总结二
简单选择器 1.:firstè选出匹配的元素中的第一个 2.:lastè选出匹配的元素中的最后一个 3.:eq(index)è选出匹配的元素中的指定索引位置的jquery对象(注:index从0开始) ...
- jQuery同步/异步调用后台方法
$.ajax({ type: "Post", url: "UserManage.aspx/SubmitPage",//页面/方法名 data: "{' ...
- Linux时间介绍
Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟.系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟, ...
- python利用paramiko执行服务器命令
话不多说直接上代码 封装连接 @staticmethod def connect(ip, server_user, server_port, server_path): ""&qu ...
- TZ_10_常用的2中加密算法MD5,spring-sucrity
1.MD5 在注册时需要进行加密,在登陆时也需要加密进行配对 public class MD5util { public static String stringToMD5(String psd) { ...
- ACdream 1108(莫队)
题目链接 The kth number Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) ...