Subversion Repositories gelsvn

Rev

Rev 601 | Rev 614 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 601 Rev 607
1
/* ----------------------------------------------------------------------- *
1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
5
 * ----------------------------------------------------------------------- */
6
 
6
 
7
#include "ManifoldRenderer.h"
7
#include "ManifoldRenderer.h"
8
 
8
 
9
#include <algorithm>
9
#include <algorithm>
10
#include <string>
10
#include <string>
11
#include <cstdlib>
11
#include <cstdlib>
12
#include "../CGLA/Mat3x3d.h"
12
#include "../CGLA/Mat3x3d.h"
13
#include "../GLGraphics/glsl_shader.h"
13
#include "../GLGraphics/glsl_shader.h"
14
#include "../HMesh/Manifold.h"
14
#include "../HMesh/Manifold.h"
15
#include "../HMesh/AttributeVector.h"
15
#include "../HMesh/AttributeVector.h"
16
#include "../HMesh/curvature.h"
16
#include "../HMesh/curvature.h"
17
 
17
 
18
using namespace CGLA;
18
using namespace CGLA;
19
using namespace HMesh;
19
using namespace HMesh;
20
using namespace std;
20
using namespace std;
21
 
21
 
22
namespace GLGraphics
22
namespace GLGraphics
23
{
23
{
24
    
24
    
25
    GLuint get_noise_texture_id()
25
    GLuint get_noise_texture_id()
26
    {
26
    {
27
        static GLuint texname=0;
27
        static GLuint texname=0;
28
        static bool was_here = false;
28
        static bool was_here = false;
29
        
29
        
30
        if(!was_here)
30
        if(!was_here)
31
        {
31
        {
32
            was_here = true;
32
            was_here = true;
33
            int width = 32;
33
            int width = 32;
34
            int height = 32;
34
            int height = 32;
35
            int depth = 32;
35
            int depth = 32;
36
            vector<unsigned char> texels(width*height*depth);
36
            vector<unsigned char> texels(width*height*depth);
37
            for (int i = 0; i < width*height*depth; ++i)
37
            for (int i = 0; i < width*height*depth; ++i)
38
            {
38
            {
39
                int intensity = 255.0 * (float(gel_rand()) / GEL_RAND_MAX);
39
                int intensity = 255.0 * (float(gel_rand()) / GEL_RAND_MAX);
40
                texels[i] = (unsigned char) intensity;
40
                texels[i] = (unsigned char) intensity;
41
            }
41
            }
42
            
42
            
43
            glGenTextures(1, &texname);	
43
            glGenTextures(1, &texname);	
44
            glBindTexture(GL_TEXTURE_3D, texname);	
44
            glBindTexture(GL_TEXTURE_3D, texname);	
45
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
45
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
46
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
46
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
47
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
47
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
48
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
48
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
49
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
49
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
50
            glTexImage3D(GL_TEXTURE_3D, 0, GL_INTENSITY8, width, height, depth, 0, GL_RED, GL_UNSIGNED_BYTE, &texels[0]);
50
            glTexImage3D(GL_TEXTURE_3D, 0, GL_INTENSITY8, width, height, depth, 0, GL_RED, GL_UNSIGNED_BYTE, &texels[0]);
51
        }
51
        }
52
        
52
        
53
        return texname;
53
        return texname;
54
    }
54
    }
55
    
55
    
56
    
56
    
57
    int WireframeRenderer::maximum_face_valency(const Manifold& m)
57
    int WireframeRenderer::maximum_face_valency(const Manifold& m)
58
    {
58
    {
59
        int max_val = 0;
59
        int max_val = 0;
60
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f)
60
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f)
61
            max_val = max(max_val, no_edges(m, *f));
61
            max_val = max(max_val, no_edges(m, *f));
62
        return max_val;
62
        return max_val;
63
    }
63
    }
64
    
64
    
65
    WireframeRenderer::WireframeRenderer(HMesh::Manifold& m, bool smooth): idbuff_renderer(0)
65
    WireframeRenderer::WireframeRenderer(HMesh::Manifold& m, bool smooth): idbuff_renderer(0)
66
    {
66
    {
67
        if(GLEW_EXT_geometry_shader4 && maximum_face_valency(m) > 3)
67
        if(GLEW_EXT_geometry_shader4 && maximum_face_valency(m) > 3)
68
        {
68
        {
69
            GLint viewp[4];
69
            GLint viewp[4];
70
            glGetIntegerv(GL_VIEWPORT,viewp);
70
            glGetIntegerv(GL_VIEWPORT,viewp);
71
            idbuff_renderer = new IDBufferWireframeRenderer(viewp[2], viewp[3], m);
71
            idbuff_renderer = new IDBufferWireframeRenderer(viewp[2], viewp[3], m);
72
        }
72
        }
73
        else
73
        else
74
        {
74
        {
75
            glNewList(display_list,GL_COMPILE);
75
            glNewList(display_list,GL_COMPILE);
76
            if(GLEW_EXT_geometry_shader4)
76
            if(GLEW_EXT_geometry_shader4)
77
                draw_triangles_in_wireframe(m,smooth, Vec3f(1,0,0));				
77
                draw_triangles_in_wireframe(m,smooth, Vec3f(1,0,0));				
78
            else
78
            else
79
                draw_wireframe_oldfashioned(m,smooth, Vec3f(1,0,0));
79
                draw_wireframe_oldfashioned(m,smooth, Vec3f(1,0,0));
80
            glEndList();
80
            glEndList();
81
        }
81
        }
82
    }
82
    }
83
    
83
    
84
    void WireframeRenderer::draw()
84
    void WireframeRenderer::draw()
85
    {
85
    {
86
        if(idbuff_renderer)
86
        if(idbuff_renderer)
87
        {
87
        {
88
            glEnable(GL_LIGHTING);
88
            glEnable(GL_LIGHTING);
89
            idbuff_renderer->draw(Vec3f(1,0,0),Vec3f(1));
89
            idbuff_renderer->draw(Vec3f(1,0,0),Vec3f(1));
90
            glDisable(GL_LIGHTING);
90
            glDisable(GL_LIGHTING);
91
        }
91
        }
92
        else
92
        else
93
            glCallList(display_list);
93
            glCallList(display_list);
94
    }
94
    }
95
    
95
    
96
    void SimpleShaderRenderer::init_shaders(const std::string& vss, 
96
    void SimpleShaderRenderer::init_shaders(const std::string& vss, 
97
                                            const std::string& fss)
97
                                            const std::string& fss)
98
    {
98
    {
99
        vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
99
        vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
100
        print_glsl_program_log(vs);
100
        print_glsl_program_log(vs);
101
        
101
        
102
        fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
102
        fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
103
        print_glsl_program_log(fs);
103
        print_glsl_program_log(fs);
104
        
104
        
105
        prog = glCreateProgram();
105
        prog = glCreateProgram();
106
        
106
        
107
        if(vs) glAttachShader(prog, vs);
107
        if(vs) glAttachShader(prog, vs);
108
        if(fs) glAttachShader(prog, fs);
108
        if(fs) glAttachShader(prog, fs);
109
        
109
        
110
        glLinkProgram(prog);
110
        glLinkProgram(prog);
111
        print_glsl_program_log(prog);
111
        print_glsl_program_log(prog);
112
        
112
        
113
    }
113
    }
114
    
114
    
115
    void SimpleShaderRenderer::compile_display_list(const Manifold& m, bool smooth)
