forked from G-A-I-N/gainlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageBitmap.cpp
199 lines (149 loc) · 4.45 KB
/
ImageBitmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2014
// ville.kankainen@gmail.com
// ville.kankainen@kangain.com
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
#include "ImageBitmap.h"
namespace Gain {
static ImageBitmapCache* gBitmapCache = 0;
ImageBitmap::ImageBitmap(float x,float y, float width, float height, const char* aImageFile) :
Bitmap(x,y,width,height)
{
readFile(aImageFile);
}
ImageBitmap::ImageBitmap(int x,int y, int width, int height, const char* aImageFile) :
Bitmap(x,y,width,height)
{
readFile(aImageFile);
}
ImageBitmap::ImageBitmap(const char* aImageFile) :
Bitmap()
{
readFile(aImageFile);
}
ImageBitmap::~ImageBitmap() {
}
void ImageBitmap::readFile(const char* aFileName)
{
if(!gBitmapCache)
{
gBitmapCache = new ImageBitmapCache();
}
#ifdef IOS
const char* new_path = get_asset_filepath(aFileName);
ImageBitmapCacheData data = gBitmapCache->loadBitmap(new_path);
#else
ImageBitmapCacheData data = gBitmapCache->loadBitmap(aFileName);
#endif
pBitmapWidth = data.width;
pBitmapHeight = data.height;
pBitmapSlotSize = data.bitsPerPixel;
pBitmap = data.bitmap;
pPngLiteral = data.literal;
}
bool ImageBitmap::initVariables() {
if(!super::initVariables()) {
return false;
}
ImageBitmapCacheData data;
data.width = pBitmapWidth;
data.height = pBitmapHeight;
data.bitsPerPixel = pBitmapSlotSize;
data.bitmap = pBitmap;
data.literal = pPngLiteral;
texture_id = gBitmapCache->openglId(&data);
GLfloat square_texcoords[] = {
// front
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
};
GL_EXT_FUNC glGenBuffers(1, &vbo_square_texcoords);
checkGlError("glGenBuffers");
GL_EXT_FUNC glBindBuffer(GL_ARRAY_BUFFER, vbo_square_texcoords);
checkGlError("glBindBuffer");
GL_EXT_FUNC glBufferData(GL_ARRAY_BUFFER, sizeof(square_texcoords), square_texcoords, GL_STATIC_DRAW);
checkGlError("glBufferData");
return true;
}
void ImageBitmap::updateG(float time, float timeDelta)
{
super::updateG( time, timeDelta);
}
void ImageBitmap::enableAttributes() const
{
super::enableAttributes();
}
void ImageBitmap::disableAttributes() const
{
GL_EXT_FUNC glDisableVertexAttribArray(attribute_texcoord);
super::disableAttributes();
}
ImageBitmapCacheData ImageBitmapCache::loadBitmap(std::string filename)
{
std::map<std::string,ImageBitmapCacheData>::iterator it = pBitmapMap.find(filename);
if(it != pBitmapMap.end())
{
return it->second;
}
ImageBitmapCacheData data;
int width, height, comp;
unsigned char *image_data;
FILE *file = fopen(filename.c_str(), "rb");
if (!file) {
return data;
}
image_data = stbi_load_from_file(file, &width, &height, &comp, 0);
fclose(file);
data.width = width;
data.height = height;
data.bitsPerPixel = comp*8;
data.bitmap = image_data;
data.literal = filename;
pBitmapMap[filename] = data;
return data;
}
GLuint ImageBitmapCache::openglId(ImageBitmapCacheData* data)
{
std::map<std::string,unsigned int>::iterator it = pOpenglCache.find(data->literal);
if(it != pOpenglCache.end())
{
return it->second;
}
GLuint texture_id;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_EDGE);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, /*GL10.GL_REPLACE*/ GL_MODULATE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, // target
0, // level, 0 = base, no minimap,
GL_RGBA, // internalformat
data->width, // width
data->height, // height
0, // border, always 0 in OpenGL ES
GL_RGBA, // format
GL_UNSIGNED_BYTE, // type
data->bitmap);
pOpenglCache.insert(std::pair<std::string, unsigned int>(data->literal,texture_id));
return texture_id;
}
} /* namespace Gain */