3dphotoshoot

annotate 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
rev   line source
nuclear@25 1 #include <stdio.h>
nuclear@25 2 #include "meshgen.h"
nuclear@25 3 #include "mesh.h"
nuclear@25 4
nuclear@25 5 // -------- sphere --------
nuclear@25 6
nuclear@25 7 #define SURAD(u) ((u) * 2.0 * M_PI)
nuclear@25 8 #define SVRAD(v) ((v) * M_PI)
nuclear@25 9
nuclear@25 10 static Vector3 sphvec(float theta, float phi)
nuclear@25 11 {
nuclear@25 12 return Vector3(sin(theta) * sin(phi),
nuclear@25 13 cos(phi),
nuclear@25 14 cos(theta) * sin(phi));
nuclear@25 15 }
nuclear@25 16
nuclear@25 17 void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float vrange)
nuclear@25 18 {
nuclear@25 19 if(usub < 4) usub = 4;
nuclear@25 20 if(vsub < 2) vsub = 2;
nuclear@25 21
nuclear@25 22 int uverts = usub + 1;
nuclear@25 23 int vverts = vsub + 1;
nuclear@25 24
nuclear@25 25 int num_verts = uverts * vverts;
nuclear@25 26 int num_quads = usub * vsub;
nuclear@25 27 int num_tri = num_quads * 2;
nuclear@25 28
nuclear@25 29 mesh->clear();
nuclear@25 30 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
nuclear@25 31 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
nuclear@25 32 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
nuclear@25 33 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
nuclear@25 34 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
nuclear@25 35
nuclear@25 36 float du = urange / (float)(uverts - 1);
nuclear@25 37 float dv = vrange / (float)(vverts - 1);
nuclear@25 38
nuclear@25 39 float u = 0.0;
nuclear@25 40 for(int i=0; i<uverts; i++) {
nuclear@25 41 float theta = SURAD(u * urange);
nuclear@25 42
nuclear@25 43 float v = 0.0;
nuclear@25 44 for(int j=0; j<vverts; j++) {
nuclear@25 45 float phi = SVRAD(v * vrange);
nuclear@25 46
nuclear@25 47 Vector3 pos = sphvec(theta, phi);
nuclear@25 48
nuclear@25 49 *varr++ = pos * rad;
nuclear@25 50 *narr++ = pos;
nuclear@25 51 *tarr++ = (sphvec(theta + 0.1f, (float)M_PI / 2.0f) - sphvec(theta - 0.1f, (float)M_PI / 2.0f)).normalized();
nuclear@25 52 *uvarr++ = Vector2(u * urange, v * vrange);
nuclear@25 53
nuclear@25 54 if(i < usub && j < vsub) {
nuclear@25 55 int idx = i * vverts + j;
nuclear@25 56 *idxarr++ = idx;
nuclear@25 57 *idxarr++ = idx + 1;
nuclear@25 58 *idxarr++ = idx + vverts + 1;
nuclear@25 59
nuclear@25 60 *idxarr++ = idx;
nuclear@25 61 *idxarr++ = idx + vverts + 1;
nuclear@25 62 *idxarr++ = idx + vverts;
nuclear@25 63 }
nuclear@25 64
nuclear@25 65 v += dv;
nuclear@25 66 }
nuclear@25 67 u += du;
nuclear@25 68 }
nuclear@25 69 }
nuclear@25 70
nuclear@25 71
nuclear@25 72 // -------- cylinder --------
nuclear@25 73
nuclear@25 74 static Vector3 cylvec(float theta, float height)
nuclear@25 75 {
nuclear@25 76 return Vector3(sin(theta), height, cos(theta));
nuclear@25 77 }
nuclear@25 78
nuclear@25 79 void gen_cylinder(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
nuclear@25 80 {
nuclear@25 81 if(usub < 4) usub = 4;
nuclear@25 82 if(vsub < 1) vsub = 1;
nuclear@25 83
nuclear@25 84 int uverts = usub + 1;
nuclear@25 85 int vverts = vsub + 1;
nuclear@25 86
nuclear@25 87 int num_body_verts = uverts * vverts;
nuclear@25 88 int num_body_quads = usub * vsub;
nuclear@25 89 int num_body_tri = num_body_quads * 2;
nuclear@25 90
nuclear@25 91 int capvverts = capsub ? capsub + 1 : 0;
nuclear@25 92 int num_cap_verts = uverts * capvverts;
nuclear@25 93 int num_cap_quads = usub * capsub;
nuclear@25 94 int num_cap_tri = num_cap_quads * 2;
nuclear@25 95
nuclear@25 96 int num_verts = num_body_verts + num_cap_verts * 2;
nuclear@25 97 int num_tri = num_body_tri + num_cap_tri * 2;
nuclear@25 98
nuclear@25 99 mesh->clear();
nuclear@25 100 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
nuclear@25 101 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
nuclear@25 102 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
nuclear@25 103 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
nuclear@25 104 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
nuclear@25 105
nuclear@25 106 float du = urange / (float)(uverts - 1);
nuclear@25 107 float dv = vrange / (float)(vverts - 1);
nuclear@25 108
nuclear@25 109 float u = 0.0;
nuclear@25 110 for(int i=0; i<uverts; i++) {
nuclear@25 111 float theta = SURAD(u);
nuclear@25 112
nuclear@25 113 float v = 0.0;
nuclear@25 114 for(int j=0; j<vverts; j++) {
nuclear@25 115 float y = (v - 0.5) * height;
nuclear@25 116 Vector3 pos = cylvec(theta, y);
nuclear@25 117
nuclear@25 118 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
nuclear@25 119 *narr++ = Vector3(pos.x, 0.0, pos.z);
nuclear@25 120 *tarr++ = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
nuclear@25 121 *uvarr++ = Vector2(u * urange, v * vrange);
nuclear@25 122
nuclear@25 123 if(i < usub && j < vsub) {
nuclear@25 124 int idx = i * vverts + j;
nuclear@25 125
nuclear@25 126 *idxarr++ = idx;
nuclear@25 127 *idxarr++ = idx + vverts + 1;
nuclear@25 128 *idxarr++ = idx + 1;
nuclear@25 129
nuclear@25 130 *idxarr++ = idx;
nuclear@25 131 *idxarr++ = idx + vverts;
nuclear@25 132 *idxarr++ = idx + vverts + 1;
nuclear@25 133 }
nuclear@25 134
nuclear@25 135 v += dv;
nuclear@25 136 }
nuclear@25 137 u += du;
nuclear@25 138 }
nuclear@25 139
nuclear@25 140
nuclear@25 141 // now the cap!
nuclear@25 142 if(!capsub) {
nuclear@25 143 return;
nuclear@25 144 }
nuclear@25 145
nuclear@25 146 dv = 1.0 / (float)(capvverts - 1);
nuclear@25 147
nuclear@25 148 u = 0.0;
nuclear@25 149 for(int i=0; i<uverts; i++) {
nuclear@25 150 float theta = SURAD(u);
nuclear@25 151
nuclear@25 152 float v = 0.0;
nuclear@25 153 for(int j=0; j<capvverts; j++) {
nuclear@25 154 float r = v * rad;
nuclear@25 155
nuclear@25 156 Vector3 pos = cylvec(theta, height / 2.0) * r;
nuclear@25 157 pos.y = height / 2.0;
nuclear@25 158 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
nuclear@25 159
nuclear@25 160 *varr++ = pos;
nuclear@25 161 *narr++ = Vector3(0, 1, 0);
nuclear@25 162 *tarr++ = tang;
nuclear@25 163 *uvarr++ = Vector2(u * urange, v);
nuclear@25 164
nuclear@25 165 pos.y = -height / 2.0;
nuclear@25 166 *varr++ = pos;
nuclear@25 167 *narr++ = Vector3(0, -1, 0);
nuclear@25 168 *tarr++ = -tang;
nuclear@25 169 *uvarr++ = Vector2(u * urange, v);
nuclear@25 170
nuclear@25 171 if(i < usub && j < capsub) {
nuclear@25 172 unsigned int idx = num_body_verts + (i * capvverts + j) * 2;
nuclear@25 173
nuclear@25 174 unsigned int vidx[4] = {
nuclear@25 175 idx,
nuclear@25 176 idx + capvverts * 2,
nuclear@25 177 idx + (capvverts + 1) * 2,
nuclear@25 178 idx + 2
nuclear@25 179 };
nuclear@25 180
nuclear@25 181 *idxarr++ = vidx[0];
nuclear@25 182 *idxarr++ = vidx[2];
nuclear@25 183 *idxarr++ = vidx[1];
nuclear@25 184 *idxarr++ = vidx[0];
nuclear@25 185 *idxarr++ = vidx[3];
nuclear@25 186 *idxarr++ = vidx[2];
nuclear@25 187
nuclear@25 188 *idxarr++ = vidx[0] + 1;
nuclear@25 189 *idxarr++ = vidx[1] + 1;
nuclear@25 190 *idxarr++ = vidx[2] + 1;
nuclear@25 191 *idxarr++ = vidx[0] + 1;
nuclear@25 192 *idxarr++ = vidx[2] + 1;
nuclear@25 193 *idxarr++ = vidx[3] + 1;
nuclear@25 194 }
nuclear@25 195
nuclear@25 196 v += dv;
nuclear@25 197 }
nuclear@25 198 u += du;
nuclear@25 199 }
nuclear@25 200 }
nuclear@25 201
nuclear@25 202 // -------- cone --------
nuclear@25 203
nuclear@25 204 static Vector3 conevec(float theta, float y, float height)
nuclear@25 205 {
nuclear@25 206 float scale = 1.0 - y / height;
nuclear@25 207 return Vector3(sin(theta) * scale, y, cos(theta) * scale);
nuclear@25 208 }
nuclear@25 209
nuclear@25 210 void gen_cone(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
nuclear@25 211 {
nuclear@25 212 if(usub < 4) usub = 4;
nuclear@25 213 if(vsub < 1) vsub = 1;
nuclear@25 214
nuclear@25 215 int uverts = usub + 1;
nuclear@25 216 int vverts = vsub + 1;
nuclear@25 217
nuclear@25 218 int num_body_verts = uverts * vverts;
nuclear@25 219 int num_body_quads = usub * vsub;
nuclear@25 220 int num_body_tri = num_body_quads * 2;
nuclear@25 221
nuclear@25 222 int capvverts = capsub ? capsub + 1 : 0;
nuclear@25 223 int num_cap_verts = uverts * capvverts;
nuclear@25 224 int num_cap_quads = usub * capsub;
nuclear@25 225 int num_cap_tri = num_cap_quads * 2;
nuclear@25 226
nuclear@25 227 int num_verts = num_body_verts + num_cap_verts;
nuclear@25 228 int num_tri = num_body_tri + num_cap_tri;
nuclear@25 229
nuclear@25 230 mesh->clear();
nuclear@25 231 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
nuclear@25 232 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
nuclear@25 233 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
nuclear@25 234 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
nuclear@25 235 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
nuclear@25 236
nuclear@25 237 float du = urange / (float)(uverts - 1);
nuclear@25 238 float dv = vrange / (float)(vverts - 1);
nuclear@25 239
nuclear@25 240 float u = 0.0;
nuclear@25 241 for(int i=0; i<uverts; i++) {
nuclear@25 242 float theta = SURAD(u);
nuclear@25 243
nuclear@25 244 float v = 0.0;
nuclear@25 245 for(int j=0; j<vverts; j++) {
nuclear@25 246 float y = v * height;
nuclear@25 247 Vector3 pos = conevec(theta, y, height);
nuclear@25 248
nuclear@25 249 Vector3 tang = (conevec(theta + 0.1, 0.0, height) - conevec(theta - 0.1, 0.0, height)).normalized();
nuclear@25 250 Vector3 bitang = (conevec(theta, y + 0.1, height) - pos).normalized();
nuclear@25 251
nuclear@25 252 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
nuclear@25 253 *narr++ = cross_product(tang, bitang);
nuclear@25 254 *tarr++ = tang;
nuclear@25 255 *uvarr++ = Vector2(u * urange, v * vrange);
nuclear@25 256
nuclear@25 257 if(i < usub && j < vsub) {
nuclear@25 258 int idx = i * vverts + j;
nuclear@25 259
nuclear@25 260 *idxarr++ = idx;
nuclear@25 261 *idxarr++ = idx + vverts + 1;
nuclear@25 262 *idxarr++ = idx + 1;
nuclear@25 263
nuclear@25 264 *idxarr++ = idx;
nuclear@25 265 *idxarr++ = idx + vverts;
nuclear@25 266 *idxarr++ = idx + vverts + 1;
nuclear@25 267 }
nuclear@25 268
nuclear@25 269 v += dv;
nuclear@25 270 }
nuclear@25 271 u += du;
nuclear@25 272 }
nuclear@25 273
nuclear@25 274
nuclear@25 275 // now the bottom cap!
nuclear@25 276 if(!capsub) {
nuclear@25 277 return;
nuclear@25 278 }
nuclear@25 279
nuclear@25 280 dv = 1.0 / (float)(capvverts - 1);
nuclear@25 281
nuclear@25 282 u = 0.0;
nuclear@25 283 for(int i=0; i<uverts; i++) {
nuclear@25 284 float theta = SURAD(u);
nuclear@25 285
nuclear@25 286 float v = 0.0;
nuclear@25 287 for(int j=0; j<capvverts; j++) {
nuclear@25 288 float r = v * rad;
nuclear@25 289
nuclear@25 290 Vector3 pos = conevec(theta, 0.0, height) * r;
nuclear@25 291 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
nuclear@25 292
nuclear@25 293 *varr++ = pos;
nuclear@25 294 *narr++ = Vector3(0, -1, 0);
nuclear@25 295 *tarr++ = tang;
nuclear@25 296 *uvarr++ = Vector2(u * urange, v);
nuclear@25 297
nuclear@25 298 if(i < usub && j < capsub) {
nuclear@25 299 unsigned int idx = num_body_verts + i * capvverts + j;
nuclear@25 300
nuclear@25 301 unsigned int vidx[4] = {
nuclear@25 302 idx,
nuclear@25 303 idx + capvverts,
nuclear@25 304 idx + (capvverts + 1),
nuclear@25 305 idx + 1
nuclear@25 306 };
nuclear@25 307
nuclear@25 308 *idxarr++ = vidx[0];
nuclear@25 309 *idxarr++ = vidx[1];
nuclear@25 310 *idxarr++ = vidx[2];
nuclear@25 311 *idxarr++ = vidx[0];
nuclear@25 312 *idxarr++ = vidx[2];
nuclear@25 313 *idxarr++ = vidx[3];
nuclear@25 314 }
nuclear@25 315
nuclear@25 316 v += dv;
nuclear@25 317 }
nuclear@25 318 u += du;
nuclear@25 319 }
nuclear@25 320 }
nuclear@25 321
nuclear@25 322
nuclear@25 323 // -------- plane --------
nuclear@25 324
nuclear@25 325 void gen_plane(Mesh *mesh, float width, float height, int usub, int vsub)
nuclear@25 326 {
nuclear@25 327 gen_heightmap(mesh, width, height, usub, vsub, 0);
nuclear@25 328 }
nuclear@25 329
nuclear@25 330
nuclear@25 331 // ----- heightmap ------
nuclear@25 332
nuclear@25 333 void gen_heightmap(Mesh *mesh, float width, float height, int usub, int vsub, float (*hf)(float, float, void*), void *hfdata)
nuclear@25 334 {
nuclear@25 335 if(usub < 1) usub = 1;
nuclear@25 336 if(vsub < 1) vsub = 1;
nuclear@25 337
nuclear@25 338 mesh->clear();
nuclear@25 339
nuclear@25 340 int uverts = usub + 1;
nuclear@25 341 int vverts = vsub + 1;
nuclear@25 342 int num_verts = uverts * vverts;
nuclear@25 343
nuclear@25 344 int num_quads = usub * vsub;
nuclear@25 345 int num_tri = num_quads * 2;
nuclear@25 346
nuclear@25 347 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
nuclear@25 348 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
nuclear@25 349 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
nuclear@25 350 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
nuclear@25 351 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
nuclear@25 352
nuclear@25 353 float du = 1.0 / (float)usub;
nuclear@25 354 float dv = 1.0 / (float)vsub;
nuclear@25 355
nuclear@25 356 float u = 0.0;
nuclear@25 357 for(int i=0; i<uverts; i++) {
nuclear@25 358 float v = 0.0;
nuclear@25 359 for(int j=0; j<vverts; j++) {
nuclear@25 360 float x = (u - 0.5) * width;
nuclear@25 361 float y = (v - 0.5) * height;
nuclear@25 362 float z = hf ? hf(u, v, hfdata) : 0.0;
nuclear@25 363
nuclear@25 364 Vector3 normal = Vector3(0, 0, 1);
nuclear@25 365 if(hf) {
nuclear@25 366 float u1z = hf(u + du, v, hfdata);
nuclear@25 367 float v1z = hf(u, v + dv, hfdata);
nuclear@25 368
nuclear@25 369 Vector3 tang = Vector3(du * width, 0, u1z - z);
nuclear@25 370 Vector3 bitan = Vector3(0, dv * height, v1z - z);
nuclear@25 371 normal = cross_product(tang, bitan).normalized();
nuclear@25 372 }
nuclear@25 373
nuclear@25 374 *varr++ = Vector3(x, y, z);
nuclear@25 375 *narr++ = normal;
nuclear@25 376 *tarr++ = Vector3(1, 0, 0);
nuclear@25 377 *uvarr++ = Vector2(u, v);
nuclear@25 378
nuclear@25 379 if(i < usub && j < vsub) {
nuclear@25 380 int idx = i * vverts + j;
nuclear@25 381
nuclear@25 382 *idxarr++ = idx;
nuclear@25 383 *idxarr++ = idx + vverts + 1;
nuclear@25 384 *idxarr++ = idx + 1;
nuclear@25 385
nuclear@25 386 *idxarr++ = idx;
nuclear@25 387 *idxarr++ = idx + vverts;
nuclear@25 388 *idxarr++ = idx + vverts + 1;
nuclear@25 389 }
nuclear@25 390
nuclear@25 391 v += dv;
nuclear@25 392 }
nuclear@25 393 u += du;
nuclear@25 394 }
nuclear@25 395 }