Skip to content

Commit 821c5a2

Browse files
committed
* Add AllegroFlare/CubemapTextureBinder
1 parent b531a57 commit 821c5a2

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
4+
#include <string>
5+
6+
7+
namespace AllegroFlare { class Cubemap; };
8+
9+
10+
namespace AllegroFlare
11+
{
12+
class CubemapTextureBinder
13+
{
14+
private:
15+
std::string name;
16+
AllegroFlare::Cubemap *cubemap;
17+
int unit;
18+
19+
public:
20+
CubemapTextureBinder(std::string name="[unset-name]", AllegroFlare::Cubemap *cubemap=nullptr, int unit=0);
21+
~CubemapTextureBinder();
22+
23+
bool bind();
24+
};
25+
}
26+
27+

include/AllegroFlare/Shader.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#include <allegro5/allegro.h>
66
#include <AllegroFlare/Vec3D.hpp>
7-
#include <AllegroFlare/Cubemap.hpp>
7+
8+
9+
namespace AllegroFlare { class Cubemap; }
810

911

1012

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+

src/AllegroFlare/Shader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <sstream>
1010

1111
#include <AllegroFlare/Logger.hpp>
12+
#include <AllegroFlare/Cubemap.hpp>
13+
1214

1315

1416
namespace AllegroFlare
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
#include <gtest/gtest.h>
3+
4+
#include <AllegroFlare/CubemapTextureBinder.hpp>
5+
6+
7+
TEST(AllegroFlare_CubemapTextureBinderTest, can_be_created_without_blowing_up)
8+
{
9+
AllegroFlare::CubemapTextureBinder cube_map_texture_binder;
10+
}
11+
12+
13+
TEST(AllegroFlare_CubemapTextureBinderTest, bind__will_bind_the_texture_to_the_shader_uniform)
14+
{
15+
// TODO
16+
}
17+
18+

0 commit comments

Comments
 (0)