Subversion Repositories gelsvn

Rev

Rev 635 | Rev 647 | 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
 
643 janba 9
#include <GLUT/GLUT.h>
10
 
557 jab 11
#include <algorithm>
12
#include <string>
13
#include <cstdlib>
601 jab 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"
557 jab 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)
584 jab 89
        {
90
            glEnable(GL_LIGHTING);
91
            idbuff_renderer->draw(Vec3f(1,0,0),Vec3f(1));
92
            glDisable(GL_LIGHTING);
93
        }
557 jab 94
        else
95
            glCallList(display_list);
96
    }
97
 
98
    void SimpleShaderRenderer::init_shaders(const std::string& vss, 
99
                                            const std::string& fss)
100
    {
101
        vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
102
        print_glsl_program_log(vs);
103
 
104
        fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
105
        print_glsl_program_log(fs);
106
 
107
        prog = glCreateProgram();
108
 
109
        if(vs) glAttachShader(prog, vs);
110
        if(fs) glAttachShader(prog, fs);
111
 
112
        glLinkProgram(prog);
113
        print_glsl_program_log(prog);
114
 
115
    }
116
 
117
    void SimpleShaderRenderer::compile_display_list(const Manifold& m, bool smooth)
118
    {
643 janba 119
        GLint old_prog;
120
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
121
        glUseProgram(prog);
557 jab 122
        glNewList(display_list,GL_COMPILE);
123
        GLGraphics::draw(m, smooth);
124
        glEndList();	
643 janba 125
        glUseProgram(old_prog);
557 jab 126
    }
127
 
128
    void SimpleShaderRenderer::draw()
129
    {
130
        GLint old_prog;
131
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
132
        glUseProgram(prog);
133
        glCallList(display_list);
134
        glUseProgram(old_prog);
135
    }
136
 
643 janba 137
    const string NormalRenderer::vss =
557 jab 138
    "varying vec3 _n;\n"
139
    "varying vec3 v;\n"
140
    "\n"
141
    "void main(void)\n"
142
    "{\n"
143
    "	gl_Position = ftransform();\n"
144
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
145
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
146
    "}\n";
147
 
643 janba 148
    const string NormalRenderer::fss =
557 jab 149
    "varying vec3 _n;\n"
150
    "varying vec3 v;\n"
151
    "\n"
152
    "void main(void)\n"
153
    "{\n"
154
    "   vec3 n = normalize(_n);\n"
155
    "	vec3 l = normalize(-v);\n"
156
    "	vec3 e = l;\n"
157
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
158
    "	\n"
159
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
160
    "   float dot_ln = abs(dot(l, n));\n"
161
    "	vec4 d = vec4(0.7) * dot_ln;\n"
162
    "	vec4 s = vec4(0.3)*smoothstep(0.98,0.9999,dot(r, e));\n"
163
    "	\n"
164
    "	gl_FragColor =  d+s;\n"
165
    "}\n";
166
 
643 janba 167
    const string DebugRenderer::vss =
168
    "varying vec3 _n;\n"
169
    "varying vec3 v;\n"
170
    "varying vec3 c;\n"
171
    "\n"
172
    "void main(void)\n"
173
    "{\n"
174
    "	gl_Position = ftransform();\n"
175
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
176
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
177
    "   c = gl_Color.rgb;\n"
178
    "}\n";
557 jab 179
 
643 janba 180
    const string DebugRenderer::fss =
181
    "varying vec3 _n;\n"
182
    "varying vec3 v;\n"
183
    "varying vec3 c;\n"
184
    "\n"
185
    "void main(void)\n"
186
    "{\n"
187
    "   vec3 n = normalize(_n);\n"
188
    "	vec3 l = normalize(-v);\n"
189
    "	vec3 e = l;\n"
190
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
191
    "	\n"
192
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
193
    "   float dot_ln = abs(dot(l, n));\n"
