Subversion Repositories gelsvn

Rev

Rev 399 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
399 jab 1
/*
2
 *  WireframeRenderer.cpp
3
 *  GEL
4
 *
5
 *  Created by J. Andreas Bærentzen on 20/09/08.
6
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
7
 *
8
 */
9
 
10
#include <algorithm>
11
#include <string>
12
#include "Renderer.h"
13
#include <GLGraphics/glsl_shader.h>
14
 
15
using namespace GLGraphics;
16
using namespace CGLA;
17
using namespace HMesh;
18
using namespace std;
19
 
20
int WireframeRenderer::maximum_face_valency(HMesh::Manifold& m)
21
{
22
	int max_val = 0;
23
	for(FaceIter f = m.faces_begin(); f != m.faces_end(); ++f)
24
		max_val = max(max_val, no_edges(f));
25
	return max_val;
26
}
27
 
28
 
29
WireframeRenderer::WireframeRenderer(HMesh::Manifold& m, bool flat): idbuff_renderer(0)
30
{
31
	if(GLEW_EXT_geometry_shader4 && maximum_face_valency(m) > 3)
32
	{
33
		GLint viewp[4];
34
		glGetIntegerv(GL_VIEWPORT,viewp);
35
		idbuff_renderer = new IDBufferWireframeRenderer(viewp[2], viewp[3], m);
36
	}
37
	else
38
	{
39
		glNewList(display_list,GL_COMPILE);
40
		if(GLEW_EXT_geometry_shader4)
41
			draw_triangles_in_wireframe(m,flat, Vec3f(1,0,0));				
42
		else
43
			draw_wireframe_oldfashioned(m,flat, Vec3f(1,0,0));
44
		glEndList();
45
	}
46
}
47
 
48
void WireframeRenderer::draw()
49
{
50
	if(idbuff_renderer)
51
		idbuff_renderer->draw(Vec3f(1,0,0),Vec3f(0.5)); 
52
	else
53
		glCallList(display_list);
54
}
55
 
56
SimpleShaderRenderer::SimpleShaderRenderer(HMesh::Manifold& m, const std::string& vss, const std::string& fss)
57
{
58
	vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
59
	print_glsl_program_log(vs);
60
	fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
61
	print_glsl_program_log(fs);
62
 
63
	prog = glCreateProgram();
64
 
65
	if(vs) glAttachShader(prog, vs);
66
	if(fs) glAttachShader(prog, fs);
67
 
68
	glLinkProgram(prog);
69
	print_glsl_program_log(prog);
70
	glNewList(display_list,GL_COMPILE);
71
	GLGraphics::draw(m, true);
72
	glEndList();
73
}
74
 
75
void SimpleShaderRenderer::draw()
76
{
77
	GLint old_prog;
78
	glGetIntegerv(GL_CURRENT_PROGRAM, &old_prog);
79
	glUseProgram(prog);
80
	glCallList(display_list);
81
	glUseProgram(old_prog);
82
}
83
 
84
const string ReflectionLineRenderer::vss = 
85
"varying vec3 n;\n"
86
"varying vec3 v;\n"
87
"\n"
88
"void main(void)\n"
89
"{\n"
90
"	gl_Position = ftransform();\n"
91
"	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
92
"	n = normalize(gl_NormalMatrix * gl_Normal);\n"
93
"}\n";
94
 
95
 
96
const string ReflectionLineRenderer::fss = 
97
"uniform float detail;\n"
98
"\n"
99
"varying vec3 n;\n"
100
"varying vec3 v;\n"
101
"\n"
102
"void main(void)\n"
103
"{\n"
104
"	// calculate the reflection\n"
105
"	vec3 r = normalize(2.0*dot(-v, n)*n + v);\n"
401 jab 106
"	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
107
"   float diff  = dot(n,viewer_lightdir);\n"
399 jab 108
"	\n"
109
"	vec2 r2 = normalize(vec2(r[0], r[2]));\n"
110
"	vec2 x = vec2(1, 0);\n"
111
"	float angle = acos(dot(r2, x));\n"
112
"	\n"
113
"	// decide if we hit a white or black ring, based on y value\n"
401 jab 114
"	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"
399 jab 115
"}\n";
116
 
