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 }
|