-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometrymaker.h
179 lines (152 loc) · 4.97 KB
/
geometrymaker.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef GEOMETRYMAKER_H
#define GEOMETRYMAKER_H
#include <cmath>
#include "cvec.h"
//--------------------------------------------------------------------------------
// Helpers for creating some special geometries such as plane, cubes, and spheres
//--------------------------------------------------------------------------------
// 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;
Cvec3f tangent, binormal;
GenericVertex(
float x, float y, float z,
float nx, float ny, float nz,
float tu, float tv,
float tx, float ty, float tz,
float bx, float by, float bz)
: pos(x,y,z), normal(nx,ny,nz), tex(tu, tv), tangent(tx, ty, tz), binormal(bx, by, bz)
{}
};
inline void getPlaneVbIbLen(int& vbLen, int& ibLen) {
vbLen = 4;
ibLen = 6;
}
template<typename VtxOutIter, typename IdxOutIter>
void makePlane(float size, VtxOutIter vtxIter, IdxOutIter idxIter) {
float h = size / 2.0;
*vtxIter = GenericVertex( -h, 0, -h, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, -1);
*(++vtxIter) = GenericVertex(-h, 0, h, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, -1);
*(++vtxIter) = GenericVertex( h, 0, h, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, -1);
*(++vtxIter) = GenericVertex( h, 0, -h, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, -1);
*idxIter = 0;
*(++idxIter) = 1;
*(++idxIter) = 2;
*(++idxIter) = 0;
*(++idxIter) = 2;
*(++idxIter) = 3;
}
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, \
tan[0], tan[1], tan[2], \
bin[0], bin[1], bin[2]); \
++vtxIter; \
}
Cvec3f tan(0, 1, 0), bin(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);
tan = Cvec3f(0, 0, 1);
bin = Cvec3f(0, 1, 0);
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);
tan = Cvec3f(0, 0, 1);
bin = Cvec3f(1, 0, 0);
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);
tan = Cvec3f(1, 0, 0);
bin = Cvec3f(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);
tan = Cvec3f(1, 0, 0);
bin = Cvec3f(0, 1, 0);
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);
tan = Cvec3f(0, 1, 0);
bin = Cvec3f(1, 0, 0);
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 = (slices + 1) * (stacks + 1);
ibLen = slices * stacks * 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);
const double radPerSlice = 2 * CS175_PI / slices;
const double radPerStack = CS175_PI / stacks;
vector<double> longSin(slices+1), longCos(slices+1);
vector<double> latSin(stacks+1), latCos(stacks+1);
for (int i = 0; i < slices + 1; ++i) {
longSin[i] = sin(radPerSlice * i);
longCos[i] = cos(radPerSlice * i);
}
for (int i = 0; i < stacks + 1; ++i) {
latSin[i] = sin(radPerStack * i);
latCos[i] = cos(radPerStack * i);
}
for (int i = 0; i < slices + 1; ++i) {
for (int j = 0; j < stacks + 1; ++j) {
float x = longCos[i] * latSin[j];
float y = longSin[i] * latSin[j];
float z = latCos[j];
Cvec3f n(x, y, z);
Cvec3f t(-longSin[i], longCos[i], 0);
Cvec3f b = cross(n, t);
*vtxIter = GenericVertex(
x * radius, y * radius, z * radius,
x, y, z,
1.0/slices*i, 1.0/stacks*j,
t[0], t[1], t[2],
b[0], b[1], b[2]);
++vtxIter;
if (i < slices && j < stacks ) {
*idxIter = (stacks+1) * i + j;
*++idxIter = (stacks+1) * i + j + 1;
*++idxIter = (stacks+1) * (i + 1) + j + 1;
*++idxIter = (stacks+1) * i + j;
*++idxIter = (stacks+1) * (i + 1) + j + 1;
*++idxIter = (stacks+1) * (i + 1) + j;
++idxIter;
}
}
}
}
#endif