Subversion Repositories gelsvn

Rev

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

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

Generated by GNU Enscript 1.6.6.
-
 
775
 
-
 
776
 
-
 
777