194
    "	vec4 d = vec4(c,1) * 0.7 * dot_ln;\n"
195
    "	vec4 s = vec4(c,1) * 0.3 * smoothstep(0.98,0.9999,dot(r, e));\n"
196
    "	\n"
197
    "	gl_FragColor =  d+s;\n"
198
    "}\n";
199
 
200
    HMesh::VertexAttributeVector<CGLA::Vec3f> DebugRenderer::vertex_colors;
201
    HMesh::FaceAttributeVector<CGLA::Vec3f> DebugRenderer::face_colors;
202
 
203
 
204
    void DebugRenderer::compile_display_list(const HMesh::Manifold& m, bool smooth)
205
    {
206
        GLint old_prog;
207
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
208
        glUseProgram(prog);
209
        glNewList(display_list,GL_COMPILE);
210
        for(FaceID f: m.faces()){
211
            Vec3f c = face_colors[f];
212
            glColor3f(c[0], c[1], c[2]);
213
            if(!smooth)
214
                glNormal3dv(normal(m, f).get());
215
            if(no_edges(m, f)== 3)
216
                glBegin(GL_TRIANGLES);
217
            else
218
                glBegin(GL_POLYGON);
219
 
220
            for(Walker w = m.walker(f); !w.full_circle(); w = w.circulate_face_ccw()){
221
                Vec3d n = normal(m, w.vertex());
222
                if(smooth)
223
                    glNormal3dv(n.get());
224
                glVertex3dv(m.pos(w.vertex()).get());
225
            }
226
            glEnd();
227
        }
228
 
229
        Vec3d c;
230
        float r;
231
        bsphere(m, c, r);
232
        for(auto vid : m.vertices())
233
        {
234
            Vec3d p = m.pos(vid);
235
            Vec3f c = vertex_colors[vid];
236
            glColor3f(c[0], c[1], c[2]);
237
            glPushMatrix();
238
            glTranslated(p[0], p[1], p[2]);
239
            glutSolidSphere(0.01*r, 7, 7);
240
            glPopMatrix();
241
        }
242
        glEndList();
243
        glUseProgram(old_prog);
244
    }
245
 
557 jab 246
    const string ReflectionLineRenderer::vss = 
247
    "varying vec3 _n;\n"
248
    "varying vec3 v;\n"
249
    "\n"
250
    "void main(void)\n"
251
    "{\n"
252
    "	gl_Position = ftransform();\n"
253
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
254
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
255
    "}\n";
256
 
257
 
258
    const string ReflectionLineRenderer::fss = 
259
    "uniform float detail;\n"
260
    "\n"
261
    "varying vec3 _n;\n"
262
    "varying vec3 v;\n"
263
    "\n"
264
    "void main(void)\n"
265
    "{\n"
266
    "   vec3 n = normalize(_n);\n"
267
    "	// calculate the reflection\n"
268
    "	vec3 r = normalize(2.0*dot(-v, n)*n + v);\n"
269
    "	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
270
    "   float diff  = dot(n,viewer_lightdir);\n"
271
    "	\n"
272
    "	vec2 r2 = normalize(vec2(r[0], r[2]));\n"
273
    "	vec2 x = vec2(1, 0);\n"
274
    "	float angle = acos(dot(r2, x));\n"
275
    "	\n"
276
    "	// decide if we hit a white or black ring, based on y value\n"
277
    "	gl_FragColor = diff * vec4(1.0) + smoothstep(0.8, 1.0,cos(13.0*angle)) * vec4(-1.0);\n"
278
    "}\n";
279
 
280
    const string IsophoteLineRenderer::vss = 
281
    "varying vec3 _n;\n"
282
    "varying vec3 v;\n"
283
    "\n"
284
    "void main(void)\n"
285
    "{\n"
286
    "	gl_Position = ftransform();\n"
287
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
288
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
289
    "}\n";
290
 
291
 
292
    const string IsophoteLineRenderer::fss = 
293
    "uniform float detail;\n"
