原文: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) 建立球形和锥形物体的更多相关文章

  1. Directx11教程(56) 建立一个skydome

    原文:Directx11教程(56) 建立一个skydome       本章建立一个skydome(天空穹),主要学习如何使用cube mapping.      cube map就是把六张纹理当作 ...

  2. Directx11教程(60) tessellation学习(2)

    原文:Directx11教程(60) tessellation学习(2)        本教程中,我们开始tessellation编程,共实现了2个程序,第一个tessellation程序,是对一个三 ...

  3. Directx11教程(5) 画一个简单的三角形(1)

    原文:Directx11教程(5) 画一个简单的三角形(1)       在本篇教程中,我们将通过D3D11画一个简单的三角形.在D3D11中,GPU的渲染主要通过shader来操作(当然还有一些操作 ...

  4. Directx11教程(66) D3D11屏幕文本输出(1)

    原文:Directx11教程(66) D3D11屏幕文本输出(1)      在D3D10中,通过ID3DX10Font接口对象,我们可以方便的在屏幕上输出文字信息,一个DrawText函数就能解决所 ...

  5. Directx11教程(59) tessellation学习(1)

    原文:Directx11教程(59) tessellation学习(1)       在D3D11管线中,新增加了3个stage, Hull shader, Tessellator, Domain s ...

  6. Directx11教程(57) 环境映射

    原文:Directx11教程(57) 环境映射       建好skydome后,如果我们想让其中的某个物体,比如那个球体来映射出周围环境的蓝天白云(不包括自己附近的物体),该怎么做呢?此时可以把这个 ...

  7. Directx11教程(54) 简单的基于GS的billboard实现

    原文:Directx11教程(54) 简单的基于GS的billboard实现     本章我们用一个billboard的实现来学习D3D11中的GS.     在VS shader中,我们输入的是顶点 ...

  8. Directx11教程(52) 实例(instancing)的简单应用

    原文:Directx11教程(52) 实例(instancing)的简单应用 有些时候,我们需要在场景中渲染大量的重复的物体,比如体育场中的观众,森林里面的树木等等,这些物体具有相似的形状,比如很多树 ...

  9. Directx11教程(49) stencil的应用-镜面反射

    原文:Directx11教程(49) stencil的应用-镜面反射      本教程中,我们利用stencil来实现一个镜面反射效果. 1.首先我们要在D3DClass中增加几个成员变量及函数. I ...

随机推荐

  1. JQuery--漂亮的三目运算与jQ选择器结合代码

    $(function($) { $("input[name='timeset']").bind('click', function() { $(this).val() == 'cu ...

  2. light oj 1084 线性dp

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  3. NSIS语法解析

    注释.!define.变量.!include.常量 ; Script generated by the HM NIS Edit Script Wizard. ; HM NIS Edit Wizard ...

  4. PAT甲级——A1036 Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  5. 给NetBeans配置javafx环境

    JavaFX开发环境安装配置,这里给大家介绍一个非常有用的步骤 从Java8开始,JDK(Java开发工具包)包括了JavaFX库. 因此,要运行JavaFX应用程序,您只需要在系统中安装Java8或 ...

  6. 逻辑备份(mysqldump/select into outfile)

    #mysqldump备份 shell> mysqldump -uroot -p -P4306 sakila actor>E:\sakila-actor.sql shell> mysq ...

  7. 在windows系统和linux系统中查询IP地址命令的不同

    在linux和windows系统上查询IP地址的命令是不一样的.         在linux中的命令行模式下,输入ifconfig即可查询到IP.而在windows系统下要查询IP地址需要先打开do ...

  8. Luogu P3953 逛公园(最短路+记忆化搜索)

    P3953 逛公园 题面 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 \(N\) 个点 \(M\) 条边构成的有向图,且没有自环和重边.其中 \(1\) 号点是公园的入口,\(N\) 号点是公 ...

  9. Leetcode220. Contains Duplicate III存在重复元素3

    给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. 示例 1: 输入: ...

  10. light7结合jquery实现开关按钮

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...