Subversion Repositories gelsvn

Rev

Rev 566 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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