294
    "\n"
295
    "varying vec3 _n;\n"
296
    "varying vec3 v;\n"
297
    "\n"
298
    "void main(void)\n"
299
    "{\n"
300
    "   vec3 n = normalize(_n);\n"
301
    "	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
302
    "	vec3 isophote_lightdir = viewer_lightdir;\n"
303
    "	float angle = acos(dot(n, isophote_lightdir));\n"
304
    "   float diff  = dot(n,viewer_lightdir);\n"
305
    "	\n"
306
    "	// decide if we hit a white or black ring, based on y value\n"
307
    "	gl_FragColor = diff * vec4(1.0) + smoothstep(0.8, 1.0,cos(20.0*angle)) * vec4(-1.0);\n"
308
    "}\n";
309
 
310
    const string ToonRenderer::vss = 
311
    "varying vec3 _n;\n"
312
    "varying vec3 v;\n"
313
    "\n"
314
    "void main(void)\n"
315
    "{\n"
316
    "	gl_Position = ftransform();\n"
317
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
318
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
319
    "}\n";
320
 
321
    const string ToonRenderer::fss = 
322
    "varying vec3 _n;\n"
323
    "varying vec3 v;\n"
324
    "\n"
325
    "void main(void)\n"
326
    "{\n"
327
    "   vec3 n = normalize(_n);\n"
328
    "	vec3 l = normalize(-v);\n"
329
    "	vec3 e = l;\n"
330
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
331
    "	\n"
332
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
333
    "   float dot_ln = abs(dot(l, n));\n"
334
    "	vec4 d = vec4(0.7,0.7,0.0,1.0) * 0.25 * (smoothstep(0.23,0.25,dot_ln)+smoothstep(0.45,0.47,dot_ln)+smoothstep(0.7,0.72,dot_ln)+smoothstep(0.9,0.92,dot_ln));\n"
335
    "	vec4 s = vec4(0.5,0.3,0.4,1.0)*smoothstep(0.96,0.98,dot(r, e));\n"
336
    "	\n"
337
    "	gl_FragColor =  d+s;\n"
338
    "}\n";
339
 
340
 
643 janba 341
    void GlazedRenderer::compile_display_list(const HMesh::Manifold& m, bool smooth, float bsphere_rad)
557 jab 342
    {
343
        GLint old_prog;
344
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
345
        glUseProgram(prog);
643 janba 346
        glNewList(display_list,GL_COMPILE);
557 jab 347
        glBindTexture(GL_TEXTURE_3D, get_noise_texture_id());
348
        glUniform1iARB(glGetUniformLocationARB(prog, "noise_tex"),0);
349
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_scale"),12.0/bsphere_rad);
643 janba 350
        GLGraphics::draw(m, smooth);
351
        glEndList();
557 jab 352
        glUseProgram(old_prog);
643 janba 353
 
557 jab 354
    }
643 janba 355
 
557 jab 356
 
357
    const string GlazedRenderer::vss = 
358
    "varying vec3 _n;\n"
359
    "varying vec3 v;\n"
360
    "varying vec3 v_obj;\n"
361
    "\n"
362
    "void main(void)\n"
363
    "{\n"
364
    "	gl_Position = ftransform();\n"
365
    "   v_obj = gl_Vertex.xyz;\n"
366
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
367
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
368
    "}\n"
369
    "\n";
370
 
371
    const string GlazedRenderer::fss =
372
    "uniform sampler3D noise_tex;\n"
373
    "uniform float noise_scale;\n"
374
    "varying vec3 _n;\n"
375
    "varying vec3 v;\n"
376
    "varying vec3 v_obj;\n"
377
    "\n"
378
    "vec4 glazed_shader(vec4 mat_col,  vec4 light_col, vec3 light_dir)\n"
379
    "{\n"
380
    "   vec3 n = normalize(_n);\n"
381
    "	vec3 e = normalize(-v);\n"
