comparison engine/core/video/opengl/glimage.cpp @ 430:ad1f09d954f9

Added the ability to render a vertex on the screen. The vertex is represented by a small square. fixes[t:455]
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 23 Feb 2010 19:25:59 +0000
parents ad7969d9460b
children 47b49b9b0c0a
comparison
equal deleted inserted replaced
429:be291458d9b5 430:ad1f09d954f9
62 m_last_col_width = 0; 62 m_last_col_width = 0;
63 m_last_row_height = 0; 63 m_last_row_height = 0;
64 m_chunk_size = RenderBackend::instance()->getChunkingSize(); 64 m_chunk_size = RenderBackend::instance()->getChunkingSize();
65 m_colorkey = RenderBackend::instance()->getColorKey(); 65 m_colorkey = RenderBackend::instance()->getColorKey();
66 } 66 }
67 67
68 void GLImage::cleanup() { 68 void GLImage::cleanup() {
69 for (unsigned int i = 0; i < m_rows*m_cols; ++i) { 69 for (unsigned int i = 0; i < m_rows*m_cols; ++i) {
70 glDeleteTextures(1, &m_textureids[i]); 70 glDeleteTextures(1, &m_textureids[i]);
71 } 71 }
72 delete[] m_textureids; 72 delete[] m_textureids;
92 float row_fill_ratio; 92 float row_fill_ratio;
93 93
94 // the amount of "zooming" for the image 94 // the amount of "zooming" for the image
95 float scale_x = static_cast<float>(rect.w) / static_cast<float>(m_surface->w); 95 float scale_x = static_cast<float>(rect.w) / static_cast<float>(m_surface->w);
96 float scale_y = static_cast<float>(rect.h) / static_cast<float>(m_surface->h); 96 float scale_y = static_cast<float>(rect.h) / static_cast<float>(m_surface->h);
97 97
98 // rectangle used for drawing 98 // rectangle used for drawing
99 Rect target; 99 Rect target;
100 // zooming causes scaling sometimes to round pixels incorrectly. Instead of 100 // zooming causes scaling sometimes to round pixels incorrectly. Instead of
101 // recalculating it all, store the values from previous round and calculate 101 // recalculating it all, store the values from previous round and calculate
102 // new x & y 102 // new x & y
103 Rect prev; 103 Rect prev;
104 104
105 /// setting transparency for the whole primitive: 105 /// setting transparency for the whole primitive:
106 glColor4ub( 255, 255, 255, alpha ); 106 glColor4ub( 255, 255, 255, alpha );
107 107
108 glEnable(GL_TEXTURE_2D); 108 glEnable(GL_TEXTURE_2D);
109 for (unsigned int i = 0; i < m_cols; ++i) { 109 for (unsigned int i = 0; i < m_cols; ++i) {
117 if (i > 0) { 117 if (i > 0) {
118 target.x = prev.x + prev.w; 118 target.x = prev.x + prev.w;
119 } else { 119 } else {
120 target.x = rect.x; 120 target.x = rect.x;
121 } 121 }
122 122
123 for (unsigned int j = 0; j < m_rows; ++j) { 123 for (unsigned int j = 0; j < m_rows; ++j) {
124 if (j == m_rows-1) { 124 if (j == m_rows-1) {
125 row_fill_ratio = m_last_row_fill_ratio; 125 row_fill_ratio = m_last_row_fill_ratio;
126 target.h = static_cast<int>(round(scale_y*m_last_row_height*m_last_row_fill_ratio)); 126 target.h = static_cast<int>(round(scale_y*m_last_row_height*m_last_row_fill_ratio));
127 } else { 127 } else {
132 target.y = prev.y + prev.h; 132 target.y = prev.y + prev.h;
133 } else { 133 } else {
134 target.y = rect.y; 134 target.y = rect.y;
135 } 135 }
136 prev = target; 136 prev = target;
137 137
138 glBindTexture(GL_TEXTURE_2D, m_textureids[j*m_cols + i]); 138 glBindTexture(GL_TEXTURE_2D, m_textureids[j*m_cols + i]);
139 glBegin(GL_QUADS); 139 glBegin(GL_QUADS);
140 glTexCoord2f(0.0f, 0.0f); 140 glTexCoord2f(0.0f, 0.0f);
141 glVertex2i(target.x, target.y); 141 glVertex2i(target.x, target.y);
142 142
260 260
261 delete[] oglbuffer; 261 delete[] oglbuffer;
262 } 262 }
263 } 263 }
264 } 264 }
265 265
266 void GLImage::saveImage(const std::string& filename) { 266 void GLImage::saveImage(const std::string& filename) {
267 const unsigned int swidth = getWidth(); 267 const unsigned int swidth = getWidth();
268 const unsigned int sheight = getHeight(); 268 const unsigned int sheight = getHeight();
269 Uint32 rmask, gmask, bmask, amask; 269 Uint32 rmask, gmask, bmask, amask;
270 SDL_Surface *surface = NULL; 270 SDL_Surface *surface = NULL;
271 uint8_t *pixels; 271 uint8_t *pixels;
272 272
273 #if SDL_BYTEORDER == SDL_BIG_ENDIAN 273 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
274 rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; 274 rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff;
275 #else 275 #else
276 rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; 276 rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000;
277 #endif 277 #endif
278 278
279 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, swidth, 279 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, swidth,
280 sheight, 24, 280 sheight, 24,
281 rmask, gmask, bmask, 0); 281 rmask, gmask, bmask, 0);
282 282
283 if(surface == NULL) { 283 if(surface == NULL) {
284 return; 284 return;
285 } 285 }
286 286
287 SDL_LockSurface(surface); 287 SDL_LockSurface(surface);
288 pixels = new uint8_t[swidth * sheight * 3]; 288 pixels = new uint8_t[swidth * sheight * 3];
289 glReadPixels(0, 0, swidth, sheight, GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<GLvoid*>(pixels)); 289 glReadPixels(0, 0, swidth, sheight, GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<GLvoid*>(pixels));
290 290
291 uint8_t *imagepixels = reinterpret_cast<uint8_t*>(surface->pixels); 291 uint8_t *imagepixels = reinterpret_cast<uint8_t*>(surface->pixels);
292 // Copy the "reversed_image" memory to the "image" memory 292 // Copy the "reversed_image" memory to the "image" memory
293 for (int y = (sheight - 1); y >= 0; --y) { 293 for (int y = (sheight - 1); y >= 0; --y) {
294 uint8_t *rowbegin = pixels + y * swidth * 3; 294 uint8_t *rowbegin = pixels + y * swidth * 3;
295 uint8_t *rowend = rowbegin + swidth * 3; 295 uint8_t *rowend = rowbegin + swidth * 3;
297 std::copy(rowbegin, rowend, imagepixels); 297 std::copy(rowbegin, rowend, imagepixels);
298 298
299 // Advance a row in the output surface. 299 // Advance a row in the output surface.
300 imagepixels += surface->pitch; 300 imagepixels += surface->pitch;
301 } 301 }
302 302
303 SDL_UnlockSurface(surface); 303 SDL_UnlockSurface(surface);
304 saveAsPng(filename, *surface); 304 saveAsPng(filename, *surface);
305 SDL_FreeSurface(surface); 305 SDL_FreeSurface(surface);
306 delete [] pixels; 306 delete [] pixels;
307 307
312 glScissor(cliparea.x, getHeight() - cliparea.y - cliparea.h, cliparea.w, cliparea.h); 312 glScissor(cliparea.x, getHeight() - cliparea.y - cliparea.h, cliparea.w, cliparea.h);
313 if (clear) { 313 if (clear) {
314 glClear(GL_COLOR_BUFFER_BIT); 314 glClear(GL_COLOR_BUFFER_BIT);
315 } 315 }
316 } 316 }
317 317
318 bool GLImage::putPixel(int x, int y, int r, int g, int b) { 318 bool GLImage::putPixel(int x, int y, int r, int g, int b) {
319 cleanup(); 319 cleanup();
320 return m_sdlimage->putPixel(x, y, r, g, b); 320 return m_sdlimage->putPixel(x, y, r, g, b);
321 } 321 }
322 322
323 void GLImage::drawLine(const Point& p1, const Point& p2, int r, int g, int b) { 323 void GLImage::drawLine(const Point& p1, const Point& p2, int r, int g, int b) {
324 cleanup(); 324 cleanup();
325 m_sdlimage->drawLine(p1, p2, r, g, b); 325 m_sdlimage->drawLine(p1, p2, r, g, b);
326 } 326 }
327 327
328 void GLImage::drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4, int r, int g, int b) { 328 void GLImage::drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4, int r, int g, int b) {
329 cleanup(); 329 cleanup();
330 m_sdlimage->drawQuad(p1, p2, p3, p4, r, g, b); 330 m_sdlimage->drawQuad(p1, p2, p3, p4, r, g, b);
331 } 331 }
332
333 void GLImage::drawVertex(const Point& p, const uint8_t size, int r, int g, int b) {
334 cleanup();
335 m_sdlimage->drawVertex(p, size, r, g, b);
336 }
332 } 337 }