-
Notifications
You must be signed in to change notification settings - Fork 0
/
tube_renderer.cpp
280 lines (210 loc) · 7.99 KB
/
tube_renderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "tube_renderer.h"
#include <cgv_gl/gl/gl.h>
#include <cgv_gl/gl/gl_tools.h>
tube_render_style::tube_render_style() : material("default") {
surface_color = cgv::media::illum::surface_material::color_type(0.4f, 0.1f, 0.7f);
culling_mode = CM_OFF;
illumination_mode = IM_ONE_SIDED;
map_color_to_material = 3;
material.ref_brdf_type() = cgv::media::illum::BrdfType(cgv::media::illum::BT_STRAUSS_DIFFUSE + cgv::media::illum::BT_STRAUSS);
radius = 1.0f;
radius_scale = 1.0f;
enable_ambient_occlusion = false;
ao_offset = 0.04f;
ao_distance = 0.8f;
ao_strength = 1.0f;
tex_offset = vec3(0.0f);
tex_scaling = vec3(1.0f);
tex_coord_scaling = vec3(1.0f);
texel_size = 1.0f;
cone_angle_factor = 1.0f;
sample_dirs.resize(3);
sample_dirs[0] = vec3(0.0f, 1.0f, 0.0f);
sample_dirs[1] = vec3(0.0f, 1.0f, 0.0f);
sample_dirs[2] = vec3(0.0f, 1.0f, 0.0f);
}
tube_render_style* tube_renderer::create_render_style() const {
return new tube_render_style();
}
tube_renderer::tube_renderer() {
has_positions = false;
has_colors = false;
has_radii = false;
trs = nullptr;
default_render_style = nullptr;
shader_defines = "";
glGenBuffers(1, &dummy_vao);
}
void tube_renderer::destruct(const context& ctx) {
aab.destruct(ctx);
vbos.clear();
has_positions = false;
has_colors = false;
has_radii = false;
rasterize_prog.destruct(ctx);
shading_prog.destruct(ctx);
if(default_render_style)
delete default_render_style;
default_render_style = nullptr;
if(dummy_vao > 0)
glDeleteBuffers(1, &dummy_vao);
dummy_vao = 0;
}
bool tube_renderer::init(cgv::render::context& ctx) {
if(!default_render_style) {
default_render_style = create_render_style();
}
if(!trs)
trs = default_render_style;
bool res = default_render_style != 0;
res = aab.create(ctx) && res;
if(!rasterize_prog.is_created() || shading_prog.is_created()) {
res = build_shader(ctx, build_define_string()) && res;
}
return res;
}
std::string tube_renderer::build_define_string() {
std::string defines = "ENABLE_AMBIENT_OCCLUSION=";
defines += std::to_string((int)trs->enable_ambient_occlusion);
return defines;
}
bool tube_renderer::build_shader(context& ctx, std::string defines) {
shader_defines = defines;
if(rasterize_prog.is_created())
rasterize_prog.destruct(ctx);
if(!rasterize_prog.is_created()) {
if(!rasterize_prog.build_program(ctx, "tube.glpr", true, defines)) {
std::cerr << "ERROR in tube_renderer::init() ... could not build program tube.glpr" << std::endl;
return false;
}
}
if(shading_prog.is_created())
shading_prog.destruct(ctx);
if(!shading_prog.is_created()) {
if(!shading_prog.build_program(ctx, "tube_shading.glpr", true, defines)) {
std::cerr << "ERROR in tube_renderer::init() ... could not build program tube_shading.glpr" << std::endl;
return false;
}
}
return true;
}
int tube_renderer::get_vbo(const context& ctx, const std::string attr_name) {
int loc = rasterize_prog.get_attribute_location(ctx, attr_name);
auto it = vbos.find(loc);
if(it != vbos.end()) {
vertex_buffer* vbo_ptr = vbos[loc];
if(vbo_ptr->handle) {
return (const int&)vbo_ptr->handle - 1;
}
}
return (const int&)-1;
}
bool tube_renderer::enable(context& ctx) {
std::string defines = build_define_string();
if(defines != shader_defines) {
if(!build_shader(ctx, defines))
return false;
}
if(!rasterize_prog.is_linked() || !shading_prog.is_linked())
return false;
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
//glDisable(GL_CULL_FACE);
bool res = aab.enable(ctx);
return true;
}
bool tube_renderer::disable(context& ctx) {
glDisable(GL_CULL_FACE);
bool res = aab.disable(ctx);
return res;
}
void tube_renderer::rasterize(context& ctx, GLsizei count) {
rasterize_prog.enable(ctx);
if(!has_radii)
rasterize_prog.set_attribute(ctx, "radius", trs->radius);
rasterize_prog.set_uniform(ctx, "radius_scale", trs->radius_scale);
rasterize_prog.set_uniform(ctx, "eye_pos", eye_position);
rasterize_prog.set_uniform(ctx, "view_dir", view_direction);
glDrawArrays(GL_LINES, (GLint)0, count);
rasterize_prog.disable(ctx);
}
void tube_renderer::shade(context& ctx) {
shading_prog.enable(ctx);
ctx.set_material(trs->material);
ctx.set_color(trs->surface_color);
shading_prog.set_uniform(ctx, "map_color_to_material", int(trs->map_color_to_material));
shading_prog.set_uniform(ctx, "culling_mode", int(trs->culling_mode));
shading_prog.set_uniform(ctx, "illumination_mode", int(trs->illumination_mode));
shading_prog.set_uniform(ctx, "viewport_dims", ivec2(ctx.get_width(), ctx.get_height()));
if(trs->enable_ambient_occlusion) {
shading_prog.set_uniform(ctx, "ao_offset", trs->ao_offset);
shading_prog.set_uniform(ctx, "ao_distance", trs->ao_distance);
shading_prog.set_uniform(ctx, "ao_strength", trs->ao_strength);
shading_prog.set_uniform(ctx, "density_tex_offset", trs->tex_offset);
shading_prog.set_uniform(ctx, "density_tex_scaling", trs->tex_scaling);
shading_prog.set_uniform(ctx, "tex_coord_scaling", trs->tex_coord_scaling);
shading_prog.set_uniform(ctx, "texel_size", trs->texel_size);
shading_prog.set_uniform(ctx, "cone_angle_factor", trs->cone_angle_factor);
shading_prog.set_uniform_array(ctx, "sample_dirs", trs->sample_dirs);
}
mat4 MV(ctx.get_modelview_matrix());
mat3 NM;
NM(0, 0) = MV(0, 0);
NM(0, 1) = MV(0, 1);
NM(0, 2) = MV(0, 2);
NM(1, 0) = MV(1, 0);
NM(1, 1) = MV(1, 1);
NM(1, 2) = MV(1, 2);
NM(2, 0) = MV(2, 0);
NM(2, 1) = MV(2, 1);
NM(2, 2) = MV(2, 2);
NM.transpose();
shading_prog.set_uniform(ctx, "inverse_modelview_mat", inv(MV));
shading_prog.set_uniform(ctx, "inverse_normal_mat", NM);
glDisable(GL_DEPTH_TEST);
glBindVertexArray(dummy_vao);
glDrawArrays(GL_TRIANGLE_STRIP, (GLint)0, (GLsizei)4);
glBindVertexArray(0);
glEnable(GL_DEPTH_TEST);
shading_prog.disable(ctx);
}
#include <cgv/gui/provider.h>
namespace cgv {
namespace gui {
struct tube_render_style_gui_creator : public gui_creator {
/// attempt to create a gui and return whether this was successful
bool create(provider* p, const std::string& label, void* value_ptr, const std::string& value_type, const std::string& gui_type, const std::string& options, bool*) {
if(value_type != cgv::type::info::type_name<tube_render_style>::get_name())
return false;
tube_render_style* trs_ptr = reinterpret_cast<tube_render_style*>(value_ptr);
cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
// Tube parameters
p->add_member_control(b, "default radius", trs_ptr->radius, "value_slider", "min=0.001;step=0.0001;max=10.0;log=true;ticks=true");
p->add_member_control(b, "radius scale", trs_ptr->radius_scale, "value_slider", "min=0.01;step=0.0001;max=100.0;log=true;ticks=true");
// Ambient occlusion parameters
p->add_decorator("ambient occlusion", "heading", "level=4");
p->add_member_control(b, "enable", trs_ptr->enable_ambient_occlusion, "check");
p->add_member_control(b, "ao offset", trs_ptr->ao_offset, "value_slider", "min=0.0;step=0.0001;max=0.2;log=true;ticks=true");
p->add_member_control(b, "ao distance", trs_ptr->ao_distance, "value_slider", "min=0.0;step=0.0001;max=1.0;log=true;ticks=true");
p->add_member_control(b, "ao strength", trs_ptr->ao_strength, "value_slider", "min=0.0;step=0.0001;max=10.0;log=true;ticks=true");
// Surface paramaters
p->add_member_control(b, "illumination_mode", trs_ptr->illumination_mode, "dropdown", "enums='off,onesided,twosided'");
if(p->begin_tree_node("color and materials", trs_ptr->surface_color, false, "level=3")) {
p->align("\a");
p->add_member_control(b, "surface_color", trs_ptr->surface_color);
if(p->begin_tree_node("material", trs_ptr->material, false, "level=3")) {
p->align("\a");
p->add_gui("front_material", trs_ptr->material);
p->align("\b");
p->end_tree_node(trs_ptr->material);
}
p->align("\b");
p->end_tree_node(trs_ptr->surface_color);
}
return true;
}
};
#include <cgv_gl/gl/lib_begin.h>
cgv::gui::gui_creator_registration<tube_render_style_gui_creator> tube_rs_gc_reg("tube_render_style_gui_creator");
}
}