382
    "	vec3 r = normalize(2.0*dot(e, n)*n - e);\n"
383
    "	float d = max(0.05,dot(light_dir, n));\n"
384
    "	vec4 diff = mat_col * light_col *d; 	\n"
385
    "	vec4 refl = smoothstep(0.7,0.75,dot(r,light_dir)) * light_col;\n"
386
    "	return 0.15*refl + diff;\n"
387
    "}\n"
388
    "\n"
389
    "void main(void)\n"
390
    "{\n"
643 janba 391
    "	vec4 mat_col = vec4(0.9,1.0,0.4,1.0) +  vec4(-0.1,-0.1,0.12,0.0) * texture3D(noise_tex, noise_scale*v_obj).x\n"
557 jab 392
    " + vec4(0.05) * texture3D(noise_tex, 500.0*v_obj).x;\n"
393
    "	\n"
394
    "	vec3 light0_dir = vec3(0.0,1.0,0.0);\n"
395
    "	vec4 light0_col = vec4(0.7,0.9,1.0,1.0);\n"
396
    "	\n"
397
    "	vec3 light1_dir = vec3(0.0,0.0,1.0);\n"
398
    "	vec4 light1_col = vec4(1.0,1.0,0.7,1.0);\n"
399
    "	\n"
400
    "	gl_FragColor = \n"
401
    "	0.5*glazed_shader(mat_col, light0_col, light0_dir)+\n"
402
    "	0.5*glazed_shader(mat_col, light1_col, light1_dir);\n"
403
    "	\n"
404
    "	gl_FragColor.a = 1.0;\n"
405
    "}\n";
406
 
407
 
408
    const string ScalarFieldRenderer::vss =
409
    "	attribute float scalar;\n"
410
    "	varying vec3 _normal;\n"
411
    "	varying float s;\n"
412
    "	\n"
413
    "	void main(void)\n"
414
    "	{\n"
415
    "		gl_Position =  ftransform();\n"
416
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
417
    "		s=scalar;\n"
418
    "	}\n";
419
 
420
    const string ScalarFieldRenderer::fss = 	
421
    "	varying vec3 _normal;\n"
422
    "	varying float s;\n"
423
    "	uniform float scalar_max;\n"
424
    "   uniform float gamma;\n"
425
    "	const vec3 light_dir = vec3(0,0,1);\n"
426
    "	\n"
427
    "	void main()\n"
428
    "	{\n"
429
    "       vec3 normal = normalize(_normal);\n"
430
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
431
    "		\n"
432
    "		float s_norm = s/scalar_max;\n"
635 janba 433
    "		float stripe_signal = 10.0 * s_norm;\n"
631 janba 434
    "		vec4 stripe_col = abs(stripe_signal) < 3.14 ? vec4(-1.0,0,0,0) : vec4(.9,.9,.9,0);\n"
557 jab 435
    "		\n"
436
    "		gl_FragColor = s_norm * vec4(-1,0,1,0);\n"
437
    "       gl_FragColor *= dot_ln;\n"
438
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0/gamma);\n"
439
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0/gamma);\n"
440
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0/gamma);\n"
635 janba 441
    "		//gl_FragColor += stripe_col * smoothstep(0.8,1.0,cos(stripe_signal));\n"
557 jab 442
    "	}\n";
443
 
643 janba 444
    void ScalarFieldRenderer::compile_display_list(const HMesh::Manifold& m, bool smooth,
445
                                                   HMesh::VertexAttributeVector<double>& field, double max_val, float gamma)
