intravenous

view src/vein.cc @ 5:aab0d8ea21cd

normalmap and sortof subsurface shader
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Apr 2012 06:26:08 +0300
parents c6a6a64df6de
children 2723dc026c4f 3fae5eb6a411
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 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::set_fog_color(const Vector3 &col)
84 {
85 fog_color = col;
87 if(sdr) {
88 set_uniform_float3(sdr, "fog_col", col.x, col.y, col.z);
89 }
90 }
92 void Vein::draw(const Vector3 &ppos) const
93 {
94 float start_z = ppos.z - gen_dist / 2.0;
95 float dz = gen_dist / subdiv;
97 int nslices = subdiv + 1;
98 int nverts = nslices * ring_subdiv;
99 int nfaces = subdiv * ring_subdiv;
100 Vertex *vbuf = (Vertex*)alloca(nverts * sizeof *vbuf);
101 Vertex *vptr = vbuf;
103 Vector3 pt = ppos;
104 pt.z = start_z;
106 for(int i=0; i<nslices; i++) {
107 Vector3 cent = calc_center(pt);
108 Vector3 dir = calc_dir(pt);
109 Vector3 up(0, 1, 0);
110 Vector3 right = cross_product(up, dir);
111 up = cross_product(dir, right);
113 Matrix3x3 vrot{right, up, dir};
114 vrot.transpose();
116 float theta = 0.0, dtheta = 2.0 * M_PI / (ring_subdiv - 1);
117 for(int j=0; j<ring_subdiv; j++) {
118 Vector3 vec = Vector3{-cos(theta) * rad, sin(theta) * rad, 0.0};
119 vec.transform(vrot);
120 vec += cent;
122 vptr->pos = vec;
123 vptr->norm = cent - vec;
124 vptr->tang = dir;
125 vptr->tc = Vector2(start_z + gen_dist * i / nslices, theta / (2.0 * M_PI));
126 vptr++;
128 theta += dtheta;
129 }
131 pt.z += dz;
132 }
134 // also create the index buffer if it's not valid
135 if(!idxbuf) {
136 ((Vein*)this)->build_idxbuf();
137 }
139 // awesome, now draw it
140 glMatrixMode(GL_TEXTURE);
141 glPushMatrix();
142 glScalef(0.125, 1.0, 1.0);
144 bind_texture(tex_norm);
145 bind_program(sdr);
146 draw_mesh(GL_QUADS, nfaces * 4, vbuf, idxbuf, attr_tang_loc);
147 bind_program(0);
148 bind_texture(0);
150 glPopMatrix();
151 }