1 #define GL_GLEXT_PROTOTYPES 1
11 Input::Input(ImageFormat image_format, unsigned width, unsigned height)
12 : image_format(image_format),
15 use_srgb_texture_format(false),
21 register_int("use_srgb_texture_format", &use_srgb_texture_format);
22 register_int("needs_mipmaps", &needs_mipmaps);
25 void Input::finalize()
27 // Translate the input format to OpenGL's enums.
28 GLenum internal_format;
29 if (use_srgb_texture_format) {
30 internal_format = GL_SRGB8;
32 internal_format = GL_RGBA8;
34 if (image_format.pixel_format == FORMAT_RGB) {
37 } else if (image_format.pixel_format == FORMAT_RGBA) {
40 } else if (image_format.pixel_format == FORMAT_BGR) {
43 } else if (image_format.pixel_format == FORMAT_BGRA) {
46 } else if (image_format.pixel_format == FORMAT_GRAYSCALE) {
47 format = GL_LUMINANCE;
53 // Create PBO to hold the texture holding the input image, and then the texture itself.
54 glGenBuffers(1, &pbo);
56 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
58 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
60 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
63 glGenTextures(1, &texture_num);
65 glBindTexture(GL_TEXTURE_2D, texture_num);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
69 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
71 // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here
72 // instead of calling glGenerateMipmap().
73 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, needs_mipmaps);
75 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
77 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
84 void Input::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
87 // Copy the pixel data into the PBO.
88 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
90 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
91 memcpy(mapped_pbo, pixel_data, pitch * height * bytes_per_pixel);
92 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
95 // Re-upload the texture from the PBO.
96 glActiveTexture(GL_TEXTURE0);
98 glBindTexture(GL_TEXTURE_2D, texture_num);
100 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
102 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
104 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
110 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
113 needs_update = false;
116 // Bind it to a sampler.
117 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
121 std::string Input::output_fragment_shader()
123 return read_file("input.frag");