Move to 'using namespace std;' in all .cpp files.
[movit] / flat_input.cpp
1 #include <string.h>
2 #include <assert.h>
3 #include <GL/glew.h>
4
5 #include "effect_util.h"
6 #include "flat_input.h"
7 #include "resource_pool.h"
8 #include "util.h"
9
10 using namespace std;
11
12 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
13         : image_format(image_format),
14           pixel_format(pixel_format),
15           type(type),
16           pbo(0),
17           texture_num(0),
18           finalized(false),
19           output_linear_gamma(false),
20           needs_mipmaps(false),
21           width(width),
22           height(height),
23           pitch(width),
24           pixel_data(NULL)
25 {
26         assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
27         register_int("output_linear_gamma", &output_linear_gamma);
28         register_int("needs_mipmaps", &needs_mipmaps);
29 }
30
31 FlatInput::~FlatInput()
32 {
33         if (texture_num != 0) {
34                 resource_pool->release_2d_texture(texture_num);
35         }
36 }
37
38 void FlatInput::finalize()
39 {
40         // Translate the input format to OpenGL's enums.
41         if (type == GL_FLOAT) {
42                 internal_format = GL_RGBA32F_ARB;
43         } else if (output_linear_gamma) {
44                 assert(type == GL_UNSIGNED_BYTE);
45                 internal_format = GL_SRGB8_ALPHA8;
46         } else {
47                 assert(type == GL_UNSIGNED_BYTE);
48                 internal_format = GL_RGBA8;
49         }
50         if (pixel_format == FORMAT_RGB) {
51                 format = GL_RGB;
52         } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
53                    pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
54                 format = GL_RGBA;
55         } else if (pixel_format == FORMAT_BGR) {
56                 format = GL_BGR;
57         } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
58                    pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
59                 format = GL_BGRA;
60         } else if (pixel_format == FORMAT_GRAYSCALE) {
61                 format = GL_LUMINANCE;
62         } else {
63                 assert(false);
64         }
65
66         finalized = true;
67 }
68         
69 void FlatInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
70 {
71         glActiveTexture(GL_TEXTURE0 + *sampler_num);
72         check_error();
73
74         if (texture_num == 0) {
75                 // (Re-)upload the texture.
76                 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
77                 glBindTexture(GL_TEXTURE_2D, texture_num);
78                 check_error();
79                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
80                 check_error();
81                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
82                 check_error();
83                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
84                 check_error();
85                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
86                 check_error();
87                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
88                 check_error();
89                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
90                 check_error();
91                 if (needs_mipmaps) {
92                         glGenerateMipmap(GL_TEXTURE_2D);
93                         check_error();
94                 }
95                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
96                 check_error();
97                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
98                 check_error();
99                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
100                 check_error();
101                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
102                 check_error();
103         } else {
104                 glBindTexture(GL_TEXTURE_2D, texture_num);
105                 check_error();
106         }
107
108         // Bind it to a sampler.
109         set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
110         ++*sampler_num;
111 }
112
113 string FlatInput::output_fragment_shader()
114 {
115         return read_file("flat_input.frag");
116 }
117
118 void FlatInput::invalidate_pixel_data()
119 {
120         if (texture_num != 0) {
121                 resource_pool->release_2d_texture(texture_num);
122                 texture_num = 0;
123         }
124 }