intravenous

view src/vein.cc @ 4:c6a6a64df6de

normalmap
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Apr 2012 05:20:03 +0300
parents 94d4c60af435
children aab0d8ea21cd
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 = 16.0;
15 subdiv = 32;
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 Vector3 Vein::calc_center(const Vector3 &ppos) const
33 {
34 Vector3 pt{0, 0, ppos.z};
35 pt.x = sin(ppos.z * 0.75);
36 pt.y = cos(ppos.z * 0.2) * 0.6;
37 return pt;
38 }
40 Vector3 Vein::calc_dir(const Vector3 &ppos) const
41 {
42 Vector3 dir = calc_center(ppos + Vector3(0, 0, 0.01)) - calc_center(ppos - Vector3(0, 0, 0.01));
43 return dir.normalized();
44 }
46 void Vein::build_idxbuf()
47 {
48 delete [] idxbuf;
50 int nfaces = subdiv * ring_subdiv;
51 int nidx = nfaces * 4;
52 idxbuf = new unsigned int[nidx];
53 unsigned int *idxptr = idxbuf;
55 for(int i=0; i<subdiv; i++) {
56 for(int j=0; j<ring_subdiv; j++) {
57 idxptr[0] = i * ring_subdiv + j;
58 idxptr[1] = i * ring_subdiv + ((j + 1) % ring_subdiv);
59 idxptr[2] = idxptr[1] + ring_subdiv;
60 idxptr[3] = idxptr[0] + ring_subdiv;
61 idxptr += 4;
62 }
63 }
64 }
66 bool Vein::init()
67 {
68 if(!(sdr = create_program_load("sdr/vein.v.glsl", "sdr/vein.p.glsl"))) {
69 fprintf(stderr, "failed to load vein shaders\n");
70 return false;
71 }
72 if((attr_tang_loc = get_attrib_loc(sdr, "attr_tang")) == -1) {
73 fprintf(stderr, "can't find tangent attribute!\n");
74 }
75 set_uniform_int(sdr, "tex_norm", 0);
77 if(!(tex_norm = load_texture("data/normal1.png"))) {
78 return false;
79 }
80 return true;
81 }
83 void Vein::draw(const Vector3 &ppos) const
84 {
85 float start_z = ppos.z - gen_dist / 2.0;
86 float dz = gen_dist / subdiv;
88 int nslices = subdiv + 1;
89 int nverts = nslices * ring_subdiv;
90 int nfaces = subdiv * ring_subdiv;
91 Vertex *vbuf = (Vertex*)alloca(nverts * sizeof *vbuf);
92 Vertex *vptr = vbuf;
94 Vector3 pt = ppos;
95 pt.z = start_z;
97 for(int i=0; i<nslices; i++) {
98 Vector3 cent = calc_center(pt);
99 Vector3 dir = calc_dir(pt);
100 Vector3 up(0, 1, 0);
101 Vector3 right = cross_product(up, dir);
102 up = cross_product(dir, right);
104 Matrix3x3 vrot{right, up, dir};
105 vrot.transpose();
107 float theta = 0.0, dtheta = 2.0 * M_PI / (ring_subdiv - 1);
108 for(int j=0; j<ring_subdiv; j++) {
109 Vector3 vec = Vector3{-cos(theta) * rad, sin(theta) * rad, 0.0};
110 vec.transform(vrot);
111 vec += cent;
113 vptr->pos = vec;
114 vptr->norm = cent - vec;
115 vptr->tang = dir;
116 vptr->tc = Vector2(start_z + gen_dist * i / nslices, theta / (2.0 * M_PI));
117 vptr++;
119 theta += dtheta;
120 }
122 pt.z += dz;
123 }
125 // also create the index buffer if it's not valid
126 if(!idxbuf) {
127 ((Vein*)this)->build_idxbuf();
128 }
130 // awesome, now draw it
131 glMatrixMode(GL_TEXTURE);
132 glPushMatrix();
133 glScalef(0.125, 1.0, 1.0);
135 bind_texture(tex_norm);
136 bind_program(sdr);
137 draw_mesh(GL_QUADS, nfaces * 4, vbuf, idxbuf, attr_tang_loc);
138 bind_program(0);
139 bind_texture(0);
141 glPopMatrix();
142 }