-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeometry.h
More file actions
137 lines (113 loc) · 3.8 KB
/
geometry.h
File metadata and controls
137 lines (113 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <vector>
#include <cmath>
#include "cvec.h"
// A generic vertex structure containing position, normal, and texture information
// Used by make* functions to pass vertex information to the caller
struct GenericVertex {
Cvec3f pos;
Cvec3f normal;
Cvec2f tex;
GenericVertex(float x, float y, float z, float nx, float ny, float nz, float tu, float tv)
: pos(x,y,z), normal(nx,ny,nz), tex(tu, tv)
{}
};
inline void getCubeVbIbLen(int& vbLen, int& ibLen) {
vbLen = 24;
ibLen = 36;
}
template<typename VtxOutIter, typename IdxOutIter>
void makeCube(float size, VtxOutIter vtxIter, IdxOutIter idxIter) {
float h = size / 2.0;
#define DEFV(x, y, z, nx, ny, nz, tu, tv) { *vtxIter = GenericVertex(x h, y h, z h, nx, ny, nz, tu, tv); ++vtxIter; }
DEFV(+, -, -, 1, 0, 0, 0, 0); // facing +X
DEFV(+, +, -, 1, 0, 0, 1, 0);
DEFV(+, +, +, 1, 0, 0, 1, 1);
DEFV(+, -, +, 1, 0, 0, 0, 1);
DEFV(-, -, -, -1, 0, 0, 0, 0); // facing -X
DEFV(-, -, +, -1, 0, 0, 1, 0);
DEFV(-, +, +, -1, 0, 0, 1, 1);
DEFV(-, +, -, -1, 0, 0, 0, 1);
DEFV(-, +, -, 0, 1, 0, 0, 0); // facing +Y
DEFV(-, +, +, 0, 1, 0, 1, 0);
DEFV(+, +, +, 0, 1, 0, 1, 1);
DEFV(+, +, -, 0, 1, 0, 0, 1);
DEFV(-, -, -, 0, -1, 0, 0, 0); // facing -Y
DEFV(+, -, -, 0, -1, 0, 1, 0);
DEFV(+, -, +, 0, -1, 0, 1, 1);
DEFV(-, -, +, 0, -1, 0, 0, 1);
DEFV(-, -, +, 0, 0, 1, 0, 0); // facing +Z
DEFV(+, -, +, 0, 0, 1, 1, 0);
DEFV(+, +, +, 0, 0, 1, 1, 1);
DEFV(-, +, +, 0, 0, 1, 0, 1);
DEFV(-, -, -, 0, 0, -1, 0, 0); // facing -Z
DEFV(-, +, -, 0, 0, -1, 1, 0);
DEFV(+, +, -, 0, 0, -1, 1, 1);
DEFV(+, -, -, 0, 0, -1, 0, 1);
#undef DEFV
for (int v = 0; v < 24; v +=4) {
*idxIter = v;
*++idxIter = v + 1;
*++idxIter = v + 2;
*++idxIter = v;
*++idxIter = v + 2;
*++idxIter = v + 3;
++idxIter;
}
}
inline void getSphereVbIbLen(int slices, int stacks, int& vbLen, int& ibLen) {
assert(slices > 1);
assert(stacks >= 2);
vbLen = 2 + slices * (stacks - 1);
ibLen = slices * (stacks - 1) * 6;
}
template<typename VtxOutIter, typename IdxOutIter>
void makeSphere(float radius, int slices, int stacks, VtxOutIter vtxIter, IdxOutIter idxIter) {
using namespace std;
assert(slices > 1);
assert(stacks >= 2);
// the two poles
*vtxIter = GenericVertex(0, 0, radius, 0, 0, 1, 0, 1);
*++vtxIter = GenericVertex(0, 0, -radius, 0, 0, -1, 0, 0);
++vtxIter;
const double radPerSlice = 2 * CS175_PI / slices;
const double radPerStack = CS175_PI / stacks;
vector<double> longSin(slices), longCos(slices);
vector<double> latSin(stacks-1), latCos(stacks-1);
for (int i = 0; i < slices; ++i) {
longSin[i] = sin(radPerSlice * i);
longCos[i] = cos(radPerSlice * i);
}
for (int i = 0; i < stacks - 1; ++i) {
latSin[i] = sin(radPerStack * (i+1));
latCos[i] = cos(radPerStack * (i+1));
}
for (int i = 0; i < slices; ++i) {
*idxIter = 0;
*++idxIter = 2 + (stacks-1) * i ;
*++idxIter = 2 + (stacks-1) * ((i + 1) % slices);
++idxIter;
for (int j = 0; j < stacks - 1; ++j) {
float x = longCos[i] * latSin[j];
float y = longSin[i] * latSin[j];
float z = latCos[j];
*vtxIter = GenericVertex(x * radius, y * radius, z * radius, x, y, z, 1.0/slices*i, 1.0/stacks*(j+1));
++vtxIter;
if (j < stacks - 2) {
*idxIter = 2 + (stacks-1) * i + j;
*++idxIter = 2 + (stacks-1) * i + j + 1;
*++idxIter = 2 + (stacks-1) * ((i + 1) % slices) + j + 1;
*++idxIter = 2 + (stacks-1) * i + j;
*++idxIter = 2 + (stacks-1) * ((i + 1) % slices) + j + 1;
*++idxIter = 2 + (stacks-1) * ((i + 1) % slices) + j;
++idxIter;
}
}
*idxIter = 2 + (stacks-1) * i + stacks - 2;
*++idxIter = 1;
*++idxIter = 2 + (stacks-1) * ((i + 1)%slices) + stacks - 2;
++idxIter;
}
}
#endif