dungeon_crawler

changeset 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 67d330038629
children b05ab29cd17d
files prototype/src/colgrade.cc prototype/src/colgrade.h prototype/src/main.cc prototype/src/renderer.cc prototype/src/renderer.h
diffstat 5 files changed, 93 insertions(+), 14 deletions(-) [+]
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)
     2.1 --- a/prototype/src/colgrade.h	Sun Oct 21 02:11:23 2012 +0300
     2.2 +++ b/prototype/src/colgrade.h	Sun Oct 21 15:56:47 2012 +0300
     2.3 @@ -3,6 +3,7 @@
     2.4  
     2.5  class GradePalette {
     2.6  private:
     2.7 +	unsigned char *palette;
     2.8  	unsigned int tex;
     2.9  	int size;
    2.10  
     3.1 --- a/prototype/src/main.cc	Sun Oct 21 02:11:23 2012 +0300
     3.2 +++ b/prototype/src/main.cc	Sun Oct 21 15:56:47 2012 +0300
     3.3 @@ -43,6 +43,7 @@
     3.4  static float stereo_eye_sep = stereo_focus_dist / 30.0;
     3.5  
     3.6  static bool show_con;
     3.7 +static bool save_grade_shot;
     3.8  
     3.9  static AudioSource *move_sound;
    3.10  static OggVorbisStream *music;
    3.11 @@ -245,6 +246,14 @@
    3.12  	if(show_con) {
    3.13  		draw_cmdcon();
    3.14  	}
    3.15 +
    3.16 +	if(save_grade_shot) {
    3.17 +		GradePalette *pal = rend->get_grade_palette();
    3.18 +		if(!pal->save_shot("gradeshot.ppm")) {
    3.19 +			fprintf(stderr, "failed to save color grading shot\n");
    3.20 +		}
    3.21 +		save_grade_shot = false;
    3.22 +	}
    3.23  }
    3.24  
    3.25  void view_matrix(int eye)
    3.26 @@ -396,6 +405,10 @@
    3.27  		}
    3.28  		break;
    3.29  
    3.30 +	case 'g':
    3.31 +		save_grade_shot = true;
    3.32 +		break;
    3.33 +
    3.34  	default:
    3.35  		break;
    3.36  	}
     4.1 --- a/prototype/src/renderer.cc	Sun Oct 21 02:11:23 2012 +0300
     4.2 +++ b/prototype/src/renderer.cc	Sun Oct 21 15:56:47 2012 +0300
     4.3 @@ -199,6 +199,16 @@
     4.4  	return true;
     4.5  }
     4.6  
     4.7 +GradePalette *Renderer::get_grade_palette()
     4.8 +{
     4.9 +	return &gradepal;
    4.10 +}
    4.11 +
    4.12 +const GradePalette *Renderer::get_grade_palette() const
    4.13 +{
    4.14 +	return &gradepal;
    4.15 +}
    4.16 +
    4.17  
    4.18  // ---- fallback forward renderer ----
    4.19  FwdRenderer::FwdRenderer()
     5.1 --- a/prototype/src/renderer.h	Sun Oct 21 02:11:23 2012 +0300
     5.2 +++ b/prototype/src/renderer.h	Sun Oct 21 15:56:47 2012 +0300
     5.3 @@ -32,6 +32,9 @@
     5.4  	virtual void render_pre(const Level *level) const;
     5.5  	virtual void render(const Level *level) const = 0;
     5.6  	virtual void render_post(const Level *level) const;
     5.7 +
     5.8 +	virtual GradePalette *get_grade_palette();
     5.9 +	virtual const GradePalette *get_grade_palette() const;
    5.10  };
    5.11  
    5.12