tavli

annotate src/board.cc @ 13:f3c5134b4914

generalized the surfaces of revolution evaluator a bit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 27 Jun 2015 22:33:27 +0300
parents ae1c60726c41
children 283eb6e9f0a3
rev   line source
nuclear@11 1 #include <float.h>
nuclear@0 2 #include "opengl.h"
nuclear@0 3 #include "board.h"
nuclear@1 4 #include "meshgen.h"
nuclear@6 5 #include "pnoise.h"
nuclear@0 6
nuclear@0 7 Board::Board()
nuclear@0 8 {
nuclear@2 9 puck_obj = 0;
nuclear@0 10 clear();
nuclear@0 11 }
nuclear@0 12
nuclear@0 13 Board::~Board()
nuclear@0 14 {
nuclear@0 15 destroy();
nuclear@0 16 }
nuclear@0 17
nuclear@0 18 bool Board::init()
nuclear@0 19 {
nuclear@4 20 if(!generate_textures()) {
nuclear@4 21 return false;
nuclear@4 22 }
nuclear@1 23 if(!generate()) {
nuclear@0 24 return false;
nuclear@0 25 }
nuclear@1 26
nuclear@0 27 return true;
nuclear@0 28 }
nuclear@0 29
nuclear@0 30 void Board::destroy()
nuclear@0 31 {
nuclear@2 32 for(size_t i=0; i<obj.size(); i++) {
nuclear@2 33 delete obj[i];
nuclear@1 34 }
nuclear@2 35 obj.clear();
nuclear@1 36
nuclear@2 37 delete puck_obj;
nuclear@2 38 puck_obj = 0;
nuclear@0 39 }
nuclear@0 40
nuclear@0 41 void Board::clear()
nuclear@0 42 {
nuclear@0 43 memset(slots, 0, sizeof slots);
nuclear@0 44 }
nuclear@0 45
nuclear@0 46 void Board::draw() const
nuclear@0 47 {
nuclear@2 48 for(size_t i=0; i<obj.size(); i++) {
nuclear@2 49 obj[i]->draw();
nuclear@1 50 }
nuclear@0 51 }
nuclear@0 52
nuclear@1 53 #define HSIZE 1.0
nuclear@1 54 #define VSIZE (2.0 * HSIZE)
nuclear@1 55 #define BOT_THICKNESS (HSIZE * 0.01)
nuclear@1 56 #define WALL_THICKNESS (HSIZE * 0.05)
nuclear@1 57 #define WALL_HEIGHT (HSIZE * 0.1)
nuclear@1 58 #define GAP (HSIZE * 0.025)
nuclear@1 59 #define HINGE_RAD (GAP * 0.5)
nuclear@1 60 #define HINGE_HEIGHT (VSIZE * 0.075)
nuclear@11 61 #define PIECE_RAD (0.45 * HSIZE / 5.0)
nuclear@11 62
nuclear@13 63 struct BezCurve {
nuclear@13 64 int numcp;
nuclear@13 65 vec2_t *cp;
nuclear@13 66 float scale;
nuclear@11 67 };
nuclear@13 68
nuclear@13 69 static const vec2_t piece_cp[] = {
nuclear@13 70 {0, 0.25},
nuclear@13 71 {1, 0.25}, // mid0
nuclear@13 72 {2, 0.5},
nuclear@13 73 {2.5, 0.5}, // mid1
nuclear@13 74 {3, 0.5},
nuclear@13 75 {4, 0.5}, // mid2
nuclear@13 76 {4, 0},
nuclear@13 77 {4, -0.5}, // mid3
nuclear@13 78 {3, -0.5},
nuclear@13 79 {2.5, -0.5}, // mid4
nuclear@13 80 {0, -0.5}
nuclear@13 81 };
nuclear@13 82 static const BezCurve piece_curve = {sizeof piece_cp / sizeof *piece_cp, (vec2_t*)piece_cp, 0.25 * PIECE_RAD};
nuclear@13 83
nuclear@11 84
nuclear@11 85 static Vector2 piece_revol(float u, float v, void *cls)
nuclear@11 86 {
nuclear@13 87 BezCurve *curve = (BezCurve*)cls;
nuclear@13 88 int nseg = (curve->numcp - 1) / 2;
nuclear@13 89
nuclear@11 90 if(v >= 1.0) v = 1.0 - 1e-6;
nuclear@13 91 int cidx = std::min((int)(v * nseg), nseg - 1);
nuclear@13 92 float t = fmod(v * (float)nseg, 1.0);
nuclear@11 93
nuclear@13 94 const vec2_t *cp = curve->cp + cidx * 2;
nuclear@13 95
nuclear@13 96 float resx = bezier(cp[0].x, cp[1].x, cp[1].x, cp[2].x, t);
nuclear@13 97 float resy = bezier(cp[0].y, cp[1].y, cp[1].y, cp[2].y, t);
nuclear@13 98 return Vector2(resx * curve->scale, resy * curve->scale);
nuclear@11 99 }
nuclear@1 100
nuclear@12 101 static Vector2 piece_revol_normal(float u, float v, void *cls)
nuclear@12 102 {
nuclear@13 103 BezCurve *curve = (BezCurve*)cls;
nuclear@13 104 int nseg = (curve->numcp - 1) / 2;
nuclear@13 105
nuclear@12 106 if(v >= 1.0) v = 1.0 - 1e-6;
nuclear@13 107 int cidx = std::min((int)(v * nseg), nseg - 1);
nuclear@13 108 float t = fmod(v * (float)nseg, 1.0);
nuclear@13 109
nuclear@13 110 const vec2_t *cp = curve->cp + cidx * 2;
nuclear@13 111 Vector2 cp0 = cp[0];
nuclear@13 112 Vector2 cp1 = cp[1];
nuclear@13 113 Vector2 cp2 = cp[2];
nuclear@12 114
nuclear@12 115 Vector2 pprev, pnext;
nuclear@12 116 for(int i=0; i<2; i++) {
nuclear@13 117 pprev[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t - 0.05);
nuclear@13 118 pnext[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t + 0.05);
nuclear@12 119 }
nuclear@12 120
nuclear@12 121 float tx = pnext.x - pprev.x;
nuclear@12 122 float ty = pnext.y - pprev.y;
nuclear@12 123
nuclear@12 124 return Vector2(-ty, tx);
nuclear@12 125 }
nuclear@12 126
nuclear@1 127 bool Board::generate()
nuclear@0 128 {
nuclear@4 129 Mesh tmp;
nuclear@1 130 Matrix4x4 xform;
nuclear@1 131
nuclear@2 132 obj.clear();
nuclear@2 133
nuclear@4 134 for(int i=0; i<2; i++) {
nuclear@4 135 int sign = i * 2 - 1;
nuclear@1 136
nuclear@4 137 // generate bottom
nuclear@4 138 Mesh *bottom = new Mesh;
nuclear@4 139 gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0);
nuclear@4 140 xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0));
nuclear@4 141 bottom->apply_xform(xform);
nuclear@2 142
nuclear@4 143 Object *obottom = new Object;
nuclear@4 144 obottom->set_mesh(bottom);
nuclear@4 145 obottom->xform().set_translation(Vector3(sign * (HSIZE / 2.0 + WALL_THICKNESS + HINGE_RAD * 0.25), 0, 0));
nuclear@4 146 obottom->set_texture(img_field.texture());
nuclear@4 147 obj.push_back(obottom);
nuclear@2 148
nuclear@1 149
nuclear@4 150 // generate the 4 sides
nuclear@4 151 Mesh *sides = new Mesh;
nuclear@4 152 gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
nuclear@4 153 xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0,
nuclear@4 154 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
nuclear@4 155 sides->apply_xform(xform);
nuclear@1 156
nuclear@4 157 gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
nuclear@4 158 xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0,
nuclear@4 159 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
nuclear@4 160 tmp.apply_xform(xform);
nuclear@4 161 sides->append(tmp);
nuclear@4 162 tmp.clear();
nuclear@1 163
nuclear@4 164 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
nuclear@4 165 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
nuclear@4 166 (VSIZE + WALL_THICKNESS) / 2.0));
nuclear@4 167 tmp.apply_xform(xform);
nuclear@4 168 sides->append(tmp);
nuclear@4 169 tmp.clear();
nuclear@1 170
nuclear@4 171 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
nuclear@4 172 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
nuclear@4 173 -(VSIZE + WALL_THICKNESS) / 2.0));
nuclear@4 174 tmp.apply_xform(xform);
nuclear@4 175 sides->append(tmp);
nuclear@4 176 tmp.clear();
nuclear@4 177
nuclear@6 178 // generate texture coordinates
nuclear@6 179 sides->texcoord_gen_box();
nuclear@6 180
nuclear@4 181 Object *osides = new Object;
nuclear@4 182 osides->set_mesh(sides);
nuclear@4 183 osides->xform() = obottom->xform();
nuclear@6 184 osides->set_texture(img_wood.texture());
nuclear@6 185 osides->tex_xform().set_scaling(Vector3(2, 2, 2));
nuclear@6 186 osides->tex_xform().rotate(-Vector3(1, 0, 0.5), M_PI / 4.0);
nuclear@4 187 obj.push_back(osides);
nuclear@4 188
nuclear@4 189 }
nuclear@2 190
nuclear@2 191
nuclear@1 192 // generate the hinges
nuclear@1 193 Mesh *hinges = new Mesh;
nuclear@7 194 for(int i=0; i<2; i++) {
nuclear@7 195 float sign = i * 2 - 1;
nuclear@1 196
nuclear@7 197 // barrel
nuclear@11 198 gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 8, 1, 1);
nuclear@7 199 xform.reset_identity();
nuclear@7 200 xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, sign * VSIZE / 4.0));
nuclear@7 201 xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
nuclear@7 202 tmp.apply_xform(xform);
nuclear@7 203 hinges->append(tmp);
nuclear@1 204
nuclear@7 205 // flange
nuclear@7 206 gen_plane(&tmp, HINGE_HEIGHT * 0.6, HINGE_HEIGHT * 0.8);
nuclear@7 207 tmp.apply_xform(xform);
nuclear@7 208
nuclear@7 209 Matrix4x4 tex_xform;
nuclear@7 210 tex_xform.set_rotation(Vector3(0, 0, M_PI / 2.0));
nuclear@7 211 tmp.texcoord_apply_xform(tex_xform);
nuclear@7 212 hinges->append(tmp);
nuclear@7 213
nuclear@7 214 // studs
nuclear@7 215 for(int j=0; j<4; j++) {
nuclear@7 216 Vector3 pos;
nuclear@7 217
nuclear@7 218 pos.x = (float)((j & 1) * 2 - 1) * HINGE_HEIGHT * 0.2;
nuclear@7 219 pos.y = (float)((j & 2) - 1) * HINGE_HEIGHT * 0.3;
nuclear@7 220
nuclear@7 221 Matrix4x4 stud_xform = xform;
nuclear@7 222 stud_xform.translate(pos);
nuclear@7 223
nuclear@7 224 Matrix4x4 squash;
nuclear@7 225 squash.set_scaling(Vector3(1, 1, 0.5));
nuclear@7 226
nuclear@7 227 gen_sphere(&tmp, HINGE_RAD * 0.5, 8, 4);
nuclear@7 228 tmp.apply_xform(stud_xform * squash);
nuclear@7 229 hinges->append(tmp);
nuclear@7 230 }
nuclear@7 231 }
nuclear@1 232
nuclear@2 233 Object *ohinges = new Object;
nuclear@2 234 ohinges->set_mesh(hinges);
nuclear@7 235 ohinges->set_texture(img_hinge.texture());
nuclear@2 236 obj.push_back(ohinges);
nuclear@1 237
nuclear@6 238 // debug object
nuclear@7 239 /*
nuclear@7 240 Mesh *dbgmesh = new Mesh;
nuclear@6 241 gen_box(dbgmesh, 0.5, 0.5, 0.5);
nuclear@6 242 xform.set_translation(Vector3(0, 0.4, 0));
nuclear@7 243 xform.set_scaling(Vector3(1, 1, 1));
nuclear@6 244 dbgmesh->apply_xform(xform);
nuclear@6 245 Object *dbgobj = new Object;
nuclear@6 246 dbgobj->set_mesh(dbgmesh);
nuclear@7 247 dbgobj->set_texture(img_hinge.texture());
nuclear@7 248 //dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3));
nuclear@7 249 obj.push_back(dbgobj);
nuclear@7 250 */
nuclear@2 251
nuclear@11 252 Mesh *piece = new Mesh;
nuclear@13 253 gen_revol(piece, 18, 17, piece_revol, piece_revol_normal, (void*)&piece_curve);
nuclear@11 254
nuclear@11 255 Object *opiece = new Object;
nuclear@11 256 opiece->set_mesh(piece);
nuclear@12 257 opiece->mtl.diffuse = Vector3(0.6, 0.6, 0.6);
nuclear@12 258 opiece->mtl.specular = Vector3(0.8, 0.8, 0.8);
nuclear@11 259 opiece->xform().set_translation(Vector3(0, 0.2, 0));
nuclear@11 260 obj.push_back(opiece);
nuclear@11 261
nuclear@11 262
nuclear@8 263 // meshgen stats
nuclear@8 264 printf("Generated board:\n %u meshes\n", (unsigned int)obj.size());
nuclear@8 265 unsigned int polycount = 0;
nuclear@8 266 for(size_t i=0; i<obj.size(); i++) {
nuclear@8 267 const Mesh *m = obj[i]->get_mesh();
nuclear@8 268 polycount += m->get_poly_count();
nuclear@8 269 }
nuclear@8 270 printf(" %u polygons\n", polycount);
nuclear@8 271
nuclear@1 272 return true;
nuclear@0 273 }
nuclear@4 274
nuclear@5 275 static float wood(float x, float y)
nuclear@5 276 {
nuclear@5 277 float u = x;
nuclear@5 278 float v = y;
nuclear@5 279 x += 1.0;
nuclear@5 280 x *= 10.0;
nuclear@5 281 y *= 20.0;
nuclear@5 282
nuclear@5 283 float len = sqrt(x * x + y * y) + turbulence2(u * 6.0, v * 12.0, 2) * 1.2 +
nuclear@5 284 turbulence2(u * 0.5, v, 2) * 15.0;
nuclear@5 285 float val = fmod(len, 1.0);
nuclear@5 286
nuclear@5 287 //val = val * 0.5 + 0.5;
nuclear@5 288 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
nuclear@5 289 }
nuclear@5 290
nuclear@6 291 static float wood_tile(float x, float y)
nuclear@6 292 {
nuclear@6 293 float u = x;
nuclear@6 294 float v = y;
nuclear@6 295 x *= 10.0;
nuclear@6 296 y *= 10.0;
nuclear@6 297
nuclear@6 298 float val = x + pnoise2(u * 6.0, v, 6, 1) * 3.0 +
nuclear@6 299 pturbulence2(u * 4, v * 2, 4, 2, 2) * 1.5 + pturbulence2(u * 8, v * 8, 8, 8, 2) * 0.5;
nuclear@6 300
nuclear@6 301 val = fmod(val, 1.0);
nuclear@6 302 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
nuclear@6 303 }
nuclear@6 304
nuclear@4 305 static bool spike(float x, float y)
nuclear@4 306 {
nuclear@4 307 x = fmod(x * 5.0, 1.0);
nuclear@4 308 return y < (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x);
nuclear@4 309 }
nuclear@4 310
nuclear@4 311 static bool circle(float x, float y, float rad)
nuclear@4 312 {
nuclear@4 313 x = fmod(x * 5.0, 1.0) - 0.5;
nuclear@4 314 y = (y - 0.65) * 5.0;
nuclear@4 315 float len = sqrt(x * x + y * y);
nuclear@4 316 return len < rad;
nuclear@4 317 }
nuclear@4 318
nuclear@4 319 static bool diamond(float x, float y)
nuclear@4 320 {
nuclear@4 321 return y >= (1.0 - (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x)) * 0.3333333 + 0.88;
nuclear@4 322 }
nuclear@4 323
nuclear@4 324 static bool center_circle(float x, float y, float rad)
nuclear@4 325 {
nuclear@4 326 x = x - 0.5;
nuclear@4 327 y = 1.0 - y;
nuclear@4 328 return sqrt(x * x + y * y) < rad;
nuclear@4 329 }
nuclear@4 330
nuclear@4 331 bool Board::generate_textures()
nuclear@4 332 {
nuclear@6 333 // ---- board field texture ----
nuclear@5 334 static const Vector3 wcol1 = Vector3(0.6, 0.4, 0.2);
nuclear@6 335 static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1);
nuclear@6 336 static const Vector3 wcol3 = Vector3(0.38, 0.25, 0.08);
nuclear@5 337
nuclear@8 338 img_field.create(1024, 1024);
nuclear@6 339 unsigned char *pptr = img_field.pixels;
nuclear@6 340 for(int i=0; i<img_field.height; i++) {
nuclear@6 341 float v = (float)i / (float)img_field.height;
nuclear@4 342
nuclear@6 343 for(int j=0; j<img_field.width; j++) {
nuclear@6 344 float u = (float)j / (float)img_field.width;
nuclear@4 345
nuclear@4 346 int r = 0, g = 0, b = 0;
nuclear@4 347
nuclear@5 348 float wood_val = wood(u, v);
nuclear@5 349
nuclear@5 350 // pattern mask
nuclear@4 351 float x = u;
nuclear@4 352 float y = v < 0.5 ? v * 2.0 : 2.0 - v * 2.0;
nuclear@4 353 bool inside = false;
nuclear@4 354
nuclear@4 355 inside |= (spike(x, y + 0.33333) && !spike(x, y + 0.4)) ||
nuclear@4 356 (spike(x, y + 0.5) && !spike(x, y + 0.68));
nuclear@4 357 inside |= (circle(x, y, 0.12) && !circle(x, y, 0.1)) || circle(x, y, 0.06);
nuclear@4 358 inside |= (diamond(x, y) && !diamond(x, y - 0.015)) ||
nuclear@4 359 (diamond(x, y - 0.023) && !diamond(x, y - 0.028));
nuclear@4 360 inside |= center_circle(x, y, 0.03);
nuclear@4 361
nuclear@5 362 Vector3 wood_color = lerp(wcol1, wcol2, wood_val) * 0.9;
nuclear@4 363 if(inside) {
nuclear@5 364 wood_color = lerp(wcol1, wcol2, 1.0 - wood_val) * 2.0;
nuclear@4 365 }
nuclear@4 366
nuclear@5 367 r = (int)(wood_color.x * 255.0);
nuclear@5 368 g = (int)(wood_color.y * 255.0);
nuclear@5 369 b = (int)(wood_color.z * 255.0);
nuclear@5 370
nuclear@5 371 pptr[0] = r > 255 ? 255 : r;
nuclear@5 372 pptr[1] = g > 255 ? 255 : g;
nuclear@5 373 pptr[2] = b > 255 ? 255 : b;
nuclear@4 374 pptr += 3;
nuclear@4 375 }
nuclear@4 376 }
nuclear@6 377 img_field.texture();
nuclear@4 378
nuclear@6 379 // ---- generic wood texture ----
nuclear@6 380 img_wood.create(256, 256);
nuclear@6 381 pptr = img_wood.pixels;
nuclear@6 382 for(int i=0; i<img_wood.height; i++) {
nuclear@6 383 float v = (float)i / (float)img_wood.height;
nuclear@6 384 for(int j=0; j<img_wood.width; j++) {
nuclear@6 385 float u = (float)j / (float)img_wood.width;
nuclear@6 386
nuclear@6 387 float wood_val = wood_tile(u, v);
nuclear@6 388 Vector3 wood_color = lerp(wcol2, wcol3, wood_val) * 0.7;
nuclear@6 389
nuclear@6 390 int r = (int)(wood_color.x * 255.0);
nuclear@6 391 int g = (int)(wood_color.y * 255.0);
nuclear@6 392 int b = (int)(wood_color.z * 255.0);
nuclear@6 393
nuclear@6 394 pptr[0] = r > 255 ? 255 : r;
nuclear@6 395 pptr[1] = g > 255 ? 255 : g;
nuclear@6 396 pptr[2] = b > 255 ? 255 : b;
nuclear@6 397 pptr += 3;
nuclear@6 398 }
nuclear@6 399 }
nuclear@6 400 img_wood.texture();
nuclear@7 401
nuclear@7 402 // ---- metal hinge diffuse texture ----
nuclear@7 403 Vector3 rusty_col1 = Vector3(0.43, 0.46, 0.52);
nuclear@7 404 Vector3 rusty_col2 = Vector3(0.52, 0.47, 0.43);
nuclear@7 405
nuclear@7 406 img_hinge.create(128, 128);
nuclear@7 407 pptr = img_hinge.pixels;
nuclear@7 408 for(int i=0; i<img_hinge.height; i++) {
nuclear@7 409 float v = (float)i / (float)img_hinge.height;
nuclear@7 410 for(int j=0; j<img_hinge.width; j++) {
nuclear@7 411 float u = (float)j / (float)img_hinge.width;
nuclear@7 412
nuclear@7 413 // rust pattern
nuclear@7 414 float w1 = fbm2(u * 4.0, v * 4.0, 3) * 0.5 + 0.5;
nuclear@9 415 //float w2 = fbm2(u * 8.0, v * 8.0, 1) * 0.5 + 0.5;
nuclear@7 416 Vector3 col = lerp(rusty_col1, rusty_col2 * 0.5, w1);
nuclear@7 417
nuclear@7 418 // center hinge split
nuclear@7 419 if(fabs(v - 0.5) < 0.01) {
nuclear@7 420 col *= 0.5;
nuclear@7 421 }
nuclear@7 422
nuclear@7 423 int r = (int)(col.x * 255.0);
nuclear@7 424 int g = (int)(col.y * 255.0);
nuclear@7 425 int b = (int)(col.z * 255.0);
nuclear@7 426
nuclear@7 427 pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
nuclear@7 428 pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
nuclear@7 429 pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
nuclear@7 430
nuclear@7 431 pptr += 3;
nuclear@7 432 }
nuclear@7 433 }
nuclear@7 434 img_hinge.texture();
nuclear@7 435
nuclear@4 436 return true;
nuclear@4 437 }