115
    void SimpleShaderRenderer::compile_display_list(const Manifold& m, bool smooth)
116
    {
116
    {
117
        glNewList(display_list,GL_COMPILE);
117
        glNewList(display_list,GL_COMPILE);
118
        GLGraphics::draw(m, smooth);
118
        GLGraphics::draw(m, smooth);
119
        glEndList();	
119
        glEndList();	
120
    }
120
    }
121
    
121
    
122
    void SimpleShaderRenderer::draw()
122
    void SimpleShaderRenderer::draw()
123
    {
123
    {
124
        GLint old_prog;
124
        GLint old_prog;
125
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
125
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
126
        glUseProgram(prog);
126
        glUseProgram(prog);
127
        glCallList(display_list);
127
        glCallList(display_list);
128
        glUseProgram(old_prog);
128
        glUseProgram(old_prog);
129
    }
129
    }
130
    
130
    
131
    const string NormalRenderer::vss = 
131
    const string NormalRenderer::vss = 
132
    "varying vec3 _n;\n"
132
    "varying vec3 _n;\n"
133
    "varying vec3 v;\n"
133
    "varying vec3 v;\n"
134
    "\n"
134
    "\n"
135
    "void main(void)\n"
135
    "void main(void)\n"
136
    "{\n"
136
    "{\n"
137
    "	gl_Position = ftransform();\n"
137
    "	gl_Position = ftransform();\n"
138
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
138
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
139
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
139
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
140
    "}\n";
140
    "}\n";
141
    
141
    
142
    const string NormalRenderer::fss = 
142
    const string NormalRenderer::fss = 
143
    "varying vec3 _n;\n"
143
    "varying vec3 _n;\n"
144
    "varying vec3 v;\n"
144
    "varying vec3 v;\n"
145
    "\n"
145
    "\n"
146
    "void main(void)\n"
146
    "void main(void)\n"
147
    "{\n"
147
    "{\n"
148
    "   vec3 n = normalize(_n);\n"
148
    "   vec3 n = normalize(_n);\n"
149
    "	vec3 l = normalize(-v);\n"
149
    "	vec3 l = normalize(-v);\n"
150
    "	vec3 e = l;\n"
150
    "	vec3 e = l;\n"
151
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
151
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
152
    "	\n"
152
    "	\n"
153
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
153
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
154
    "   float dot_ln = abs(dot(l, n));\n"
154
    "   float dot_ln = abs(dot(l, n));\n"
155
    "	vec4 d = vec4(0.7) * dot_ln;\n"
155
    "	vec4 d = vec4(0.7) * dot_ln;\n"
156
    "	vec4 s = vec4(0.3)*smoothstep(0.98,0.9999,dot(r, e));\n"
156
    "	vec4 s = vec4(0.3)*smoothstep(0.98,0.9999,dot(r, e));\n"
157
    "	\n"
157
    "	\n"
158
    "	gl_FragColor =  d+s;\n"
158
    "	gl_FragColor =  d+s;\n"
159
    "}\n";
159
    "}\n";
160
    
160
    
161
    
161
    
162
    const string ReflectionLineRenderer::vss = 
162
    const string ReflectionLineRenderer::vss = 
163
    "varying vec3 _n;\n"
163
    "varying vec3 _n;\n"
164
    "varying vec3 v;\n"
164
    "varying vec3 v;\n"
165
    "\n"
165
    "\n"
166
    "void main(void)\n"
166
    "void main(void)\n"
167
    "{\n"
167
    "{\n"
168
    "	gl_Position = ftransform();\n"
168
    "	gl_Position = ftransform();\n"
169
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
169
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
170
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
170
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
171
    "}\n";
171
    "}\n";
172
    
172
    
173
    
173
    
174
    const string ReflectionLineRenderer::fss = 
174
    const string ReflectionLineRenderer::fss = 
175
    "uniform float detail;\n"
175
    "uniform float detail;\n"
176
    "\n"
176
    "\n"
177
    "varying vec3 _n;\n"
177
    "varying vec3 _n;\n"
178
    "varying vec3 v;\n"
178
    "varying vec3 v;\n"
179
    "\n"
179
    "\n"
180
    "void main(void)\n"
180
    "void main(void)\n"
181
    "{\n"
181
    "{\n"
182
    "   vec3 n = normalize(_n);\n"
182
    "   vec3 n = normalize(_n);\n"
183
    "	// calculate the reflection\n"
183
    "	// calculate the reflection\n"
184
    "	vec3 r = normalize(2.0*dot(-v, n)*n + v);\n"
184
    "	vec3 r = normalize(2.0*dot(-v, n)*n + v);\n"
185
    "	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
185
    "	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
186
    "   float diff  = dot(n,viewer_lightdir);\n"
186
    "   float diff  = dot(n,viewer_lightdir);\n"
187
    "	\n"
187
    "	\n"
188
    "	vec2 r2 = normalize(vec2(r[0], r[2]));\n"
188
    "	vec2 r2 = normalize(vec2(r[0], r[2]));\n"
189
    "	vec2 x = vec2(1, 0);\n"
189
    "	vec2 x = vec2(1, 0);\n"
190
    "	float angle = acos(dot(r2, x));\n"
190
    "	float angle = acos(dot(r2, x));\n"
191
    "	\n"
191
    "	\n"
192
    "	// decide if we hit a white or black ring, based on y value\n"
192
    "	// decide if we hit a white or black ring, based on y value\n"
193
    "	gl_FragColor = diff * vec4(1.0) + smoothstep(0.8, 1.0,cos(13.0*angle)) * vec4(-1.0);\n"
193
    "	gl_FragColor = diff * vec4(1.0) + smoothstep(0.8, 1.0,cos(13.0*angle)) * vec4(-1.0);\n"
194
    "}\n";
194
    "}\n";
195
    
195
    
196
    const string IsophoteLineRenderer::vss = 
196
    const string IsophoteLineRenderer::vss = 
197
    "varying vec3 _n;\n"
197
    "varying vec3 _n;\n"
198
    "varying vec3 v;\n"
198
    "varying vec3 v;\n"
199
    "\n"
199
    "\n"
200
    "void main(void)\n"
200
    "void main(void)\n"
201
    "{\n"
201
    "{\n"
202
    "	gl_Position = ftransform();\n"
202
    "	gl_Position = ftransform();\n"
203
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
203
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
204
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
204
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
205
    "}\n";
205
    "}\n";
206
    
206
    
207
    
207
    
208
    const string IsophoteLineRenderer::fss = 
208
    const string IsophoteLineRenderer::fss = 
209
    "uniform float detail;\n"
209
    "uniform float detail;\n"
210
    "\n"
210
    "\n"
211
    "varying vec3 _n;\n"
211
    "varying vec3 _n;\n"
212
    "varying vec3 v;\n"
212
    "varying vec3 v;\n"
213
    "\n"
213
    "\n"
214
    "void main(void)\n"
214
    "void main(void)\n"
215
    "{\n"
215
    "{\n"
216
    "   vec3 n = normalize(_n);\n"
216
    "   vec3 n = normalize(_n);\n"
217
    "	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
217
    "	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
218
    "	vec3 isophote_lightdir = viewer_lightdir;\n"
218
    "	vec3 isophote_lightdir = viewer_lightdir;\n"
219
    "	float angle = acos(dot(n, isophote_lightdir));\n"
219
    "	float angle = acos(dot(n, isophote_lightdir));\n"
