Subversion Repositories gelsvn

Rev

Rev 663 | Rev 665 | 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>
647 janba 14
#include "../Geometry/TriMesh.h"
601 jab 15
#include "../CGLA/Mat3x3d.h"
16
#include "../GLGraphics/glsl_shader.h"
647 janba 17
#include "../GLGraphics/draw.h"
601 jab 18
#include "../HMesh/Manifold.h"
19
#include "../HMesh/AttributeVector.h"
20
#include "../HMesh/curvature.h"
557 jab 21
 
22
using namespace CGLA;
23
using namespace HMesh;
24
using namespace std;
647 janba 25
using namespace Geometry;
557 jab 26
namespace GLGraphics
657 janba 27
{    
557 jab 28
    GLuint get_noise_texture_id()
29
    {
30
        static GLuint texname=0;
31
        static bool was_here = false;
32
 
33
        if(!was_here)
34
        {
35
            was_here = true;
36
            int width = 32;
37
            int height = 32;
38
            int depth = 32;
39
            vector<unsigned char> texels(width*height*depth);
40
            for (int i = 0; i < width*height*depth; ++i)
41
            {
42
                int intensity = 255.0 * (float(gel_rand()) / GEL_RAND_MAX);
43
                texels[i] = (unsigned char) intensity;
44
            }
45
 
46
            glGenTextures(1, &texname);	
647 janba 47
            glBindTexture(GL_TEXTURE_3D, texname);
557 jab 48
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
49
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
50
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
51
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
52
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
53
            glTexImage3D(GL_TEXTURE_3D, 0, GL_INTENSITY8, width, height, depth, 0, GL_RED, GL_UNSIGNED_BYTE, &texels[0]);
54
        }
55
 
56
        return texname;
57
    }
58
 
59
 
60
    int WireframeRenderer::maximum_face_valency(const Manifold& m)
61
    {
62
        int max_val = 0;
63
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f)
64
            max_val = max(max_val, no_edges(m, *f));
65
        return max_val;
66
    }
67
 
68
    WireframeRenderer::WireframeRenderer(HMesh::Manifold& m, bool smooth): idbuff_renderer(0)
69
    {
70
        if(GLEW_EXT_geometry_shader4 && maximum_face_valency(m) > 3)
71
        {
72
            GLint viewp[4];
73
            glGetIntegerv(GL_VIEWPORT,viewp);
74
            idbuff_renderer = new IDBufferWireframeRenderer(viewp[2], viewp[3], m);
75
        }
76
        else
77
        {
78
            glNewList(display_list,GL_COMPILE);
79
            if(GLEW_EXT_geometry_shader4)
80
                draw_triangles_in_wireframe(m,smooth, Vec3f(1,0,0));				
81
            else
82
                draw_wireframe_oldfashioned(m,smooth, Vec3f(1,0,0));
83
            glEndList();
84
        }
85
    }
86
 
87
    void WireframeRenderer::draw()
88
    {
89
        if(idbuff_renderer)
584 jab 90
        {
91
            glEnable(GL_LIGHTING);
92
            idbuff_renderer->draw(Vec3f(1,0,0),Vec3f(1));
93
            glDisable(GL_LIGHTING);
94
        }
557 jab 95
        else
96
            glCallList(display_list);
97
    }
98
 
99
    void SimpleShaderRenderer::init_shaders(const std::string& vss, 
100
                                            const std::string& fss)
101
    {
102
        vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
103
        print_glsl_program_log(vs);
104
 
105
        fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
106
        print_glsl_program_log(fs);
107
 
108
        prog = glCreateProgram();
109
 
110
        if(vs) glAttachShader(prog, vs);
111
        if(fs) glAttachShader(prog, fs);
112
 
113
        glLinkProgram(prog);
114
        print_glsl_program_log(prog);
115
 
116
    }
117
 
118
    void SimpleShaderRenderer::compile_display_list(const Manifold& m, bool smooth)
119
    {
643 janba 120
        GLint old_prog;
121
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
122
        glUseProgram(prog);
557 jab 123
        glNewList(display_list,GL_COMPILE);
124
        GLGraphics::draw(m, smooth);
125
        glEndList();	
643 janba 126
        glUseProgram(old_prog);
557 jab 127
    }
128
 
129
    void SimpleShaderRenderer::draw()
130
    {
131
        GLint old_prog;
132
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
133
        glUseProgram(prog);
134
        glCallList(display_list);
135
        glUseProgram(old_prog);
136
    }
137
 
643 janba 138
    const string NormalRenderer::vss =
557 jab 139
    "varying vec3 _n;\n"
140
    "varying vec3 v;\n"
141
    "\n"
142
    "void main(void)\n"
143
    "{\n"
144
    "	gl_Position = ftransform();\n"
145
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
146
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
147
    "}\n";
148
 
643 janba 149
    const string NormalRenderer::fss =
