Subversion Repositories gelsvn

Rev

Rev 566 | Rev 584 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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