dungeon_crawler

diff prototype/src/colgrade.cc @ 74:5981917093ff

color grading palette output done, all is left is the input
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 21 Oct 2012 15:56:47 +0300
parents a27528035e20
children b05ab29cd17d
line diff
     1.1 --- a/prototype/src/colgrade.cc	Sun Oct 21 02:11:23 2012 +0300
     1.2 +++ b/prototype/src/colgrade.cc	Sun Oct 21 15:56:47 2012 +0300
     1.3 @@ -1,10 +1,13 @@
     1.4  #include <stdio.h>
     1.5 +#include <string.h>
     1.6  #include "opengl.h"
     1.7  #include "colgrade.h"
     1.8 +#include "imago2.h"
     1.9  
    1.10  GradePalette::GradePalette()
    1.11  {
    1.12  	tex = 0;
    1.13 +	palette = 0;
    1.14  }
    1.15  
    1.16  GradePalette::~GradePalette()
    1.17 @@ -18,18 +21,14 @@
    1.18  
    1.19  	destroy();
    1.20  
    1.21 -	glGenTextures(1, &tex);
    1.22 -	glBindTexture(GL_TEXTURE_3D, tex);
    1.23 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.24 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.25 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, clamp);
    1.26 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, clamp);
    1.27 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, clamp);
    1.28 -	glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, sz, sz, sz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
    1.29 +	this->size = sz;
    1.30  
    1.31 -	unsigned char *scanline = new unsigned char[sz * 3];
    1.32 +	int nbytes = sz * sz * sz * 3;
    1.33 +	printf("allocating %d bytes for a color grading palette\n", nbytes);
    1.34 +	palette = new unsigned char[nbytes];
    1.35  
    1.36 -	size = sz;
    1.37 +	unsigned char *scanline = palette;
    1.38 +
    1.39  	for(int i=0; i<sz; i++) {	// for each slice...
    1.40  		int b = 255 * i / (sz - 1);
    1.41  		for(int j=0; j<sz; j++) {	// for each scanline...
    1.42 @@ -42,12 +41,19 @@
    1.43  				scanline[k * 3 + 1] = g;
    1.44  				scanline[k * 3 + 2] = b;
    1.45  			}
    1.46 -			glTexSubImage3D(GL_TEXTURE_3D, 0, 0, j, i, sz, 1, 1, GL_RGB,
    1.47 -					GL_UNSIGNED_BYTE, scanline);
    1.48 +			scanline += sz * 3;
    1.49  		}
    1.50  	}
    1.51  
    1.52 -	delete [] scanline;
    1.53 +	glGenTextures(1, &tex);
    1.54 +	glBindTexture(GL_TEXTURE_3D, tex);
    1.55 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.56 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.57 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, clamp);
    1.58 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, clamp);
    1.59 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, clamp);
    1.60 +	glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, sz, sz, sz, 0, GL_RGB, GL_UNSIGNED_BYTE, palette);
    1.61 +
    1.62  	return true;
    1.63  }
    1.64  
    1.65 @@ -55,12 +61,58 @@
    1.66  {
    1.67  	if(tex) {
    1.68  		glDeleteTextures(1, &tex);
    1.69 +		tex = 0;
    1.70  	}
    1.71 +	delete [] palette;
    1.72 +	palette = 0;
    1.73  }
    1.74  
    1.75  bool GradePalette::save_shot(const char *fname) const
    1.76  {
    1.77 -	return false;	// TODO
    1.78 +	if(!tex) {
    1.79 +		return false;
    1.80 +	}
    1.81 +
    1.82 +	int slice_bytes = size * size * 3;
    1.83 +	int nslices = size;
    1.84 +
    1.85 +	int vp[4];
    1.86 +	glGetIntegerv(GL_VIEWPORT, vp);
    1.87 +
    1.88 +	int xsz = vp[2];
    1.89 +	int ysz = vp[3];
    1.90 +
    1.91 +	// allocate more scanlines to cram the palette slices in there
    1.92 +	unsigned char *img = new unsigned char[xsz * (ysz + nslices) * 3];
    1.93 +
    1.94 +	glReadPixels(0, 0, vp[2], vp[3], GL_RGB, GL_UNSIGNED_BYTE, img);
    1.95 +
    1.96 +	// invert the image
    1.97 +	unsigned char *top = img;
    1.98 +	unsigned char *bot = img + xsz * (ysz - 1) * 3;
    1.99 +
   1.100 +	while(top < bot) {
   1.101 +		for(int j=0; j<xsz * 3; j++) {
   1.102 +			unsigned char tmp = top[j];
   1.103 +			top[j] = bot[j];
   1.104 +			bot[j] = tmp;
   1.105 +		}
   1.106 +		top += xsz * 3;
   1.107 +		bot -= xsz * 3;
   1.108 +	}
   1.109 +
   1.110 +	unsigned char *pal = img + xsz * ysz * 3;
   1.111 +	memset(pal, 0, nslices * xsz * 3);
   1.112 +
   1.113 +	for(int i=0; i<nslices; i++) {
   1.114 +		memcpy(pal, palette + i * slice_bytes, slice_bytes);
   1.115 +		pal += xsz * 3;
   1.116 +	}
   1.117 +
   1.118 +	int res = img_save_pixels(fname, img, xsz, ysz + nslices, IMG_FMT_RGB24);
   1.119 +	delete [] img;
   1.120 +
   1.121 +	return res == 0;
   1.122  }
   1.123  
   1.124  bool GradePalette::load_shot(const char *fname)