Subversion Repositories gelsvn

Rev

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