tavli

changeset 7:f1ecc2439802

hinges
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Jun 2015 05:23:46 +0300
parents a0d30f6f20d4
children 464c45a0bc24
files src/board.cc src/board.h src/mesh.cc
diffstat 3 files changed, 85 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/src/board.cc	Fri Jun 26 04:22:06 2015 +0300
     1.2 +++ b/src/board.cc	Fri Jun 26 05:23:46 2015 +0300
     1.3 @@ -125,35 +125,63 @@
     1.4  
     1.5  	// generate the hinges
     1.6  	Mesh *hinges = new Mesh;
     1.7 -	gen_cylinder(hinges, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
     1.8 -	xform.reset_identity();
     1.9 -	xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, VSIZE / 4.0));
    1.10 -	xform.rotate(Vector3(M_PI / 2.0, 0, 0));
    1.11 -	hinges->apply_xform(xform);
    1.12 +	for(int i=0; i<2; i++) {
    1.13 +		float sign = i * 2 - 1;
    1.14  
    1.15 -	gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
    1.16 -	xform.reset_identity();
    1.17 -	xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, -VSIZE / 4.0));
    1.18 -	xform.rotate(Vector3(M_PI / 2.0, 0, 0));
    1.19 -	tmp.apply_xform(xform);
    1.20 +		// barrel
    1.21 +		gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
    1.22 +		xform.reset_identity();
    1.23 +		xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, sign * VSIZE / 4.0));
    1.24 +		xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
    1.25 +		tmp.apply_xform(xform);
    1.26 +		hinges->append(tmp);
    1.27  
    1.28 -	hinges->append(tmp);
    1.29 +		// flange
    1.30 +		gen_plane(&tmp, HINGE_HEIGHT * 0.6, HINGE_HEIGHT * 0.8);
    1.31 +		tmp.apply_xform(xform);
    1.32 +
    1.33 +		Matrix4x4 tex_xform;
    1.34 +		tex_xform.set_rotation(Vector3(0, 0, M_PI / 2.0));
    1.35 +		tmp.texcoord_apply_xform(tex_xform);
    1.36 +		hinges->append(tmp);
    1.37 +
    1.38 +		// studs
    1.39 +		for(int j=0; j<4; j++) {
    1.40 +			Vector3 pos;
    1.41 +
    1.42 +			pos.x = (float)((j & 1) * 2 - 1) * HINGE_HEIGHT * 0.2;
    1.43 +			pos.y = (float)((j & 2) - 1) * HINGE_HEIGHT * 0.3;
    1.44 +
    1.45 +			Matrix4x4 stud_xform = xform;
    1.46 +			stud_xform.translate(pos);
    1.47 +
    1.48 +			Matrix4x4 squash;
    1.49 +			squash.set_scaling(Vector3(1, 1, 0.5));
    1.50 +
    1.51 +			gen_sphere(&tmp, HINGE_RAD * 0.5, 8, 4);
    1.52 +			tmp.apply_xform(stud_xform * squash);
    1.53 +			hinges->append(tmp);
    1.54 +		}
    1.55 +	}
    1.56  
    1.57  	Object *ohinges = new Object;
    1.58  	ohinges->set_mesh(hinges);
    1.59 +	ohinges->set_texture(img_hinge.texture());
    1.60  	obj.push_back(ohinges);
    1.61  
    1.62  	// debug object
    1.63 -	/*Mesh *dbgmesh = new Mesh;
    1.64 +	/*
    1.65 +	Mesh *dbgmesh = new Mesh;
    1.66  	gen_box(dbgmesh, 0.5, 0.5, 0.5);
    1.67  	xform.set_translation(Vector3(0, 0.4, 0));
    1.68 -	xform.set_scaling(Vector3(4, 1, 4));
    1.69 +	xform.set_scaling(Vector3(1, 1, 1));
    1.70  	dbgmesh->apply_xform(xform);
    1.71  	Object *dbgobj = new Object;
    1.72  	dbgobj->set_mesh(dbgmesh);
    1.73 -	dbgobj->set_texture(img_wood.texture());
    1.74 -	dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3));
    1.75 -	obj.push_back(dbgobj);*/
    1.76 +	dbgobj->set_texture(img_hinge.texture());
    1.77 +	//dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3));
    1.78 +	obj.push_back(dbgobj);
    1.79 +	*/
    1.80  
    1.81  	return true;
    1.82  }
    1.83 @@ -284,5 +312,40 @@
    1.84  		}
    1.85  	}
    1.86  	img_wood.texture();
    1.87 +
    1.88 +	// ---- metal hinge diffuse texture ----
    1.89 +	Vector3 rusty_col1 = Vector3(0.43, 0.46, 0.52);
    1.90 +	Vector3 rusty_col2 = Vector3(0.52, 0.47, 0.43);
    1.91 +
    1.92 +	img_hinge.create(128, 128);
    1.93 +	pptr = img_hinge.pixels;
    1.94 +	for(int i=0; i<img_hinge.height; i++) {
    1.95 +		float v = (float)i / (float)img_hinge.height;
    1.96 +		for(int j=0; j<img_hinge.width; j++) {
    1.97 +			float u = (float)j / (float)img_hinge.width;
    1.98 +
    1.99 +			// rust pattern
   1.100 +			float w1 = fbm2(u * 4.0, v * 4.0, 3) * 0.5 + 0.5;
   1.101 +			float w2 = fbm2(u * 8.0, v * 8.0, 1) * 0.5 + 0.5;
   1.102 +			Vector3 col = lerp(rusty_col1, rusty_col2 * 0.5, w1);
   1.103 +
   1.104 +			// center hinge split
   1.105 +			if(fabs(v - 0.5) < 0.01) {
   1.106 +				col *= 0.5;
   1.107 +			}
   1.108 +
   1.109 +			int r = (int)(col.x * 255.0);
   1.110 +			int g = (int)(col.y * 255.0);
   1.111 +			int b = (int)(col.z * 255.0);
   1.112 +
   1.113 +			pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
   1.114 +			pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
   1.115 +			pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
   1.116 +
   1.117 +			pptr += 3;
   1.118 +		}
   1.119 +	}
   1.120 +	img_hinge.texture();
   1.121 +
   1.122  	return true;
   1.123  }
     2.1 --- a/src/board.h	Fri Jun 26 04:22:06 2015 +0300
     2.2 +++ b/src/board.h	Fri Jun 26 05:23:46 2015 +0300
     2.3 @@ -16,7 +16,7 @@
     2.4  	std::vector<Object*> obj;
     2.5  	Object *puck_obj;
     2.6  
     2.7 -	Image img_wood, img_field;
     2.8 +	Image img_wood, img_field, img_hinge;
     2.9  
    2.10  	bool generate();
    2.11  	bool generate_textures();
     3.1 --- a/src/mesh.cc	Fri Jun 26 04:22:06 2015 +0300
     3.2 +++ b/src/mesh.cc	Fri Jun 26 05:23:46 2015 +0300
     3.3 @@ -322,6 +322,11 @@
     3.4  {
     3.5  	unsigned int idxoffs = nverts;
     3.6  
     3.7 +	if(!nverts) {
     3.8 +		clone(mesh);
     3.9 +		return;
    3.10 +	}
    3.11 +
    3.12  	nverts += mesh.nverts;
    3.13  	nfaces += mesh.nfaces;
    3.14