intravenous

view src/vein.cc @ 10:8fbdc6f84f64

fixed after the change in vmath
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 31 May 2013 01:30:14 +0300
parents 3fae5eb6a411 2723dc026c4f
children
line source
1 #include <stdio.h>
2 #ifndef _MSC_VER
3 #include <alloca.h>
4 #else
5 #include <malloc.h>
6 #endif
7 #include "vein.h"
8 #include "geom.h"
9 #include "sdr.h"
10 #include "tex.h"
12 Vein::Vein()
13 {
14 gen_dist = 40.0;
15 subdiv = 64;
16 ring_subdiv = 24;
17 rad = 2.0;
19 idxbuf = 0;
21 sdr = tex_norm = 0;
22 }
24 Vein::~Vein()
25 {
26 delete [] idxbuf;
28 free_texture(tex_norm);
29 free_program(sdr);
30 }
32 void Vein::build_idxbuf()
33 {
34 delete [] idxbuf;
36 int nfaces = subdiv * ring_subdiv;
37 int nidx = nfaces * 4;
38 idxbuf = new unsigned int[nidx];
39 unsigned int *idxptr = idxbuf;
41 for(int i=0; i<subdiv; i++) {
42 for(int j=0; j<ring_subdiv; j++) {
43 idxptr[0] = i * ring_subdiv + j;
44 idxptr[1] = i * ring_subdiv + ((j + 1) % ring_subdiv);
45 idxptr[2] = idxptr[1] + ring_subdiv;
46 idxptr[3] = idxptr[0] + ring_subdiv;
47 idxptr += 4;
48 }
49 }
50 }
52 bool Vein::init()
53 {
54 if(!(sdr = create_program_load("sdr/vein.v.glsl", "sdr/vein.p.glsl"))) {
55 fprintf(stderr, "failed to load vein shaders\n");
56 return false;
57 }
58 if((attr_tang_loc = get_attrib_loc(sdr, "attr_tang")) == -1) {
59 fprintf(stderr, "can't find tangent attribute!\n");
60 }
61 set_uniform_int(sdr, "tex_norm", 0);
63 if(!(tex_norm = load_texture("data/normal1.png"))) {
64 return false;
65 }
66 return true;
67 }
69 void Vein::set_radius(float rad)
70 {
71 this->rad = rad;
72 }
74 float Vein::get_radius() const
75 {
76 return rad;
77 }
79 void Vein::set_fog_color(const Vector3 &col)
80 {
81 fog_color = col;
83 if(sdr) {
84 set_uniform_float3(sdr, "fog_col", col.x, col.y, col.z);
85 }
86 }
88 void Vein::draw(const Vector3 &ppos) const
89 {
90 float start_z = ppos.z - gen_dist / 2.0;
91 float dz = gen_dist / subdiv;
93 int nslices = subdiv + 1;
94 int nverts = nslices * ring_subdiv;
95 int nfaces = subdiv * ring_subdiv;
96 Vertex *vbuf = (Vertex*)alloca(nverts * sizeof *vbuf);
97 Vertex *vptr = vbuf;
99 Vector3 pt = ppos;
100 pt.z = start_z;
102 for(int i=0; i<nslices; i++) {
103 Vector3 cent = calc_center(pt);
104 Vector3 dir = calc_dir(pt);
105 Vector3 up(0, 1, 0);
106 Vector3 right = cross_product(up, dir);
107 up = cross_product(dir, right);
109 Matrix3x3 vrot(right, up, dir);
110 vrot.transpose();
112 float theta = 0.0, dtheta = 2.0 * M_PI / (ring_subdiv - 1);
113 for(int j=0; j<ring_subdiv; j++) {
114 Vector3 vec = Vector3(-cos(theta) * rad, sin(theta) * rad, 0.0);
115 vec.transform(vrot);
116 vec += cent;
118 vptr->pos = vec;
119 vptr->norm = cent - vec;
120 vptr->tang = dir;
121 vptr->tc = Vector2(start_z + gen_dist * i / nslices, theta / (2.0 * M_PI));
122 vptr++;
124 theta += dtheta;
125 }
127 pt.z += dz;
128 }
130 // also create the index buffer if it's not valid
131 if(!idxbuf) {
132 ((Vein*)this)->build_idxbuf();
133 }
135 // awesome, now draw it
136 glMatrixMode(GL_TEXTURE);
137 glPushMatrix();
138 glScalef(0.125, 1.0, 1.0);
140 bind_texture(tex_norm);
141 bind_program(sdr);
142 draw_mesh(GL_QUADS, nfaces * 4, vbuf, idxbuf, attr_tang_loc);
143 bind_program(0);
144 bind_texture(0);
146 glPopMatrix();
147 }
149 Vector3 Vein::calc_center(const Vector3 &ppos) const
150 {
151 Vector3 pt(0, 0, ppos.z);
152 pt.x = sin(ppos.z * 0.75);
153 pt.y = cos(ppos.z * 0.2) * 0.6;
154 return pt;
155 }
157 Vector3 Vein::calc_dir(const Vector3 &ppos) const
158 {
159 Vector3 dir = calc_center(ppos + Vector3(0, 0, 0.01)) - calc_center(ppos - Vector3(0, 0, 0.01));
160 return dir.normalized();
161 }