557 jab 150
    "varying vec3 _n;\n"
151
    "varying vec3 v;\n"
152
    "\n"
153
    "void main(void)\n"
154
    "{\n"
155
    "   vec3 n = normalize(_n);\n"
156
    "	vec3 l = normalize(-v);\n"
157
    "	vec3 e = l;\n"
158
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
159
    "	\n"
160
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
161
    "   float dot_ln = abs(dot(l, n));\n"
162
    "	vec4 d = vec4(0.7) * dot_ln;\n"
163
    "	vec4 s = vec4(0.3)*smoothstep(0.98,0.9999,dot(r, e));\n"
164
    "	\n"
165
    "	gl_FragColor =  d+s;\n"
166
    "}\n";
167
 
643 janba 168
    const string DebugRenderer::vss =
169
    "varying vec3 _n;\n"
170
    "varying vec3 v;\n"
171
    "varying vec3 c;\n"
172
    "\n"
173
    "void main(void)\n"
174
    "{\n"
175
    "	gl_Position = ftransform();\n"
176
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
177
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
178
    "   c = gl_Color.rgb;\n"
179
    "}\n";
557 jab 180
 
643 janba 181
    const string DebugRenderer::fss =
182
    "varying vec3 _n;\n"
183
    "varying vec3 v;\n"
184
    "varying vec3 c;\n"
185
    "\n"
186
    "void main(void)\n"
187
    "{\n"
188
    "   vec3 n = normalize(_n);\n"
189
    "	vec3 l = normalize(-v);\n"
190
    "	vec3 e = l;\n"
191
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
192
    "	\n"
193
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
194
    "   float dot_ln = abs(dot(l, n));\n"
195
    "	vec4 d = vec4(c,1) * 0.7 * dot_ln;\n"
196
    "	vec4 s = vec4(c,1) * 0.3 * smoothstep(0.98,0.9999,dot(r, e));\n"
197
    "	\n"
198
    "	gl_FragColor =  d+s;\n"
199
    "}\n";
200
 
201
    HMesh::VertexAttributeVector<CGLA::Vec3f> DebugRenderer::vertex_colors;
647 janba 202
    HMesh::HalfEdgeAttributeVector<CGLA::Vec3f> DebugRenderer::edge_colors;
643 janba 203
    HMesh::FaceAttributeVector<CGLA::Vec3f> DebugRenderer::face_colors;
204
 
205
 
206
    void DebugRenderer::compile_display_list(const HMesh::Manifold& m, bool smooth)
207
    {
208
        GLint old_prog;
209
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
210
        glUseProgram(prog);
211
        glNewList(display_list,GL_COMPILE);
647 janba 212
        glEnable(GL_POLYGON_OFFSET_FILL);
213
        glPolygonOffset(1,1);
643 janba 214
        for(FaceID f: m.faces()){
215
            Vec3f c = face_colors[f];
216
            glColor3f(c[0], c[1], c[2]);
217
            if(!smooth)
218
                glNormal3dv(normal(m, f).get());
219
            if(no_edges(m, f)== 3)
220
                glBegin(GL_TRIANGLES);
221
            else
222
                glBegin(GL_POLYGON);
223
 
224
            for(Walker w = m.walker(f); !w.full_circle(); w = w.circulate_face_ccw()){
225
                Vec3d n = normal(m, w.vertex());
226
                if(smooth)
227
                    glNormal3dv(n.get());
228
                glVertex3dv(m.pos(w.vertex()).get());
229
            }
230
            glEnd();
231
        }
647 janba 232
        glLineWidth(2);
233
        glDisable(GL_POLYGON_OFFSET_FILL);
234
        glBegin(GL_LINES);
235
        for(auto hid: m.halfedges())
236
        {
237
            Walker w = m.walker(hid);
238
            Vec3f c = edge_colors[hid];
239
            glColor3fv(c.get());
240
            glNormal3dv(normal(m, w.opp().vertex()).get());
241
            glVertex3dv(m.pos(w.opp().vertex()).get());
242
            glNormal3dv(normal(m, w.vertex()).get());
243
            glVertex3dv(m.pos(w.vertex()).get());
244
        }
245
        glEnd();
246
        glLineWidth(1);
643 janba 247
        Vec3d c;
248
        float r;
249
        bsphere(m, c, r);
647 janba 250
        r *= 0.003;
643 janba 251
        for(auto vid : m.vertices())
252
        {
253
            Vec3d p = m.pos(vid);
254
            Vec3f c = vertex_colors[vid];
255
            glColor3f(c[0], c[1], c[2]);
256
            glPushMatrix();
257
            glTranslated(p[0], p[1], p[2]);
647 janba 258
            glScalef(r, r, r);
259
            draw_ball();
643 janba 260
            glPopMatrix();
261
        }
647 janba 262
        glEnd();
643 janba 263
        glEndList();
264
        glUseProgram(old_prog);
265
    }
