Add a diffusion effect, and hook it up in the GUI.
[movit] / effect_chain.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <assert.h>
6
7 #include <GL/gl.h>
8 #include <GL/glext.h>
9
10 #include <algorithm>
11 #include <set>
12
13 #include "util.h"
14 #include "effect_chain.h"
15 #include "gamma_expansion_effect.h"
16 #include "gamma_compression_effect.h"
17 #include "lift_gamma_gain_effect.h"
18 #include "colorspace_conversion_effect.h"
19 #include "sandbox_effect.h"
20 #include "saturation_effect.h"
21 #include "mirror_effect.h"
22 #include "vignette_effect.h"
23 #include "blur_effect.h"
24 #include "diffusion_effect.h"
25
26 EffectChain::EffectChain(unsigned width, unsigned height)
27         : width(width),
28           height(height),
29           last_added_effect(NULL),
30           use_srgb_texture_format(false),
31           finalized(false) {}
32
33 void EffectChain::add_input(const ImageFormat &format)
34 {
35         input_format = format;
36         output_color_space.insert(std::make_pair(static_cast<Effect *>(NULL), format.color_space));
37         output_gamma_curve.insert(std::make_pair(static_cast<Effect *>(NULL), format.gamma_curve));
38         effect_ids.insert(std::make_pair(static_cast<Effect *>(NULL), "src_image"));
39 }
40
41 void EffectChain::add_output(const ImageFormat &format)
42 {
43         output_format = format;
44 }
45
46 void EffectChain::add_effect_raw(Effect *effect, const std::vector<Effect *> &inputs)
47 {
48         char effect_id[256];
49         sprintf(effect_id, "eff%u", (unsigned)effects.size());
50
51         effects.push_back(effect);
52         effect_ids.insert(std::make_pair(effect, effect_id));
53         assert(inputs.size() == effect->num_inputs());
54         for (unsigned i = 0; i < inputs.size(); ++i) {
55                 if (inputs[i] != NULL) {
56                         assert(std::find(effects.begin(), effects.end(), inputs[i]) != effects.end());
57                 }
58                 outgoing_links[inputs[i]].push_back(effect);
59         }
60         incoming_links.insert(std::make_pair(effect, inputs));
61         last_added_effect = effect;
62 }
63
64 Effect *instantiate_effect(EffectId effect)
65 {
66         switch (effect) {
67         case EFFECT_GAMMA_EXPANSION:
68                 return new GammaExpansionEffect();
69         case EFFECT_GAMMA_COMPRESSION:
70                 return new GammaCompressionEffect();
71         case EFFECT_COLOR_SPACE_CONVERSION:
72                 return new ColorSpaceConversionEffect();
73         case EFFECT_SANDBOX:
74                 return new SandboxEffect();
75         case EFFECT_LIFT_GAMMA_GAIN:
76                 return new LiftGammaGainEffect();
77         case EFFECT_SATURATION:
78                 return new SaturationEffect();
79         case EFFECT_MIRROR:
80                 return new MirrorEffect();
81         case EFFECT_VIGNETTE:
82                 return new VignetteEffect();
83         case EFFECT_BLUR:
84                 return new BlurEffect();
85         case EFFECT_DIFFUSION:
86                 return new DiffusionEffect();
87         }
88         assert(false);
89 }
90
91 Effect *EffectChain::normalize_to_linear_gamma(Effect *input)
92 {
93         GammaCurve current_gamma_curve = output_gamma_curve[input];
94         if (current_gamma_curve == GAMMA_sRGB) {
95                 // TODO: check if the extension exists
96                 use_srgb_texture_format = true;
97                 current_gamma_curve = GAMMA_LINEAR;
98                 return input;
99         } else {
100                 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
101                 gamma_conversion->set_int("source_curve", current_gamma_curve);
102                 std::vector<Effect *> inputs;
103                 inputs.push_back(input);
104                 gamma_conversion->add_self_to_effect_chain(this, inputs);
105                 current_gamma_curve = GAMMA_LINEAR;
106                 return gamma_conversion;
107         }
108 }
109
110 Effect *EffectChain::normalize_to_srgb(Effect *input)
111 {
112         GammaCurve current_gamma_curve = output_gamma_curve[input];
113         ColorSpace current_color_space = output_color_space[input];
114         assert(current_gamma_curve == GAMMA_LINEAR);
115         ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
116         colorspace_conversion->set_int("source_space", current_color_space);
117         colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
118         std::vector<Effect *> inputs;
119         inputs.push_back(input);
120         colorspace_conversion->add_self_to_effect_chain(this, inputs);
121         current_color_space = COLORSPACE_sRGB;
122         return colorspace_conversion;
123 }
124
125 Effect *EffectChain::add_effect(EffectId effect_id, const std::vector<Effect *> &inputs)
126 {
127         Effect *effect = instantiate_effect(effect_id);
128
129         assert(inputs.size() == effect->num_inputs());
130
131         std::vector<Effect *> normalized_inputs = inputs;
132         for (unsigned i = 0; i < normalized_inputs.size(); ++i) {
133                 if (effect->needs_linear_light() && output_gamma_curve[normalized_inputs[i]] != GAMMA_LINEAR) {
134                         normalized_inputs[i] = normalize_to_linear_gamma(normalized_inputs[i]);
135                 }
136                 if (effect->needs_srgb_primaries() && output_color_space[normalized_inputs[i]] != COLORSPACE_sRGB) {
137                         normalized_inputs[i] = normalize_to_srgb(normalized_inputs[i]);
138                 }
139         }
140
141         effect->add_self_to_effect_chain(this, normalized_inputs);
142         return effect;
143 }
144
145 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
146 std::string replace_prefix(const std::string &text, const std::string &prefix)
147 {
148         std::string output;
149         size_t start = 0;
150
151         while (start < text.size()) {
152                 size_t pos = text.find("PREFIX(", start);
153                 if (pos == std::string::npos) {
154                         output.append(text.substr(start, std::string::npos));
155                         break;
156                 }
157
158                 output.append(text.substr(start, pos - start));
159                 output.append(prefix);
160                 output.append("_");
161
162                 pos += strlen("PREFIX(");
163         
164                 // Output stuff until we find the matching ), which we then eat.
165                 int depth = 1;
166                 size_t end_arg_pos = pos;
167                 while (end_arg_pos < text.size()) {
168                         if (text[end_arg_pos] == '(') {
169                                 ++depth;
170                         } else if (text[end_arg_pos] == ')') {
171                                 --depth;
172                                 if (depth == 0) {
173                                         break;
174                                 }
175                         }
176                         ++end_arg_pos;
177                 }
178                 output.append(text.substr(pos, end_arg_pos - pos));
179                 ++end_arg_pos;
180                 assert(depth == 0);
181                 start = end_arg_pos;
182         }
183         return output;
184 }
185
186 EffectChain::Phase EffectChain::compile_glsl_program(const std::vector<Effect *> &inputs, const std::vector<Effect *> &effects)
187 {
188         assert(!inputs.empty());
189         assert(!effects.empty());
190
191         // Figure out the true set of inputs to this phase. These are the ones
192         // that we need somehow but don't calculate ourselves.
193         std::set<Effect *> effect_set(effects.begin(), effects.end());
194         std::set<Effect *> input_set(inputs.begin(), inputs.end());
195         std::vector<Effect *> true_inputs;
196         std::set_difference(input_set.begin(), input_set.end(),
197                 effect_set.begin(), effect_set.end(),
198                 std::back_inserter(true_inputs));
199
200         bool input_needs_mipmaps = false;
201         std::string frag_shader = read_file("header.frag");
202
203         // Create functions for all the texture inputs that we need.
204         for (unsigned i = 0; i < true_inputs.size(); ++i) {
205                 Effect *effect = true_inputs[i];
206                 assert(effect_ids.count(effect) != 0);
207                 std::string effect_id = effect_ids[effect];
208         
209                 frag_shader += std::string("uniform sampler2D tex_") + effect_id + ";\n";       
210                 frag_shader += std::string("vec4 ") + effect_id + "(vec2 tc) {\n";
211                 if (effect == NULL) {
212                         // OpenGL's origin is bottom-left, but most graphics software assumes
213                         // a top-left origin. Thus, for inputs that come from the user,
214                         // we flip the y coordinate. However, for FBOs, the origin
215                         // is all correct, so don't do anything.
216                         frag_shader += "\ttc.y = 1.0f - tc.y;\n";
217                 }
218                 frag_shader += "\treturn texture2D(tex_" + effect_id + ", tc);\n";
219                 frag_shader += "}\n";
220                 frag_shader += "\n";
221         }
222
223         std::string last_effect_id;
224         for (unsigned i = 0; i < effects.size(); ++i) {
225                 Effect *effect = effects[i];
226                 assert(effect != NULL);
227                 assert(effect_ids.count(effect) != 0);
228                 std::string effect_id = effect_ids[effect];
229                 last_effect_id = effect_id;
230
231                 if (incoming_links[effect].size() == 1) {
232                         frag_shader += std::string("#define INPUT ") + effect_ids[incoming_links[effect][0]] + "\n";
233                 } else {
234                         for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
235                                 char buf[256];
236                                 sprintf(buf, "#define INPUT%d %s\n", j + 1, effect_ids[incoming_links[effect][j]].c_str());
237                                 frag_shader += buf;
238                         }
239                 }
240         
241                 frag_shader += "\n";
242                 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
243                 frag_shader += replace_prefix(effect->output_convenience_uniforms(), effect_id);
244                 frag_shader += replace_prefix(effect->output_fragment_shader(), effect_id);
245                 frag_shader += "#undef PREFIX\n";
246                 frag_shader += "#undef FUNCNAME\n";
247                 if (incoming_links[effect].size() == 1) {
248                         frag_shader += "#undef INPUT\n";
249                 } else {
250                         for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
251                                 char buf[256];
252                                 sprintf(buf, "#undef INPUT%d\n", j + 1);
253                                 frag_shader += buf;
254                         }
255                 }
256                 frag_shader += "\n";
257
258                 input_needs_mipmaps |= effect->needs_mipmaps();
259         }
260         assert(!last_effect_id.empty());
261         frag_shader += std::string("#define INPUT ") + last_effect_id + "\n";
262         frag_shader.append(read_file("footer.frag"));
263         printf("%s\n", frag_shader.c_str());
264         
265         GLuint glsl_program_num = glCreateProgram();
266         GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
267         GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
268         glAttachShader(glsl_program_num, vs_obj);
269         check_error();
270         glAttachShader(glsl_program_num, fs_obj);
271         check_error();
272         glLinkProgram(glsl_program_num);
273         check_error();
274
275         Phase phase;
276         phase.glsl_program_num = glsl_program_num;
277         phase.input_needs_mipmaps = input_needs_mipmaps;
278         phase.inputs = true_inputs;
279         phase.effects = effects;
280
281         return phase;
282 }
283
284 // Construct GLSL programs, starting at the given effect and following
285 // the chain from there. We end a program every time we come to an effect
286 // marked as "needs texture bounce", one that is used by multiple other
287 // effects, and of course at the end.
288 void EffectChain::construct_glsl_programs(Effect *start, std::set<Effect *> *completed_effects)
289 {
290         if (completed_effects->count(start) != 0) {
291                 // This has already been done for us.
292                 return;
293         }
294
295         std::vector<Effect *> this_phase_inputs;  // Also includes all intermediates; these will be filtered away later.
296         std::vector<Effect *> this_phase_effects;
297         Effect *node = start;
298         for ( ;; ) {  // Termination condition within loop.
299                 if (node == NULL) {
300                         this_phase_inputs.push_back(node);
301                 } else {
302                         // Check that we have all the inputs we need for this effect.
303                         // If not, we end the phase here right away; the other side
304                         // of the input chain will eventually come and pick the effect up.
305                         assert(incoming_links.count(node) != 0);
306                         std::vector<Effect *> deps = incoming_links[node];
307                         assert(!deps.empty());
308                         bool have_all_deps = true;
309                         for (unsigned i = 0; i < deps.size(); ++i) {
310                                 if (completed_effects->count(deps[i]) == 0) {
311                                         have_all_deps = false;
312                                         break;
313                                 }
314                         }
315                 
316                         if (!have_all_deps) {
317                                 if (!this_phase_effects.empty()) {
318                                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
319                                 }
320                                 return;
321                         }
322                         this_phase_inputs.insert(this_phase_inputs.end(), deps.begin(), deps.end());    
323                         this_phase_effects.push_back(node);
324                 }
325                 completed_effects->insert(node);        
326
327                 // Find all the effects that use this one as a direct input.
328                 if (outgoing_links.count(node) == 0) {
329                         // End of the line; output.
330                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
331                         return;
332                 }
333
334                 std::vector<Effect *> next = outgoing_links[node];
335                 assert(!next.empty());
336                 if (next.size() > 1) {
337                         // More than one effect uses this as the input.
338                         // The easiest thing to do (and probably also the safest
339                         // performance-wise in most cases) is to bounce it to a texture
340                         // and then let the next passes read from that.
341                         if (node != NULL) {
342                                 phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
343                         }
344
345                         // Start phases for all the effects that need us (in arbitrary order).
346                         for (unsigned i = 0; i < next.size(); ++i) {
347                                 construct_glsl_programs(next[i], completed_effects);
348                         }
349                         return;
350                 }
351         
352                 // OK, only one effect uses this as the input. Keep iterating,
353                 // but first see if it requires a texture bounce; if so, give it
354                 // one by starting a new phase.
355                 node = next[0];
356                 if (node->needs_texture_bounce()) {
357                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
358                         this_phase_inputs.clear();
359                         this_phase_effects.clear();
360                 }
361         }
362 }
363
364 void EffectChain::finalize()
365 {
366         // Add normalizers to get the output format right.
367         GammaCurve current_gamma_curve = output_gamma_curve[last_added_effect];  // FIXME
368         ColorSpace current_color_space = output_color_space[last_added_effect];  // FIXME
369         if (current_color_space != output_format.color_space) {
370                 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
371                 colorspace_conversion->set_int("source_space", current_color_space);
372                 colorspace_conversion->set_int("destination_space", output_format.color_space);
373                 effects.push_back(colorspace_conversion);
374                 current_color_space = output_format.color_space;
375         }
376         if (current_gamma_curve != output_format.gamma_curve) {
377                 if (current_gamma_curve != GAMMA_LINEAR) {
378                         normalize_to_linear_gamma(last_added_effect);  // FIXME
379                 }
380                 assert(current_gamma_curve == GAMMA_LINEAR);
381                 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
382                 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
383                 effects.push_back(gamma_conversion);
384                 current_gamma_curve = output_format.gamma_curve;
385         }
386
387         // Construct all needed GLSL programs, starting at the input.
388         std::set<Effect *> completed_effects;
389         construct_glsl_programs(NULL, &completed_effects);
390
391         // If we have more than one phase, we need intermediate render-to-texture.
392         // Construct an FBO, and then as many textures as we need.
393         // We choose the simplest option of having one texture per output,
394         // since otherwise this turns into an (albeit simple)
395         // register allocation problem.
396         if (phases.size() > 1) {
397                 glGenFramebuffers(1, &fbo);
398
399                 for (unsigned i = 0; i < phases.size() - 1; ++i) {
400                         Effect *output_effect = phases[i].effects.back();
401                         GLuint temp_texture;
402                         glGenTextures(1, &temp_texture);
403                         check_error();
404                         glBindTexture(GL_TEXTURE_2D, temp_texture);
405                         check_error();
406                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
407                         check_error();
408                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
409                         check_error();
410                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
411                         check_error();
412                         effect_output_textures.insert(std::make_pair(output_effect, temp_texture));
413                 }
414         }
415         
416         // Translate the input format to OpenGL's enums.
417         GLenum internal_format;
418         if (use_srgb_texture_format) {
419                 internal_format = GL_SRGB8;
420         } else {
421                 internal_format = GL_RGBA8;
422         }
423         if (input_format.pixel_format == FORMAT_RGB) {
424                 format = GL_RGB;
425                 bytes_per_pixel = 3;
426         } else if (input_format.pixel_format == FORMAT_RGBA) {
427                 format = GL_RGBA;
428                 bytes_per_pixel = 4;
429         } else if (input_format.pixel_format == FORMAT_BGR) {
430                 format = GL_BGR;
431                 bytes_per_pixel = 3;
432         } else if (input_format.pixel_format == FORMAT_BGRA) {
433                 format = GL_BGRA;
434                 bytes_per_pixel = 4;
435         } else {
436                 assert(false);
437         }
438
439         // Create PBO to hold the texture holding the input image, and then the texture itself.
440         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
441         check_error();
442         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
443         check_error();
444         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
445         check_error();
446         
447         glGenTextures(1, &source_image_num);
448         check_error();
449         glBindTexture(GL_TEXTURE_2D, source_image_num);
450         check_error();
451         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
452         check_error();
453         // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here.
454         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, phases[0].input_needs_mipmaps ? GL_TRUE : GL_FALSE);
455         check_error();
456         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
457         check_error();
458
459         finalized = true;
460 }
461
462 void EffectChain::render_to_screen(unsigned char *src)
463 {
464         assert(finalized);
465
466         // Copy the pixel data into the PBO.
467         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
468         check_error();
469         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
470         memcpy(mapped_pbo, src, width * height * bytes_per_pixel);
471         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
472         check_error();
473
474         // Re-upload the texture from the PBO.
475         glActiveTexture(GL_TEXTURE0);
476         check_error();
477         glBindTexture(GL_TEXTURE_2D, source_image_num);
478         check_error();
479         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
480         check_error();
481         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
482         check_error();
483         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
484         check_error();
485         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
486         check_error();
487
488         // Basic state.
489         glDisable(GL_BLEND);
490         check_error();
491         glDisable(GL_DEPTH_TEST);
492         check_error();
493         glDepthMask(GL_FALSE);
494         check_error();
495
496         glMatrixMode(GL_PROJECTION);
497         glLoadIdentity();
498         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
499
500         glMatrixMode(GL_MODELVIEW);
501         glLoadIdentity();
502
503         if (phases.size() > 1) {
504                 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
505                 check_error();
506         }
507
508         std::set<Effect *> generated_mipmaps;
509         generated_mipmaps.insert(NULL);  // Already done further up.
510
511         for (unsigned phase = 0; phase < phases.size(); ++phase) {
512                 glUseProgram(phases[phase].glsl_program_num);
513                 check_error();
514
515                 // Set up inputs for this phase.
516                 assert(!phases[phase].inputs.empty());
517                 for (unsigned sampler = 0; sampler < phases[phase].inputs.size(); ++sampler) {
518                         glActiveTexture(GL_TEXTURE0 + sampler);
519                         Effect *input = phases[phase].inputs[sampler];
520                         if (input == NULL) {
521                                 glBindTexture(GL_TEXTURE_2D, source_image_num);
522                                 check_error();
523                         } else {
524                                 assert(effect_output_textures.count(input) != 0);
525                                 glBindTexture(GL_TEXTURE_2D, effect_output_textures[input]);
526                                 check_error();
527                         }
528                         if (phases[phase].input_needs_mipmaps) {
529                                 if (generated_mipmaps.count(input) == 0) {
530                                         glGenerateMipmap(GL_TEXTURE_2D);
531                                         check_error();
532                                         generated_mipmaps.insert(input);
533                                 }
534                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
535                                 check_error();
536                         } else {
537                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
538                                 check_error();
539                         }
540
541                         assert(effect_ids.count(input));
542                         std::string texture_name = std::string("tex_") + effect_ids[input];
543                         glUniform1i(glGetUniformLocation(phases[phase].glsl_program_num, texture_name.c_str()), sampler);
544                         check_error();
545                 }
546
547                 // And now the output.
548                 if (phase == phases.size() - 1) {
549                         // Last phase goes directly to the screen.
550                         glBindFramebuffer(GL_FRAMEBUFFER, 0);
551                         check_error();
552                 } else {
553                         Effect *last_effect = phases[phase].effects.back();
554                         assert(effect_output_textures.count(last_effect) != 0);
555                         glFramebufferTexture2D(
556                                 GL_FRAMEBUFFER,
557                                 GL_COLOR_ATTACHMENT0,
558                                 GL_TEXTURE_2D,
559                                 effect_output_textures[last_effect],
560                                 0);
561                         check_error();
562                 }
563
564                 // Give the required parameters to all the effects.
565                 unsigned sampler_num = phases[phase].inputs.size();
566                 for (unsigned i = 0; i < phases[phase].effects.size(); ++i) {
567                         Effect *effect = phases[phase].effects[i];
568                         effect->set_uniforms(phases[phase].glsl_program_num, effect_ids[effect], &sampler_num);
569                 }
570
571                 // Now draw!
572                 glBegin(GL_QUADS);
573
574                 glTexCoord2f(0.0f, 0.0f);
575                 glVertex2f(0.0f, 0.0f);
576
577                 glTexCoord2f(1.0f, 0.0f);
578                 glVertex2f(1.0f, 0.0f);
579
580                 glTexCoord2f(1.0f, 1.0f);
581                 glVertex2f(1.0f, 1.0f);
582
583                 glTexCoord2f(0.0f, 1.0f);
584                 glVertex2f(0.0f, 1.0f);
585
586                 glEnd();
587                 check_error();
588
589                 // HACK
590                 glActiveTexture(GL_TEXTURE0);
591                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
592                 check_error();
593                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
594                 check_error();
595         }
596 }