tavli

annotate src/board.cc @ 7:f1ecc2439802

hinges
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Jun 2015 05:23:46 +0300
parents a0d30f6f20d4
children 464c45a0bc24
rev   line source
nuclear@0 1 #include "opengl.h"
nuclear@0 2 #include "board.h"
nuclear@1 3 #include "meshgen.h"
nuclear@6 4 #include "pnoise.h"
nuclear@0 5
nuclear@0 6 Board::Board()
nuclear@0 7 {
nuclear@2 8 puck_obj = 0;
nuclear@0 9 clear();
nuclear@0 10 }
nuclear@0 11
nuclear@0 12 Board::~Board()
nuclear@0 13 {
nuclear@0 14 destroy();
nuclear@0 15 }
nuclear@0 16
nuclear@0 17 bool Board::init()
nuclear@0 18 {
nuclear@4 19 if(!generate_textures()) {
nuclear@4 20 return false;
nuclear@4 21 }
nuclear@1 22 if(!generate()) {
nuclear@0 23 return false;
nuclear@0 24 }
nuclear@1 25
nuclear@0 26 return true;
nuclear@0 27 }
nuclear@0 28
nuclear@0 29 void Board::destroy()
nuclear@0 30 {
nuclear@2 31 for(size_t i=0; i<obj.size(); i++) {
nuclear@2 32 delete obj[i];
nuclear@1 33 }
nuclear@2 34 obj.clear();
nuclear@1 35
nuclear@2 36 delete puck_obj;
nuclear@2 37 puck_obj = 0;
nuclear@0 38 }
nuclear@0 39
nuclear@0 40 void Board::clear()
nuclear@0 41 {
nuclear@0 42 memset(slots, 0, sizeof slots);
nuclear@0 43 }
nuclear@0 44
nuclear@0 45 void Board::draw() const
nuclear@0 46 {
nuclear@2 47 for(size_t i=0; i<obj.size(); i++) {
nuclear@2 48 obj[i]->draw();
nuclear@1 49 }
nuclear@0 50 }
nuclear@0 51
nuclear@1 52 #define HSIZE 1.0
nuclear@1 53 #define VSIZE (2.0 * HSIZE)
nuclear@1 54 #define BOT_THICKNESS (HSIZE * 0.01)
nuclear@1 55 #define WALL_THICKNESS (HSIZE * 0.05)
nuclear@1 56 #define WALL_HEIGHT (HSIZE * 0.1)
nuclear@1 57 #define GAP (HSIZE * 0.025)
nuclear@1 58 #define HINGE_RAD (GAP * 0.5)
nuclear@1 59 #define HINGE_HEIGHT (VSIZE * 0.075)
nuclear@1 60
nuclear@1 61 bool Board::generate()
nuclear@0 62 {
nuclear@4 63 Mesh tmp;
nuclear@1 64 Matrix4x4 xform;
nuclear@1 65
nuclear@2 66 obj.clear();
nuclear@2 67
nuclear@4 68 for(int i=0; i<2; i++) {
nuclear@4 69 int sign = i * 2 - 1;
nuclear@1 70
nuclear@4 71 // generate bottom
nuclear@4 72 Mesh *bottom = new Mesh;
nuclear@4 73 gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0);
nuclear@4 74 xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0));
nuclear@4 75 bottom->apply_xform(xform);
nuclear@2 76
nuclear@4 77 Object *obottom = new Object;
nuclear@4 78 obottom->set_mesh(bottom);
nuclear@4 79 obottom->xform().set_translation(Vector3(sign * (HSIZE / 2.0 + WALL_THICKNESS + HINGE_RAD * 0.25), 0, 0));
nuclear@4 80 obottom->set_texture(img_field.texture());
nuclear@4 81 obj.push_back(obottom);
nuclear@2 82
nuclear@1 83
nuclear@4 84 // generate the 4 sides
nuclear@4 85 Mesh *sides = new Mesh;
nuclear@4 86 gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
nuclear@4 87 xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0,
nuclear@4 88 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
nuclear@4 89 sides->apply_xform(xform);
nuclear@1 90
nuclear@4 91 gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
nuclear@4 92 xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0,
nuclear@4 93 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
nuclear@4 94 tmp.apply_xform(xform);
nuclear@4 95 sides->append(tmp);
nuclear@4 96 tmp.clear();
nuclear@1 97
nuclear@4 98 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
nuclear@4 99 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
nuclear@4 100 (VSIZE + WALL_THICKNESS) / 2.0));
nuclear@4 101 tmp.apply_xform(xform);
nuclear@4 102 sides->append(tmp);
nuclear@4 103 tmp.clear();
nuclear@1 104
nuclear@4 105 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
nuclear@4 106 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
nuclear@4 107 -(VSIZE + WALL_THICKNESS) / 2.0));
nuclear@4 108 tmp.apply_xform(xform);
nuclear@4 109 sides->append(tmp);
nuclear@4 110 tmp.clear();
nuclear@4 111
nuclear@6 112 // generate texture coordinates
nuclear@6 113 sides->texcoord_gen_box();
nuclear@6 114
nuclear@4 115 Object *osides = new Object;
nuclear@4 116 osides->set_mesh(sides);
nuclear@4 117 osides->xform() = obottom->xform();
nuclear@6 118 osides->set_texture(img_wood.texture());
nuclear@6 119 osides->tex_xform().set_scaling(Vector3(2, 2, 2));
nuclear@6 120 osides->tex_xform().rotate(-Vector3(1, 0, 0.5), M_PI / 4.0);
nuclear@4 121 obj.push_back(osides);
nuclear@4 122
nuclear@4 123 }
nuclear@2 124
nuclear@2 125
nuclear@1 126 // generate the hinges
nuclear@1 127 Mesh *hinges = new Mesh;
nuclear@7 128 for(int i=0; i<2; i++) {
nuclear@7 129 float sign = i * 2 - 1;
nuclear@1 130
nuclear@7 131 // barrel
nuclear@7 132 gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
nuclear@7 133 xform.reset_identity();
nuclear@7 134 xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, sign * VSIZE / 4.0));
nuclear@7 135 xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
nuclear@7 136 tmp.apply_xform(xform);
nuclear@7 137 hinges->append(tmp);
nuclear@1 138
nuclear@7 139 // flange
nuclear@7 140 gen_plane(&tmp, HINGE_HEIGHT * 0.6, HINGE_HEIGHT * 0.8);
nuclear@7 141 tmp.apply_xform(xform);
nuclear@7 142
nuclear@7 143 Matrix4x4 tex_xform;
nuclear@7 144 tex_xform.set_rotation(Vector3(0, 0, M_PI / 2.0));
nuclear@7 145 tmp.texcoord_apply_xform(tex_xform);
nuclear@7 146 hinges->append(tmp);
nuclear@7 147
nuclear@7 148 // studs
nuclear@7 149 for(int j=0; j<4; j++) {
nuclear@7 150 Vector3 pos;
nuclear@7 151
nuclear@7 152 pos.x = (float)((j & 1) * 2 - 1) * HINGE_HEIGHT * 0.2;
nuclear@7 153 pos.y = (float)((j & 2) - 1) * HINGE_HEIGHT * 0.3;
nuclear@7 154
nuclear@7 155 Matrix4x4 stud_xform = xform;
nuclear@7 156 stud_xform.translate(pos);
nuclear@7 157
nuclear@7 158 Matrix4x4 squash;
nuclear@7 159 squash.set_scaling(Vector3(1, 1, 0.5));
nuclear@7 160
nuclear@7 161 gen_sphere(&tmp, HINGE_RAD * 0.5, 8, 4);
nuclear@7 162 tmp.apply_xform(stud_xform * squash);
nuclear@7 163 hinges->append(tmp);
nuclear@7 164 }
nuclear@7 165 }
nuclear@1 166
nuclear@2 167 Object *ohinges = new Object;
nuclear@2 168 ohinges->set_mesh(hinges);
nuclear@7 169 ohinges->set_texture(img_hinge.texture());
nuclear@2 170 obj.push_back(ohinges);
nuclear@1 171
nuclear@6 172 // debug object
nuclear@7 173 /*
nuclear@7 174 Mesh *dbgmesh = new Mesh;
nuclear@6 175 gen_box(dbgmesh, 0.5, 0.5, 0.5);
nuclear@6 176 xform.set_translation(Vector3(0, 0.4, 0));
nuclear@7 177 xform.set_scaling(Vector3(1, 1, 1));
nuclear@6 178 dbgmesh->apply_xform(xform);
nuclear@6 179 Object *dbgobj = new Object;
nuclear@6 180 dbgobj->set_mesh(dbgmesh);
nuclear@7 181 dbgobj->set_texture(img_hinge.texture());
nuclear@7 182 //dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3));
nuclear@7 183 obj.push_back(dbgobj);
nuclear@7 184 */
nuclear@2 185
nuclear@1 186 return true;
nuclear@0 187 }
nuclear@4 188
nuclear@5 189 static float wood(float x, float y)
nuclear@5 190 {
nuclear@5 191 float u = x;
nuclear@5 192 float v = y;
nuclear@5 193 x += 1.0;
nuclear@5 194 x *= 10.0;
nuclear@5 195 y *= 20.0;
nuclear@5 196
nuclear@5 197 float len = sqrt(x * x + y * y) + turbulence2(u * 6.0, v * 12.0, 2) * 1.2 +
nuclear@5 198 turbulence2(u * 0.5, v, 2) * 15.0;
nuclear@5 199 float val = fmod(len, 1.0);
nuclear@5 200
nuclear@5 201 //val = val * 0.5 + 0.5;
nuclear@5 202 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
nuclear@5 203 }
nuclear@5 204
nuclear@6 205 static float wood_tile(float x, float y)
nuclear@6 206 {
nuclear@6 207 float u = x;
nuclear@6 208 float v = y;
nuclear@6 209 x *= 10.0;
nuclear@6 210 y *= 10.0;
nuclear@6 211
nuclear@6 212 float val = x + pnoise2(u * 6.0, v, 6, 1) * 3.0 +
nuclear@6 213 pturbulence2(u * 4, v * 2, 4, 2, 2) * 1.5 + pturbulence2(u * 8, v * 8, 8, 8, 2) * 0.5;
nuclear@6 214
nuclear@6 215 val = fmod(val, 1.0);
nuclear@6 216 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
nuclear@6 217 }
nuclear@6 218
nuclear@4 219 static bool spike(float x, float y)
nuclear@4 220 {
nuclear@4 221 x = fmod(x * 5.0, 1.0);
nuclear@4 222 return y < (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x);
nuclear@4 223 }
nuclear@4 224
nuclear@4 225 static bool circle(float x, float y, float rad)
nuclear@4 226 {
nuclear@4 227 x = fmod(x * 5.0, 1.0) - 0.5;
nuclear@4 228 y = (y - 0.65) * 5.0;
nuclear@4 229 float len = sqrt(x * x + y * y);
nuclear@4 230 return len < rad;
nuclear@4 231 }
nuclear@4 232
nuclear@4 233 static bool diamond(float x, float y)
nuclear@4 234 {
nuclear@4 235 return y >= (1.0 - (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x)) * 0.3333333 + 0.88;
nuclear@4 236 }
nuclear@4 237
nuclear@4 238 static bool center_circle(float x, float y, float rad)
nuclear@4 239 {
nuclear@4 240 x = x - 0.5;
nuclear@4 241 y = 1.0 - y;
nuclear@4 242 return sqrt(x * x + y * y) < rad;
nuclear@4 243 }
nuclear@4 244
nuclear@4 245 bool Board::generate_textures()
nuclear@4 246 {
nuclear@6 247 // ---- board field texture ----
nuclear@5 248 static const Vector3 wcol1 = Vector3(0.6, 0.4, 0.2);
nuclear@6 249 static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1);
nuclear@6 250 static const Vector3 wcol3 = Vector3(0.38, 0.25, 0.08);
nuclear@5 251
nuclear@6 252 img_field.create(512, 1024);
nuclear@6 253 unsigned char *pptr = img_field.pixels;
nuclear@6 254 for(int i=0; i<img_field.height; i++) {
nuclear@6 255 float v = (float)i / (float)img_field.height;
nuclear@4 256
nuclear@6 257 for(int j=0; j<img_field.width; j++) {
nuclear@6 258 float u = (float)j / (float)img_field.width;
nuclear@4 259
nuclear@4 260 int r = 0, g = 0, b = 0;
nuclear@4 261
nuclear@5 262 float wood_val = wood(u, v);
nuclear@5 263
nuclear@5 264 // pattern mask
nuclear@4 265 float x = u;
nuclear@4 266 float y = v < 0.5 ? v * 2.0 : 2.0 - v * 2.0;
nuclear@4 267 bool inside = false;
nuclear@4 268
nuclear@4 269 inside |= (spike(x, y + 0.33333) && !spike(x, y + 0.4)) ||
nuclear@4 270 (spike(x, y + 0.5) && !spike(x, y + 0.68));
nuclear@4 271 inside |= (circle(x, y, 0.12) && !circle(x, y, 0.1)) || circle(x, y, 0.06);
nuclear@4 272 inside |= (diamond(x, y) && !diamond(x, y - 0.015)) ||
nuclear@4 273 (diamond(x, y - 0.023) && !diamond(x, y - 0.028));
nuclear@4 274 inside |= center_circle(x, y, 0.03);
nuclear@4 275
nuclear@5 276 Vector3 wood_color = lerp(wcol1, wcol2, wood_val) * 0.9;
nuclear@4 277 if(inside) {
nuclear@5 278 wood_color = lerp(wcol1, wcol2, 1.0 - wood_val) * 2.0;
nuclear@4 279 }
nuclear@4 280
nuclear@5 281 r = (int)(wood_color.x * 255.0);
nuclear@5 282 g = (int)(wood_color.y * 255.0);
nuclear@5 283 b = (int)(wood_color.z * 255.0);
nuclear@5 284
nuclear@5 285 pptr[0] = r > 255 ? 255 : r;
nuclear@5 286 pptr[1] = g > 255 ? 255 : g;
nuclear@5 287 pptr[2] = b > 255 ? 255 : b;
nuclear@4 288 pptr += 3;
nuclear@4 289 }
nuclear@4 290 }
nuclear@6 291 img_field.texture();
nuclear@4 292
nuclear@6 293 // ---- generic wood texture ----
nuclear@6 294 img_wood.create(256, 256);
nuclear@6 295 pptr = img_wood.pixels;
nuclear@6 296 for(int i=0; i<img_wood.height; i++) {
nuclear@6 297 float v = (float)i / (float)img_wood.height;
nuclear@6 298 for(int j=0; j<img_wood.width; j++) {
nuclear@6 299 float u = (float)j / (float)img_wood.width;
nuclear@6 300
nuclear@6 301 float wood_val = wood_tile(u, v);
nuclear@6 302 Vector3 wood_color = lerp(wcol2, wcol3, wood_val) * 0.7;
nuclear@6 303
nuclear@6 304 int r = (int)(wood_color.x * 255.0);
nuclear@6 305 int g = (int)(wood_color.y * 255.0);
nuclear@6 306 int b = (int)(wood_color.z * 255.0);
nuclear@6 307
nuclear@6 308 pptr[0] = r > 255 ? 255 : r;
nuclear@6 309 pptr[1] = g > 255 ? 255 : g;
nuclear@6 310 pptr[2] = b > 255 ? 255 : b;
nuclear@6 311 pptr += 3;
nuclear@6 312 }
nuclear@6 313 }
nuclear@6 314 img_wood.texture();
nuclear@7 315
nuclear@7 316 // ---- metal hinge diffuse texture ----
nuclear@7 317 Vector3 rusty_col1 = Vector3(0.43, 0.46, 0.52);
nuclear@7 318 Vector3 rusty_col2 = Vector3(0.52, 0.47, 0.43);
nuclear@7 319
nuclear@7 320 img_hinge.create(128, 128);
nuclear@7 321 pptr = img_hinge.pixels;
nuclear@7 322 for(int i=0; i<img_hinge.height; i++) {
nuclear@7 323 float v = (float)i / (float)img_hinge.height;
nuclear@7 324 for(int j=0; j<img_hinge.width; j++) {
nuclear@7 325 float u = (float)j / (float)img_hinge.width;
nuclear@7 326
nuclear@7 327 // rust pattern
nuclear@7 328 float w1 = fbm2(u * 4.0, v * 4.0, 3) * 0.5 + 0.5;
nuclear@7 329 float w2 = fbm2(u * 8.0, v * 8.0, 1) * 0.5 + 0.5;
nuclear@7 330 Vector3 col = lerp(rusty_col1, rusty_col2 * 0.5, w1);
nuclear@7 331
nuclear@7 332 // center hinge split
nuclear@7 333 if(fabs(v - 0.5) < 0.01) {
nuclear@7 334 col *= 0.5;
nuclear@7 335 }
nuclear@7 336
nuclear@7 337 int r = (int)(col.x * 255.0);
nuclear@7 338 int g = (int)(col.y * 255.0);
nuclear@7 339 int b = (int)(col.z * 255.0);
nuclear@7 340
nuclear@7 341 pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
nuclear@7 342 pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
nuclear@7 343 pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
nuclear@7 344
nuclear@7 345 pptr += 3;
nuclear@7 346 }
nuclear@7 347 }
nuclear@7 348 img_hinge.texture();
nuclear@7 349
nuclear@4 350 return true;
nuclear@4 351 }