266
 
557 jab 267
    const string ReflectionLineRenderer::vss = 
268
    "varying vec3 _n;\n"
269
    "varying vec3 v;\n"
270
    "\n"
271
    "void main(void)\n"
272
    "{\n"
273
    "	gl_Position = ftransform();\n"
274
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
275
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
276
    "}\n";
277
 
278
 
279
    const string ReflectionLineRenderer::fss = 
280
    "uniform float detail;\n"
281
    "\n"
282
    "varying vec3 _n;\n"
283
    "varying vec3 v;\n"
284
    "\n"
285
    "void main(void)\n"
286
    "{\n"
287
    "   vec3 n = normalize(_n);\n"
288
    "	// calculate the reflection\n"
289
    "	vec3 r = normalize(2.0*dot(-v, n)*n + v);\n"
290
    "	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
291
    "   float diff  = dot(n,viewer_lightdir);\n"
292
    "	\n"
293
    "	vec2 r2 = normalize(vec2(r[0], r[2]));\n"
294
    "	vec2 x = vec2(1, 0);\n"
295
    "	float angle = acos(dot(r2, x));\n"
296
    "	\n"
297
    "	// decide if we hit a white or black ring, based on y value\n"
298
    "	gl_FragColor = diff * vec4(1.0) + smoothstep(0.8, 1.0,cos(13.0*angle)) * vec4(-1.0);\n"
299
    "}\n";
300
 
301
    const string IsophoteLineRenderer::vss = 
302
    "varying vec3 _n;\n"
303
    "varying vec3 v;\n"
304
    "\n"
305
    "void main(void)\n"
306
    "{\n"
307
    "	gl_Position = ftransform();\n"
308
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
309
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
310
    "}\n";
311
 
312
 
313
    const string IsophoteLineRenderer::fss = 
314
    "uniform float detail;\n"
315
    "\n"
316
    "varying vec3 _n;\n"
317
    "varying vec3 v;\n"
318
    "\n"
319
    "void main(void)\n"
320
    "{\n"
321
    "   vec3 n = normalize(_n);\n"
322
    "	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
323
    "	vec3 isophote_lightdir = viewer_lightdir;\n"
324
    "	float angle = acos(dot(n, isophote_lightdir));\n"
325
    "   float diff  = dot(n,viewer_lightdir);\n"
326
    "	\n"
327
    "	// decide if we hit a white or black ring, based on y value\n"
328
    "	gl_FragColor = diff * vec4(1.0) + smoothstep(0.8, 1.0,cos(20.0*angle)) * vec4(-1.0);\n"
329
    "}\n";
330
 
331
    const string ToonRenderer::vss = 
332
    "varying vec3 _n;\n"
333
    "varying vec3 v;\n"
334
    "\n"
335
    "void main(void)\n"
336
    "{\n"
337
    "	gl_Position = ftransform();\n"
338
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
339
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
340
    "}\n";
341
 
342
    const string ToonRenderer::fss = 
343
    "varying vec3 _n;\n"
344
    "varying vec3 v;\n"
345
    "\n"
346
    "void main(void)\n"
347
    "{\n"
348
    "   vec3 n = normalize(_n);\n"
349
    "	vec3 l = normalize(-v);\n"
350
    "	vec3 e = l;\n"
351
    "	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
352
    "	\n"
353
    "	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
354
    "   float dot_ln = abs(dot(l, n));\n"
355
    "	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"
356
    "	vec4 s = vec4(0.5,0.3,0.4,1.0)*smoothstep(0.96,0.98,dot(r, e));\n"
357
    "	\n"
358
    "	gl_FragColor =  d+s;\n"
359
    "}\n";
360
 
361
 
647 janba 362
    void GlazedRenderer::compile_display_list(const HMesh::Manifold& m, bool smooth)
557 jab 363
    {
364
        GLint old_prog;
365
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
366
        glUseProgram(prog);
643 janba 367
        glNewList(display_list,GL_COMPILE);
557 jab 368
        glBindTexture(GL_TEXTURE_3D, get_noise_texture_id());
369
        glUniform1iARB(glGetUniformLocationARB(prog, "noise_tex"),0);
647 janba 370
        float r;
371
        Vec3d c;
372
        bsphere(m, c, r);
373
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_scale"),12.0/r);
643 janba 374
        GLGraphics::draw(m, smooth);
375
        glEndList();
557 jab 376
        glUseProgram(old_prog);
643 janba 377
 
557 jab 378
    }
643 janba 379
 
557 jab 380
 
381
    const string GlazedRenderer::vss = 
382
    "varying vec3 _n;\n"
383
    "varying vec3 v;\n"
384
    "varying vec3 v_obj;\n"
385
    "\n"
386
    "void main(void)\n"
387
    "{\n"
388
    "	gl_Position = ftransform();\n"
389
    "   v_obj = gl_Vertex.xyz;\n"