220
    "   float diff  = dot(n,viewer_lightdir);\n"
220
    "   float diff  = dot(n,viewer_lightdir);\n"
221
    "	\n"
221
    "	\n"
222
    "	// decide if we hit a white or black ring, based on y value\n"
222
    "	// decide if we hit a white or black ring, based on y value\n"
223
    "	gl_FragColor = diff * vec4(1.0) + smoothstep(0.8, 1.0,cos(20.0*angle)) * vec4(-1.0);\n"
223
    "	gl_FragColor = diff * vec4(1.0) + smoothstep(0.8, 1.0,cos(20.0*angle)) * vec4(-1.0);\n"
224
    "}\n";
224
    "}\n";
225
    
225
    
226
    const string ToonRenderer::vss = 
226
    const string ToonRenderer::vss = 
227
    "varying vec3 _n;\n"
227
    "varying vec3 _n;\n"
228
    "varying vec3 v;\n"
228
    "varying vec3 v;\n"
229
    "\n"
229
    "\n"
230
    "void main(void)\n"
230
    "void main(void)\n"
231
    "{\n"
231
    "{\n"
232
    "	gl_Position = ftransform();\n"
232
    "	gl_Position = ftransform();\n"
233
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
233
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
234
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
234
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
235
    "}\n";
235
    "}\n";
236
    
236
    
237
    const string ToonRenderer::fss = 
237
    const string ToonRenderer::fss = 
238
    "varying vec3 _n;\n"
238
    "varying vec3 _n;\n"
239
    "varying vec3 v;\n"
239
    "varying vec3 v;\n"
240
    "\n"
240
    "\n"
241
    "void main(void)\n"
241
    "void main(void)\n"
242
    "{\n"
242
    "{\n"
243
    "   vec3 n = normalize(_n);\n"
243
    "   vec3 n = normalize(_n);\n"
244
    "	vec3 l = normalize(-v);\n"
244
    "	vec3 l = normalize(-v);\n"
245
    "	vec3 e = l;\n"
245
    "	vec3 e = l;\n"
246
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
246
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
247
    "	\n"
247
    "	\n"
248
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
248
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
249
    "   float dot_ln = abs(dot(l, n));\n"
249
    "   float dot_ln = abs(dot(l, n));\n"
250
    "	vec4 d = vec4(0.7,0.7,0.0,1.0) * 0.25 * (smoothstep(0.23,0.25,dot_ln)+smoothstep(0.45,0.47,dot_ln)+smoothstep(0.7,0.72,dot_ln)+smoothstep(0.9,0.92,dot_ln));\n"
250
    "	vec4 d = vec4(0.7,0.7,0.0,1.0) * 0.25 * (smoothstep(0.23,0.25,dot_ln)+smoothstep(0.45,0.47,dot_ln)+smoothstep(0.7,0.72,dot_ln)+smoothstep(0.9,0.92,dot_ln));\n"
251
    "	vec4 s = vec4(0.5,0.3,0.4,1.0)*smoothstep(0.96,0.98,dot(r, e));\n"
251
    "	vec4 s = vec4(0.5,0.3,0.4,1.0)*smoothstep(0.96,0.98,dot(r, e));\n"
252
    "	\n"
252
    "	\n"
253
    "	gl_FragColor =  d+s;\n"
253
    "	gl_FragColor =  d+s;\n"
254
    "}\n";
254
    "}\n";
255
    
255
    
256
    
256
    
257
    void GlazedRenderer::draw()
257
    void GlazedRenderer::draw()
258
    {
258
    {
259
        GLint old_prog;
259
        GLint old_prog;
260
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
260
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
261
        glUseProgram(prog);
261
        glUseProgram(prog);
262
        glBindTexture(GL_TEXTURE_3D, get_noise_texture_id());
262
        glBindTexture(GL_TEXTURE_3D, get_noise_texture_id());
263
        glUniform1iARB(glGetUniformLocationARB(prog, "noise_tex"),0);
263
        glUniform1iARB(glGetUniformLocationARB(prog, "noise_tex"),0);
264
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_scale"),12.0/bsphere_rad);
264
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_scale"),12.0/bsphere_rad);
265
        glCallList(display_list);
265
        glCallList(display_list);
266
        glUseProgram(old_prog);
266
        glUseProgram(old_prog);
267
    }
267
    }
268
    
268
    
269
    
269
    
270
    
270
    
271
    const string GlazedRenderer::vss = 
271
    const string GlazedRenderer::vss = 
272
    "varying vec3 _n;\n"
272
    "varying vec3 _n;\n"
273
    "varying vec3 v;\n"
273
    "varying vec3 v;\n"
274
    "varying vec3 v_obj;\n"
274
    "varying vec3 v_obj;\n"
275
    "\n"
275
    "\n"
276
    "void main(void)\n"
276
    "void main(void)\n"
277
    "{\n"
277
    "{\n"
278
    "	gl_Position = ftransform();\n"
278
    "	gl_Position = ftransform();\n"
279
    "   v_obj = gl_Vertex.xyz;\n"
279
    "   v_obj = gl_Vertex.xyz;\n"
280
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
280
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
281
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
281
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
282
    "}\n"
282
    "}\n"
283
    "\n";
283
    "\n";
284
    
284
    
285
    const string GlazedRenderer::fss =
285
    const string GlazedRenderer::fss =
286
    "uniform sampler3D noise_tex;\n"
286
    "uniform sampler3D noise_tex;\n"
287
    "uniform float noise_scale;\n"
287
    "uniform float noise_scale;\n"
288
    "varying vec3 _n;\n"
288
    "varying vec3 _n;\n"
289
    "varying vec3 v;\n"
289
    "varying vec3 v;\n"
290
    "varying vec3 v_obj;\n"
290
    "varying vec3 v_obj;\n"
291
    "\n"
291
    "\n"
292
    "vec4 glazed_shader(vec4 mat_col,  vec4 light_col, vec3 light_dir)\n"
292
    "vec4 glazed_shader(vec4 mat_col,  vec4 light_col, vec3 light_dir)\n"
293
    "{\n"
293
    "{\n"
294
    "   vec3 n = normalize(_n);\n"
294
    "   vec3 n = normalize(_n);\n"
295
    "	vec3 e = normalize(-v);\n"
295
    "	vec3 e = normalize(-v);\n"
296
    "	vec3 r = normalize(2.0*dot(e, n)*n - e);\n"
296
    "	vec3 r = normalize(2.0*dot(e, n)*n - e);\n"
297
    "	float d = max(0.05,dot(light_dir, n));\n"
297
    "	float d = max(0.05,dot(light_dir, n));\n"
298
    "	vec4 diff = mat_col * light_col *d; 	\n"
298
    "	vec4 diff = mat_col * light_col *d; 	\n"
299
    "	vec4 refl = smoothstep(0.7,0.75,dot(r,light_dir)) * light_col;\n"
299
    "	vec4 refl = smoothstep(0.7,0.75,dot(r,light_dir)) * light_col;\n"
300
    "	return 0.15*refl + diff;\n"
300
    "	return 0.15*refl + diff;\n"
301
    "}\n"
301
    "}\n"
302
    "\n"
302
    "\n"
303
    "void main(void)\n"
303
    "void main(void)\n"
304
    "{\n"
304
    "{\n"
305
    "	vec4 mat_col = vec4(0.9,1.0,0.4,1.0) + vec4(-0.1,-0.1,0.12,0.0) * texture3D(noise_tex, noise_scale*v_obj).x\n"
