Subversion Repositories gelsvn

Rev

Rev 614 | 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>
601 jab 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"
557 jab 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
    {
618 jab 539
        float noise_scale = 10.0f/r;
540
        float line_scale = 0.003f;
566 jab 541
 
557 jab 542
        GLint old_prog;
543
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
544
        glUseProgram(prog);	
618 jab 545
        glUniform1fARB(glGetUniformLocationARB(prog, "scale_line"),line_scale*noise_scale);
557 jab 546
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_scale"),noise_scale);
547
        glUniform1iARB(glGetUniformLocationARB(prog, "noise_tex"),0);
548
        GLuint direction = glGetAttribLocation(prog, "direction");	
549
        glNewList(display_list,GL_COMPILE);
550
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
551
            if(!smooth) 
587 jab 552
                glNormal3dv(normal(m, *f).get());
557 jab 553
            if(no_edges(m, *f) == 3) 
554
                glBegin(GL_TRIANGLES);
555
            else 
556
                glBegin(GL_POLYGON);
557
 
587 jab 558
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
557 jab 559
                Vec3d n(normal(m, w.vertex()));
560
                if(smooth) 
561
                    glNormal3dv(n.get());
562
 
563
                Vec3d d = lines[w.vertex()];
564
                d = normalize(d-n*dot(n,d));
565
                glVertexAttrib3dv(direction, d.get());
587 jab 566
                glVertex3dv(m.pos(w.vertex()).get());
557 jab 567
            }
568
            glEnd();
569
        }
570
 
571
        glBindTexture(GL_TEXTURE_3D, 0);
572
        glEndList();	
573
        glUseProgram(old_prog);
574
 
575
    }
576
 
577
    void LineFieldRenderer::draw()
578
    {
579
        GLint old_prog;
580
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
581
        glUseProgram(prog);
582
        glBindTexture(GL_TEXTURE_3D, get_noise_texture_id());
583
        glCallList(display_list);
584
        glBindTexture(GL_TEXTURE_3D, 0);
585
        glUseProgram(old_prog);	
586
    }
587
 
588
    const string LineFieldRenderer::vss = 
589
    "attribute vec3 direction;\n"
590
    "varying vec3 _n;\n"
591
    "varying vec3 dir_obj;\n"
592
    "varying vec3 v_obj;\n"
593
    "\n"
594
    "void main(void)\n"
595
    "{\n"
596
    "	gl_Position = ftransform();\n"
597
    "   v_obj = gl_Vertex.xyz;\n"
598
    "	dir_obj = direction;\n"
599
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
600
    "}\n";
601
 
602
    const string LineFieldRenderer::fss =
603
    "uniform sampler3D noise_tex;\n"
604
    "uniform float scale_line;\n"
605
    "uniform float noise_scale;\n"
606
    "varying vec3 _n;\n"
607
    "varying vec3 dir_obj;\n"
608
    "varying vec3 v_obj;\n"
609
    "\n"
566 jab 610
    "float tex(vec3 p) {return smoothstep(0.2,0.3,texture3D(noise_tex, p).x);}\n"
557 jab 611
    "void main(void)\n"
612
    "{\n"
613
    "   vec3 n = normalize(_n);\n"
614
    "   float I = "
615
    "             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"
617
    "             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"
619
    "             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"
621
    "             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"
623
    "             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"
625
    "             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"
627
    "			  tex(noise_scale*v_obj); \n"
628
    "	\n"
629
    "   float diff = max(0.0,dot(n,vec3(0.0, 0.0, 1.0)));\n"
566 jab 630
    "	gl_FragColor.rgb = vec3(diff*I*(1.0/13.0));\n"
557 jab 631
    "	gl_FragColor.a = 1.0;\n"
632
    "}\n";
633
 
634
    const string DualVertexRenderer::vss = 
635
    "#version 120\n"
636
    "#extension GL_EXT_gpu_shader4 : enable\n"
637
    "varying vec4 diffuseIn;\n"
638
    "varying vec3 _normalIn;\n"
639
    "void main(void)\n"
640
    "{\n"
641
    "   diffuseIn = gl_Color;\n"
642
    "   _normalIn = normalize(gl_NormalMatrix*gl_Normal);\n"
643
    "   gl_Position =  ftransform();\n"
644
    "}\n";
645
 
646
    const string DualVertexRenderer::gss = 
647
    "#version 120\n"
648
    "#extension GL_EXT_gpu_shader4 : enable\n"