390
    "	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
391
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
392
    "}\n"
393
    "\n";
394
 
395
    const string GlazedRenderer::fss =
396
    "uniform sampler3D noise_tex;\n"
397
    "uniform float noise_scale;\n"
398
    "varying vec3 _n;\n"
399
    "varying vec3 v;\n"
400
    "varying vec3 v_obj;\n"
401
    "\n"
402
    "vec4 glazed_shader(vec4 mat_col,  vec4 light_col, vec3 light_dir)\n"
403
    "{\n"
404
    "   vec3 n = normalize(_n);\n"
405
    "	vec3 e = normalize(-v);\n"
406
    "	vec3 r = normalize(2.0*dot(e, n)*n - e);\n"
407
    "	float d = max(0.05,dot(light_dir, n));\n"
408
    "	vec4 diff = mat_col * light_col *d; 	\n"
409
    "	vec4 refl = smoothstep(0.7,0.75,dot(r,light_dir)) * light_col;\n"
410
    "	return 0.15*refl + diff;\n"
411
    "}\n"
412
    "\n"
413
    "void main(void)\n"
414
    "{\n"
643 janba 415
    "	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 416
    " + vec4(0.05) * texture3D(noise_tex, 500.0*v_obj).x;\n"
417
    "	\n"
418
    "	vec3 light0_dir = vec3(0.0,1.0,0.0);\n"
419
    "	vec4 light0_col = vec4(0.7,0.9,1.0,1.0);\n"
420
    "	\n"
421
    "	vec3 light1_dir = vec3(0.0,0.0,1.0);\n"
422
    "	vec4 light1_col = vec4(1.0,1.0,0.7,1.0);\n"
423
    "	\n"
424
    "	gl_FragColor = \n"
425
    "	0.5*glazed_shader(mat_col, light0_col, light0_dir)+\n"
426
    "	0.5*glazed_shader(mat_col, light1_col, light1_dir);\n"
427
    "	\n"
428
    "	gl_FragColor.a = 1.0;\n"
429
    "}\n";
430
 
431
 
432
    const string ScalarFieldRenderer::vss =
433
    "	attribute float scalar;\n"
434
    "	varying vec3 _normal;\n"
435
    "	varying float s;\n"
436
    "	\n"
437
    "	void main(void)\n"
438
    "	{\n"
439
    "		gl_Position =  ftransform();\n"
440
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
441
    "		s=scalar;\n"
442
    "	}\n";
443
 
662 janba 444
    const string ScalarFieldRenderer::fss =
557 jab 445
    "	varying vec3 _normal;\n"
446
    "	varying float s;\n"
447
    "	uniform float scalar_max;\n"
448
    "   uniform float gamma;\n"
449
    "	const vec3 light_dir = vec3(0,0,1);\n"
450
    "	\n"
451
    "	void main()\n"
452
    "	{\n"
453
    "       vec3 normal = normalize(_normal);\n"
454
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
455
    "		\n"
456
    "		float s_norm = s/scalar_max;\n"
663 janba 457
    "		float stripe_signal = 50.0 * s_norm;\n"
662 janba 458
    "		vec4 stripe_col = vec4(.9,.9,.9,0);\n"
557 jab 459
    "		\n"
662 janba 460
    "		gl_FragColor = max(vec4(0), s_norm * vec4(-1,0,1,0));\n"
557 jab 461
    "       gl_FragColor *= dot_ln;\n"
462
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0/gamma);\n"
463
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0/gamma);\n"
464
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0/gamma);\n"
664 janba 465
    "		gl_FragColor += 0.2*stripe_col * pow(cos(stripe_signal),70.0);\n"
557 jab 466
    "	}\n";
467
 
643 janba 468
    void ScalarFieldRenderer::compile_display_list(const HMesh::Manifold& m, bool smooth,
469
                                                   HMesh::VertexAttributeVector<double>& field, double max_val, float gamma)
557 jab 470
    {
471
 
472
        GLint old_prog;
473
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
474
        glUseProgram(prog);
475
 
476
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
477
        glUniform1fARB(glGetUniformLocationARB(prog, "scalar_max"), max_val);
478
 
479
        //    static float& gamma = CreateCVar("display.scalar_field_renderer.gamma",2.2f);
480
        glUniform1fARB(glGetUniformLocationARB(prog, "gamma"), gamma);
481
        glNewList(display_list,GL_COMPILE);
482
 
662 janba 483
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
484
            if(!smooth)
587 jab 485
                glNormal3dv(normal(m, *f).get());
662 janba 486
            if(no_edges(m, *f)== 3)
557 jab 487
                glBegin(GL_TRIANGLES);
662 janba 488
            else
557 jab 489
                glBegin(GL_POLYGON);
490
 
491
 
587 jab 492
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
557 jab 493
                Vec3d n(normal(m, w.vertex()));
662 janba 494
                if(smooth)
557 jab 495
                    glNormal3dv(n.get());
496
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
587 jab 497
                glVertex3dv(m.pos(w.vertex()).get());
557 jab 498
            }
