rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@1
|
2 #include "meshgen.h"
|
nuclear@1
|
3 #include "mesh.h"
|
nuclear@1
|
4
|
nuclear@1
|
5 // -------- sphere --------
|
nuclear@1
|
6
|
nuclear@1
|
7 #define SURAD(u) ((u) * 2.0 * M_PI)
|
nuclear@1
|
8 #define SVRAD(v) ((v) * M_PI)
|
nuclear@1
|
9
|
nuclear@1
|
10 static Vector3 sphvec(float theta, float phi)
|
nuclear@1
|
11 {
|
nuclear@1
|
12 return Vector3(sin(theta) * sin(phi),
|
nuclear@1
|
13 cos(phi),
|
nuclear@1
|
14 cos(theta) * sin(phi));
|
nuclear@1
|
15 }
|
nuclear@1
|
16
|
nuclear@1
|
17 void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float vrange)
|
nuclear@1
|
18 {
|
nuclear@1
|
19 if(usub < 4) usub = 4;
|
nuclear@1
|
20 if(vsub < 2) vsub = 2;
|
nuclear@1
|
21
|
nuclear@1
|
22 int uverts = usub + 1;
|
nuclear@1
|
23 int vverts = vsub + 1;
|
nuclear@1
|
24
|
nuclear@1
|
25 int num_verts = uverts * vverts;
|
nuclear@1
|
26 int num_quads = usub * vsub;
|
nuclear@1
|
27 int num_tri = num_quads * 2;
|
nuclear@1
|
28
|
nuclear@1
|
29 mesh->clear();
|
nuclear@1
|
30 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
|
nuclear@1
|
31 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
|
nuclear@1
|
32 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
|
nuclear@1
|
33 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
|
nuclear@1
|
34 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
|
nuclear@1
|
35
|
nuclear@1
|
36 float du = urange / (float)(uverts - 1);
|
nuclear@1
|
37 float dv = vrange / (float)(vverts - 1);
|
nuclear@1
|
38
|
nuclear@1
|
39 float u = 0.0;
|
nuclear@1
|
40 for(int i=0; i<uverts; i++) {
|
nuclear@1
|
41 float theta = SURAD(u * urange);
|
nuclear@1
|
42
|
nuclear@1
|
43 float v = 0.0;
|
nuclear@1
|
44 for(int j=0; j<vverts; j++) {
|
nuclear@1
|
45 float phi = SVRAD(v * vrange);
|
nuclear@1
|
46
|
nuclear@1
|
47 Vector3 pos = sphvec(theta, phi);
|
nuclear@1
|
48
|
nuclear@1
|
49 *varr++ = pos * rad;
|
nuclear@1
|
50 *narr++ = pos;
|
nuclear@1
|
51 *tarr++ = (sphvec(theta + 0.1f, (float)M_PI / 2.0f) - sphvec(theta - 0.1f, (float)M_PI / 2.0f)).normalized();
|
nuclear@1
|
52 *uvarr++ = Vector2(u * urange, v * vrange);
|
nuclear@1
|
53
|
nuclear@1
|
54 if(i < usub && j < vsub) {
|
nuclear@1
|
55 int idx = i * vverts + j;
|
nuclear@1
|
56 *idxarr++ = idx;
|
nuclear@1
|
57 *idxarr++ = idx + 1;
|
nuclear@1
|
58 *idxarr++ = idx + vverts + 1;
|
nuclear@1
|
59
|
nuclear@1
|
60 *idxarr++ = idx;
|
nuclear@1
|
61 *idxarr++ = idx + vverts + 1;
|
nuclear@1
|
62 *idxarr++ = idx + vverts;
|
nuclear@1
|
63 }
|
nuclear@1
|
64
|
nuclear@1
|
65 v += dv;
|
nuclear@1
|
66 }
|
nuclear@1
|
67 u += du;
|
nuclear@1
|
68 }
|
nuclear@1
|
69 }
|
nuclear@1
|
70
|
nuclear@1
|
71
|
nuclear@1
|
72 // -------- cylinder --------
|
nuclear@1
|
73
|
nuclear@1
|
74 static Vector3 cylvec(float theta, float height)
|
nuclear@1
|
75 {
|
nuclear@1
|
76 return Vector3(sin(theta), height, cos(theta));
|
nuclear@1
|
77 }
|
nuclear@1
|
78
|
nuclear@1
|
79 void gen_cylinder(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
|
nuclear@1
|
80 {
|
nuclear@1
|
81 if(usub < 4) usub = 4;
|
nuclear@1
|
82 if(vsub < 1) vsub = 1;
|
nuclear@1
|
83
|
nuclear@1
|
84 int uverts = usub + 1;
|
nuclear@1
|
85 int vverts = vsub + 1;
|
nuclear@1
|
86
|
nuclear@1
|
87 int num_body_verts = uverts * vverts;
|
nuclear@1
|
88 int num_body_quads = usub * vsub;
|
nuclear@1
|
89 int num_body_tri = num_body_quads * 2;
|
nuclear@1
|
90
|
nuclear@1
|
91 int capvverts = capsub ? capsub + 1 : 0;
|
nuclear@1
|
92 int num_cap_verts = uverts * capvverts;
|
nuclear@1
|
93 int num_cap_quads = usub * capsub;
|
nuclear@1
|
94 int num_cap_tri = num_cap_quads * 2;
|
nuclear@1
|
95
|
nuclear@1
|
96 int num_verts = num_body_verts + num_cap_verts * 2;
|
nuclear@1
|
97 int num_tri = num_body_tri + num_cap_tri * 2;
|
nuclear@1
|
98
|
nuclear@1
|
99 mesh->clear();
|
nuclear@1
|
100 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
|
nuclear@1
|
101 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
|
nuclear@1
|
102 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
|
nuclear@1
|
103 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
|
nuclear@1
|
104 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
|
nuclear@1
|
105
|
nuclear@1
|
106 float du = urange / (float)(uverts - 1);
|
nuclear@1
|
107 float dv = vrange / (float)(vverts - 1);
|
nuclear@1
|
108
|
nuclear@1
|
109 float u = 0.0;
|
nuclear@1
|
110 for(int i=0; i<uverts; i++) {
|
nuclear@1
|
111 float theta = SURAD(u);
|
nuclear@1
|
112
|
nuclear@1
|
113 float v = 0.0;
|
nuclear@1
|
114 for(int j=0; j<vverts; j++) {
|
nuclear@1
|
115 float y = (v - 0.5) * height;
|
nuclear@1
|
116 Vector3 pos = cylvec(theta, y);
|
nuclear@1
|
117
|
nuclear@1
|
118 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
|
nuclear@1
|
119 *narr++ = Vector3(pos.x, 0.0, pos.z);
|
nuclear@1
|
120 *tarr++ = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
|
nuclear@1
|
121 *uvarr++ = Vector2(u * urange, v * vrange);
|
nuclear@1
|
122
|
nuclear@1
|
123 if(i < usub && j < vsub) {
|
nuclear@1
|
124 int idx = i * vverts + j;
|
nuclear@1
|
125
|
nuclear@1
|
126 *idxarr++ = idx;
|
nuclear@1
|
127 *idxarr++ = idx + vverts + 1;
|
nuclear@1
|
128 *idxarr++ = idx + 1;
|
nuclear@1
|
129
|
nuclear@1
|
130 *idxarr++ = idx;
|
nuclear@1
|
131 *idxarr++ = idx + vverts;
|
nuclear@1
|
132 *idxarr++ = idx + vverts + 1;
|
nuclear@1
|
133 }
|
nuclear@1
|
134
|
nuclear@1
|
135 v += dv;
|
nuclear@1
|
136 }
|
nuclear@1
|
137 u += du;
|
nuclear@1
|
138 }
|
nuclear@1
|
139
|
nuclear@1
|
140
|
nuclear@1
|
141 // now the cap!
|
nuclear@1
|
142 if(!capsub) {
|
nuclear@1
|
143 return;
|
nuclear@1
|
144 }
|
nuclear@1
|
145
|
nuclear@1
|
146 dv = 1.0 / (float)(capvverts - 1);
|
nuclear@1
|
147
|
nuclear@1
|
148 u = 0.0;
|
nuclear@1
|
149 for(int i=0; i<uverts; i++) {
|
nuclear@1
|
150 float theta = SURAD(u);
|
nuclear@1
|
151
|
nuclear@1
|
152 float v = 0.0;
|
nuclear@1
|
153 for(int j=0; j<capvverts; j++) {
|
nuclear@1
|
154 float r = v * rad;
|
nuclear@1
|
155
|
nuclear@1
|
156 Vector3 pos = cylvec(theta, height / 2.0) * r;
|
nuclear@1
|
157 pos.y = height / 2.0;
|
nuclear@1
|
158 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
|
nuclear@1
|
159
|
nuclear@1
|
160 *varr++ = pos;
|
nuclear@1
|
161 *narr++ = Vector3(0, 1, 0);
|
nuclear@1
|
162 *tarr++ = tang;
|
nuclear@1
|
163 *uvarr++ = Vector2(u * urange, v);
|
nuclear@1
|
164
|
nuclear@1
|
165 pos.y = -height / 2.0;
|
nuclear@1
|
166 *varr++ = pos;
|
nuclear@1
|
167 *narr++ = Vector3(0, -1, 0);
|
nuclear@1
|
168 *tarr++ = -tang;
|
nuclear@1
|
169 *uvarr++ = Vector2(u * urange, v);
|
nuclear@1
|
170
|
nuclear@1
|
171 if(i < usub && j < capsub) {
|
nuclear@1
|
172 unsigned int idx = num_body_verts + (i * capvverts + j) * 2;
|
nuclear@1
|
173
|
nuclear@1
|
174 unsigned int vidx[4] = {
|
nuclear@1
|
175 idx,
|
nuclear@1
|
176 idx + capvverts * 2,
|
nuclear@1
|
177 idx + (capvverts + 1) * 2,
|
nuclear@1
|
178 idx + 2
|
nuclear@1
|
179 };
|
nuclear@1
|
180
|
nuclear@1
|
181 *idxarr++ = vidx[0];
|
nuclear@1
|
182 *idxarr++ = vidx[2];
|
nuclear@1
|
183 *idxarr++ = vidx[1];
|
nuclear@1
|
184 *idxarr++ = vidx[0];
|
nuclear@1
|
185 *idxarr++ = vidx[3];
|
nuclear@1
|
186 *idxarr++ = vidx[2];
|
nuclear@1
|
187
|
nuclear@1
|
188 *idxarr++ = vidx[0] + 1;
|
nuclear@1
|
189 *idxarr++ = vidx[1] + 1;
|
nuclear@1
|
190 *idxarr++ = vidx[2] + 1;
|
nuclear@1
|
191 *idxarr++ = vidx[0] + 1;
|
nuclear@1
|
192 *idxarr++ = vidx[2] + 1;
|
nuclear@1
|
193 *idxarr++ = vidx[3] + 1;
|
nuclear@1
|
194 }
|
nuclear@1
|
195
|
nuclear@1
|
196 v += dv;
|
nuclear@1
|
197 }
|
nuclear@1
|
198 u += du;
|
nuclear@1
|
199 }
|
nuclear@1
|
200 }
|
nuclear@1
|
201
|
nuclear@1
|
202 // -------- cone --------
|
nuclear@1
|
203
|
nuclear@1
|
204 static Vector3 conevec(float theta, float y, float height)
|
nuclear@1
|
205 {
|
nuclear@1
|
206 float scale = 1.0 - y / height;
|
nuclear@1
|
207 return Vector3(sin(theta) * scale, y, cos(theta) * scale);
|
nuclear@1
|
208 }
|
nuclear@1
|
209
|
nuclear@1
|
210 void gen_cone(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
|
nuclear@1
|
211 {
|
nuclear@1
|
212 if(usub < 4) usub = 4;
|
nuclear@1
|
213 if(vsub < 1) vsub = 1;
|
nuclear@1
|
214
|
nuclear@1
|
215 int uverts = usub + 1;
|
nuclear@1
|
216 int vverts = vsub + 1;
|
nuclear@1
|
217
|
nuclear@1
|
218 int num_body_verts = uverts * vverts;
|
nuclear@1
|
219 int num_body_quads = usub * vsub;
|
nuclear@1
|
220 int num_body_tri = num_body_quads * 2;
|
nuclear@1
|
221
|
nuclear@1
|
222 int capvverts = capsub ? capsub + 1 : 0;
|
nuclear@1
|
223 int num_cap_verts = uverts * capvverts;
|
nuclear@1
|
224 int num_cap_quads = usub * capsub;
|
nuclear@1
|
225 int num_cap_tri = num_cap_quads * 2;
|
nuclear@1
|
226
|
nuclear@1
|
227 int num_verts = num_body_verts + num_cap_verts;
|
nuclear@1
|
228 int num_tri = num_body_tri + num_cap_tri;
|
nuclear@1
|
229
|
nuclear@1
|
230 mesh->clear();
|
nuclear@1
|
231 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
|
nuclear@1
|
232 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
|
nuclear@1
|
233 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
|
nuclear@1
|
234 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
|
nuclear@1
|
235 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
|
nuclear@1
|
236
|
nuclear@1
|
237 float du = urange / (float)(uverts - 1);
|
nuclear@1
|
238 float dv = vrange / (float)(vverts - 1);
|
nuclear@1
|
239
|
nuclear@1
|
240 float u = 0.0;
|
nuclear@1
|
241 for(int i=0; i<uverts; i++) {
|
nuclear@1
|
242 float theta = SURAD(u);
|
nuclear@1
|
243
|
nuclear@1
|
244 float v = 0.0;
|
nuclear@1
|
245 for(int j=0; j<vverts; j++) {
|
nuclear@1
|
246 float y = v * height;
|
nuclear@1
|
247 Vector3 pos = conevec(theta, y, height);
|
nuclear@1
|
248
|
nuclear@1
|
249 Vector3 tang = (conevec(theta + 0.1, 0.0, height) - conevec(theta - 0.1, 0.0, height)).normalized();
|
nuclear@1
|
250 Vector3 bitang = (conevec(theta, y + 0.1, height) - pos).normalized();
|
nuclear@1
|
251
|
nuclear@1
|
252 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
|
nuclear@1
|
253 *narr++ = cross_product(tang, bitang);
|
nuclear@1
|
254 *tarr++ = tang;
|
nuclear@1
|
255 *uvarr++ = Vector2(u * urange, v * vrange);
|
nuclear@1
|
256
|
nuclear@1
|
257 if(i < usub && j < vsub) {
|
nuclear@1
|
258 int idx = i * vverts + j;
|
nuclear@1
|
259
|
nuclear@1
|
260 *idxarr++ = idx;
|
nuclear@1
|
261 *idxarr++ = idx + vverts + 1;
|
nuclear@1
|
262 *idxarr++ = idx + 1;
|
nuclear@1
|
263
|
nuclear@1
|
264 *idxarr++ = idx;
|
nuclear@1
|
265 *idxarr++ = idx + vverts;
|
nuclear@1
|
266 *idxarr++ = idx + vverts + 1;
|
nuclear@1
|
267 }
|
nuclear@1
|
268
|
nuclear@1
|
269 v += dv;
|
nuclear@1
|
270 }
|
nuclear@1
|
271 u += du;
|
nuclear@1
|
272 }
|
nuclear@1
|
273
|
nuclear@1
|
274
|
nuclear@1
|
275 // now the bottom cap!
|
nuclear@1
|
276 if(!capsub) {
|
nuclear@1
|
277 return;
|
nuclear@1
|
278 }
|
nuclear@1
|
279
|
nuclear@1
|
280 dv = 1.0 / (float)(capvverts - 1);
|
nuclear@1
|
281
|
nuclear@1
|
282 u = 0.0;
|
nuclear@1
|
283 for(int i=0; i<uverts; i++) {
|
nuclear@1
|
284 float theta = SURAD(u);
|
nuclear@1
|
285
|
nuclear@1
|
286 float v = 0.0;
|
nuclear@1
|
287 for(int j=0; j<capvverts; j++) {
|
nuclear@1
|
288 float r = v * rad;
|
nuclear@1
|
289
|
nuclear@1
|
290 Vector3 pos = conevec(theta, 0.0, height) * r;
|
nuclear@1
|
291 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
|
nuclear@1
|
292
|
nuclear@1
|
293 *varr++ = pos;
|
nuclear@1
|
294 *narr++ = Vector3(0, -1, 0);
|
nuclear@1
|
295 *tarr++ = tang;
|
nuclear@1
|
296 *uvarr++ = Vector2(u * urange, v);
|
nuclear@1
|
297
|
nuclear@1
|
298 if(i < usub && j < capsub) {
|
nuclear@1
|
299 unsigned int idx = num_body_verts + i * capvverts + j;
|
nuclear@1
|
300
|
nuclear@1
|
301 unsigned int vidx[4] = {
|
nuclear@1
|
302 idx,
|
nuclear@1
|
303 idx + capvverts,
|
nuclear@1
|
304 idx + (capvverts + 1),
|
nuclear@1
|
305 idx + 1
|
nuclear@1
|
306 };
|
nuclear@1
|
307
|
nuclear@1
|
308 *idxarr++ = vidx[0];
|
nuclear@1
|
309 *idxarr++ = vidx[1];
|
nuclear@1
|
310 *idxarr++ = vidx[2];
|
nuclear@1
|
311 *idxarr++ = vidx[0];
|
nuclear@1
|
312 *idxarr++ = vidx[2];
|
nuclear@1
|
313 *idxarr++ = vidx[3];
|
nuclear@1
|
314 }
|
nuclear@1
|
315
|
nuclear@1
|
316 v += dv;
|
nuclear@1
|
317 }
|
nuclear@1
|
318 u += du;
|
nuclear@1
|
319 }
|
nuclear@1
|
320 }
|
nuclear@1
|
321
|
nuclear@1
|
322
|
nuclear@1
|
323 // -------- plane --------
|
nuclear@1
|
324
|
nuclear@1
|
325 void gen_plane(Mesh *mesh, float width, float height, int usub, int vsub)
|
nuclear@1
|
326 {
|
nuclear@1
|
327 gen_heightmap(mesh, width, height, usub, vsub, 0);
|
nuclear@1
|
328 }
|
nuclear@1
|
329
|
nuclear@1
|
330
|
nuclear@1
|
331 // ----- heightmap ------
|
nuclear@1
|
332
|
nuclear@1
|
333 void gen_heightmap(Mesh *mesh, float width, float height, int usub, int vsub, float (*hf)(float, float, void*), void *hfdata)
|
nuclear@1
|
334 {
|
nuclear@1
|
335 if(usub < 1) usub = 1;
|
nuclear@1
|
336 if(vsub < 1) vsub = 1;
|
nuclear@1
|
337
|
nuclear@1
|
338 mesh->clear();
|
nuclear@1
|
339
|
nuclear@1
|
340 int uverts = usub + 1;
|
nuclear@1
|
341 int vverts = vsub + 1;
|
nuclear@1
|
342 int num_verts = uverts * vverts;
|
nuclear@1
|
343
|
nuclear@1
|
344 int num_quads = usub * vsub;
|
nuclear@1
|
345 int num_tri = num_quads * 2;
|
nuclear@1
|
346
|
nuclear@1
|
347 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
|
nuclear@1
|
348 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
|
nuclear@1
|
349 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
|
nuclear@1
|
350 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
|
nuclear@1
|
351 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
|
nuclear@1
|
352
|
nuclear@1
|
353 float du = 1.0 / (float)usub;
|
nuclear@1
|
354 float dv = 1.0 / (float)vsub;
|
nuclear@1
|
355
|
nuclear@1
|
356 float u = 0.0;
|
nuclear@1
|
357 for(int i=0; i<uverts; i++) {
|
nuclear@1
|
358 float v = 0.0;
|
nuclear@1
|
359 for(int j=0; j<vverts; j++) {
|
nuclear@1
|
360 float x = (u - 0.5) * width;
|
nuclear@1
|
361 float y = (v - 0.5) * height;
|
nuclear@1
|
362 float z = hf ? hf(u, v, hfdata) : 0.0;
|
nuclear@1
|
363
|
nuclear@1
|
364 Vector3 normal = Vector3(0, 0, 1);
|
nuclear@1
|
365 if(hf) {
|
nuclear@1
|
366 float u1z = hf(u + du, v, hfdata);
|
nuclear@1
|
367 float v1z = hf(u, v + dv, hfdata);
|
nuclear@1
|
368
|
nuclear@1
|
369 Vector3 tang = Vector3(du * width, 0, u1z - z);
|
nuclear@1
|
370 Vector3 bitan = Vector3(0, dv * height, v1z - z);
|
nuclear@1
|
371 normal = cross_product(tang, bitan).normalized();
|
nuclear@1
|
372 }
|
nuclear@1
|
373
|
nuclear@1
|
374 *varr++ = Vector3(x, y, z);
|
nuclear@1
|
375 *narr++ = normal;
|
nuclear@1
|
376 *tarr++ = Vector3(1, 0, 0);
|
nuclear@1
|
377 *uvarr++ = Vector2(u, v);
|
nuclear@1
|
378
|
nuclear@1
|
379 if(i < usub && j < vsub) {
|
nuclear@1
|
380 int idx = i * vverts + j;
|
nuclear@1
|
381
|
nuclear@1
|
382 *idxarr++ = idx;
|
nuclear@1
|
383 *idxarr++ = idx + vverts + 1;
|
nuclear@1
|
384 *idxarr++ = idx + 1;
|
nuclear@1
|
385
|
nuclear@1
|
386 *idxarr++ = idx;
|
nuclear@1
|
387 *idxarr++ = idx + vverts;
|
nuclear@1
|
388 *idxarr++ = idx + vverts + 1;
|
nuclear@1
|
389 }
|
nuclear@1
|
390
|
nuclear@1
|
391 v += dv;
|
nuclear@1
|
392 }
|
nuclear@1
|
393 u += du;
|
nuclear@1
|
394 }
|
nuclear@1
|
395 }
|
nuclear@1
|
396
|
nuclear@1
|
397 // ----- heightmap ------
|
nuclear@1
|
398
|
nuclear@1
|
399 void gen_box(Mesh *mesh, float xsz, float ysz, float zsz)
|
nuclear@1
|
400 {
|
nuclear@1
|
401 mesh->clear();
|
nuclear@1
|
402
|
nuclear@1
|
403 const int num_faces = 6;
|
nuclear@1
|
404 int num_verts = num_faces * 4;
|
nuclear@1
|
405 int num_tri = num_faces * 2;
|
nuclear@1
|
406
|
nuclear@1
|
407 float x = xsz / 2.0;
|
nuclear@1
|
408 float y = ysz / 2.0;
|
nuclear@1
|
409 float z = zsz / 2.0;
|
nuclear@1
|
410
|
nuclear@1
|
411 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
|
nuclear@1
|
412 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
|
nuclear@1
|
413 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
|
nuclear@1
|
414 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
|
nuclear@1
|
415 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
|
nuclear@1
|
416
|
nuclear@1
|
417 static const Vector2 uv[] = { Vector2(0, 0), Vector2(1, 0), Vector2(1, 1), Vector2(0, 1) };
|
nuclear@1
|
418
|
nuclear@1
|
419 // front
|
nuclear@1
|
420 for(int i=0; i<4; i++) {
|
nuclear@1
|
421 *narr++ = Vector3(0, 0, 1);
|
nuclear@1
|
422 *tarr++ = Vector3(1, 0, 0);
|
nuclear@1
|
423 *uvarr++ = uv[i];
|
nuclear@1
|
424 }
|
nuclear@1
|
425 *varr++ = Vector3(-x, -y, z);
|
nuclear@1
|
426 *varr++ = Vector3(x, -y, z);
|
nuclear@1
|
427 *varr++ = Vector3(x, y, z);
|
nuclear@1
|
428 *varr++ = Vector3(-x, y, z);
|
nuclear@1
|
429 // right
|
nuclear@1
|
430 for(int i=0; i<4; i++) {
|
nuclear@1
|
431 *narr++ = Vector3(1, 0, 0);
|
nuclear@1
|
432 *tarr++ = Vector3(0, 0, -1);
|
nuclear@1
|
433 *uvarr++ = uv[i];
|
nuclear@1
|
434 }
|
nuclear@1
|
435 *varr++ = Vector3(x, -y, z);
|
nuclear@1
|
436 *varr++ = Vector3(x, -y, -z);
|
nuclear@1
|
437 *varr++ = Vector3(x, y, -z);
|
nuclear@1
|
438 *varr++ = Vector3(x, y, z);
|
nuclear@1
|
439 // back
|
nuclear@1
|
440 for(int i=0; i<4; i++) {
|
nuclear@1
|
441 *narr++ = Vector3(0, 0, -1);
|
nuclear@1
|
442 *tarr++ = Vector3(-1, 0, 0);
|
nuclear@1
|
443 *uvarr++ = uv[i];
|
nuclear@1
|
444 }
|
nuclear@1
|
445 *varr++ = Vector3(x, -y, -z);
|
nuclear@1
|
446 *varr++ = Vector3(-x, -y, -z);
|
nuclear@1
|
447 *varr++ = Vector3(-x, y, -z);
|
nuclear@1
|
448 *varr++ = Vector3(x, y, -z);
|
nuclear@1
|
449 // left
|
nuclear@1
|
450 for(int i=0; i<4; i++) {
|
nuclear@1
|
451 *narr++ = Vector3(-1, 0, 0);
|
nuclear@1
|
452 *tarr++ = Vector3(0, 0, 1);
|
nuclear@1
|
453 *uvarr++ = uv[i];
|
nuclear@1
|
454 }
|
nuclear@1
|
455 *varr++ = Vector3(-x, -y, -z);
|
nuclear@1
|
456 *varr++ = Vector3(-x, -y, z);
|
nuclear@1
|
457 *varr++ = Vector3(-x, y, z);
|
nuclear@1
|
458 *varr++ = Vector3(-x, y, -z);
|
nuclear@1
|
459 // top
|
nuclear@1
|
460 for(int i=0; i<4; i++) {
|
nuclear@1
|
461 *narr++ = Vector3(0, 1, 0);
|
nuclear@1
|
462 *tarr++ = Vector3(1, 0, 0);
|
nuclear@1
|
463 *uvarr++ = uv[i];
|
nuclear@1
|
464 }
|
nuclear@1
|
465 *varr++ = Vector3(-x, y, z);
|
nuclear@1
|
466 *varr++ = Vector3(x, y, z);
|
nuclear@1
|
467 *varr++ = Vector3(x, y, -z);
|
nuclear@1
|
468 *varr++ = Vector3(-x, y, -z);
|
nuclear@1
|
469 // bottom
|
nuclear@1
|
470 for(int i=0; i<4; i++) {
|
nuclear@1
|
471 *narr++ = Vector3(0, -1, 0);
|
nuclear@1
|
472 *tarr++ = Vector3(1, 0, 0);
|
nuclear@1
|
473 *uvarr++ = uv[i];
|
nuclear@1
|
474 }
|
nuclear@1
|
475 *varr++ = Vector3(-x, -y, -z);
|
nuclear@1
|
476 *varr++ = Vector3(x, -y, -z);
|
nuclear@1
|
477 *varr++ = Vector3(x, -y, z);
|
nuclear@1
|
478 *varr++ = Vector3(-x, -y, z);
|
nuclear@1
|
479
|
nuclear@1
|
480 // index array
|
nuclear@1
|
481 static const int faceidx[] = {0, 1, 2, 0, 2, 3};
|
nuclear@1
|
482 for(int i=0; i<num_faces; i++) {
|
nuclear@1
|
483 for(int j=0; j<6; j++) {
|
nuclear@1
|
484 *idxarr++ = faceidx[j] + i * 4;
|
nuclear@1
|
485 }
|
nuclear@1
|
486 }
|
nuclear@1
|
487 }
|
nuclear@11
|
488
|
nuclear@11
|
489 static inline Vector3 rev_vert(float u, float v, Vector2 (*rf)(float, float, void*), void *cls)
|
nuclear@11
|
490 {
|
nuclear@11
|
491 Vector2 pos = rf(u, v, cls);
|
nuclear@11
|
492
|
nuclear@11
|
493 float angle = u * 2.0 * M_PI;
|
nuclear@11
|
494 float x = pos.x * cos(angle);
|
nuclear@11
|
495 float y = pos.y;
|
nuclear@11
|
496 float z = pos.x * sin(angle);
|
nuclear@11
|
497
|
nuclear@11
|
498 return Vector3(x, y, z);
|
nuclear@11
|
499 }
|
nuclear@11
|
500
|
nuclear@11
|
501 // ------ surface of revolution -------
|
nuclear@11
|
502 void gen_revol(Mesh *mesh, int usub, int vsub, Vector2 (*rfunc)(float, float, void*), void *cls)
|
nuclear@11
|
503 {
|
nuclear@12
|
504 gen_revol(mesh, usub, vsub, rfunc, 0, cls);
|
nuclear@12
|
505 }
|
nuclear@12
|
506
|
nuclear@12
|
507 void gen_revol(Mesh *mesh, int usub, int vsub, Vector2 (*rfunc)(float, float, void*),
|
nuclear@12
|
508 Vector2 (*nfunc)(float, float, void*), void *cls)
|
nuclear@12
|
509 {
|
nuclear@11
|
510 if(!rfunc) return;
|
nuclear@11
|
511 if(usub < 3) usub = 3;
|
nuclear@11
|
512 if(vsub < 1) vsub = 1;
|
nuclear@11
|
513
|
nuclear@11
|
514 mesh->clear();
|
nuclear@11
|
515
|
nuclear@11
|
516 int uverts = usub + 1;
|
nuclear@11
|
517 int vverts = vsub + 1;
|
nuclear@11
|
518 int num_verts = uverts * vverts;
|
nuclear@11
|
519
|
nuclear@11
|
520 int num_quads = usub * vsub;
|
nuclear@11
|
521 int num_tri = num_quads * 2;
|
nuclear@11
|
522
|
nuclear@11
|
523 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
|
nuclear@11
|
524 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
|
nuclear@11
|
525 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
|
nuclear@11
|
526 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
|
nuclear@11
|
527 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
|
nuclear@11
|
528
|
nuclear@11
|
529 float du = 1.0 / (float)(uverts - 1);
|
nuclear@11
|
530 float dv = 1.0 / (float)(vverts - 1);
|
nuclear@11
|
531
|
nuclear@11
|
532 float u = 0.0;
|
nuclear@11
|
533 for(int i=0; i<uverts; i++) {
|
nuclear@11
|
534 float v = 0.0;
|
nuclear@11
|
535 for(int j=0; j<vverts; j++) {
|
nuclear@11
|
536 Vector3 pos = rev_vert(u, v, rfunc, cls);
|
nuclear@11
|
537
|
nuclear@11
|
538 Vector3 nextu = rev_vert(fmod(u + du, 1.0), v, rfunc, cls);
|
nuclear@11
|
539 Vector3 tang = nextu - pos;
|
nuclear@11
|
540 if(tang.length_sq() < 1e-6) {
|
nuclear@11
|
541 float new_v = v > 0.5 ? v - dv * 0.25 : v + dv * 0.25;
|
nuclear@11
|
542 nextu = rev_vert(fmod(u + du, 1.0), new_v, rfunc, cls);
|
nuclear@11
|
543 tang = nextu - pos;
|
nuclear@11
|
544 }
|
nuclear@11
|
545
|
nuclear@12
|
546 Vector3 normal;
|
nuclear@12
|
547 if(nfunc) {
|
nuclear@12
|
548 normal = rev_vert(u, v, nfunc, cls);
|
nuclear@12
|
549 } else {
|
nuclear@12
|
550 Vector3 nextv = rev_vert(u, v + dv, rfunc, cls);
|
nuclear@12
|
551 Vector3 bitan = nextv - pos;
|
nuclear@12
|
552 if(bitan.length_sq() < 1e-6) {
|
nuclear@12
|
553 nextv = rev_vert(u, v - dv, rfunc, cls);
|
nuclear@12
|
554 bitan = pos - nextv;
|
nuclear@12
|
555 }
|
nuclear@12
|
556
|
nuclear@12
|
557 normal = cross_product(tang, bitan);
|
nuclear@11
|
558 }
|
nuclear@11
|
559
|
nuclear@11
|
560 *varr++ = pos;
|
nuclear@11
|
561 *narr++ = normal.normalized();
|
nuclear@11
|
562 *tarr++ = tang.normalized();
|
nuclear@11
|
563 *uvarr++ = Vector2(u, v);
|
nuclear@11
|
564
|
nuclear@11
|
565 if(i < usub && j < vsub) {
|
nuclear@11
|
566 int idx = i * vverts + j;
|
nuclear@11
|
567
|
nuclear@11
|
568 *idxarr++ = idx;
|
nuclear@11
|
569 *idxarr++ = idx + vverts + 1;
|
nuclear@11
|
570 *idxarr++ = idx + 1;
|
nuclear@11
|
571
|
nuclear@11
|
572 *idxarr++ = idx;
|
nuclear@11
|
573 *idxarr++ = idx + vverts;
|
nuclear@11
|
574 *idxarr++ = idx + vverts + 1;
|
nuclear@11
|
575 }
|
nuclear@11
|
576
|
nuclear@11
|
577 v += dv;
|
nuclear@11
|
578 }
|
nuclear@11
|
579 u += du;
|
nuclear@11
|
580 }
|
nuclear@11
|
581 }
|