tavli

annotate src/board.cc @ 21:c3fbf9616dbd

slot bounds, and ray testing
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 02 Jul 2015 00:01:39 +0300
parents 37dead56f01e
children c2a2069a49ec
rev   line source
nuclear@11 1 #include <float.h>
nuclear@0 2 #include "opengl.h"
nuclear@0 3 #include "board.h"
nuclear@14 4 #include "game.h"
nuclear@1 5 #include "meshgen.h"
nuclear@6 6 #include "pnoise.h"
nuclear@14 7 #include "revol.h"
nuclear@17 8 #include "opt.h"
nuclear@17 9
nuclear@17 10
nuclear@17 11 #define HSIZE 1.0
nuclear@17 12 #define VSIZE (2.0 * HSIZE)
nuclear@17 13 #define BOT_THICKNESS (HSIZE * 0.01)
nuclear@17 14 #define WALL_THICKNESS (HSIZE * 0.05)
nuclear@17 15 #define WALL_HEIGHT (HSIZE * 0.1)
nuclear@17 16 #define GAP (HSIZE * 0.025)
nuclear@17 17 #define HINGE_RAD (GAP * 0.5)
nuclear@17 18 #define HINGE_HEIGHT (VSIZE * 0.075)
nuclear@17 19 #define PIECE_RAD (0.45 * HSIZE / 5.0)
nuclear@17 20 #define BOARD_OFFSET (HSIZE / 2.0 + WALL_THICKNESS + HINGE_RAD * 0.25)
nuclear@17 21 #define PIECES_PER_LAYER 5
nuclear@21 22 #define SLOT_WIDTH (HSIZE / 5.0)
nuclear@21 23 #define SLOT_HEIGHT (VSIZE * 0.4)
nuclear@17 24
nuclear@17 25
nuclear@18 26 static const vec2_t piece_cp[] = {
nuclear@18 27 {0, 0.25},
nuclear@18 28 {1, 0.25}, // mid0
nuclear@18 29 {2, 0.5},
nuclear@18 30 {2.5, 0.5}, // mid1
nuclear@18 31 {3, 0.5},
nuclear@18 32 {4, 0.5}, // mid2
nuclear@18 33 {4, 0},
nuclear@18 34 {4, -0.5}, // mid3
nuclear@18 35 {3, -0.5},
nuclear@18 36 {2.5, -0.5}, // mid4
nuclear@18 37 {0, -0.5}
nuclear@18 38 };
nuclear@18 39 static const BezCurve piece_curve = {
nuclear@18 40 sizeof piece_cp / sizeof *piece_cp,
nuclear@18 41 (vec2_t*)piece_cp,
nuclear@18 42 0.25 * PIECE_RAD
nuclear@18 43 };
nuclear@18 44
nuclear@18 45 #define PIECE_HEIGHT (0.25 * PIECE_RAD)
nuclear@18 46
nuclear@18 47
nuclear@17 48 Piece::Piece()
nuclear@17 49 {
nuclear@17 50 owner = 0;
nuclear@17 51 slot = prev_slot = -1;
nuclear@17 52 level = 0;
nuclear@17 53 move_start = 0;
nuclear@17 54 }
nuclear@17 55
nuclear@17 56 void Piece::move_to(int slot, int level, bool anim)
nuclear@17 57 {
nuclear@17 58 int prev_slot = this->slot;
nuclear@17 59 int prev_level = this->level;
nuclear@17 60
nuclear@17 61 this->slot = slot;
nuclear@17 62 this->level = level;
nuclear@17 63
nuclear@17 64 if(anim) {
nuclear@17 65 this->prev_slot = prev_slot;
nuclear@17 66 this->prev_level = prev_level;
nuclear@17 67 move_start = cur_time;
nuclear@17 68 }
nuclear@17 69 }
nuclear@17 70
nuclear@21 71 Quad::Quad()
nuclear@21 72 {
nuclear@21 73 }
nuclear@21 74
nuclear@21 75 Quad::Quad(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
nuclear@21 76 : tri0(v0, v1, v2), tri1(v0, v2, v3)
nuclear@21 77 {
nuclear@21 78 }
nuclear@21 79
nuclear@21 80 bool Quad::intersect(const Ray &ray, HitPoint *hit) const
nuclear@21 81 {
nuclear@21 82 return tri0.intersect(ray, hit) || tri1.intersect(ray, hit);
nuclear@21 83 }
nuclear@0 84
nuclear@0 85 Board::Board()
nuclear@0 86 {
nuclear@17 87 piece_obj = 0;
nuclear@0 88 clear();
nuclear@21 89
nuclear@21 90 for(int i=0; i<NUM_SLOTS; i++) {
nuclear@21 91 Vector3 p = piece_pos(i, 0);
nuclear@21 92 bool top_side = i >= NUM_SLOTS / 2;
nuclear@21 93
nuclear@21 94 float z0 = top_side ? -PIECE_RAD : PIECE_RAD;
nuclear@21 95 float z1 = top_side ? SLOT_HEIGHT : -SLOT_HEIGHT;
nuclear@21 96
nuclear@21 97 slotbb[i] = Quad(p + Vector3(-SLOT_WIDTH / 2.0, 0, z0),
nuclear@21 98 p + Vector3(SLOT_WIDTH / 2.0, 0, z0),
nuclear@21 99 p + Vector3(SLOT_WIDTH / 2.0, 0, z1),
nuclear@21 100 p + Vector3(-SLOT_WIDTH / 2.0, 0, z1));
nuclear@21 101 }
nuclear@0 102 }
nuclear@0 103
nuclear@0 104 Board::~Board()
nuclear@0 105 {
nuclear@0 106 destroy();
nuclear@0 107 }
nuclear@0 108
nuclear@0 109 bool Board::init()
nuclear@0 110 {
nuclear@4 111 if(!generate_textures()) {
nuclear@4 112 return false;
nuclear@4 113 }
nuclear@1 114 if(!generate()) {
nuclear@0 115 return false;
nuclear@0 116 }
nuclear@1 117
nuclear@0 118 return true;
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 void Board::destroy()
nuclear@0 122 {
nuclear@2 123 for(size_t i=0; i<obj.size(); i++) {
nuclear@2 124 delete obj[i];
nuclear@1 125 }
nuclear@2 126 obj.clear();
nuclear@1 127
nuclear@17 128 delete piece_obj;
nuclear@17 129 piece_obj = 0;
nuclear@0 130 }
nuclear@0 131
nuclear@0 132 void Board::clear()
nuclear@0 133 {
nuclear@17 134 memset(hist, 0, sizeof hist);
nuclear@17 135
nuclear@17 136 for(int i=0; i<MAX_PIECES; i++) {
nuclear@17 137 pieces[i].owner = i < PLAYER_PIECES ? MINE : OTHER;
nuclear@17 138 move_piece(i, -1, false);
nuclear@17 139 }
nuclear@17 140 }
nuclear@17 141
nuclear@17 142 void Board::setup()
nuclear@17 143 {
nuclear@17 144 static const int initial[] = { 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
nuclear@17 145
nuclear@17 146 clear();
nuclear@17 147
nuclear@17 148 int id = 0;
nuclear@17 149 for(int i=0; i<NUM_SLOTS; i++) {
nuclear@17 150 for(int j=0; j<initial[i]; j++) {
nuclear@17 151 move_piece(id, i, false);
nuclear@17 152 move_piece(PLAYER_PIECES + id, NUM_SLOTS - i - 1, false);
nuclear@17 153 ++id;
nuclear@17 154 }
nuclear@17 155 }
nuclear@17 156 }
nuclear@17 157
nuclear@17 158 int Board::slot_pieces(int slot) const
nuclear@17 159 {
nuclear@17 160 return hist[slot + 1];
nuclear@17 161 }
nuclear@17 162
nuclear@17 163 bool Board::move_piece(int id, int slot, bool anim)
nuclear@17 164 {
nuclear@17 165 // TODO do validity checking first
nuclear@17 166 int prev_slot = pieces[id].slot;
nuclear@17 167
nuclear@17 168 pieces[id].move_to(slot, slot_pieces(slot), anim);
nuclear@17 169 --hist[prev_slot + 1];
nuclear@17 170 ++hist[slot + 1];
nuclear@17 171 return true;
nuclear@17 172 }
nuclear@17 173
nuclear@17 174 Vector3 Board::piece_pos(int slot, int level) const
nuclear@17 175 {
nuclear@17 176 int top_side = slot / 10;
nuclear@17 177 int sidx = (top_side ? (19 - slot) : slot) % 5;
nuclear@17 178 int left_side = (top_side ? (19 - slot) : slot) / 5;
nuclear@17 179
nuclear@17 180 Vector3 pos;
nuclear@17 181
nuclear@17 182 if(left_side) {
nuclear@17 183 pos.x = -(sidx * HSIZE / 5.0 + BOARD_OFFSET - HSIZE / 2.0) - PIECE_RAD;
nuclear@17 184 } else {
nuclear@17 185 pos.x = (4 - sidx) * HSIZE / 5.0 + BOARD_OFFSET - HSIZE / 2.0 + PIECE_RAD;
nuclear@17 186 }
nuclear@17 187
nuclear@17 188 int layer = level / PIECES_PER_LAYER;
nuclear@17 189 int layer_level = level % PIECES_PER_LAYER;
nuclear@17 190
nuclear@18 191 pos.y = (layer + 0.5) * PIECE_HEIGHT;
nuclear@17 192
nuclear@17 193 pos.z = (-VSIZE * 0.5 + PIECE_RAD + PIECE_RAD * 2.0 * layer_level);
nuclear@21 194 if(!top_side) {
nuclear@17 195 pos.z = -pos.z;
nuclear@17 196 }
nuclear@17 197
nuclear@17 198 return pos;
nuclear@0 199 }
nuclear@0 200
nuclear@21 201 int Board::slot_hit(const Ray &ray) const
nuclear@21 202 {
nuclear@21 203 for(int i=0; i<NUM_SLOTS; i++) {
nuclear@21 204 if(slotbb[i].intersect(ray)) {
nuclear@21 205 return i;
nuclear@21 206 }
nuclear@21 207 }
nuclear@21 208 return -1;
nuclear@21 209 }
nuclear@21 210
nuclear@0 211 void Board::draw() const
nuclear@0 212 {
nuclear@19 213 bool use_shadows = opt.shadows && sdr_shadow;
nuclear@19 214 unsigned int board_sdr = use_shadows ? sdr_shadow : sdr_phong;
nuclear@19 215 unsigned int piece_sdr = use_shadows ? sdr_shadow_notex : sdr_phong_notex;
nuclear@19 216
nuclear@2 217 for(size_t i=0; i<obj.size(); i++) {
nuclear@14 218 if(wireframe) {
nuclear@14 219 obj[i]->draw_wire();
nuclear@14 220 obj[i]->draw_normals(0.075);
nuclear@14 221 } else {
nuclear@19 222 obj[i]->set_shader(board_sdr);
nuclear@14 223 obj[i]->draw();
nuclear@14 224 }
nuclear@1 225 }
nuclear@17 226
nuclear@17 227 for(int i=0; i<MAX_PIECES; i++) {
nuclear@17 228 Vector3 pos = piece_pos(pieces[i].slot, pieces[i].level);
nuclear@17 229 piece_obj->xform().set_translation(pos);
nuclear@17 230 piece_obj->mtl.diffuse = opt.piece_color[pieces[i].owner];
nuclear@19 231 piece_obj->set_shader(piece_sdr);
nuclear@17 232 piece_obj->draw();
nuclear@17 233 }
nuclear@21 234
nuclear@21 235 // draw the slot bounds
nuclear@21 236 /*
nuclear@21 237 static const float pal[][3] = {
nuclear@21 238 {1, 0, 0},
nuclear@21 239 {0, 1, 0},
nuclear@21 240 {0, 0, 1},
nuclear@21 241 {1, 1, 0},
nuclear@21 242 {0, 1, 1},
nuclear@21 243 {1, 0, 1}
nuclear@21 244 };
nuclear@21 245 int idx = dbg_int % NUM_SLOTS;
nuclear@21 246 if(idx >= 0) {
nuclear@21 247 glUseProgram(0);
nuclear@21 248
nuclear@21 249 glPushAttrib(GL_ENABLE_BIT);
nuclear@21 250 glDisable(GL_LIGHTING);
nuclear@21 251 glDisable(GL_CULL_FACE);
nuclear@21 252 glDisable(GL_DEPTH_TEST);
nuclear@21 253
nuclear@21 254 glBegin(GL_TRIANGLES);
nuclear@21 255 glColor3fv(pal[idx % (sizeof pal / sizeof *pal)]);
nuclear@21 256 glVertex3f(slotbb[idx].tri0.v[0].x, slotbb[idx].tri0.v[0].y, slotbb[idx].tri0.v[0].z);
nuclear@21 257 glVertex3f(slotbb[idx].tri0.v[1].x, slotbb[idx].tri0.v[1].y, slotbb[idx].tri0.v[1].z);
nuclear@21 258 glVertex3f(slotbb[idx].tri0.v[2].x, slotbb[idx].tri0.v[2].y, slotbb[idx].tri0.v[2].z);
nuclear@21 259 glVertex3f(slotbb[idx].tri1.v[0].x, slotbb[idx].tri1.v[0].y, slotbb[idx].tri1.v[0].z);
nuclear@21 260 glVertex3f(slotbb[idx].tri1.v[1].x, slotbb[idx].tri1.v[1].y, slotbb[idx].tri1.v[1].z);
nuclear@21 261 glVertex3f(slotbb[idx].tri1.v[2].x, slotbb[idx].tri1.v[2].y, slotbb[idx].tri1.v[2].z);
nuclear@21 262 glEnd();
nuclear@21 263
nuclear@21 264 glPopAttrib();
nuclear@21 265 }
nuclear@21 266 */
nuclear@21 267 // TODO slot highlighting
nuclear@0 268 }
nuclear@0 269
nuclear@13 270
nuclear@1 271 bool Board::generate()
nuclear@0 272 {
nuclear@18 273 static const float board_spec = 0.4;
nuclear@18 274
nuclear@4 275 Mesh tmp;
nuclear@1 276 Matrix4x4 xform;
nuclear@1 277
nuclear@2 278 obj.clear();
nuclear@2 279
nuclear@4 280 for(int i=0; i<2; i++) {
nuclear@4 281 int sign = i * 2 - 1;
nuclear@1 282
nuclear@4 283 // generate bottom
nuclear@4 284 Mesh *bottom = new Mesh;
nuclear@4 285 gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0);
nuclear@4 286 xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0));
nuclear@4 287 bottom->apply_xform(xform);
nuclear@2 288
nuclear@4 289 Object *obottom = new Object;
nuclear@4 290 obottom->set_mesh(bottom);
nuclear@17 291 obottom->xform().set_translation(Vector3(sign * BOARD_OFFSET, 0, 0));
nuclear@4 292 obottom->set_texture(img_field.texture());
nuclear@18 293 obottom->mtl.specular = Vector3(board_spec, board_spec, board_spec);
nuclear@4 294 obj.push_back(obottom);
nuclear@2 295
nuclear@1 296
nuclear@4 297 // generate the 4 sides
nuclear@4 298 Mesh *sides = new Mesh;
nuclear@4 299 gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
nuclear@4 300 xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0,
nuclear@4 301 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
nuclear@4 302 sides->apply_xform(xform);
nuclear@1 303
nuclear@4 304 gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
nuclear@4 305 xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0,
nuclear@4 306 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
nuclear@4 307 tmp.apply_xform(xform);
nuclear@4 308 sides->append(tmp);
nuclear@4 309 tmp.clear();
nuclear@1 310
nuclear@4 311 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
nuclear@4 312 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
nuclear@4 313 (VSIZE + WALL_THICKNESS) / 2.0));
nuclear@4 314 tmp.apply_xform(xform);
nuclear@4 315 sides->append(tmp);
nuclear@4 316 tmp.clear();
nuclear@1 317
nuclear@4 318 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
nuclear@4 319 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
nuclear@4 320 -(VSIZE + WALL_THICKNESS) / 2.0));
nuclear@4 321 tmp.apply_xform(xform);
nuclear@4 322 sides->append(tmp);
nuclear@4 323 tmp.clear();
nuclear@4 324
nuclear@6 325 // generate texture coordinates
nuclear@6 326 sides->texcoord_gen_box();
nuclear@6 327
nuclear@4 328 Object *osides = new Object;
nuclear@4 329 osides->set_mesh(sides);
nuclear@4 330 osides->xform() = obottom->xform();
nuclear@6 331 osides->set_texture(img_wood.texture());
nuclear@6 332 osides->tex_xform().set_scaling(Vector3(2, 2, 2));
nuclear@6 333 osides->tex_xform().rotate(-Vector3(1, 0, 0.5), M_PI / 4.0);
nuclear@18 334 osides->mtl.specular = Vector3(board_spec, board_spec, board_spec);
nuclear@4 335 obj.push_back(osides);
nuclear@4 336
nuclear@4 337 }
nuclear@2 338
nuclear@2 339
nuclear@1 340 // generate the hinges
nuclear@1 341 Mesh *hinges = new Mesh;
nuclear@7 342 for(int i=0; i<2; i++) {
nuclear@7 343 float sign = i * 2 - 1;
nuclear@1 344
nuclear@7 345 // barrel
nuclear@11 346 gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 8, 1, 1);
nuclear@7 347 xform.reset_identity();
nuclear@7 348 xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, sign * VSIZE / 4.0));
nuclear@7 349 xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
nuclear@7 350 tmp.apply_xform(xform);
nuclear@7 351 hinges->append(tmp);
nuclear@1 352
nuclear@7 353 // flange
nuclear@7 354 gen_plane(&tmp, HINGE_HEIGHT * 0.6, HINGE_HEIGHT * 0.8);
nuclear@7 355 tmp.apply_xform(xform);
nuclear@7 356
nuclear@7 357 Matrix4x4 tex_xform;
nuclear@7 358 tex_xform.set_rotation(Vector3(0, 0, M_PI / 2.0));
nuclear@7 359 tmp.texcoord_apply_xform(tex_xform);
nuclear@7 360 hinges->append(tmp);
nuclear@7 361
nuclear@7 362 // studs
nuclear@7 363 for(int j=0; j<4; j++) {
nuclear@7 364 Vector3 pos;
nuclear@7 365
nuclear@7 366 pos.x = (float)((j & 1) * 2 - 1) * HINGE_HEIGHT * 0.2;
nuclear@7 367 pos.y = (float)((j & 2) - 1) * HINGE_HEIGHT * 0.3;
nuclear@7 368
nuclear@7 369 Matrix4x4 stud_xform = xform;
nuclear@7 370 stud_xform.translate(pos);
nuclear@7 371
nuclear@7 372 Matrix4x4 squash;
nuclear@7 373 squash.set_scaling(Vector3(1, 1, 0.5));
nuclear@7 374
nuclear@7 375 gen_sphere(&tmp, HINGE_RAD * 0.5, 8, 4);
nuclear@7 376 tmp.apply_xform(stud_xform * squash);
nuclear@7 377 hinges->append(tmp);
nuclear@7 378 }
nuclear@7 379 }
nuclear@1 380
nuclear@2 381 Object *ohinges = new Object;
nuclear@2 382 ohinges->set_mesh(hinges);
nuclear@7 383 ohinges->set_texture(img_hinge.texture());
nuclear@2 384 obj.push_back(ohinges);
nuclear@1 385
nuclear@6 386 // debug object
nuclear@7 387 /*
nuclear@7 388 Mesh *dbgmesh = new Mesh;
nuclear@6 389 gen_box(dbgmesh, 0.5, 0.5, 0.5);
nuclear@6 390 xform.set_translation(Vector3(0, 0.4, 0));
nuclear@7 391 xform.set_scaling(Vector3(1, 1, 1));
nuclear@6 392 dbgmesh->apply_xform(xform);
nuclear@6 393 Object *dbgobj = new Object;
nuclear@6 394 dbgobj->set_mesh(dbgmesh);
nuclear@7 395 dbgobj->set_texture(img_hinge.texture());
nuclear@7 396 //dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3));
nuclear@7 397 obj.push_back(dbgobj);
nuclear@7 398 */
nuclear@2 399
nuclear@11 400 Mesh *piece = new Mesh;
nuclear@14 401 gen_revol(piece, 18, 17, bezier_revol, bezier_revol_normal, (void*)&piece_curve);
nuclear@11 402
nuclear@11 403 Object *opiece = new Object;
nuclear@11 404 opiece->set_mesh(piece);
nuclear@12 405 opiece->mtl.diffuse = Vector3(0.6, 0.6, 0.6);
nuclear@12 406 opiece->mtl.specular = Vector3(0.8, 0.8, 0.8);
nuclear@11 407 opiece->xform().set_translation(Vector3(0, 0.2, 0));
nuclear@17 408 //obj.push_back(opiece);
nuclear@17 409
nuclear@17 410 piece_obj = opiece;
nuclear@11 411
nuclear@8 412 // meshgen stats
nuclear@8 413 printf("Generated board:\n %u meshes\n", (unsigned int)obj.size());
nuclear@8 414 unsigned int polycount = 0;
nuclear@8 415 for(size_t i=0; i<obj.size(); i++) {
nuclear@8 416 const Mesh *m = obj[i]->get_mesh();
nuclear@8 417 polycount += m->get_poly_count();
nuclear@8 418 }
nuclear@8 419 printf(" %u polygons\n", polycount);
nuclear@8 420
nuclear@1 421 return true;
nuclear@0 422 }
nuclear@4 423
nuclear@5 424 static float wood(float x, float y)
nuclear@5 425 {
nuclear@5 426 float u = x;
nuclear@5 427 float v = y;
nuclear@5 428 x += 1.0;
nuclear@5 429 x *= 10.0;
nuclear@5 430 y *= 20.0;
nuclear@5 431
nuclear@5 432 float len = sqrt(x * x + y * y) + turbulence2(u * 6.0, v * 12.0, 2) * 1.2 +
nuclear@5 433 turbulence2(u * 0.5, v, 2) * 15.0;
nuclear@5 434 float val = fmod(len, 1.0);
nuclear@5 435
nuclear@5 436 //val = val * 0.5 + 0.5;
nuclear@5 437 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
nuclear@5 438 }
nuclear@5 439
nuclear@6 440 static float wood_tile(float x, float y)
nuclear@6 441 {
nuclear@6 442 float u = x;
nuclear@6 443 float v = y;
nuclear@6 444 x *= 10.0;
nuclear@6 445 y *= 10.0;
nuclear@6 446
nuclear@6 447 float val = x + pnoise2(u * 6.0, v, 6, 1) * 3.0 +
nuclear@6 448 pturbulence2(u * 4, v * 2, 4, 2, 2) * 1.5 + pturbulence2(u * 8, v * 8, 8, 8, 2) * 0.5;
nuclear@6 449
nuclear@6 450 val = fmod(val, 1.0);
nuclear@6 451 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
nuclear@6 452 }
nuclear@6 453
nuclear@4 454 static bool spike(float x, float y)
nuclear@4 455 {
nuclear@4 456 x = fmod(x * 5.0, 1.0);
nuclear@4 457 return y < (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x);
nuclear@4 458 }
nuclear@4 459
nuclear@4 460 static bool circle(float x, float y, float rad)
nuclear@4 461 {
nuclear@4 462 x = fmod(x * 5.0, 1.0) - 0.5;
nuclear@4 463 y = (y - 0.65) * 5.0;
nuclear@4 464 float len = sqrt(x * x + y * y);
nuclear@4 465 return len < rad;
nuclear@4 466 }
nuclear@4 467
nuclear@4 468 static bool diamond(float x, float y)
nuclear@4 469 {
nuclear@4 470 return y >= (1.0 - (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x)) * 0.3333333 + 0.88;
nuclear@4 471 }
nuclear@4 472
nuclear@4 473 static bool center_circle(float x, float y, float rad)
nuclear@4 474 {
nuclear@4 475 x = x - 0.5;
nuclear@4 476 y = 1.0 - y;
nuclear@4 477 return sqrt(x * x + y * y) < rad;
nuclear@4 478 }
nuclear@4 479
nuclear@4 480 bool Board::generate_textures()
nuclear@4 481 {
nuclear@6 482 // ---- board field texture ----
nuclear@5 483 static const Vector3 wcol1 = Vector3(0.6, 0.4, 0.2);
nuclear@6 484 static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1);
nuclear@6 485 static const Vector3 wcol3 = Vector3(0.38, 0.25, 0.08);
nuclear@5 486
nuclear@8 487 img_field.create(1024, 1024);
nuclear@6 488 unsigned char *pptr = img_field.pixels;
nuclear@6 489 for(int i=0; i<img_field.height; i++) {
nuclear@6 490 float v = (float)i / (float)img_field.height;
nuclear@4 491
nuclear@6 492 for(int j=0; j<img_field.width; j++) {
nuclear@6 493 float u = (float)j / (float)img_field.width;
nuclear@4 494
nuclear@4 495 int r = 0, g = 0, b = 0;
nuclear@4 496
nuclear@5 497 float wood_val = wood(u, v);
nuclear@5 498
nuclear@5 499 // pattern mask
nuclear@4 500 float x = u;
nuclear@4 501 float y = v < 0.5 ? v * 2.0 : 2.0 - v * 2.0;
nuclear@4 502 bool inside = false;
nuclear@4 503
nuclear@4 504 inside |= (spike(x, y + 0.33333) && !spike(x, y + 0.4)) ||
nuclear@4 505 (spike(x, y + 0.5) && !spike(x, y + 0.68));
nuclear@4 506 inside |= (circle(x, y, 0.12) && !circle(x, y, 0.1)) || circle(x, y, 0.06);
nuclear@4 507 inside |= (diamond(x, y) && !diamond(x, y - 0.015)) ||
nuclear@4 508 (diamond(x, y - 0.023) && !diamond(x, y - 0.028));
nuclear@4 509 inside |= center_circle(x, y, 0.03);
nuclear@4 510
nuclear@5 511 Vector3 wood_color = lerp(wcol1, wcol2, wood_val) * 0.9;
nuclear@4 512 if(inside) {
nuclear@5 513 wood_color = lerp(wcol1, wcol2, 1.0 - wood_val) * 2.0;
nuclear@4 514 }
nuclear@4 515
nuclear@5 516 r = (int)(wood_color.x * 255.0);
nuclear@5 517 g = (int)(wood_color.y * 255.0);
nuclear@5 518 b = (int)(wood_color.z * 255.0);
nuclear@5 519
nuclear@5 520 pptr[0] = r > 255 ? 255 : r;
nuclear@5 521 pptr[1] = g > 255 ? 255 : g;
nuclear@5 522 pptr[2] = b > 255 ? 255 : b;
nuclear@4 523 pptr += 3;
nuclear@4 524 }
nuclear@4 525 }
nuclear@6 526 img_field.texture();
nuclear@4 527
nuclear@6 528 // ---- generic wood texture ----
nuclear@6 529 img_wood.create(256, 256);
nuclear@6 530 pptr = img_wood.pixels;
nuclear@6 531 for(int i=0; i<img_wood.height; i++) {
nuclear@6 532 float v = (float)i / (float)img_wood.height;
nuclear@6 533 for(int j=0; j<img_wood.width; j++) {
nuclear@6 534 float u = (float)j / (float)img_wood.width;
nuclear@6 535
nuclear@6 536 float wood_val = wood_tile(u, v);
nuclear@6 537 Vector3 wood_color = lerp(wcol2, wcol3, wood_val) * 0.7;
nuclear@6 538
nuclear@6 539 int r = (int)(wood_color.x * 255.0);
nuclear@6 540 int g = (int)(wood_color.y * 255.0);
nuclear@6 541 int b = (int)(wood_color.z * 255.0);
nuclear@6 542
nuclear@6 543 pptr[0] = r > 255 ? 255 : r;
nuclear@6 544 pptr[1] = g > 255 ? 255 : g;
nuclear@6 545 pptr[2] = b > 255 ? 255 : b;
nuclear@6 546 pptr += 3;
nuclear@6 547 }
nuclear@6 548 }
nuclear@6 549 img_wood.texture();
nuclear@7 550
nuclear@7 551 // ---- metal hinge diffuse texture ----
nuclear@7 552 Vector3 rusty_col1 = Vector3(0.43, 0.46, 0.52);
nuclear@7 553 Vector3 rusty_col2 = Vector3(0.52, 0.47, 0.43);
nuclear@7 554
nuclear@7 555 img_hinge.create(128, 128);
nuclear@7 556 pptr = img_hinge.pixels;
nuclear@7 557 for(int i=0; i<img_hinge.height; i++) {
nuclear@7 558 float v = (float)i / (float)img_hinge.height;
nuclear@7 559 for(int j=0; j<img_hinge.width; j++) {
nuclear@7 560 float u = (float)j / (float)img_hinge.width;
nuclear@7 561
nuclear@7 562 // rust pattern
nuclear@7 563 float w1 = fbm2(u * 4.0, v * 4.0, 3) * 0.5 + 0.5;
nuclear@9 564 //float w2 = fbm2(u * 8.0, v * 8.0, 1) * 0.5 + 0.5;
nuclear@7 565 Vector3 col = lerp(rusty_col1, rusty_col2 * 0.5, w1);
nuclear@7 566
nuclear@7 567 // center hinge split
nuclear@7 568 if(fabs(v - 0.5) < 0.01) {
nuclear@7 569 col *= 0.5;
nuclear@7 570 }
nuclear@7 571
nuclear@7 572 int r = (int)(col.x * 255.0);
nuclear@7 573 int g = (int)(col.y * 255.0);
nuclear@7 574 int b = (int)(col.z * 255.0);
nuclear@7 575
nuclear@7 576 pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
nuclear@7 577 pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
nuclear@7 578 pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
nuclear@7 579
nuclear@7 580 pptr += 3;
nuclear@7 581 }
nuclear@7 582 }
nuclear@7 583 img_hinge.texture();
nuclear@7 584
nuclear@4 585 return true;
nuclear@4 586 }