26
26
#define MODULE_NAME " Teapot::Texture"
27
27
#include " android_debug.h"
28
28
29
- /* *
30
- * Cubemap and Texture2d implementations for Class Texture.
31
- */
32
- class TextureCubemap : public Texture {
33
- protected:
34
- GLuint texId_ = GL_INVALID_VALUE;
35
- bool activated_ = false ;
36
-
37
- public:
38
- virtual ~TextureCubemap ();
39
- TextureCubemap (std::vector<std::string>& texFiles,
40
- AAssetManager* assetManager);
41
- virtual bool GetActiveSamplerInfo (std::vector<std::string>& names,
42
- std::vector<GLint>& units);
43
- virtual bool Activate (void );
44
- virtual GLuint GetTexType ();
45
- virtual GLuint GetTexId ();
46
- };
47
-
48
- class Texture2d : public Texture {
49
- protected:
50
- GLuint texId_ = GL_INVALID_VALUE;
51
- bool activated_ = false ;
52
-
53
- public:
54
- virtual ~Texture2d ();
55
- // Implement just one texture
56
- Texture2d (std::string& texFiles, AAssetManager* assetManager);
57
- virtual bool GetActiveSamplerInfo (std::vector<std::string>& names,
58
- std::vector<GLint>& units);
59
- virtual bool Activate (void );
60
- virtual GLuint GetTexType ();
61
- virtual GLuint GetTexId ();
62
- };
63
-
64
- /* *
65
- * Capability debug string
66
- */
67
- static const std::string supportedTextureTypes =
68
- " GL_TEXTURE_2D(0x0DE1) GL_TEXTURE_CUBE_MAP(0x8513)" ;
69
-
70
29
/* *
71
30
* Interface implementations
72
31
*/
73
- Texture::Texture () {}
74
- Texture::~Texture () {}
75
- /* *
76
- * Create Texture Object
77
- * @param texFiles holds the texture file name(s) under APK's assets
78
- * @param assetManager is used to open texture files inside assets
79
- * @return is the newly created Texture Object
80
- */
81
- Texture* Texture::Create (std::vector<std::string>& texFiles,
82
- AAssetManager* assetManager) {
83
- return new TextureCubemap (texFiles, assetManager);
84
- }
85
-
86
- /* *
87
- * TextureCubemap implementations
88
- */
89
- bool TextureCubemap::Activate (void ) {
90
- assert (texId_ != GL_INVALID_VALUE);
91
-
92
- glBindTexture (texId_, GL_TEXTURE0);
93
- glActiveTexture (GL_TEXTURE0 + 0 );
94
- activated_ = true ;
95
- return true ;
96
- }
97
-
98
- GLuint TextureCubemap::GetTexType () { return GL_TEXTURE_CUBE_MAP; }
99
-
100
- GLuint TextureCubemap::GetTexId () {
101
- assert (texId_ != GL_INVALID_VALUE);
102
- return texId_;
103
- }
104
-
105
- TextureCubemap::TextureCubemap (std::vector<std::string>& files,
106
- AAssetManager* mgr) {
32
+ Texture::Texture (std::vector<std::string>& asset_paths,
33
+ AAssetManager* asset_manager) {
107
34
// For Cubemap, we use world normal to sample the textures
108
35
// so no texture vbo necessary
109
36
110
37
int32_t imgWidth, imgHeight, channelCount;
111
38
std::vector<uint8_t > fileBits;
112
39
113
- if (!mgr || files .size () != 6 ) {
40
+ if (!asset_manager || asset_paths .size () != 6 ) {
114
41
assert (false );
115
42
return ;
116
43
}
@@ -125,9 +52,9 @@ TextureCubemap::TextureCubemap(std::vector<std::string>& files,
125
52
126
53
for (GLuint i = 0 ; i < 6 ; i++) {
127
54
fileBits.clear ();
128
- AssetReadFile (mgr, files [i], fileBits);
55
+ AssetReadFile (asset_manager, asset_paths [i], fileBits);
129
56
130
- // tga/bmp files are saved as vertical mirror images ( at least more than
57
+ // tga/bmp asset_paths are saved as vertical mirror images ( at least more than
131
58
// half ).
132
59
stbi_set_flip_vertically_on_load (1 );
133
60
@@ -147,110 +74,35 @@ TextureCubemap::TextureCubemap(std::vector<std::string>& files,
147
74
glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_REPEAT);
148
75
149
76
glActiveTexture (GL_TEXTURE0);
150
- activated_ = true ;
151
77
}
152
78
153
- /* *
154
- * Dtor
155
- * clean-up function
156
- */
157
- TextureCubemap::~TextureCubemap () {
79
+ Texture::~Texture () {
158
80
if (texId_ != GL_INVALID_VALUE) {
159
81
glDeleteTextures (1 , &texId_);
160
82
texId_ = GL_INVALID_VALUE;
161
- activated_ = false ;
162
83
}
163
84
}
164
85
165
86
/* *
166
- Return used sampler names and units
167
- so application could configure shader's sampler uniform(s).
168
- Cubemap just used one sampler at unit 0 with "samplerObj" as its name.
87
+ * TextureCubemap implementations
169
88
*/
170
- bool TextureCubemap::GetActiveSamplerInfo (std::vector<std::string>& names,
171
- std::vector<GLint>& units) {
172
- names.clear ();
173
- names.push_back (std::string (" samplerObj" ));
174
- units.clear ();
175
- units.push_back (0 );
89
+ bool Texture::Activate (void ) {
90
+ assert (texId_ != GL_INVALID_VALUE);
176
91
92
+ glBindTexture (texId_, GL_TEXTURE0);
93
+ glActiveTexture (GL_TEXTURE0 + 0 );
177
94
return true ;
178
95
}
179
96
180
97
/* *
181
- * Texture2D implementation
182
- */
183
- Texture2d::Texture2d (std::string& fileName, AAssetManager* assetManager) {
184
- if (!assetManager) {
185
- LOGE (" AssetManager to Texture2D() could not be null!!!" );
186
- assert (false );
187
- return ;
188
- }
189
-
190
- int32_t imgWidth, imgHeight, channelCount;
191
- std::string texName (fileName);
192
- std::vector<uint8_t > fileBits;
193
-
194
- glGenTextures (1 , &texId_);
195
- glBindTexture (GL_TEXTURE_2D, texId_);
196
-
197
- if (texId_ == GL_INVALID_VALUE) {
198
- assert (false );
199
- return ;
200
- }
201
-
202
- AssetReadFile (assetManager, texName, fileBits);
203
-
204
- // tga/bmp files are saved as vertical mirror images ( at least more than half
205
- // ).
206
- stbi_set_flip_vertically_on_load (1 );
207
-
208
- uint8_t * imageBits =
209
- stbi_load_from_memory (fileBits.data (), fileBits.size (), &imgWidth,
210
- &imgHeight, &channelCount, 4 );
211
- glTexImage2D (GL_TEXTURE_2D, 0 , // mip level
212
- GL_RGBA, imgWidth, imgHeight,
213
- 0 , // border color
214
- GL_RGBA, GL_UNSIGNED_BYTE, imageBits);
215
-
216
- glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
217
- glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
218
- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
219
- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
220
-
221
- glActiveTexture (GL_TEXTURE0);
222
-
223
- stbi_image_free (imageBits);
224
- }
225
-
226
- Texture2d::~Texture2d () {
227
- if (texId_ != GL_INVALID_VALUE) {
228
- glDeleteTextures (1 , &texId_);
229
- texId_ = GL_INVALID_VALUE;
230
- }
231
- activated_ = false ;
232
- }
233
-
234
- /* *
235
- * Same as the Cubemap::GetActiveSamplerInfo()
98
+ Return used sampler names and units
99
+ so application could configure shader's sampler uniform(s).
100
+ Cubemap just used one sampler at unit 0 with "samplerObj" as its name.
236
101
*/
237
- bool Texture2d ::GetActiveSamplerInfo (std::vector<std::string>& names,
238
- std::vector<GLint>& units) {
102
+ void Texture ::GetActiveSamplerInfo (std::vector<std::string>& names,
103
+ std::vector<GLint>& units) {
239
104
names.clear ();
240
105
names.push_back (std::string (" samplerObj" ));
241
106
units.clear ();
242
107
units.push_back (0 );
243
-
244
- return true ;
245
- }
246
-
247
- bool Texture2d::Activate (void ) {
248
- glBindTexture (texId_, GL_TEXTURE0);
249
- glActiveTexture (GL_TEXTURE0 + 0 );
250
- activated_ = true ;
251
- return true ;
252
108
}
253
-
254
- GLuint Texture2d::GetTexType () { return GL_TEXTURE_2D; }
255
-
256
- GLuint Texture2d::GetTexId () { return texId_; }
0 commit comments