557 jab 446
    {
447
 
448
        GLint old_prog;
449
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
450
        glUseProgram(prog);
451
 
452
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
453
        glUniform1fARB(glGetUniformLocationARB(prog, "scalar_max"), max_val);
454
 
455
        //    static float& gamma = CreateCVar("display.scalar_field_renderer.gamma",2.2f);
456
        glUniform1fARB(glGetUniformLocationARB(prog, "gamma"), gamma);
457
        glNewList(display_list,GL_COMPILE);
458
 
459
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){      
460
            if(!smooth) 
587 jab 461
                glNormal3dv(normal(m, *f).get());
557 jab 462
            if(no_edges(m, *f)== 3) 
463
                glBegin(GL_TRIANGLES);
464
            else 
465
                glBegin(GL_POLYGON);
466
 
467
 
587 jab 468
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
557 jab 469
                Vec3d n(normal(m, w.vertex()));
470
                if(smooth) 
471
                    glNormal3dv(n.get());
472
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
587 jab 473
                glVertex3dv(m.pos(w.vertex()).get());
557 jab 474
            }
475
            glEnd();
476
        }
477
        glEndList();	
478
        glUseProgram(old_prog);
479
 
480
    }
572 jab 481
 
482
 
631 janba 483
 
557 jab 484
    const string AmbientOcclusionRenderer::vss =
485
    "	attribute float scalar;\n"
486
    "	varying vec3 _normal;\n"
487
    "	varying float s;\n"
488
    "	\n"
489
    "	void main(void)\n"
490
    "	{\n"
491
    "		gl_Position =  ftransform();\n"
492
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
493
    "		s=scalar;\n"
494
    "	}\n";
495
 
496
    const string AmbientOcclusionRenderer::fss = 	
497
    "	varying vec3 _normal;\n"
498
    "	varying float s;\n"
499
    "	uniform float scalar_max;\n"
500
    "	const vec3 light_dir = vec3(0,0,1);\n"
501
    "	\n"
502
    "	void main()\n"
503
    "	{\n"
504
    "   vec3 normal = normalize(_normal);\n"
505
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
506
    "		\n"
507
    "		float s_norm = min(1.0,s/scalar_max+1.0);\n"
508
    "		\n"
509
    "		gl_FragColor = s_norm * vec4(1.0);\n"
510
    "       gl_FragColor *= dot_ln;\n"
511
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0);\n"
512
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0);\n"
513
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0);\n"
514
    "	}\n";
515
 
643 janba 516
    void AmbientOcclusionRenderer::compile_display_list(const HMesh::Manifold& m, HMesh::VertexAttributeVector<double>& field, double max_val)
557 jab 517
    {	
518
        GLint old_prog;
519
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
520
        glUseProgram(prog);
521
 
522
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
523
        glUniform1fARB(glGetUniformLocationARB(prog, "scalar_max"), max_val);
524
 
525
        glNewList(display_list,GL_COMPILE);
526
 
527
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
643 janba 528
 
529
            if(no_edges(m, *f)== 3)
557 jab 530
                glBegin(GL_TRIANGLES);
531
            else 
532
                glBegin(GL_POLYGON);
533
 
587 jab 534
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw())
557 jab 535
            {
536
                Vec3d n(normal(m, w.vertex()));
643 janba 537
                glNormal3dv(n.get());
557 jab 538
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
587 jab 539
                glVertex3dv(m.pos(w.vertex()).get());
557 jab 540
            }
541
            glEnd();
542
        }
543
        glEndList();	
544
        glUseProgram(old_prog);
545
 
546
    }
547
 
548
 
643 janba 549
    void LineFieldRenderer::compile_display_list(const HMesh::Manifold& m,HMesh::VertexAttributeVector<CGLA::Vec3d>& lines, float _r)
