3dphotoshoot
view src/meshgen.cc @ 25:ac80210d5fbe
preparing a pc version for easier development of non-android-specifics
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 18 Jun 2015 03:12:30 +0300 |
parents | |
children |
line source
1 #include <stdio.h>
2 #include "meshgen.h"
3 #include "mesh.h"
5 // -------- sphere --------
7 #define SURAD(u) ((u) * 2.0 * M_PI)
8 #define SVRAD(v) ((v) * M_PI)
10 static Vector3 sphvec(float theta, float phi)
11 {
12 return Vector3(sin(theta) * sin(phi),
13 cos(phi),
14 cos(theta) * sin(phi));
15 }
17 void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float vrange)
18 {
19 if(usub < 4) usub = 4;
20 if(vsub < 2) vsub = 2;
22 int uverts = usub + 1;
23 int vverts = vsub + 1;
25 int num_verts = uverts * vverts;
26 int num_quads = usub * vsub;
27 int num_tri = num_quads * 2;
29 mesh->clear();
30 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
31 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
32 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
33 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
34 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
36 float du = urange / (float)(uverts - 1);
37 float dv = vrange / (float)(vverts - 1);
39 float u = 0.0;
40 for(int i=0; i<uverts; i++) {
41 float theta = SURAD(u * urange);
43 float v = 0.0;
44 for(int j=0; j<vverts; j++) {
45 float phi = SVRAD(v * vrange);
47 Vector3 pos = sphvec(theta, phi);
49 *varr++ = pos * rad;
50 *narr++ = pos;
51 *tarr++ = (sphvec(theta + 0.1f, (float)M_PI / 2.0f) - sphvec(theta - 0.1f, (float)M_PI / 2.0f)).normalized();
52 *uvarr++ = Vector2(u * urange, v * vrange);
54 if(i < usub && j < vsub) {
55 int idx = i * vverts + j;
56 *idxarr++ = idx;
57 *idxarr++ = idx + 1;
58 *idxarr++ = idx + vverts + 1;
60 *idxarr++ = idx;
61 *idxarr++ = idx + vverts + 1;
62 *idxarr++ = idx + vverts;
63 }
65 v += dv;
66 }
67 u += du;
68 }
69 }
72 // -------- cylinder --------
74 static Vector3 cylvec(float theta, float height)
75 {
76 return Vector3(sin(theta), height, cos(theta));
77 }
79 void gen_cylinder(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
80 {
81 if(usub < 4) usub = 4;
82 if(vsub < 1) vsub = 1;
84 int uverts = usub + 1;
85 int vverts = vsub + 1;
87 int num_body_verts = uverts * vverts;
88 int num_body_quads = usub * vsub;
89 int num_body_tri = num_body_quads * 2;
91 int capvverts = capsub ? capsub + 1 : 0;
92 int num_cap_verts = uverts * capvverts;
93 int num_cap_quads = usub * capsub;
94 int num_cap_tri = num_cap_quads * 2;
96 int num_verts = num_body_verts + num_cap_verts * 2;
97 int num_tri = num_body_tri + num_cap_tri * 2;
99 mesh->clear();
100 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
101 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
102 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
103 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
104 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
106 float du = urange / (float)(uverts - 1);
107 float dv = vrange / (float)(vverts - 1);
109 float u = 0.0;
110 for(int i=0; i<uverts; i++) {
111 float theta = SURAD(u);
113 float v = 0.0;
114 for(int j=0; j<vverts; j++) {
115 float y = (v - 0.5) * height;
116 Vector3 pos = cylvec(theta, y);
118 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
119 *narr++ = Vector3(pos.x, 0.0, pos.z);
120 *tarr++ = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
121 *uvarr++ = Vector2(u * urange, v * vrange);
123 if(i < usub && j < vsub) {
124 int idx = i * vverts + j;
126 *idxarr++ = idx;
127 *idxarr++ = idx + vverts + 1;
128 *idxarr++ = idx + 1;
130 *idxarr++ = idx;
131 *idxarr++ = idx + vverts;
132 *idxarr++ = idx + vverts + 1;
133 }
135 v += dv;
136 }
137 u += du;
138 }
141 // now the cap!
142 if(!capsub) {
143 return;
144 }
146 dv = 1.0 / (float)(capvverts - 1);
148 u = 0.0;
149 for(int i=0; i<uverts; i++) {
150 float theta = SURAD(u);
152 float v = 0.0;
153 for(int j=0; j<capvverts; j++) {
154 float r = v * rad;
156 Vector3 pos = cylvec(theta, height / 2.0) * r;
157 pos.y = height / 2.0;
158 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
160 *varr++ = pos;
161 *narr++ = Vector3(0, 1, 0);
162 *tarr++ = tang;
163 *uvarr++ = Vector2(u * urange, v);
165 pos.y = -height / 2.0;
166 *varr++ = pos;
167 *narr++ = Vector3(0, -1, 0);
168 *tarr++ = -tang;
169 *uvarr++ = Vector2(u * urange, v);
171 if(i < usub && j < capsub) {
172 unsigned int idx = num_body_verts + (i * capvverts + j) * 2;
174 unsigned int vidx[4] = {
175 idx,
176 idx + capvverts * 2,
177 idx + (capvverts + 1) * 2,
178 idx + 2
179 };
181 *idxarr++ = vidx[0];
182 *idxarr++ = vidx[2];
183 *idxarr++ = vidx[1];
184 *idxarr++ = vidx[0];
185 *idxarr++ = vidx[3];
186 *idxarr++ = vidx[2];
188 *idxarr++ = vidx[0] + 1;
189 *idxarr++ = vidx[1] + 1;
190 *idxarr++ = vidx[2] + 1;
191 *idxarr++ = vidx[0] + 1;
192 *idxarr++ = vidx[2] + 1;
193 *idxarr++ = vidx[3] + 1;
194 }
196 v += dv;
197 }
198 u += du;
199 }
200 }
202 // -------- cone --------
204 static Vector3 conevec(float theta, float y, float height)
205 {
206 float scale = 1.0 - y / height;
207 return Vector3(sin(theta) * scale, y, cos(theta) * scale);
208 }
210 void gen_cone(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
211 {
212 if(usub < 4) usub = 4;
213 if(vsub < 1) vsub = 1;
215 int uverts = usub + 1;
216 int vverts = vsub + 1;
218 int num_body_verts = uverts * vverts;
219 int num_body_quads = usub * vsub;
220 int num_body_tri = num_body_quads * 2;
222 int capvverts = capsub ? capsub + 1 : 0;
223 int num_cap_verts = uverts * capvverts;
224 int num_cap_quads = usub * capsub;
225 int num_cap_tri = num_cap_quads * 2;
227 int num_verts = num_body_verts + num_cap_verts;
228 int num_tri = num_body_tri + num_cap_tri;
230 mesh->clear();
231 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
232 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
233 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
234 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
235 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
237 float du = urange / (float)(uverts - 1);
238 float dv = vrange / (float)(vverts - 1);
240 float u = 0.0;
241 for(int i=0; i<uverts; i++) {
242 float theta = SURAD(u);
244 float v = 0.0;
245 for(int j=0; j<vverts; j++) {
246 float y = v * height;
247 Vector3 pos = conevec(theta, y, height);
249 Vector3 tang = (conevec(theta + 0.1, 0.0, height) - conevec(theta - 0.1, 0.0, height)).normalized();
250 Vector3 bitang = (conevec(theta, y + 0.1, height) - pos).normalized();
252 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
253 *narr++ = cross_product(tang, bitang);
254 *tarr++ = tang;
255 *uvarr++ = Vector2(u * urange, v * vrange);
257 if(i < usub && j < vsub) {
258 int idx = i * vverts + j;
260 *idxarr++ = idx;
261 *idxarr++ = idx + vverts + 1;
262 *idxarr++ = idx + 1;
264 *idxarr++ = idx;
265 *idxarr++ = idx + vverts;
266 *idxarr++ = idx + vverts + 1;
267 }
269 v += dv;
270 }
271 u += du;
272 }
275 // now the bottom cap!
276 if(!capsub) {
277 return;
278 }
280 dv = 1.0 / (float)(capvverts - 1);
282 u = 0.0;
283 for(int i=0; i<uverts; i++) {
284 float theta = SURAD(u);
286 float v = 0.0;
287 for(int j=0; j<capvverts; j++) {
288 float r = v * rad;
290 Vector3 pos = conevec(theta, 0.0, height) * r;
291 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
293 *varr++ = pos;
294 *narr++ = Vector3(0, -1, 0);
295 *tarr++ = tang;
296 *uvarr++ = Vector2(u * urange, v);
298 if(i < usub && j < capsub) {
299 unsigned int idx = num_body_verts + i * capvverts + j;
301 unsigned int vidx[4] = {
302 idx,
303 idx + capvverts,
304 idx + (capvverts + 1),
305 idx + 1
306 };
308 *idxarr++ = vidx[0];
309 *idxarr++ = vidx[1];
310 *idxarr++ = vidx[2];
311 *idxarr++ = vidx[0];
312 *idxarr++ = vidx[2];
313 *idxarr++ = vidx[3];
314 }
316 v += dv;
317 }
318 u += du;
319 }
320 }
323 // -------- plane --------
325 void gen_plane(Mesh *mesh, float width, float height, int usub, int vsub)
326 {
327 gen_heightmap(mesh, width, height, usub, vsub, 0);
328 }
331 // ----- heightmap ------
333 void gen_heightmap(Mesh *mesh, float width, float height, int usub, int vsub, float (*hf)(float, float, void*), void *hfdata)
334 {
335 if(usub < 1) usub = 1;
336 if(vsub < 1) vsub = 1;
338 mesh->clear();
340 int uverts = usub + 1;
341 int vverts = vsub + 1;
342 int num_verts = uverts * vverts;
344 int num_quads = usub * vsub;
345 int num_tri = num_quads * 2;
347 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
348 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
349 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
350 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
351 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
353 float du = 1.0 / (float)usub;
354 float dv = 1.0 / (float)vsub;
356 float u = 0.0;
357 for(int i=0; i<uverts; i++) {
358 float v = 0.0;
359 for(int j=0; j<vverts; j++) {
360 float x = (u - 0.5) * width;
361 float y = (v - 0.5) * height;
362 float z = hf ? hf(u, v, hfdata) : 0.0;
364 Vector3 normal = Vector3(0, 0, 1);
365 if(hf) {
366 float u1z = hf(u + du, v, hfdata);
367 float v1z = hf(u, v + dv, hfdata);
369 Vector3 tang = Vector3(du * width, 0, u1z - z);
370 Vector3 bitan = Vector3(0, dv * height, v1z - z);
371 normal = cross_product(tang, bitan).normalized();
372 }
374 *varr++ = Vector3(x, y, z);
375 *narr++ = normal;
376 *tarr++ = Vector3(1, 0, 0);
377 *uvarr++ = Vector2(u, v);
379 if(i < usub && j < vsub) {
380 int idx = i * vverts + j;
382 *idxarr++ = idx;
383 *idxarr++ = idx + vverts + 1;
384 *idxarr++ = idx + 1;
386 *idxarr++ = idx;
387 *idxarr++ = idx + vverts;
388 *idxarr++ = idx + vverts + 1;
389 }
391 v += dv;
392 }
393 u += du;
394 }
395 }