117
const string IsophoteLineRenderer::vss = 
118
"varying vec3 n;\n"
119
"varying vec3 v;\n"
120
"\n"
121
"void main(void)\n"
122
"{\n"
123
"	gl_Position = ftransform();\n"
124
"	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
125
"	n = normalize(gl_NormalMatrix * gl_Normal);\n"
126
"}\n";
127
 
128
 
129
const string IsophoteLineRenderer::fss = 
130
"uniform float detail;\n"
131
"\n"
132
"varying vec3 n;\n"
133
"varying vec3 v;\n"
134
"\n"
135
"void main(void)\n"
136
"{\n"
401 jab 137
"	vec3 viewer_lightdir = vec3(0, 0, 1.0);\n"
138
"	vec3 isophote_lightdir = viewer_lightdir;\n"
139
"	float angle = acos(dot(n, isophote_lightdir));\n"
140
"   float diff  = dot(n,viewer_lightdir);\n"
399 jab 141
"	\n"
142
"	// decide if we hit a white or black ring, based on y value\n"
401 jab 143
"	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"
399 jab 144
"}\n";
145
 
146
const string MetallicRenderer::vss = 
147
"varying vec3 n;\n"
148
"varying vec3 v;\n"
149
"\n"
150
"void main(void)\n"
151
"{\n"
152
"	gl_Position = ftransform();\n"
153
"	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
154
"	n = normalize(gl_NormalMatrix * gl_Normal);\n"
155
"}\n";
156
 
157
const string MetallicRenderer::fss = 
158
"varying vec3 n;\n"
159
"varying vec3 v;\n"
160
"\n"
161
"void main(void)\n"
162
"{\n"
163
"	vec3 l = normalize(-v);\n"
164
"	vec3 e = l;\n"
165
"	vec3 r = normalize(2.0*dot(l, n)*n - l);\n"
166
"	\n"
167
"	vec4 a = vec4(0.0,0.1,.3,1.0);\n"
168
"	vec4 d = vec4(0.7,0.7,0.0,1.0) * abs(dot(l, n));\n"
401 jab 169
"	float sr = 0.2*smoothstep(0.2,0.95,dot(r, e));\n"
170
"	float sg = 0.09*smoothstep(0.4,0.95,dot(r, e));\n"
171
"	float sb = 0.07*smoothstep(0.9,0.95,dot(r, e));\n"
399 jab 172
"	\n"
173
"	gl_FragColor =  a + d + vec4(sr,sg,sb,1.0);\n"
174
"}\n";
175
 
401 jab 176
const string GlazedRenderer::vss = 
177
"varying vec3 n;\n"
178
"varying vec3 v;\n"
179
"\n"
180
"void main(void)\n"
181
"{\n"
182
"	gl_Position = ftransform();\n"
183
"	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
184
"	n = normalize(gl_NormalMatrix * gl_Normal);\n"
185
"}\n"
186
"\n";
187
 
188
const string GlazedRenderer::fss =
189
"varying vec3 n;\n"
190
"varying vec3 v;\n"
191
"\n"
192
"vec4 glazed_shader(vec4 mat_col,  vec4 light_col, vec3 light_dir)\n"
193
"{\n"
194
"	vec3 e = normalize(-v);\n"
195
"	vec3 r = normalize(2.0*dot(e, n)*n - e);\n"
196
"	float d = max(0.05,dot(light_dir, n));\n"
197
"	vec4 diff = mat_col * light_col *d; 	\n"
198
"	vec4 refl = smoothstep(0.7,0.75,dot(r,light_dir)) * light_col;\n"
199
"	return 0.15*refl + diff;\n"
200
"}\n"
201
"\n"
202
"void main(void)\n"
203
"{\n"
204
"	vec4 mat_col = vec4(0.9,1.0,0.4,1.0);\n"
205
"	\n"
206
"	vec3 light0_dir = vec3(0.0,1.0,0.0);\n"
207
"	vec4 light0_col = vec4(0.7,0.9,1.0,1.0);\n"
208
"	\n"
209
"	vec3 light1_dir = vec3(0.0,0.0,1.0);\n"
210
"	vec4 light1_col = vec4(1.0,1.0,0.7,1.0);\n"
211
"	\n"
212
"	gl_FragColor = \n"
213
"	0.5*glazed_shader(mat_col, light0_col, light0_dir)+\n"
214
"	0.5*glazed_shader(mat_col, light1_col, light1_dir);\n"
215
"	\n"
216
"	gl_FragColor.a = 1.0;\n"
217
"}\n";
218
 
219
 
220