499
            glEnd();
500
        }
662 janba 501
        glEndList();
557 jab 502
        glUseProgram(old_prog);
503
 
504
    }
662 janba 505
 
506
    HMesh::VertexAttributeVector<CGLA::Vec2f> CheckerBoardRenderer::param;
572 jab 507
 
662 janba 508
    const string CheckerBoardRenderer::vss =
509
    "	attribute vec2 param;\n"
510
    "	varying vec3 _normal;\n"
511
    "	varying vec2 uv;\n"
512
    "	\n"
513
    "	void main(void)\n"
514
    "	{\n"
515
    "		gl_Position =  ftransform();\n"
516
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
517
    "		uv=param;\n"
518
    "	}\n";
519
 
520
    const string CheckerBoardRenderer::fss =
521
    "	varying vec3 _normal;\n"
522
    "	varying vec2 uv;\n"
523
    "   const float pi = 3.14159265359;\n"
524
    "	const vec3 light_dir = vec3(0,0,1);\n"
525
    "	\n"
526
    "	void main()\n"
527
    "	{\n"
528
    "       vec3 normal = normalize(_normal);\n"
529
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
530
    "		vec2 rt = uv;//vec2(length(uv),atan(uv.y, uv.x));\n"
531
    "		float stripe_signal = smoothstep(-0.001,0.001,sin(2.0*pi*rt.x)*sin(10.0*rt.y));\n"
532
    "		\n"
533
    "		gl_FragColor = dot_ln * vec4(0.35,0.25,0.5,0);\n"
534
   "		gl_FragColor.rgb += 0.7*stripe_signal;\n"
535
    "	}\n";
536
 
537
    void CheckerBoardRenderer::compile_display_list(const HMesh::Manifold& m, bool smooth)
538
    {
539
 
540
        GLint old_prog;
541
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
542
        glUseProgram(prog);
543
 
544
        GLuint param_attrib = glGetAttribLocation(prog, "param");
545
        glNewList(display_list,GL_COMPILE);
572 jab 546
 
662 janba 547
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
548
            if(!smooth)
549
                glNormal3dv(normal(m, *f).get());
550
            if(no_edges(m, *f)== 3)
551
                glBegin(GL_TRIANGLES);
552
            else
553
                glBegin(GL_POLYGON);
554
 
555
 
556
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
557
                Vec3d n(normal(m, w.vertex()));
558
                if(smooth)
559
                    glNormal3dv(n.get());
560
                glVertexAttrib2fv(param_attrib, param[w.vertex()].get());
561
                glVertex3dv(m.pos(w.vertex()).get());
562
            }
563
            glEnd();
564
        }
565
        glEndList();
566
        glUseProgram(old_prog);
567
 
568
    }
631 janba 569
 
662 janba 570
 
571
 
557 jab 572
    const string AmbientOcclusionRenderer::vss =
573
    "	attribute float scalar;\n"
574
    "	varying vec3 _normal;\n"
575
    "	varying float s;\n"
576
    "	\n"
577
    "	void main(void)\n"
578
    "	{\n"
579
    "		gl_Position =  ftransform();\n"
580
    "		_normal = normalize(gl_NormalMatrix * gl_Normal);\n"
581
    "		s=scalar;\n"
582
    "	}\n";
583
 
584
    const string AmbientOcclusionRenderer::fss = 	
585
    "	varying vec3 _normal;\n"
586
    "	varying float s;\n"
587
    "	uniform float scalar_max;\n"
588
    "	const vec3 light_dir = vec3(0,0,1);\n"
589
    "	\n"
590
    "	void main()\n"
591
    "	{\n"
592
    "   vec3 normal = normalize(_normal);\n"
593
    "		float dot_ln = max(0.0,dot(light_dir, normal));\n"
594
    "		\n"
595
    "		float s_norm = min(1.0,s/scalar_max+1.0);\n"
596
    "		\n"
597
    "		gl_FragColor = s_norm * vec4(1.0);\n"
598
    "       gl_FragColor *= dot_ln;\n"
599
    "       gl_FragColor.r = pow(gl_FragColor.r, 1.0);\n"
600
    "       gl_FragColor.g = pow(gl_FragColor.g, 1.0);\n"
601
    "       gl_FragColor.b = pow(gl_FragColor.b, 1.0);\n"
602
    "	}\n";
603
 
643 janba 604
    void AmbientOcclusionRenderer::compile_display_list(const HMesh::Manifold& m, HMesh::VertexAttributeVector<double>& field, double max_val)
