Subversion Repositories gelsvn

Rev

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