tavli

annotate src/board.cc @ 17:16a420432aa3

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