Subversion Repositories gelsvn

Rev

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

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