~brandontschaefer/nux/base-window-x-input-window-wm-updates

« back to all changes in this revision

Viewing changes to NuxGraphics/IOpenGLBaseTexture.cpp

  • Committer: Tarmac
  • Author(s): Eleni Maria Stea
  • Date: 2013-02-14 07:55:37 UTC
  • mfrom: (758.1.1 nux)
  • Revision ID: tarmac-20130214075537-0e2zmi0dtfigwaor
moved the debug function that saves textures to disk to the IOpenGLBaseTexture class.

Approved by Mirco Müller, Eleni Maria Stea.

Show diffs side-by-side

added added

removed removed

Lines of Context:
264
264
    return 0;
265
265
  }
266
266
 
 
267
  void IOpenGLBaseTexture::Save(const char* filename)
 
268
  {
 
269
    GLuint tex_id = GetOpenGLID();
 
270
    glBindTexture(GL_TEXTURE_2D, tex_id);
 
271
 
 
272
    int width, height;
 
273
 
 
274
#ifndef NUX_OPENGLES_20
 
275
      glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
 
276
      glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
 
277
#else
 
278
      width = GetWidth();
 
279
      height = GetHeight();
 
280
#endif
 
281
 
 
282
      if(!width || !height)
 
283
        return;
 
284
 
 
285
      uint32_t* pixels = new uint32_t[width * height];
 
286
      uint32_t* tmp = pixels;
 
287
 
 
288
#ifndef NUX_OPENGLES_20
 
289
      glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
 
290
#else
 
291
      GLuint fbo;
 
292
      glGenFramebuffers(1, &fbo);
 
293
      glBindFramebuffer(GL_FRAMEBUFFER, fbo);
 
294
      glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_id, 0);
 
295
      glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
 
296
      glBindFramebuffer(GL_FRAMEBUFFER, 0);
 
297
      glDeleteFramebuffers(1, &fbo);
 
298
#endif
 
299
 
 
300
      FILE* fp;
 
301
      if(!(fp = fopen(filename, "wb"))) {
 
302
        fprintf(stderr, "Cannot open file: %s\n", filename);
 
303
        return;
 
304
      }
 
305
 
 
306
      fprintf(fp, "P6\n%d %d\n255\n", width, height);
 
307
 
 
308
      int sz = width * height;
 
309
      for(int i=0; i<sz; i++) {
 
310
        uint32_t pix = *tmp++;
 
311
        int r = (pix >> 16) & 0xff;
 
312
        int g = (pix >> 8) & 0xff;
 
313
        int b = pix & 0xff;
 
314
 
 
315
        fputc(r, fp);
 
316
        fputc(g, fp);
 
317
        fputc(b, fp);
 
318
      }
 
319
 
 
320
      fclose(fp);
 
321
      delete [] pixels;
 
322
  }
267
323
 
268
324
  int GetTextureSize(IOpenGLBaseTexture *pTexture)
269
325
  {