557 jab 550
    {
643 janba 551
        glNewList(display_list,GL_COMPILE);
618 jab 552
        float noise_scale = 10.0f/r;
553
        float line_scale = 0.003f;
566 jab 554
 
557 jab 555
        GLint old_prog;
556
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
557
        glUseProgram(prog);	
618 jab 558
        glUniform1fARB(glGetUniformLocationARB(prog, "scale_line"),line_scale*noise_scale);
557 jab 559
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_scale"),noise_scale);
560
        glUniform1iARB(glGetUniformLocationARB(prog, "noise_tex"),0);
561
        GLuint direction = glGetAttribLocation(prog, "direction");	
643 janba 562
        glBindTexture(GL_TEXTURE_3D, get_noise_texture_id());
563
 
557 jab 564
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
643 janba 565
            if(no_edges(m, *f) == 3)
557 jab 566
                glBegin(GL_TRIANGLES);
567
            else 
568
                glBegin(GL_POLYGON);
569
 
587 jab 570
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
557 jab 571
                Vec3d n(normal(m, w.vertex()));
643 janba 572
                glNormal3dv(n.get());
557 jab 573
 
574
                Vec3d d = lines[w.vertex()];
575
                d = normalize(d-n*dot(n,d));
576
                glVertexAttrib3dv(direction, d.get());
587 jab 577
                glVertex3dv(m.pos(w.vertex()).get());
557 jab 578
            }
579
            glEnd();
580
        }
581
 
582
        glBindTexture(GL_TEXTURE_3D, 0);
583
        glEndList();	
584
        glUseProgram(old_prog);
585
 
586
    }
587
 
588
 
589
    const string LineFieldRenderer::vss = 
590
    "attribute vec3 direction;\n"
591
    "varying vec3 _n;\n"
592
    "varying vec3 dir_obj;\n"
593
    "varying vec3 v_obj;\n"
594
    "\n"
595
    "void main(void)\n"
596
    "{\n"
597
    "	gl_Position = ftransform();\n"
598
    "   v_obj = gl_Vertex.xyz;\n"
599
    "	dir_obj = direction;\n"
600
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
601
    "}\n";
602
 
603
    const string LineFieldRenderer::fss =
604
    "uniform sampler3D noise_tex;\n"
605
    "uniform float scale_line;\n"
606
    "uniform float noise_scale;\n"
607
    "varying vec3 _n;\n"
608
    "varying vec3 dir_obj;\n"
609
    "varying vec3 v_obj;\n"
610
    "\n"
566 jab 611
    "float tex(vec3 p) {return smoothstep(0.2,0.3,texture3D(noise_tex, p).x);}\n"
557 jab 612
    "void main(void)\n"
613
    "{\n"
614
    "   vec3 n = normalize(_n);\n"
615
    "   float I = "
616
    "             tex(noise_scale*v_obj + 6.0*scale_line*dir_obj) + \n"
617
    "             tex(noise_scale*v_obj - 6.0*scale_line*dir_obj) + \n"
618
    "             tex(noise_scale*v_obj + 5.0*scale_line*dir_obj) + \n"
619
    "             tex(noise_scale*v_obj - 5.0*scale_line*dir_obj) + \n"
620
    "             tex(noise_scale*v_obj + 4.0*scale_line*dir_obj) + \n"
621
    "             tex(noise_scale*v_obj - 4.0*scale_line*dir_obj) + \n"
622
    "             tex(noise_scale*v_obj + 3.0*scale_line*dir_obj) + \n"
623
    "             tex(noise_scale*v_obj - 3.0*scale_line*dir_obj) + \n"
624
    "             tex(noise_scale*v_obj + 2.0*scale_line*dir_obj) + \n"
625
    "             tex(noise_scale*v_obj - 2.0*scale_line*dir_obj) + \n"
626
    "             tex(noise_scale*v_obj + 1.0*scale_line*dir_obj) + \n"
627
    "             tex(noise_scale*v_obj - 1.0*scale_line*dir_obj) + \n"
628
    "			  tex(noise_scale*v_obj); \n"
629
    "	\n"
630
    "   float diff = max(0.0,dot(n,vec3(0.0, 0.0, 1.0)));\n"
566 jab 631
    "	gl_FragColor.rgb = vec3(diff*I*(1.0/13.0));\n"
557 jab 632
    "	gl_FragColor.a = 1.0;\n"
633
    "}\n";
634
 
635
 
636
}
637
 
638