comparison engine/core/video/opengl/glimage.cpp @ 627:79257869a6e9

* GLImage now uses the optimal next power of 2 size for both width and height.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 04 Oct 2010 18:15:03 +0000
parents 92290efadab7
children f3457443c95f
comparison
equal deleted inserted replaced
626:01994b7e505a 627:79257869a6e9
44 resetGlimage(); 44 resetGlimage();
45 } 45 }
46 46
47 GLImage::GLImage(const uint8_t* data, unsigned int width, unsigned int height): 47 GLImage::GLImage(const uint8_t* data, unsigned int width, unsigned int height):
48 Image(data, width, height) { 48 Image(data, width, height) {
49 assert(m_surface);
50 m_sdlimage = new SDLImage(m_surface);
49 51
50 m_textureids = NULL; 52 m_textureids = NULL;
51 53
52 resetGlimage(); 54 resetGlimage();
53 } 55 }
61 } 63 }
62 64
63 void GLImage::resetGlimage() { 65 void GLImage::resetGlimage() {
64 cleanup(); 66 cleanup();
65 67
66 m_chunk_size = 0; 68 m_chunk_size_w = 0;
69 m_chunk_size_h = 0;
70
67 m_colorkey = RenderBackend::instance()->getColorKey(); 71 m_colorkey = RenderBackend::instance()->getColorKey();
68 } 72 }
69 73
70 void GLImage::cleanup() { 74 void GLImage::cleanup() {
71 if (m_textureids) { 75 if (m_textureids) {
127 131
128 void GLImage::generateGLTexture() { 132 void GLImage::generateGLTexture() {
129 const unsigned int width = m_surface->w; 133 const unsigned int width = m_surface->w;
130 const unsigned int height = m_surface->h; 134 const unsigned int height = m_surface->h;
131 135
132 uint32_t size;
133
134 if (width > height){
135 size = width;
136 }
137 else {
138 size = height;
139 }
140
141 //calculate the nearest larger power of 2 136 //calculate the nearest larger power of 2
142 m_chunk_size = nextPow2(size); 137 m_chunk_size_w = nextPow2(width);
138 m_chunk_size_h = nextPow2(height);
143 139
144 // used to calculate the fill ratio for given chunk 140 // used to calculate the fill ratio for given chunk
145 m_col_tex_coord = static_cast<float>(m_surface->w%m_chunk_size) / static_cast<float>(m_chunk_size); 141 m_col_tex_coord = static_cast<float>(m_surface->w%m_chunk_size_w) / static_cast<float>(m_chunk_size_w);
146 m_row_tex_coord = static_cast<float>(m_surface->h%m_chunk_size) / static_cast<float>(m_chunk_size); 142 m_row_tex_coord = static_cast<float>(m_surface->h%m_chunk_size_h) / static_cast<float>(m_chunk_size_h);
147 143
148 if (m_col_tex_coord == 0.0f){ 144 if (m_col_tex_coord == 0.0f){
149 m_col_tex_coord = 1.0f; 145 m_col_tex_coord = 1.0f;
150 } 146 }
151 147
161 157
162 m_textureids = new GLuint[1]; 158 m_textureids = new GLuint[1];
163 memset(m_textureids, 0x00, 1*sizeof(GLuint)); 159 memset(m_textureids, 0x00, 1*sizeof(GLuint));
164 160
165 161
166 uint32_t* oglbuffer = new uint32_t[m_chunk_size * m_chunk_size]; 162 uint32_t* oglbuffer = new uint32_t[m_chunk_size_w * m_chunk_size_h];
167 memset(oglbuffer, 0x00, m_chunk_size*m_chunk_size*sizeof(uint32_t)); 163 memset(oglbuffer, 0x00, m_chunk_size_w*m_chunk_size_h*sizeof(uint32_t));
168 164
169 for (unsigned int y = 0; y < height; ++y) { 165 for (unsigned int y = 0; y < height; ++y) {
170 for (unsigned int x = 0; x < width; ++x) { 166 for (unsigned int x = 0; x < width; ++x) {
171 unsigned int pos = (y * pitch) + (x * 4); 167 unsigned int pos = (y * pitch) + (x * 4);
172 168
180 if (r == m_colorkey.r && g == m_colorkey.g && b == m_colorkey.b) { 176 if (r == m_colorkey.r && g == m_colorkey.g && b == m_colorkey.b) {
181 a = 0; 177 a = 0;
182 } 178 }
183 } 179 }
184 180
185 oglbuffer[(y*m_chunk_size) + x] = r | (g << 8) | (b << 16) | (a<<24); 181 oglbuffer[(y*m_chunk_size_w) + x] = r | (g << 8) | (b << 16) | (a<<24);
186 } 182 }
187 } 183 }
188 184
189 // get texture id from opengl 185 // get texture id from opengl
190 glGenTextures(1, &m_textureids[0]); 186 glGenTextures(1, &m_textureids[0]);
192 glBindTexture(GL_TEXTURE_2D, m_textureids[0]); 188 glBindTexture(GL_TEXTURE_2D, m_textureids[0]);
193 // set filters for texture 189 // set filters for texture
194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
196 // transfer data from sdl buffer 192 // transfer data from sdl buffer
197 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_chunk_size, m_chunk_size, 0, GL_RGBA, GL_UNSIGNED_BYTE, static_cast<GLvoid*>(oglbuffer)); 193 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_chunk_size_w, m_chunk_size_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, static_cast<GLvoid*>(oglbuffer));
198 194
199 delete[] oglbuffer; 195 delete[] oglbuffer;
200 } 196 }
201 197
202 void GLImage::saveImage(const std::string& filename) { 198 void GLImage::saveImage(const std::string& filename) {