|
| 1 | + |
| 2 | + |
| 3 | +#define ALLEGRO_UNSTABLE |
| 4 | + |
| 5 | +#include <AllegroFlare/CubemapTextureBinder.hpp> |
| 6 | + |
| 7 | +#include <AllegroFlare/Cubemap.hpp> |
| 8 | +#include <iostream> // for cout, TODO: replace with AllegroFlare::Logger |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +namespace AllegroFlare |
| 13 | +{ |
| 14 | + |
| 15 | + |
| 16 | +CubemapTextureBinder::CubemapTextureBinder(std::string name, AllegroFlare::Cubemap *cubemap, int unit) |
| 17 | + : name(name) |
| 18 | + , cubemap(cubemap) |
| 19 | + , unit(unit) |
| 20 | +{ |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +CubemapTextureBinder::~CubemapTextureBinder() |
| 25 | +{ |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +bool CubemapTextureBinder::bind() |
| 30 | +{ |
| 31 | + if (!cubemap) return false; |
| 32 | + // grab the currently active shader program |
| 33 | + GLint currProgram; |
| 34 | + glGetIntegerv(GL_CURRENT_PROGRAM, &currProgram); |
| 35 | + GLint shader_program_object = currProgram; |
| 36 | + |
| 37 | + // get and verify that the uniform name is there and valid |
| 38 | + GLint handle = glGetUniformLocation(shader_program_object, name.c_str()); |
| 39 | + if (handle < 0) |
| 40 | + { |
| 41 | + // this warning is silenced because there are instances where a user |
| 42 | + // intentionally attempts to assign the uniform even knowing it is not there. |
| 43 | + // a better reporting mechanisim might be used for all Shader::set_* functions. |
| 44 | + //std::cout << "uniform not found for \"" << name << "\" in shader" << std::endl; |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + // bind it |
| 49 | + glActiveTexture(GL_TEXTURE0 + unit); |
| 50 | + glBindTexture(GL_TEXTURE_CUBE_MAP_EXT, cubemap->get_id()); |
| 51 | + glUniform1i(handle, unit); |
| 52 | + |
| 53 | + // check for errors |
| 54 | + GLenum err = glGetError(); |
| 55 | + if (err != 0) |
| 56 | + { |
| 57 | + // NOTE: in the internal Allegro 5 code, this pattern returns the acual message, rather |
| 58 | + // than the error number. However, the funciton to get the message for the error |
| 59 | + // is an internal Allegro function. This will work for now. |
| 60 | + std::cout << "[Shader::set_sampler] error: glGetError() returned " << err << std::endl; |
| 61 | + return err; |
| 62 | + } |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +} // namespace AllegroFlare |
| 68 | + |
| 69 | + |
0 commit comments