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