649
    "#extension GL_EXT_geometry_shader4 : enable\n"
650
    "\n"
651
    "varying in vec4 diffuseIn[3];\n"
652
    "varying in vec3 _normalIn[3];\n"
653
    "varying vec4 diffuse[3];\n"
654
    "varying float f;\n"
655
    "varying vec3 _normal;\n"
656
    "void main(void)\n"
657
    "{\n"
658
    "  diffuse[0] = diffuseIn[0];\n"
659
    "  diffuse[1] = diffuseIn[1];\n"
660
    "  diffuse[2] = diffuseIn[2];\n"
661
    "\n"
662
    "  f = diffuseIn[0].a;\n"
663
    "  gl_Position = gl_PositionIn[0];\n"
664
    "  _normal = _normalIn[0];\n"
665
    "  EmitVertex();\n"
666
    "	\n"
667
    "  f = diffuseIn[1].a;\n"
668
    "  gl_Position = gl_PositionIn[1];\n"
669
    "  _normal = _normalIn[1];\n"
670
    "  EmitVertex();\n"
671
    "\n"
672
    "  f = diffuseIn[2].a;\n"
673
    "  gl_Position = gl_PositionIn[2];\n"
674
    "  _normal = _normalIn[2];\n"
675
    "  EmitVertex();\n"
676
    "\n"
677
    "  EndPrimitive();\n"
678
    "}\n";
679
 
680
    const string DualVertexRenderer::fss =
681
    "#version 120\n"
682
    "#extension GL_EXT_gpu_shader4 : enable\n"
683
    "\n"
684
    "varying float f;\n"
685
    "varying vec4 diffuse[3];\n"
686
    "varying vec3 _normal;\n"
687
    "\n"
688
    "void main(void)\n"
689
    "{\n"
690
    "   vec3 normal = normalize(_normal);\n"
691
    "   float col_idx=0;\n"
692
    "   if(f>diffuse[0].g && f<diffuse[0].b)\n"
693
    "      col_idx = diffuse[0].r;\n"
694
    "   else if(f>diffuse[1].g && f<diffuse[1].b)\n"
695
    "      col_idx = diffuse[1].r;\n"
696
    "   else if(f>diffuse[2].g && f<diffuse[2].b)\n"
697
    "      col_idx = diffuse[2].r;\n"
698
    "   vec4 col = col_idx < .5 ? vec4(1,0,0,0) : vec4(0,0,1,0);\n"
699
    "\n"
700
    " 	gl_FragColor =col*dot(normal, vec3(0,0,1));\n"
701
    "}\n";
702
 
703
 
704
 
705
 
706
    DualVertexRenderer::DualVertexRenderer(const HMesh::Manifold& m, VertexAttributeVector<Vec4d>& field)
707
    {		
708
        // Create the program
709
        static GLuint prog = glCreateProgram();
710
 
711
        static bool was_here = false;
712
        if(!was_here)
713
        {
714
            was_here = true;
715
            // Create s	haders directly from file
716
            static GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
717
            static GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, gss);
718
            static GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
719
 
720
            // Attach all shaders
721
            if(vs) glAttachShader(prog, vs);
722
            if(gs) glAttachShader(prog, gs);
723
            if(fs) glAttachShader(prog, fs);
724
 
725
            // Specify input and output for the geometry shader. Note that this must be
726
            // done before linking the program.
727
            glProgramParameteriEXT(prog,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
728
            glProgramParameteriEXT(prog,GL_GEOMETRY_VERTICES_OUT_EXT,3);
729
            glProgramParameteriEXT(prog,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
730
 
731
            // Link the program object and print out the info log
732
            glLinkProgram(prog);
733
        }
734
 
735
 
736
 
737
        GLint old_prog;
738
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
739
 
740
        glNewList(display_list,GL_COMPILE);
741
        glUseProgram(prog);
742
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
743
            if(no_edges(m, *f) != 3) 
744
                continue;
745
            else 
746
                glBegin(GL_TRIANGLES);
747
 
587 jab 748
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
557 jab 749
                Vec3d n(normal(m, w.vertex()));
750
                glNormal3dv(n.get());
751
                glColor4dv(field[w.vertex()].get());
587 jab 752
                glVertex3dv(m.pos(w.vertex()).get());
557 jab 753
            }
754
            glEnd();
755
        }
756
        glUseProgram(old_prog);
757
        glEndList();	
758
 
759
    }
760
 
761
}
762
 
763