intravenous

view src/vein.cc @ 2:472c28b8b875

I think I pretty much nailed the camera
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Apr 2012 23:03:36 +0300
parents
children 94d4c60af435
line source
1 #ifndef _MSC_VER
2 #include <alloca.h>
3 #else
4 #include <malloc.h>
5 #endif
6 #include "vein.h"
7 #include "geom.h"
9 Vein::Vein()
10 {
11 gen_dist = 8.0;
12 subdiv = 8;
13 ring_subdiv = 16;
14 rad = 1.0;
16 idxbuf = 0;
17 }
19 Vein::~Vein()
20 {
21 delete [] idxbuf;
22 }
24 Vector3 Vein::calc_center(const Vector3 &ppos) const
25 {
26 // TODO add variation
27 return Vector3(0.0, 0.0, ppos.z);
28 }
30 Vector3 Vein::calc_dir(const Vector3 &ppos) const
31 {
32 // TODO add variation
33 return Vector3(0, 0, 1);
34 }
36 void Vein::build_idxbuf()
37 {
38 delete [] idxbuf;
40 int nfaces = subdiv * ring_subdiv;
41 int nidx = nfaces * 4;
42 idxbuf = new unsigned int[nidx];
43 unsigned int *idxptr = idxbuf;
45 for(int i=0; i<subdiv; i++) {
46 for(int j=0; j<ring_subdiv; j++) {
47 idxptr[0] = i * ring_subdiv + j;
48 idxptr[1] = i * ring_subdiv + ((j + 1) % ring_subdiv);
49 idxptr[2] = idxptr[1] + ring_subdiv;
50 idxptr[3] = idxptr[0] + ring_subdiv;
51 idxptr += 4;
52 }
53 }
54 }
56 void Vein::draw(const Vector3 &ppos) const
57 {
58 float start_z = ppos.z - gen_dist / 2.0;
59 float dz = gen_dist / subdiv;
61 int nslices = subdiv + 1;
62 int nverts = nslices * ring_subdiv;
63 int nfaces = subdiv * ring_subdiv;
64 Vertex *vbuf = (Vertex*)alloca(nverts * sizeof *vbuf);
65 Vertex *vptr = vbuf;
67 Vector3 pt = ppos;
68 pt.z = start_z;
70 for(int i=0; i<nslices; i++) {
71 Vector3 cent = calc_center(pt);
72 Vector3 dir = calc_dir(pt);
73 Vector3 up(0, 1, 0);
74 Vector3 right = cross_product(up, dir);
75 up = cross_product(dir, right);
77 Matrix3x3 vrot{right, up, dir};
78 //Quaternion vrot(dri, dtheta);
80 float theta = 0.0, dtheta = 2.0 * M_PI / ring_subdiv;
81 for(int j=0; j<ring_subdiv; j++) {
82 Vector3 vec = Vector3{-cos(theta) * rad, sin(theta) * rad, 0.0} + cent;
83 vec.transform(vrot);
85 vptr->pos = vec;
86 vptr->norm = cent - vec;
87 vptr->tang = Vector3(); // TODO
88 vptr->tc = Vector2(); // TODO
89 vptr++;
91 theta += dtheta;
92 }
94 pt.z += dz;
95 }
97 // also create the index buffer if it's not valid
98 if(!idxbuf) {
99 ((Vein*)this)->build_idxbuf();
100 }
102 // awesome, now draw it
103 draw_mesh(GL_QUADS, nfaces * 4, vbuf, idxbuf);
104 }