305
    "	vec4 mat_col = vec4(0.9,1.0,0.4,1.0) + vec4(-0.1,-0.1,0.12,0.0) * texture3D(noise_tex, noise_scale*v_obj).x\n"
306
    " + vec4(0.05) * texture3D(noise_tex, 500.0*v_obj).x;\n"
306
    " + vec4(0.05) * texture3D(noise_tex, 500.0*v_obj).x;\n"
307
    "	\n"
307
    "	\n"
308
    "	vec3 light0_dir = vec3(0.0,1.0,0.0);\n"
308
    "	vec3 light0_dir = vec3(0.0,1.0,0.0);\n"
309
    "	vec4 light0_col = vec4(0.7,0.9,1.0,1.0);\n"
309
    "	vec4 light0_col = vec4(0.7,0.9,1.0,1.0);\n"
310
    "	\n"
310
    "	\n"
311
    "	vec3 light1_dir = vec3(0.0,0.0,1.0);\n"
311
    "	vec3 light1_dir = vec3(0.0,0.0,1.0);\n"
312
    "	vec4 light1_col = vec4(1.0,1.0,0.7,1.0);\n"
312
    "	vec4 light1_col = vec4(1.0,1.0,0.7,1.0);\n"
313
    "	\n"
313
    "	\n"
314
    "	gl_FragColor = \n"
314
    "	gl_FragColor = \n"
315
    "	0.5*glazed_shader(mat_col, light0_col, light0_dir)+\n"
315
    "	0.5*glazed_shader(mat_col, light0_col, light0_dir)+\n"
316
    "	0.5*glazed_shader(mat_col, light1_col, light1_dir);\n"
316
    "	0.5*glazed_shader(mat_col, light1_col, light1_dir);\n"
317
    "	\n"
317
    "	\n"
318
    "	gl_FragColor.a = 1.0;\n"
318
    "	gl_FragColor.a = 1.0;\n"
319
    "}\n";
319
    "}\n";
320
    
320
    
321
    
321
    
322
    const string ScalarFieldRenderer::vss =
322
    const string ScalarFieldRenderer::vss =
323
    "	attribute float scalar;\n"
323
    "	attribute float scalar;\n"
324
    "	varying vec3 _normal;\n"
324
    "	varying vec3 _normal;\n"
325
    "	varying float s;\n"
325
    "	varying float s;\n"
326
    "	\n"
326
    "	\n"
327
    "	void main(void)\n"
327
    "	void main(void)\n"
328
    "	{\n"
328
    "	{\n"
329
    "		gl_Position =  ftransform();\n"
329
    "		gl_Position =  ftransform();\n"
330
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
330
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
331
    "		s=scalar;\n"
331
    "		s=scalar;\n"
332
    "	}\n";
332
    "	}\n";
333
    
333
    
334
    const string ScalarFieldRenderer::fss = 	
334
    const string ScalarFieldRenderer::fss = 	
335
    "	varying vec3 _normal;\n"
335
    "	varying vec3 _normal;\n"
336
    "	varying float s;\n"
336
    "	varying float s;\n"
337
    "	uniform float scalar_max;\n"
337
    "	uniform float scalar_max;\n"
338
    "   uniform float gamma;\n"
338
    "   uniform float gamma;\n"
339
    "	const vec3 light_dir = vec3(0,0,1);\n"
339
    "	const vec3 light_dir = vec3(0,0,1);\n"
340
    "	\n"
340
    "	\n"
341
    "	void main()\n"
341
    "	void main()\n"
342
    "	{\n"
342
    "	{\n"
343
    "       vec3 normal = normalize(_normal);\n"
343
    "       vec3 normal = normalize(_normal);\n"
344
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
344
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
345
    "		\n"
345
    "		\n"
346
    "		float s_norm = s/scalar_max;\n"
346
    "		float s_norm = s/scalar_max;\n"
347
    "		float stripe_signal = 100.0 * s_norm;\n"
347
    "		float stripe_signal = 100.0 * s_norm;\n"
348
    "		vec4 stripe_col = abs(stripe_signal) < 3.14 ? vec4(0,0,0,0) : vec4(.1,.1,.1,0);\n"
348
    "		vec4 stripe_col = abs(stripe_signal) < 3.14 ? vec4(0,0,0,0) : vec4(.1,.1,.1,0);\n"
349
    "		\n"
349
    "		\n"
350
    "		gl_FragColor = s_norm * vec4(-1,0,1,0);\n"
350
    "		gl_FragColor = s_norm * vec4(-1,0,1,0);\n"
351
    "       gl_FragColor *= dot_ln;\n"
351
    "       gl_FragColor *= dot_ln;\n"
352
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0/gamma);\n"
352
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0/gamma);\n"
353
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0/gamma);\n"
353
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0/gamma);\n"
354
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0/gamma);\n"
354
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0/gamma);\n"
355
    "		gl_FragColor += stripe_col * smoothstep(0.8,1.0,cos(stripe_signal));\n"
355
    "		gl_FragColor += stripe_col * smoothstep(0.8,1.0,cos(stripe_signal));\n"
356
    "	}\n";
356
    "	}\n";
357
    
357
    
358
    ScalarFieldRenderer::ScalarFieldRenderer(const Manifold& m, 
358
    ScalarFieldRenderer::ScalarFieldRenderer(const Manifold& m, 
359
                                             bool smooth, 
359
                                             bool smooth, 
360
                                             VertexAttributeVector<double>& field, 
360
                                             VertexAttributeVector<double>& field, 
361
                                             double max_val, float gamma): SimpleShaderRenderer(vss, fss)
361
                                             double max_val, float gamma): SimpleShaderRenderer(vss, fss)
362
    {
362
    {
363
        
363
        
364
        GLint old_prog;
364
        GLint old_prog;
365
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
365
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
366
        glUseProgram(prog);
366
        glUseProgram(prog);
367
        
367
        
368
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
368
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
369
        glUniform1fARB(glGetUniformLocationARB(prog, "scalar_max"), max_val);
369
        glUniform1fARB(glGetUniformLocationARB(prog, "scalar_max"), max_val);
370
        
370
        
371
        //    static float& gamma = CreateCVar("display.scalar_field_renderer.gamma",2.2f);
371
        //    static float& gamma = CreateCVar("display.scalar_field_renderer.gamma",2.2f);
372
        glUniform1fARB(glGetUniformLocationARB(prog, "gamma"), gamma);
372
        glUniform1fARB(glGetUniformLocationARB(prog, "gamma"), gamma);
373
        glNewList(display_list,GL_COMPILE);
373
        glNewList(display_list,GL_COMPILE);
374
        
374
        
375
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){      
375
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){      
376
            if(!smooth) 
376
            if(!smooth) 
377
                glNormal3dv(normal(m, *f).get());
377
                glNormal3dv(normal(m, *f).get());
378
            if(no_edges(m, *f)== 3) 
378
            if(no_edges(m, *f)== 3) 
379
                glBegin(GL_TRIANGLES);
379
                glBegin(GL_TRIANGLES);
380
            else 
380
            else 
381
                glBegin(GL_POLYGON);
381
                glBegin(GL_POLYGON);
382
            
382
            
383
            
383
            
384
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
384
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
385
                Vec3d n(normal(m, w.vertex()));
385
                Vec3d n(normal(m, w.vertex()));
386
                if(smooth) 
386
                if(smooth) 
387
                    glNormal3dv(n.get());
387
                    glNormal3dv(n.get());
388
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
388
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
389
                glVertex3dv(m.pos(w.vertex()).get());
389
                glVertex3dv(m.pos(w.vertex()).get());
390
            }
390
            }
