intravenous

diff src/vein.cc @ 1:3ea290d35984

it's never going to finish but wth :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Apr 2012 22:42:43 +0300
parents
children 94d4c60af435
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/vein.cc	Sat Apr 21 22:42:43 2012 +0300
     1.3 @@ -0,0 +1,104 @@
     1.4 +#ifndef _MSC_VER
     1.5 +#include <alloca.h>
     1.6 +#else
     1.7 +#include <malloc.h>
     1.8 +#endif
     1.9 +#include "vein.h"
    1.10 +#include "geom.h"
    1.11 +
    1.12 +Vein::Vein()
    1.13 +{
    1.14 +	gen_dist = 8.0;
    1.15 +	subdiv = 8;
    1.16 +	ring_subdiv = 16;
    1.17 +	rad = 1.0;
    1.18 +
    1.19 +	idxbuf = 0;
    1.20 +}
    1.21 +
    1.22 +Vein::~Vein()
    1.23 +{
    1.24 +	delete [] idxbuf;
    1.25 +}
    1.26 +
    1.27 +Vector3 Vein::calc_center(const Vector3 &ppos) const
    1.28 +{
    1.29 +	// TODO add variation
    1.30 +	return Vector3(0.0, 0.0, ppos.z);
    1.31 +}
    1.32 +
    1.33 +Vector3 Vein::calc_dir(const Vector3 &ppos) const
    1.34 +{
    1.35 +	// TODO add variation
    1.36 +	return Vector3(0, 0, 1);
    1.37 +}
    1.38 +
    1.39 +void Vein::build_idxbuf()
    1.40 +{
    1.41 +	delete [] idxbuf;
    1.42 +
    1.43 +	int nfaces = subdiv * ring_subdiv;
    1.44 +	int nidx = nfaces * 4;
    1.45 +	idxbuf = new unsigned int[nidx];
    1.46 +	unsigned int *idxptr = idxbuf;
    1.47 +
    1.48 +	for(int i=0; i<subdiv; i++) {
    1.49 +		for(int j=0; j<ring_subdiv; j++) {
    1.50 +			idxptr[0] = i * ring_subdiv + j;
    1.51 +			idxptr[1] = i * ring_subdiv + ((j + 1) % ring_subdiv);
    1.52 +			idxptr[2] = idxptr[1] + ring_subdiv;
    1.53 +			idxptr[3] = idxptr[0] + ring_subdiv;
    1.54 +			idxptr += 4;
    1.55 +		}
    1.56 +	}
    1.57 +}
    1.58 +
    1.59 +void Vein::draw(const Vector3 &ppos) const
    1.60 +{
    1.61 +	float start_z = ppos.z - gen_dist / 2.0;
    1.62 +	float dz = gen_dist / subdiv;
    1.63 +
    1.64 +	int nslices = subdiv + 1;
    1.65 +	int nverts = nslices * ring_subdiv;
    1.66 +	int nfaces = subdiv * ring_subdiv;
    1.67 +	Vertex *vbuf = (Vertex*)alloca(nverts * sizeof *vbuf);
    1.68 +	Vertex *vptr = vbuf;
    1.69 +
    1.70 +	Vector3 pt = ppos;
    1.71 +	pt.z = start_z;
    1.72 +
    1.73 +	for(int i=0; i<nslices; i++) {
    1.74 +		Vector3 cent = calc_center(pt);
    1.75 +		Vector3 dir = calc_dir(pt);
    1.76 +		Vector3 up(0, 1, 0);
    1.77 +		Vector3 right = cross_product(up, dir);
    1.78 +		up = cross_product(dir, right);
    1.79 +
    1.80 +		Matrix3x3 vrot{right, up, dir};
    1.81 +		//Quaternion vrot(dri, dtheta);
    1.82 +
    1.83 +		float theta = 0.0, dtheta = 2.0 * M_PI / ring_subdiv;
    1.84 +		for(int j=0; j<ring_subdiv; j++) {
    1.85 +			Vector3 vec = Vector3{-cos(theta) * rad, sin(theta) * rad, 0.0} + cent;
    1.86 +			vec.transform(vrot);
    1.87 +
    1.88 +			vptr->pos = vec;
    1.89 +			vptr->norm = cent - vec;
    1.90 +			vptr->tang = Vector3();	// TODO
    1.91 +			vptr->tc = Vector2();	// TODO
    1.92 +			vptr++;
    1.93 +
    1.94 +			theta += dtheta;
    1.95 +		}
    1.96 +
    1.97 +		pt.z += dz;
    1.98 +	}
    1.99 +
   1.100 +	// also create the index buffer if it's not valid
   1.101 +	if(!idxbuf) {
   1.102 +		((Vein*)this)->build_idxbuf();
   1.103 +	}
   1.104 +
   1.105 +	// awesome, now draw it
   1.106 +	draw_mesh(GL_QUADS, nfaces * 4, vbuf, idxbuf);
   1.107 +}