Subversion Repositories gelsvn

Rev

Rev 614 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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