391
            glEnd();
391
            glEnd();
392
        }
392
        }
393
        glEndList();	
393
        glEndList();	
394
        glUseProgram(old_prog);
394
        glUseProgram(old_prog);
395
        
395
        
396
    }
396
    }
397
 
397
 
398
    const string PeriodicScalarFieldRenderer::vss =
398
    const string PeriodicScalarFieldRenderer::vss =
399
    "	attribute float scalar;\n"
399
    "	attribute float scalar;\n"
400
    "	varying vec3 _normal;\n"
400
    "	varying vec3 _normal;\n"
401
    "	varying float s;\n"
401
    "	varying float s;\n"
402
    "	\n"
402
    "	\n"
403
    "	void main(void)\n"
403
    "	void main(void)\n"
404
    "	{\n"
404
    "	{\n"
405
    "		gl_Position =  ftransform();\n"
405
    "		gl_Position =  ftransform();\n"
406
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
406
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
407
    "		s=scalar;\n"
407
    "		s=scalar;\n"
408
    "	}\n";
408
    "	}\n";
409
    
409
    
410
    const string PeriodicScalarFieldRenderer::fss = 	
410
    const string PeriodicScalarFieldRenderer::fss = 	
411
    "	varying vec3 _normal;\n"
411
    "	varying vec3 _normal;\n"
412
    "	varying float s;\n"
412
    "	varying float s;\n"
413
    "   uniform float gamma;\n"
413
    "   uniform float gamma;\n"
414
    "	const vec3 light_dir = vec3(0,0,1);\n"
414
    "	const vec3 light_dir = vec3(0,0,1);\n"
415
    "	\n"
415
    "	\n"
416
    "	void main()\n"
416
    "	void main()\n"
417
    "	{\n"
417
    "	{\n"
418
    "       vec3 normal = normalize(_normal);\n"
418
    "       vec3 normal = normalize(_normal);\n"
419
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
419
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
420
    "		\n"
420
    "		\n"
421
    "		gl_FragColor = sin(s) * vec4(-1,0,1,0);\n"
421
    "		gl_FragColor = sin(s) * vec4(-1,0,1,0);\n"
422
    "       gl_FragColor *= dot_ln;\n"
422
    "       gl_FragColor *= dot_ln;\n"
423
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0/gamma);\n"
423
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0/gamma);\n"
424
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0/gamma);\n"
424
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0/gamma);\n"
425
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0/gamma);\n"
425
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0/gamma);\n"
426
    "	}\n";
426
    "	}\n";
427
    
427
    
428
    PeriodicScalarFieldRenderer::PeriodicScalarFieldRenderer(const Manifold& m, 
428
    PeriodicScalarFieldRenderer::PeriodicScalarFieldRenderer(const Manifold& m, 
429
                                             bool smooth, 
429
                                             bool smooth, 
430
                                             VertexAttributeVector<double>& field, 
430
                                             VertexAttributeVector<double>& field, 
431
                                             float gamma): SimpleShaderRenderer(vss, fss)
431
                                             float gamma): SimpleShaderRenderer(vss, fss)
432
    {
432
    {
433
        
433
        
434
        GLint old_prog;
434
        GLint old_prog;
435
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
435
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
436
        glUseProgram(prog);
436
        glUseProgram(prog);
437
        
437
        
438
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
438
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
439
        
439
        
440
        //    static float& gamma = CreateCVar("display.scalar_field_renderer.gamma",2.2f);
440
        //    static float& gamma = CreateCVar("display.scalar_field_renderer.gamma",2.2f);
441
        glUniform1fARB(glGetUniformLocationARB(prog, "gamma"), gamma);
441
        glUniform1fARB(glGetUniformLocationARB(prog, "gamma"), gamma);
442
        glNewList(display_list,GL_COMPILE);
442
        glNewList(display_list,GL_COMPILE);
443
        
443
        
444
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){      
444
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){      
445
            if(!smooth) 
445
            if(!smooth) 
446
                glNormal3dv(normal(m, *f).get());
446
                glNormal3dv(normal(m, *f).get());
447
            if(no_edges(m, *f)== 3) 
447
            if(no_edges(m, *f)== 3) 
448
                glBegin(GL_TRIANGLES);
448
                glBegin(GL_TRIANGLES);
449
            else 
449
            else 
450
                glBegin(GL_POLYGON);
450
                glBegin(GL_POLYGON);
451
            
451
            
452
            
452
            
453
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
453
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
454
                Vec3d n(normal(m, w.vertex()));
454
                Vec3d n(normal(m, w.vertex()));
455
                if(smooth) 
455
                if(smooth) 
456
                    glNormal3dv(n.get());
456
                    glNormal3dv(n.get());
457
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
457
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
458
                glVertex3dv(m.pos(w.vertex()).get());
458
                glVertex3dv(m.pos(w.vertex()).get());
459
            }
459
            }
460
            glEnd();
460
            glEnd();
461
        }
461
        }
462
        glEndList();	
462
        glEndList();	
463
        glUseProgram(old_prog);
463
        glUseProgram(old_prog);
464
        
464
        
465
    }
465
    }
466
 
466
 
467
    const string AmbientOcclusionRenderer::vss =
467
    const string AmbientOcclusionRenderer::vss =
468
    "	attribute float scalar;\n"
468
    "	attribute float scalar;\n"
469
    "	varying vec3 _normal;\n"
469
    "	varying vec3 _normal;\n"
470
    "	varying float s;\n"
470
    "	varying float s;\n"
471
    "	\n"
471
    "	\n"
472
    "	void main(void)\n"
472
    "	void main(void)\n"
473
    "	{\n"
473
    "	{\n"
474
    "		gl_Position =  ftransform();\n"
474
    "		gl_Position =  ftransform();\n"
475
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
475
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
476
    "		s=scalar;\n"
476
    "		s=scalar;\n"
477
    "	}\n";
477
    "	}\n";
478
    
478
    
479
    const string AmbientOcclusionRenderer::fss = 	
479
    const string AmbientOcclusionRenderer::fss = 	
480
    "	varying vec3 _normal;\n"
480
    "	varying vec3 _normal;\n"
481
    "	varying float s;\n"
481
    "	varying float s;\n"
482
    "	uniform float scalar_max;\n"
482
    "	uniform float scalar_max;\n"
483
    "	const vec3 light_dir = vec3(0,0,1);\n"
483
    "	const vec3 light_dir = vec3(0,0,1);\n"
484
    "	\n"
484
    "	\n"
485
    "	void main()\n"
485
    "	void main()\n"
486
    "	{\n"
486
    "	{\n"
487
    "   vec3 normal = normalize(_normal);\n"
487
    "   vec3 normal = normalize(_normal);\n"
488
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
488
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
489
    "		\n"
489
    "		\n"
490
    "		float s_norm = min(1.0,s/scalar_max+1.0);\n"
490
    "		float s_norm = min(1.0,s/scalar_max+1.0);\n"
491
    "		\n"
491
    "		\n"
492
    "		gl_FragColor = s_norm * vec4(1.0);\n"
492
    "		gl_FragColor = s_norm * vec4(1.0);\n"
493
    "       gl_FragColor *= dot_ln;\n"
493
    "       gl_FragColor *= dot_ln;\n"
