tavli
diff src/board.cc @ 6:a0d30f6f20d4
- texture coordinate generation in class Mesh
- wood texture
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 26 Jun 2015 04:22:06 +0300 |
parents | e48b40a3c82a |
children | f1ecc2439802 |
line diff
1.1 --- a/src/board.cc Thu Jun 25 20:43:34 2015 +0300 1.2 +++ b/src/board.cc Fri Jun 26 04:22:06 2015 +0300 1.3 @@ -1,7 +1,7 @@ 1.4 #include "opengl.h" 1.5 #include "board.h" 1.6 #include "meshgen.h" 1.7 - 1.8 +#include "pnoise.h" 1.9 1.10 Board::Board() 1.11 { 1.12 @@ -109,9 +109,15 @@ 1.13 sides->append(tmp); 1.14 tmp.clear(); 1.15 1.16 + // generate texture coordinates 1.17 + sides->texcoord_gen_box(); 1.18 + 1.19 Object *osides = new Object; 1.20 osides->set_mesh(sides); 1.21 osides->xform() = obottom->xform(); 1.22 + osides->set_texture(img_wood.texture()); 1.23 + osides->tex_xform().set_scaling(Vector3(2, 2, 2)); 1.24 + osides->tex_xform().rotate(-Vector3(1, 0, 0.5), M_PI / 4.0); 1.25 obj.push_back(osides); 1.26 1.27 } 1.28 @@ -137,6 +143,17 @@ 1.29 ohinges->set_mesh(hinges); 1.30 obj.push_back(ohinges); 1.31 1.32 + // debug object 1.33 + /*Mesh *dbgmesh = new Mesh; 1.34 + gen_box(dbgmesh, 0.5, 0.5, 0.5); 1.35 + xform.set_translation(Vector3(0, 0.4, 0)); 1.36 + xform.set_scaling(Vector3(4, 1, 4)); 1.37 + dbgmesh->apply_xform(xform); 1.38 + Object *dbgobj = new Object; 1.39 + dbgobj->set_mesh(dbgmesh); 1.40 + dbgobj->set_texture(img_wood.texture()); 1.41 + dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3)); 1.42 + obj.push_back(dbgobj);*/ 1.43 1.44 return true; 1.45 } 1.46 @@ -157,6 +174,20 @@ 1.47 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val); 1.48 } 1.49 1.50 +static float wood_tile(float x, float y) 1.51 +{ 1.52 + float u = x; 1.53 + float v = y; 1.54 + x *= 10.0; 1.55 + y *= 10.0; 1.56 + 1.57 + float val = x + pnoise2(u * 6.0, v, 6, 1) * 3.0 + 1.58 + pturbulence2(u * 4, v * 2, 4, 2, 2) * 1.5 + pturbulence2(u * 8, v * 8, 8, 8, 2) * 0.5; 1.59 + 1.60 + val = fmod(val, 1.0); 1.61 + return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val); 1.62 +} 1.63 + 1.64 static bool spike(float x, float y) 1.65 { 1.66 x = fmod(x * 5.0, 1.0); 1.67 @@ -185,20 +216,18 @@ 1.68 1.69 bool Board::generate_textures() 1.70 { 1.71 + // ---- board field texture ---- 1.72 static const Vector3 wcol1 = Vector3(0.6, 0.4, 0.2); 1.73 - static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1);//Vector3(0.38, 0.25, 0.08); 1.74 + static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1); 1.75 + static const Vector3 wcol3 = Vector3(0.38, 0.25, 0.08); 1.76 1.77 - const int xsz = 512; 1.78 - const int ysz = 1024; 1.79 + img_field.create(512, 1024); 1.80 + unsigned char *pptr = img_field.pixels; 1.81 + for(int i=0; i<img_field.height; i++) { 1.82 + float v = (float)i / (float)img_field.height; 1.83 1.84 - img_field.create(xsz, ysz); 1.85 - 1.86 - unsigned char *pptr = img_field.pixels; 1.87 - for(int i=0; i<ysz; i++) { 1.88 - float v = (float)i / (float)ysz; 1.89 - 1.90 - for(int j=0; j<xsz; j++) { 1.91 - float u = (float)j / (float)xsz; 1.92 + for(int j=0; j<img_field.width; j++) { 1.93 + float u = (float)j / (float)img_field.width; 1.94 1.95 int r = 0, g = 0, b = 0; 1.96 1.97 @@ -231,7 +260,29 @@ 1.98 pptr += 3; 1.99 } 1.100 } 1.101 + img_field.texture(); 1.102 1.103 - img_field.texture(); 1.104 + // ---- generic wood texture ---- 1.105 + img_wood.create(256, 256); 1.106 + pptr = img_wood.pixels; 1.107 + for(int i=0; i<img_wood.height; i++) { 1.108 + float v = (float)i / (float)img_wood.height; 1.109 + for(int j=0; j<img_wood.width; j++) { 1.110 + float u = (float)j / (float)img_wood.width; 1.111 + 1.112 + float wood_val = wood_tile(u, v); 1.113 + Vector3 wood_color = lerp(wcol2, wcol3, wood_val) * 0.7; 1.114 + 1.115 + int r = (int)(wood_color.x * 255.0); 1.116 + int g = (int)(wood_color.y * 255.0); 1.117 + int b = (int)(wood_color.z * 255.0); 1.118 + 1.119 + pptr[0] = r > 255 ? 255 : r; 1.120 + pptr[1] = g > 255 ? 255 : g; 1.121 + pptr[2] = b > 255 ? 255 : b; 1.122 + pptr += 3; 1.123 + } 1.124 + } 1.125 + img_wood.texture(); 1.126 return true; 1.127 }