557 jab 605
    {	
606
        GLint old_prog;
607
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
608
        glUseProgram(prog);
609
 
610
        GLuint scalar_attrib = glGetAttribLocation(prog, "scalar");
611
        glUniform1fARB(glGetUniformLocationARB(prog, "scalar_max"), max_val);
612
 
613
        glNewList(display_list,GL_COMPILE);
614
 
615
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
643 janba 616
 
617
            if(no_edges(m, *f)== 3)
557 jab 618
                glBegin(GL_TRIANGLES);
619
            else 
620
                glBegin(GL_POLYGON);
621
 
587 jab 622
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw())
557 jab 623
            {
624
                Vec3d n(normal(m, w.vertex()));
643 janba 625
                glNormal3dv(n.get());
557 jab 626
                glVertexAttrib1d(scalar_attrib, field[w.vertex()]);
587 jab 627
                glVertex3dv(m.pos(w.vertex()).get());
557 jab 628
            }
629
            glEnd();
630
        }
631
        glEndList();	
632
        glUseProgram(old_prog);
633
 
634
    }
635
 
636
 
647 janba 637
    void LineFieldRenderer::compile_display_list(const HMesh::Manifold& m,HMesh::VertexAttributeVector<CGLA::Vec3d>& lines)
557 jab 638
    {
647 janba 639
        float r;
640
        Vec3d c;
641
        bsphere(m, c, r);
618 jab 642
        float noise_scale = 10.0f/r;
647 janba 643
        float line_scale = 0.02f;
566 jab 644
 
557 jab 645
        GLint old_prog;
646
        glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
647 janba 647
        glUseProgram(prog);
648
        glNewList(display_list,GL_COMPILE);
649
        glUniform1fARB(glGetUniformLocationARB(prog, "line_scale"),line_scale);
557 jab 650
        glUniform1fARB(glGetUniformLocationARB(prog, "noise_scale"),noise_scale);
651
        glUniform1iARB(glGetUniformLocationARB(prog, "noise_tex"),0);
652
        GLuint direction = glGetAttribLocation(prog, "direction");	
643 janba 653
        glBindTexture(GL_TEXTURE_3D, get_noise_texture_id());
654
 
647 janba 655
 
557 jab 656
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
643 janba 657
            if(no_edges(m, *f) == 3)
557 jab 658
                glBegin(GL_TRIANGLES);
659
            else 
660
                glBegin(GL_POLYGON);
661
 
647 janba 662
            Vec3d n(normal(m, *f));
663
            Vec3d d0 = lines[m.walker(*f).vertex()];
664
            d0 = normalize(d0-n*dot(n,d0));
587 jab 665
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
557 jab 666
                Vec3d n(normal(m, w.vertex()));
643 janba 667
                glNormal3dv(n.get());
557 jab 668
 
669
                Vec3d d = lines[w.vertex()];
670
                d = normalize(d-n*dot(n,d));
647 janba 671
                if(dot(d,d0)<0) d=-d;
557 jab 672
                glVertexAttrib3dv(direction, d.get());
587 jab 673
                glVertex3dv(m.pos(w.vertex()).get());
557 jab 674
            }
675
            glEnd();
676
        }
647 janba 677
 
557 jab 678
        glBindTexture(GL_TEXTURE_3D, 0);
679
        glEndList();	
680
        glUseProgram(old_prog);
681
 
682
    }
683
 
684
 
685
    const string LineFieldRenderer::vss = 
686
    "attribute vec3 direction;\n"
687
    "varying vec3 _n;\n"
688
    "varying vec3 dir_obj;\n"
689
    "varying vec3 v_obj;\n"
690
    "\n"
691
    "void main(void)\n"
692
    "{\n"
693
    "	gl_Position = ftransform();\n"
694
    "   v_obj = gl_Vertex.xyz;\n"
695
    "	dir_obj = direction;\n"
696
    "	_n = normalize(gl_NormalMatrix * gl_Normal);\n"
697
    "}\n";
698
 
699
    const string LineFieldRenderer::fss =
700
    "uniform sampler3D noise_tex;\n"
647 janba 701
    "uniform float line_scale;\n"
557 jab 702
    "uniform float noise_scale;\n"
703
    "varying vec3 _n;\n"
704
    "varying vec3 dir_obj;\n"
705
    "varying vec3 v_obj;\n"
706
    "\n"
647 janba 707
    "float tex(vec3 p) {return smoothstep(0.2,0.4,texture3D(noise_tex, p).x);}\n"
557 jab 708
    "void main(void)\n"
709
    "{\n"
710
    "   vec3 n = normalize(_n);\n"
647 janba 711
    "   vec3 d = normalize(dir_obj);\n"
557 jab 712
    "   float I = "
647 janba 713
    "             tex(noise_scale*v_obj + 6.0*line_scale*d) + \n"
714
    "             tex(noise_scale*v_obj - 6.0*line_scale*d) + \n"
715
    "             tex(noise_scale*v_obj + 5.0*line_scale*d) + \n"
716
    "             tex(noise_scale*v_obj - 5.0*line_scale*d) + \n"
