Ult3D
Implementation of "Ultimate 3D Game Engine Design & Architecture" by Allan Sherrod
Loading...
Searching...
No Matches
SphereShape.h
Go to the documentation of this file.
1#ifndef ULT_SPHERE_SHAPE_H_INCLUDED
2#define ULT_SPHERE_SHAPE_H_INCLUDED
3
4#include <cmath>
5#include "../ModelData.h"
6
7#define ULT_UNIT_SPHERE_STRIDE sizeof(Ult::stUnitSphereVertex)
8
9namespace Ult
10{
11
13{
14 float x,y,z;
15 float tu1,tv1;
16 float nx,ny,nz;
17};
18
24static inline bool CreateSphereMesh(
25 float radius,
26 int hRes, int vRes,
27 ModelData* model
28)
29{
30 if (!model) { return false; }
31 model->Clear();
32
33 int numFaces = hRes * (vRes*2) * 2;
34 int numIndices = numFaces * 3;
35 int numVerts = (hRes+1) * (vRes+1);
36 const float fPI = 3.141592654f;
37
38 stUnitSphereVertex* verts = new stUnitSphereVertex[numVerts];
39 stUnitSphereVertex* vertPtr = verts;
40 if (!verts) {
41 return false;
42 }
43
44 unsigned int* indices = new unsigned int[numFaces*3];
45 unsigned int* indexPtr = indices;
46 if (!indices) {
47 return false;
48 }
49
50 for (int i=0; i<=hRes; i++) {
51 for (int j=0; j<=vRes; j++) {
52 float ii = i/float(hRes);
53 float jj = j/float(vRes);
54 float theta = ii*2*fPI;
55 float phi = (jj-0.5f)*fPI;
56 float phi2 = phi + fPI * 0.5f;
57 float nx = cosf(theta)*cosf(phi);
58 float ny = sinf(theta)*cosf(phi);
59 float nz = sinf(phi);
60
61 vertPtr->x = radius * nx;
62 vertPtr->y = radius * ny;
63 vertPtr->z = radius * nz;
64
65 vertPtr->tu1 = ii;
66 vertPtr->tv1 = jj;
67
68 vertPtr->nx = -nx;
69 vertPtr->ny = -ny;
70 vertPtr->nz = -nz;
71
72 vertPtr++;
73 }
74 }
75
76 for (int i=0; i<hRes; i++) {
77 for (int j=0; j<vRes; j++) {
78 *indexPtr++ = (i+0) * (vRes+1) + j + 0;
79 *indexPtr++ = (i+1) * (vRes+1) + j + 1;
80 *indexPtr++ = (i+0) * (vRes+1) + j + 1;
81
82 *indexPtr++ = (i+0) * (vRes+1) + j + 0;
83 *indexPtr++ = (i+1) * (vRes+1) + j + 0;
84 *indexPtr++ = (i+1) * (vRes+1) + j + 1;
85 }
86 }
87
91
92 if (!model->SetVertices(ULT_TRI_LIST, numVerts, ULT_UNIT_SPHERE_STRIDE, (char*)verts)) {
93 return false;
94 }
95 if (!model->SetIndices(numIndices, indices)) {
96 return false;
97 }
98 if (verts) { delete[] verts; }
99 if (indices) { delete[] indices; }
100
101 return true;
102}
103
104} // namespace Ult
105
106#endif // ULT_SPHERE_SHAPE_H_INCLUDED
107
#define ULT_UNIT_SPHERE_STRIDE
Definition SphereShape.h:7
Definition ModelData.h:26
void Clear()
Definition ModelData.cpp:21
bool SetIndices(int totalIndices, unsigned int *indices)
Definition ModelData.cpp:36
bool AddDescriptorElement(ElementType type)
Definition ModelData.h:49
bool SetVertices(PrimitiveType type, int totalVertices, int stride, char *vertices)
Definition ModelData.cpp:53
@ ULT_TRI_LIST
Definition PrimitiveType.h:12
@ ULT_VERTEX_3F
Definition ElementType.h:13
@ ULT_TEX1_2F
Definition ElementType.h:16
@ ULT_NORMAL_3F
Definition ElementType.h:14
Definition Archive.h:13
Definition SphereShape.h:13
float nx
Definition SphereShape.h:16
float x
Definition SphereShape.h:14
float tu1
Definition SphereShape.h:15
float tv1
Definition SphereShape.h:15
float nz
Definition SphereShape.h:16
float ny
Definition SphereShape.h:16
float z
Definition SphereShape.h:14
float y
Definition SphereShape.h:14