c++生成球的顶点和面索引CreateSphere
来源:互联网 发布:js 拍照上传是横屏的 编辑:程序博客网 时间:2024/05/21 19:50
我想找个代码在D3D11中创建一个球体,结果发现很多代码都不合适。它们往往只生成顶点,不生成面索引,比如:
http://gamedev.stackexchange.com/questions/16585/how-do-you-programmatically-generate-a-sphere
所以,自己写了一个。
#include "DXUT.h"#include "wrMesh.h"#include "wrMath.h"struct wrSimpleVertexInput{ DirectX::XMFLOAT3 position; DirectX::XMFLOAT3 normal;};using namespace DirectX;void CreateSphere(float r, int stacks, int slices, wrSimpleVertexInput** vArr, DWORD** iArr){ const float PI = WR_M_PI; wrSimpleVertexInput*& pV = *vArr; DWORD*& pI = *iArr; int nv = (stacks - 1)*slices + 2; int nf = (stacks-1)*slices * 2; const int offsetv1 = 1; const int offsetv2 = nv - slices - 1; const int offseti1 = 3 * slices; const int offseti2 = 3 * (nf - slices); pV = new wrSimpleVertexInput[nv]; pV[0].position = XMFLOAT3(0.f, r, 0.f); pV[nv - 1].position = XMFLOAT3(0.f, -r, 0.f); for (int t = 1; t < stacks; t++) // stacks are ELEVATION so they count theta { float theta = ((float)(t) / stacks)*PI; for (int p = 0; p < slices; p++) // slices are ORANGE SLICES so the count azimuth { float phi = ((float)(p) / slices) * 2 * PI; // azimuth goes around 0 .. 2*PI auto &v = pV[slices * (t-1) + p + offsetv1]; vec3 pos, norm; pos[0] = sinf(theta) * cosf(phi); pos[1] = cosf(theta); pos[2] = sinf(theta) * sinf(phi); vec3_copy(reinterpret_cast<float*>(&v.position), pos); vec3_norm(norm, pos); vec3_copy(reinterpret_cast<float*>(&v.normal), norm); } } pI = new DWORD[3*nf]; for (int t = 0; t < slices; t++) { pI[3 * t] = 0; pI[3 * t + 1] = t + offsetv1; pI[3 * t + 2] = (t + 1) % slices + offsetv1; pI[3 * t + offseti2] = nv - 1; pI[3 * t + 1 + offseti2] = (t + 1) % slices + offsetv2; pI[3 * t + 2 + offseti2] = t + offsetv2; } for (int t = 0; t < stacks - 2; t++) // stacks are ELEVATION so they count theta { auto ptr = pI + t * slices * 6 + offseti1; for (int p = 0; p < slices; p++) // slices are ORANGE SLICES so the count azimuth { //phi1 phi2 // | | // 1------2 -- theta1 // |\ _ | // | \ | // 4------3 -- theta2 // int v1 = offsetv1 + slices * t + p; int v2 = offsetv1 + slices * t + p + 1; int v3 = offsetv1 + slices * (t + 1) + p + 1; int v4 = offsetv1 + slices * (t + 1) + p; ptr[0] = v1; ptr[1] = v3; ptr[2] = v2; ptr[3] = v1; ptr[4] = v4; ptr[5] = v3; ptr += 6; } }}
望指正。
0 0
- c++生成球的顶点和面索引CreateSphere
- 顶点法线和面法线
- 顶点法线和面法线
- 索引顶点
- 索引顶点
- Directx11 顶点以及索引顶点缓存的的应用1
- 计算当前场景顶点数和面数
- 【Visual C++】游戏开发笔记三十七 浅墨DirectX提高班之五 顶点缓存的红颜知己:索引缓存的故事
- 【Visual C++】游戏开发笔记三十七 浅墨DirectX提高班之五 顶点缓存的红颜知己:索引缓存的故事
- 【Visual C++】游戏开发笔记三十七 浅墨DirectX提高班之五 顶点缓存的红颜知己:索引缓存的故事
- 【Visual C++】游戏开发笔记三十七 浅墨DirectX提高班之五 顶点缓存的红颜知己:索引缓存的故事
- 【Visual C++】游戏开发笔记三十七 浅墨DirectX提高班之五 顶点缓存的红颜知己:索引缓存的故事
- 索引顶点混合
- 索引顶点缓冲
- 顶点/索引缓存绘制
- 5.5.3 顶点索引
- OpenGL 顶点索引 绘制
- 索引顶点的VBO与多重纹理下的VBO
- Weblogic 节点管理器 nodemanager
- 使用 C++11 让程序更简洁、更现代
- 垂直方向的viewPager
- REST API设计步骤
- [leetcode] 283. Move Zeroes 解题报告
- c++生成球的顶点和面索引CreateSphere
- TCP连接的状态详解以及故障排查
- 简易版2048 (KeyListener需重写所有函数&&JFrame和JLabel设置颜色)
- UVA 11235 Frequent values
- linux下的时间调整
- Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
- 多态
- IOS RSA加密
- 最小生成树之Kruskal算法