717
    "             tex(noise_scale*v_obj + 4.0*line_scale*d) + \n"
718
    "             tex(noise_scale*v_obj - 4.0*line_scale*d) + \n"
719
    "             tex(noise_scale*v_obj + 3.0*line_scale*d) + \n"
720
    "             tex(noise_scale*v_obj - 3.0*line_scale*d) + \n"
721
    "             tex(noise_scale*v_obj + 2.0*line_scale*d) + \n"
722
    "             tex(noise_scale*v_obj - 2.0*line_scale*d) + \n"
723
    "             tex(noise_scale*v_obj + 1.0*line_scale*d) + \n"
724
    "             tex(noise_scale*v_obj - 1.0*line_scale*d) + \n"
557 jab 725
    "			  tex(noise_scale*v_obj); \n"
726
    "	\n"
727
    "   float diff = max(0.0,dot(n,vec3(0.0, 0.0, 1.0)));\n"
647 janba 728
    "	gl_FragColor.rgb = vec3(diff*I/13.0);\n"
557 jab 729
    "	gl_FragColor.a = 1.0;\n"
730
    "}\n";
731
 
647 janba 732
    GLuint HarmonicsRenderer::prog_P0 = 0;
733
    GLGraphics::Console::variable<float> HarmonicsRenderer::display_harmonics_time;
734
    GLGraphics::Console::variable<int> HarmonicsRenderer::display_harmonics_diffuse;
735
    GLGraphics::Console::variable<int> HarmonicsRenderer::display_harmonics_highlight;
663 janba 736
 
737
    GLGraphics::Console::variable<int> HarmonicsRenderer::display_harmonics_e0;
738
    GLGraphics::Console::variable<int> HarmonicsRenderer::display_harmonics_e1;
557 jab 739
 
647 janba 740
    string vss =
741
	"#version 120\n"
742
	"#extension GL_EXT_gpu_shader4 : enable\n"
743
	"	\n"
744
	"	\n"
745
	"	attribute float eigenvalue;\n"
746
	"	attribute float eigenvalue2;\n"
747
	"	varying vec3 normal;\n"
748
	"	varying float eig;\n"
749
	"	varying float eig2;\n"
750
	"	\n"
751
	"	void main(void)\n"
752
	"	{\n"
753
	"		gl_Position =  ftransform();\n"
754
	"		normal = normalize(gl_NormalMatrix * gl_Normal);\n"
755
	"		eig = eigenvalue;\n"
756
	"		eig2 = eigenvalue2;\n"
757
	"	}\n";
758
 
759
	string fss =
760
	"#version 120\n"
761
	"#extension GL_EXT_gpu_shader4 : enable\n"
762
	"	\n"
763
	"	varying vec3 normal;\n"
764
	"	varying float eig;\n"
765
	"	varying float eig2;\n"
766
	"	uniform float eig_max;\n"
767
	"	uniform float eig_max2;\n"
768
	"	uniform bool do_highlight;\n"
769
	"	uniform bool do_diffuse;\n"
770
	"	const vec3 light_dir = vec3(0,0,1);\n"
771
	"	\n"
772
	" float basef(float x) {return max(0.0,min(1.0,2.0-4.0*abs(x)));\n}"
773
	"	void main()\n"
774
	"	{\n"
775
	"		float dot_ln = max(0.0,dot(light_dir, normal));\n"
776
	"		\n"
777
	"		float eig_norm = eig/eig_max;\n"
778
	"		float stripe_signal = 250 * eig_norm;\n"
779
	//"		vec4 stripe_col = abs(stripe_signal) < 3.14 ? vec4(1,1,0,0) : vec4(.4,.4,.4,0);\n"
780
	"		vec4 stripe_col = -vec4(.4,.4,.4,0);\n"
781
	"		\n"
782
	"       float alpha = (1.0-eig_norm) * 2.0 * 3.1415926;\n"
783
	"       float offs = 2.0*3.1415/3.0;\n"
784
	"		gl_FragColor = vec4(0,0,1,0)*basef(eig_norm)+vec4(0,1,0,0)*basef(eig_norm-0.5)+vec4(1,0,0,0)* basef(eig_norm-1.0);\n"
785
	"		if(do_diffuse)   gl_FragColor *= dot_ln;\n"
786
	"		if(do_highlight) gl_FragColor += dot_ln*dot_ln*dot_ln*dot_ln*dot_ln*dot_ln*dot_ln*vec4(.5,.5,.5,0);\n"
787
	"		gl_FragColor -= vec4(.4,.4,.4,.4)*smoothstep(0.2,0.6,cos(stripe_signal));\n"
788
	"	}\n";
789
 
790
 
791
    HarmonicsRenderer::HarmonicsRenderer(HMesh::Manifold& _m, HMesh::Harmonics* _h, GLGraphics::Console& cs): m(&_m), h(_h)