494
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0);\n"
494
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0);\n"
495
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0);\n"
495
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0);\n"
496
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0);\n"
496
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0);\n"
497
    "	}\n";
497
    "	}\n";
498
    
498
    
499
    AmbientOcclusionRenderer::AmbientOcclusionRenderer(const Manifold& m, bool smooth, VertexAttributeVector<double>& field, double max_val):
499
    AmbientOcclusionRenderer::AmbientOcclusionRenderer(const Manifold& m, bool smooth, VertexAttributeVector<double>& field, double max_val):
500
    SimpleShaderRenderer(vss,fss)
500
    SimpleShaderRenderer(vss,fss)
501
    {	
501
    {	
502
        GLint old_prog;
502
        GLint old_prog;
503
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
503
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
504
        glUseProgram(prog);
504
        glUseProgram(prog);
505
        
505
        
506
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
506
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
507
        glUniform1fARB(glGetUniformLocationARB(prog, "scalar_max"), max_val);
507
        glUniform1fARB(glGetUniformLocationARB(prog, "scalar_max"), max_val);
508
        
508
        
509
        glNewList(display_list,GL_COMPILE);
509
        glNewList(display_list,GL_COMPILE);
510
        
510
        
511
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
511
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
512
            
512
            
513
            if(!smooth) 
513
            if(!smooth) 
514
                glNormal3dv(normal(m, *f).get());
514
                glNormal3dv(normal(m, *f).get());
515
            if(no_edges(m, *f)== 3) 
515
            if(no_edges(m, *f)== 3) 
516
                glBegin(GL_TRIANGLES);
516
                glBegin(GL_TRIANGLES);
517
            else 
517
            else 
518
                glBegin(GL_POLYGON);
518
                glBegin(GL_POLYGON);
519
            
519
            
520
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw())
520
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw())
521
            {
521
            {
522
                Vec3d n(normal(m, w.vertex()));
522
                Vec3d n(normal(m, w.vertex()));
523
                if(smooth) 
523
                if(smooth) 
524
                    glNormal3dv(n.get());
524
                    glNormal3dv(n.get());
525
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
525
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
526
                glVertex3dv(m.pos(w.vertex()).get());
526
                glVertex3dv(m.pos(w.vertex()).get());
527
            }
527
            }
528
            glEnd();
528
            glEnd();
529
        }
529
        }
530
        glEndList();	
530
        glEndList();	
531
        glUseProgram(old_prog);
531
        glUseProgram(old_prog);
532
        
532
        
533
    }
533
    }
534
    
534
    
535
    
535
    
536
    LineFieldRenderer::LineFieldRenderer(const Manifold& m, bool smooth, VertexAttributeVector<Vec3d>& lines, float _r): 
536
    LineFieldRenderer::LineFieldRenderer(const Manifold& m, bool smooth, VertexAttributeVector<Vec3d>& lines, float _r): 
537
    SimpleShaderRenderer(vss,fss), r(_r)
537
    SimpleShaderRenderer(vss,fss), r(_r)
538
    {
538
    {
539
        float noise_scale = 30.0/r;
539
        float noise_scale = 30.0f/r;
540
        float line_scale = 0.05/r;
540
        float line_scale = 0.05f/r;
541
        float noise_amplitude = 0.1;
541
        float noise_amplitude = 0.1f;
542
        
542
        
543
        GLint old_prog;
543
        GLint old_prog;
544
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
544
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
545
        glUseProgram(prog);	
545
        glUseProgram(prog);	
546
        glUniform1fARB(glGetUniformLocationARB(prog, "scale_line"),line_scale);
546
        glUniform1fARB(glGetUniformLocationARB(prog, "scale_line"),line_scale);
547
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_scale"),noise_scale);
547
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_scale"),noise_scale);
548
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_amplitude"),noise_amplitude);	
548
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_amplitude"),noise_amplitude);	
549
        glUniform1iARB(glGetUniformLocationARB(prog, "noise_tex"),0);
549
        glUniform1iARB(glGetUniformLocationARB(prog, "noise_tex"),0);
550
        GLuint direction = glGetAttribLocation(prog, "direction");	
550
        GLuint direction = glGetAttribLocation(prog, "direction");	
551
        glNewList(display_list,GL_COMPILE);
551
        glNewList(display_list,GL_COMPILE);
552
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
552
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
553
            if(!smooth) 
553
            if(!smooth) 
554
                glNormal3dv(normal(m, *f).get());
554
                glNormal3dv(normal(m, *f).get());
555
            if(no_edges(m, *f) == 3) 
555
            if(no_edges(m, *f) == 3) 
556
                glBegin(GL_TRIANGLES);
556
                glBegin(GL_TRIANGLES);
557
            else 
557
            else 
558
                glBegin(GL_POLYGON);
558
                glBegin(GL_POLYGON);
559
            
559
            
560
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
560
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
561
                Vec3d n(normal(m, w.vertex()));
561
                Vec3d n(normal(m, w.vertex()));
562
                if(smooth) 
562
                if(smooth) 
563
                    glNormal3dv(n.get());
563
                    glNormal3dv(n.get());
564
                
564
                
565
                Vec3d d = lines[w.vertex()];
565
                Vec3d d = lines[w.vertex()];
566
                d = normalize(d-n*dot(n,d));
566
                d = normalize(d-n*dot(n,d));
567
                glVertexAttrib3dv(direction, d.get());
567
                glVertexAttrib3dv(direction, d.get());
568
                glVertex3dv(m.pos(w.vertex()).get());
568
                glVertex3dv(m.pos(w.vertex()).get());
569
            }
569
            }
570
            glEnd();
570
            glEnd();
571
        }
571
        }
572
        
572
        
573
        glBindTexture(GL_TEXTURE_3D, 0);
573
        glBindTexture(GL_TEXTURE_3D, 0);
574
        glEndList();	
574
        glEndList();	
575
        glUseProgram(old_prog);
575
        glUseProgram(old_prog);
576
        
576
        
577
    }
577
    }
578
    
578
    
579
    void LineFieldRenderer::draw()
579
    void LineFieldRenderer::draw()
580
    {
580
    {
581
        GLint old_prog;
581
        GLint old_prog;
582
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
582
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
583
        glUseProgram(prog);
583
        glUseProgram(prog);
584
        glBindTexture(GL_TEXTURE_3D, get_noise_texture_id());
584
        glBindTexture(GL_TEXTURE_3D, get_noise_texture_id());
585
        glCallList(display_list);
585
        glCallList(display_list);
586
        glBindTexture(GL_TEXTURE_3D, 0);
586
        glBindTexture(GL_TEXTURE_3D, 0);
587
        glUseProgram(old_prog);	
587
        glUseProgram(old_prog);	
588
    }
588
    }
589
    
589
    
590
    const string LineFieldRenderer::vss = 
590
    const string LineFieldRenderer::vss = 
591
    "attribute vec3 direction;\n"
591
    "attribute vec3 direction;\n"
592
    "varying vec3 _n;\n"
592
    "varying vec3 _n;\n"
593
    "varying vec3 dir_obj;\n"
593
    "varying vec3 dir_obj;\n"
594
    "varying vec3 v_obj;\n"
594
    "varying vec3 v_obj;\n"
595
    "\n"
595
    "\n"
596
    "void main(void)\n"
596
    "void main(void)\n"
597
    "{\n"
597
    "{\n"
598
    "	gl_Position = ftransform();\n"
598
    "	gl_Position = ftransform();\n"
