1 #define GL_GLEXT_PROTOTYPES 1
11 Input::Input(ImageFormat image_format, unsigned width, unsigned height)
12 : image_format(image_format),
14 use_srgb_texture_format(false),
19 register_int("use_srgb_texture_format", &use_srgb_texture_format);
20 register_int("needs_mipmaps", &needs_mipmaps);
23 void Input::finalize()
25 // Translate the input format to OpenGL's enums.
26 GLenum internal_format;
27 if (use_srgb_texture_format) {
28 internal_format = GL_SRGB8;
30 internal_format = GL_RGBA8;
32 if (image_format.pixel_format == FORMAT_RGB) {
35 } else if (image_format.pixel_format == FORMAT_RGBA) {
38 } else if (image_format.pixel_format == FORMAT_BGR) {
41 } else if (image_format.pixel_format == FORMAT_BGRA) {
48 // Create PBO to hold the texture holding the input image, and then the texture itself.
49 glGenBuffers(1, &pbo);
51 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
53 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
55 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
58 glGenTextures(1, &texture_num);
60 glBindTexture(GL_TEXTURE_2D, texture_num);
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
64 // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here
65 // instead of calling glGenerateMipmap().
66 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, needs_mipmaps);
68 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
74 void Input::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
77 // Copy the pixel data into the PBO.
78 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
80 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
81 memcpy(mapped_pbo, pixel_data, width * height * bytes_per_pixel);
82 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
85 // Re-upload the texture from the PBO.
86 glActiveTexture(GL_TEXTURE0);
88 glBindTexture(GL_TEXTURE_2D, texture_num);
90 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
96 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
102 // Bind it to a sampler.
103 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
107 std::string Input::output_fragment_shader()
109 return read_file("input.frag");