792
    {
793
        if (prog_P0 == 0) {
794
            string shader_path = "/Users/jab/GEL/apps/MeshEdit/";
795
            GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
796
            GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
797
 
798
            // Create the program
799
            prog_P0 = glCreateProgram();
800
 
801
            // Attach all shaders
802
            if(vs) glAttachShader(prog_P0, vs);
803
            if(fs) glAttachShader(prog_P0, fs);
804
 
805
            // Link the program object and print out the info log
806
            glLinkProgram(prog_P0);
807
            print_glsl_program_log(prog_P0);
808
 
809
            // Install program object as part of current state
810
            glUseProgram(0);
811
 
812
 
813
            display_harmonics_diffuse.reg(cs, "display.harmonics.diffuse", "");
814
            display_harmonics_time.reg(cs, "display.harmonics.time", "");
815
            display_harmonics_highlight.reg(cs, "display.harmonics.highlight", "");
663 janba 816
            display_harmonics_e0.reg(cs,"display.harmonics.e0","");
817
            display_harmonics_e1.reg(cs,"display.harmonics.e1","");
647 janba 818
        }
663 janba 819
        draw_esum();
647 janba 820
    }
821
 
822
 
823
    void HarmonicsRenderer::parse_key(unsigned char key)
824
    {
825
        switch(key) {
826
            case '+':
827
                display_harmonics_time = display_harmonics_time+0.001;
828
                break;
829
            case '-':
830
                display_harmonics_time = display_harmonics_time-0.001;
831
                break;
832
            case 'd':
833
                display_harmonics_diffuse = !display_harmonics_diffuse;
834
                break;
835
            case 'h':
836
                display_harmonics_highlight = !display_harmonics_highlight;
837
                break;
838
        }
839
 
840
    }
841
 
842
 
843
 
844
 
845
    void HarmonicsRenderer::draw_adf()
846
    {
847
        VertexAttributeVector<double> F;
848
        double F_max = h->compute_adf(F, display_harmonics_time);
849
        cout << "F max" <<  F_max << endl;
850
 
851
        glNewList(display_list, GL_COMPILE);
852
        glUseProgram(prog_P0);
853
        glUniform1f(glGetUniformLocation(prog_P0,"eig_max"),F_max);//2*M_PI);
854
        glUniform1i(glGetUniformLocation(prog_P0,"do_diffuse"),display_harmonics_diffuse);
855
        glUniform1i(glGetUniformLocation(prog_P0,"do_highlight"),display_harmonics_highlight);
856
        GLuint attrib = glGetAttribLocationARB(prog_P0, "eigenvalue");
857
 
858
        glFrontFace(GL_CW);
859
        for(FaceIDIterator f = m->faces_begin(); f != m->faces_end(); ++f){
860
            glBegin(GL_TRIANGLES);
861
            for(Walker w = m->walker(*f); !w.full_circle(); w = w.circulate_face_cw()){
862
                glVertexAttrib1f(attrib,F[w.vertex()]);
863
                glNormal3dv(normal(*m, w.vertex()).get());
864
                glVertex3dv(m->pos(w.vertex()).get());
865
            }
866
            glEnd();
867
        }
868
        glFrontFace(GL_CCW);
869
        glUseProgram(0);
870
        glEndList();
871
    }
872
 
663 janba 873
    void HarmonicsRenderer::draw_esum()
874
    {
875
        VertexAttributeVector<double> F;
876
        double F_max = h->compute_esum(F, display_harmonics_e0, display_harmonics_e1);
877
        cout << "F max" <<  F_max << endl;
878
 
879
        glNewList(display_list, GL_COMPILE);
880
        glUseProgram(prog_P0);
881
        glUniform1f(glGetUniformLocation(prog_P0,"eig_max"),F_max);//2*M_PI);
882
        glUniform1i(glGetUniformLocation(prog_P0,"do_diffuse"),display_harmonics_diffuse);
883
        glUniform1i(glGetUniformLocation(prog_P0,"do_highlight"),display_harmonics_highlight);
884
        GLuint attrib = glGetAttribLocationARB(prog_P0, "eigenvalue");
885
 
886
        glFrontFace(GL_CW);
887
        for(FaceIDIterator f = m->faces_begin(); f != m->faces_end(); ++f){
888
            glBegin(GL_TRIANGLES);
889
            for(Walker w = m->walker(*f); !w.full_circle(); w = w.circulate_face_cw()){
890
                glVertexAttrib1f(attrib,F[w.vertex()]);
891
                glNormal3dv(normal(*m, w.vertex()).get());
892
                glVertex3dv(m->pos(w.vertex()).get());
893
            }
894
            glEnd();
895
        }
896
        glFrontFace(GL_CCW);
897
        glUseProgram(0);
898
        glEndList();
899
    }
900
 
901
 
647 janba 902
 
557 jab 903
}
904
 
905