599
    "   v_obj = gl_Vertex.xyz;\n"
599
    "   v_obj = gl_Vertex.xyz;\n"
600
    "	dir_obj = direction;\n"
600
    "	dir_obj = direction;\n"
601
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
601
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
602
    "}\n";
602
    "}\n";
603
    
603
    
604
    const string LineFieldRenderer::fss =
604
    const string LineFieldRenderer::fss =
605
    "uniform sampler3D noise_tex;\n"
605
    "uniform sampler3D noise_tex;\n"
606
    "uniform float scale_line;\n"
606
    "uniform float scale_line;\n"
607
    "uniform float noise_amplitude;\n"
607
    "uniform float noise_amplitude;\n"
608
    "uniform float noise_scale;\n"
608
    "uniform float noise_scale;\n"
609
    "varying vec3 _n;\n"
609
    "varying vec3 _n;\n"
610
    "varying vec3 dir_obj;\n"
610
    "varying vec3 dir_obj;\n"
611
    "varying vec3 v_obj;\n"
611
    "varying vec3 v_obj;\n"
612
    "\n"
612
    "\n"
613
    "float tex(vec3 p) {return smoothstep(0.2,0.3,texture3D(noise_tex, p).x);}\n"
613
    "float tex(vec3 p) {return smoothstep(0.2,0.3,texture3D(noise_tex, p).x);}\n"
614
    "void main(void)\n"
614
    "void main(void)\n"
615
    "{\n"
615
    "{\n"
616
    "   vec3 n = normalize(_n);\n"
616
    "   vec3 n = normalize(_n);\n"
617
    "   float I = "
617
    "   float I = "
618
    "             tex(noise_scale*v_obj + 6.0*scale_line*dir_obj) + \n"
618
    "             tex(noise_scale*v_obj + 6.0*scale_line*dir_obj) + \n"
619
    "             tex(noise_scale*v_obj - 6.0*scale_line*dir_obj) + \n"
619
    "             tex(noise_scale*v_obj - 6.0*scale_line*dir_obj) + \n"
620
    "             tex(noise_scale*v_obj + 5.0*scale_line*dir_obj) + \n"
620
    "             tex(noise_scale*v_obj + 5.0*scale_line*dir_obj) + \n"
621
    "             tex(noise_scale*v_obj - 5.0*scale_line*dir_obj) + \n"
621
    "             tex(noise_scale*v_obj - 5.0*scale_line*dir_obj) + \n"
622
    "             tex(noise_scale*v_obj + 4.0*scale_line*dir_obj) + \n"
622
    "             tex(noise_scale*v_obj + 4.0*scale_line*dir_obj) + \n"
623
    "             tex(noise_scale*v_obj - 4.0*scale_line*dir_obj) + \n"
623
    "             tex(noise_scale*v_obj - 4.0*scale_line*dir_obj) + \n"
624
    "             tex(noise_scale*v_obj + 3.0*scale_line*dir_obj) + \n"
624
    "             tex(noise_scale*v_obj + 3.0*scale_line*dir_obj) + \n"
625
    "             tex(noise_scale*v_obj - 3.0*scale_line*dir_obj) + \n"
625
    "             tex(noise_scale*v_obj - 3.0*scale_line*dir_obj) + \n"
626
    "             tex(noise_scale*v_obj + 2.0*scale_line*dir_obj) + \n"
626
    "             tex(noise_scale*v_obj + 2.0*scale_line*dir_obj) + \n"
627
    "             tex(noise_scale*v_obj - 2.0*scale_line*dir_obj) + \n"
627
    "             tex(noise_scale*v_obj - 2.0*scale_line*dir_obj) + \n"
628
    "             tex(noise_scale*v_obj + 1.0*scale_line*dir_obj) + \n"
628
    "             tex(noise_scale*v_obj + 1.0*scale_line*dir_obj) + \n"
629
    "             tex(noise_scale*v_obj - 1.0*scale_line*dir_obj) + \n"
629
    "             tex(noise_scale*v_obj - 1.0*scale_line*dir_obj) + \n"
630
    "			  tex(noise_scale*v_obj); \n"
630
    "			  tex(noise_scale*v_obj); \n"
631
    "	\n"
631
    "	\n"
632
    "   float diff = max(0.0,dot(n,vec3(0.0, 0.0, 1.0)));\n"
632
    "   float diff = max(0.0,dot(n,vec3(0.0, 0.0, 1.0)));\n"
633
    "	gl_FragColor.rgb = vec3(diff*I*(1.0/13.0));\n"
633
    "	gl_FragColor.rgb = vec3(diff*I*(1.0/13.0));\n"
634
    "	gl_FragColor.a = 1.0;\n"
634
    "	gl_FragColor.a = 1.0;\n"
635
    "}\n";
635
    "}\n";
636
    
636
    
637
    const string DualVertexRenderer::vss = 
637
    const string DualVertexRenderer::vss = 
638
    "#version 120\n"
638
    "#version 120\n"
639
    "#extension GL_EXT_gpu_shader4 : enable\n"
639
    "#extension GL_EXT_gpu_shader4 : enable\n"
640
    "varying vec4 diffuseIn;\n"
640
    "varying vec4 diffuseIn;\n"
641
    "varying vec3 _normalIn;\n"
641
    "varying vec3 _normalIn;\n"
642
    "void main(void)\n"
642
    "void main(void)\n"
643
    "{\n"
643
    "{\n"
644
    "   diffuseIn = gl_Color;\n"
644
    "   diffuseIn = gl_Color;\n"
645
    "   _normalIn = normalize(gl_NormalMatrix*gl_Normal);\n"
645
    "   _normalIn = normalize(gl_NormalMatrix*gl_Normal);\n"
646
    "   gl_Position =  ftransform();\n"
646
    "   gl_Position =  ftransform();\n"
647
    "}\n";
647
    "}\n";
648
    
648
    
649
    const string DualVertexRenderer::gss = 
649
    const string DualVertexRenderer::gss = 
650
    "#version 120\n"
650
    "#version 120\n"
651
    "#extension GL_EXT_gpu_shader4 : enable\n"
651
    "#extension GL_EXT_gpu_shader4 : enable\n"
652
    "#extension GL_EXT_geometry_shader4 : enable\n"
652
    "#extension GL_EXT_geometry_shader4 : enable\n"
653
    "\n"
653
    "\n"
654
    "varying in vec4 diffuseIn[3];\n"
654
    "varying in vec4 diffuseIn[3];\n"
655
    "varying in vec3 _normalIn[3];\n"
655
    "varying in vec3 _normalIn[3];\n"
656
    "varying vec4 diffuse[3];\n"
656
    "varying vec4 diffuse[3];\n"
657
    "varying float f;\n"
657
    "varying float f;\n"
658
    "varying vec3 _normal;\n"
658
    "varying vec3 _normal;\n"
659
    "void main(void)\n"
659
    "void main(void)\n"
660
    "{\n"
660
    "{\n"
661
    "  diffuse[0] = diffuseIn[0];\n"
661
    "  diffuse[0] = diffuseIn[0];\n"
662
    "  diffuse[1] = diffuseIn[1];\n"
662
    "  diffuse[1] = diffuseIn[1];\n"
663
    "  diffuse[2] = diffuseIn[2];\n"
663
    "  diffuse[2] = diffuseIn[2];\n"
664
    "\n"
664
    "\n"
665
    "  f = diffuseIn[0].a;\n"
