Emulate glReadPixels of GL_ALPHA.
[movit] / slice_effect.cpp
1 #include <epoxy/gl.h>
2
3 #include "effect_chain.h"
4 #include "slice_effect.h"
5 #include "effect_util.h"
6 #include "util.h"
7
8 using namespace std;
9
10 namespace movit {
11
12 SliceEffect::SliceEffect()
13         : input_slice_size(1),
14           output_slice_size(1),
15           offset(0),
16           direction(VERTICAL)
17 {
18         register_int("input_slice_size", &input_slice_size);
19         register_int("output_slice_size", &output_slice_size);
20         register_int("offset", &offset);
21         register_int("direction", (int *)&direction);
22 }
23
24 string SliceEffect::output_fragment_shader()
25 {
26         char buf[256];
27         sprintf(buf, "#define DIRECTION_VERTICAL %d\n", (direction == VERTICAL));
28         return buf + read_file("slice_effect.frag");
29 }
30         
31 void SliceEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
32 {
33         assert(input_num == 0);
34         input_width = width;
35         input_height = height;
36 }
37
38 void SliceEffect::get_output_size(unsigned *width, unsigned *height,
39                                   unsigned *virtual_width, unsigned *virtual_height) const
40 {
41         if (direction == HORIZONTAL) {
42                 *width = div_round_up(input_width, input_slice_size) * output_slice_size;
43                 *height = input_height; 
44         } else {
45                 *width = input_width;   
46                 *height = div_round_up(input_height, input_slice_size) * output_slice_size;
47         }
48         *virtual_width = *width;
49         *virtual_height = *height;
50 }
51
52 void SliceEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
53 {
54         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
55
56         unsigned output_width, output_height;
57         get_output_size(&output_width, &output_height, &output_width, &output_height);
58
59         if (direction == HORIZONTAL) {
60                 set_uniform_float(glsl_program_num, prefix, "output_coord_to_slice_num", float(output_width) / float(output_slice_size));
61                 set_uniform_float(glsl_program_num, prefix, "slice_num_to_input_coord", float(input_slice_size) / float(input_width));
62                 set_uniform_float(glsl_program_num, prefix, "slice_offset_to_input_coord", float(output_slice_size) / float(input_width));
63                 set_uniform_float(glsl_program_num, prefix, "offset", float(offset) / float(input_width));
64         } else {
65                 set_uniform_float(glsl_program_num, prefix, "output_coord_to_slice_num", float(output_height) / float(output_slice_size));
66                 set_uniform_float(glsl_program_num, prefix, "slice_num_to_input_coord", float(input_slice_size) / float(input_height));
67                 set_uniform_float(glsl_program_num, prefix, "slice_offset_to_input_coord", float(output_slice_size) / float(input_height));
68                 set_uniform_float(glsl_program_num, prefix, "offset", float(offset) / float(input_height));
69         }
70
71         // Normalized coordinates could potentially cause blurring of the image.
72         // It isn't critical, but still good practice.
73         Node *self = chain->find_node_for_effect(this);
74         glActiveTexture(chain->get_input_sampler(self, 0));
75         check_error();
76         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
77         check_error();
78         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
79         check_error();
80 }
81
82 }  // namespace movit