665
    "  f = diffuseIn[0].a;\n"
666
    "  gl_Position = gl_PositionIn[0];\n"
666
    "  gl_Position = gl_PositionIn[0];\n"
667
    "  _normal = _normalIn[0];\n"
667
    "  _normal = _normalIn[0];\n"
668
    "  EmitVertex();\n"
668
    "  EmitVertex();\n"
669
    "	\n"
669
    "	\n"
670
    "  f = diffuseIn[1].a;\n"
670
    "  f = diffuseIn[1].a;\n"
671
    "  gl_Position = gl_PositionIn[1];\n"
671
    "  gl_Position = gl_PositionIn[1];\n"
672
    "  _normal = _normalIn[1];\n"
672
    "  _normal = _normalIn[1];\n"
673
    "  EmitVertex();\n"
673
    "  EmitVertex();\n"
674
    "\n"
674
    "\n"
675
    "  f = diffuseIn[2].a;\n"
675
    "  f = diffuseIn[2].a;\n"
676
    "  gl_Position = gl_PositionIn[2];\n"
676
    "  gl_Position = gl_PositionIn[2];\n"
677
    "  _normal = _normalIn[2];\n"
677
    "  _normal = _normalIn[2];\n"
678
    "  EmitVertex();\n"
678
    "  EmitVertex();\n"
679
    "\n"
679
    "\n"
680
    "  EndPrimitive();\n"
680
    "  EndPrimitive();\n"
681
    "}\n";
681
    "}\n";
682
    
682
    
683
    const string DualVertexRenderer::fss =
683
    const string DualVertexRenderer::fss =
684
    "#version 120\n"
684
    "#version 120\n"
685
    "#extension GL_EXT_gpu_shader4 : enable\n"
685
    "#extension GL_EXT_gpu_shader4 : enable\n"
686
    "\n"
686
    "\n"
687
    "varying float f;\n"
687
    "varying float f;\n"
688
    "varying vec4 diffuse[3];\n"
688
    "varying vec4 diffuse[3];\n"
689
    "varying vec3 _normal;\n"
689
    "varying vec3 _normal;\n"
690
    "\n"
690
    "\n"
691
    "void main(void)\n"
691
    "void main(void)\n"
692
    "{\n"
692
    "{\n"
693
    "   vec3 normal = normalize(_normal);\n"
693
    "   vec3 normal = normalize(_normal);\n"
694
    "   float col_idx=0;\n"
694
    "   float col_idx=0;\n"
695
    "   if(f>diffuse[0].g && f<diffuse[0].b)\n"
695
    "   if(f>diffuse[0].g && f<diffuse[0].b)\n"
696
    "      col_idx = diffuse[0].r;\n"
696
    "      col_idx = diffuse[0].r;\n"
697
    "   else if(f>diffuse[1].g && f<diffuse[1].b)\n"
697
    "   else if(f>diffuse[1].g && f<diffuse[1].b)\n"
698
    "      col_idx = diffuse[1].r;\n"
698
    "      col_idx = diffuse[1].r;\n"
699
    "   else if(f>diffuse[2].g && f<diffuse[2].b)\n"
699
    "   else if(f>diffuse[2].g && f<diffuse[2].b)\n"
700
    "      col_idx = diffuse[2].r;\n"
700
    "      col_idx = diffuse[2].r;\n"
701
    "   vec4 col = col_idx < .5 ? vec4(1,0,0,0) : vec4(0,0,1,0);\n"
701
    "   vec4 col = col_idx < .5 ? vec4(1,0,0,0) : vec4(0,0,1,0);\n"
702
    "\n"
702
    "\n"
703
    " 	gl_FragColor =col*dot(normal, vec3(0,0,1));\n"
703
    " 	gl_FragColor =col*dot(normal, vec3(0,0,1));\n"
704
    "}\n";
704
    "}\n";
705
    
705
    
706
    
706
    
707
    
707
    
708
    
708
    
709
    DualVertexRenderer::DualVertexRenderer(const HMesh::Manifold& m, VertexAttributeVector<Vec4d>& field)
709
    DualVertexRenderer::DualVertexRenderer(const HMesh::Manifold& m, VertexAttributeVector<Vec4d>& field)
710
    {		
710
    {		
711
        // Create the program
711
        // Create the program
712
        static GLuint prog = glCreateProgram();
712
        static GLuint prog = glCreateProgram();
713
        
713
        
714
        static bool was_here = false;
714
        static bool was_here = false;
715
        if(!was_here)
715
        if(!was_here)
716
        {
716
        {
717
            was_here = true;
717
            was_here = true;
718
            // Create s	haders directly from file
718
            // Create s	haders directly from file
719
            static GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
719
            static GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
720
            static GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, gss);
720
            static GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, gss);
721
            static GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
721
            static GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
722
            
722
            
723
            // Attach all shaders
723
            // Attach all shaders
724
            if(vs) glAttachShader(prog, vs);
724
            if(vs) glAttachShader(prog, vs);
725
            if(gs) glAttachShader(prog, gs);
725
            if(gs) glAttachShader(prog, gs);
726
            if(fs) glAttachShader(prog, fs);
726
            if(fs) glAttachShader(prog, fs);
727
            
727
            
728
            // Specify input and output for the geometry shader. Note that this must be
728
            // Specify input and output for the geometry shader. Note that this must be
729
            // done before linking the program.
729
            // done before linking the program.
730
            glProgramParameteriEXT(prog,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
730
            glProgramParameteriEXT(prog,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
731
            glProgramParameteriEXT(prog,GL_GEOMETRY_VERTICES_OUT_EXT,3);
731
            glProgramParameteriEXT(prog,GL_GEOMETRY_VERTICES_OUT_EXT,3);
732
            glProgramParameteriEXT(prog,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
732
            glProgramParameteriEXT(prog,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
733
            
733
            
734
            // Link the program object and print out the info log
734
            // Link the program object and print out the info log
735
            glLinkProgram(prog);
735
            glLinkProgram(prog);
736
        }
736
        }
737
        
737
        
738
        
738
        
739
        
739
        
740
        GLint old_prog;
740
        GLint old_prog;
741
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
741
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
742
        
742
        
743
        glNewList(display_list,GL_COMPILE);
743
        glNewList(display_list,GL_COMPILE);
744
        glUseProgram(prog);
744
        glUseProgram(prog);
745
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
745
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
746
            if(no_edges(m, *f) != 3) 
746
            if(no_edges(m, *f) != 3) 
747
                continue;
747
                continue;
748
            else 
748
            else 
749
                glBegin(GL_TRIANGLES);
749
                glBegin(GL_TRIANGLES);
750
            
750
            
751
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
751
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
752
                Vec3d n(normal(m, w.vertex()));
752
                Vec3d n(normal(m, w.vertex()));
753
                glNormal3dv(n.get());
753
                glNormal3dv(n.get());
754
                glColor4dv(field[w.vertex()].get());
754
                glColor4dv(field[w.vertex()].get());
755
                glVertex3dv(m.pos(w.vertex()).get());
755
                glVertex3dv(m.pos(w.vertex()).get());
756
            }
756
            }
757
            glEnd();
757
            glEnd();
758
        }
758
        }
759
        glUseProgram(old_prog);
759
        glUseProgram(old_prog);
760
        glEndList();	
760
        glEndList();	
761
        
761
        
762
    }
762
    }
763
    
763
    
764
}
764
}
765
 
765
 
766
 
766
 
767
 
767