tavli

changeset 23:3e6430028d54

slot highlghting
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 08 Jul 2015 02:31:36 +0300
parents c2a2069a49ec
children 0aadb519b5ee
files src/board.cc src/image.cc src/image.h
diffstat 3 files changed, 121 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/src/board.cc	Tue Jul 07 21:56:37 2015 +0300
     1.2 +++ b/src/board.cc	Wed Jul 08 02:31:36 2015 +0300
     1.3 @@ -268,7 +268,7 @@
     1.4  
     1.5  		glBegin(GL_TRIANGLES);
     1.6  		//glColor3fv(pal[idx % (sizeof pal / sizeof *pal)]);
     1.7 -		glColor4f(1, 1, 1, 0.4);
     1.8 +		glColor4f(1, 1, 1, 0.5);
     1.9  		glTexCoord2f(0, 0);
    1.10  		glVertex3f(slotbb[idx].tri0.v[0].x, slotbb[idx].tri0.v[0].y, slotbb[idx].tri0.v[0].z);
    1.11  		glTexCoord2f(1, 0);
    1.12 @@ -572,6 +572,9 @@
    1.13  	}
    1.14  	img_highlight.texture();
    1.15  
    1.16 +	float kern[] = {1,   5,   11,  18,  22,  18,  11,  5,   1};
    1.17 +	convolve_horiz_image(&img_highlight, kern, sizeof kern / sizeof *kern);
    1.18 +	convolve_vert_image(&img_highlight, kern, sizeof kern / sizeof *kern);
    1.19  
    1.20  	// ---- generic wood texture ----
    1.21  	img_wood.create(256, 256);
     2.1 --- a/src/image.cc	Tue Jul 07 21:56:37 2015 +0300
     2.2 +++ b/src/image.cc	Wed Jul 08 02:31:36 2015 +0300
     2.3 @@ -1,4 +1,5 @@
     2.4  #include <string.h>
     2.5 +#include <alloca.h>
     2.6  #include "opengl.h"
     2.7  #include "image.h"
     2.8  #include "imago2.h"
     2.9 @@ -233,6 +234,119 @@
    2.10  	return true;
    2.11  }
    2.12  
    2.13 +void convolve_horiz_image(Image *dest, float *kern, int ksz, float scale)
    2.14 +{
    2.15 +	if((ksz & 1) == 0) {
    2.16 +		fprintf(stderr, "%s: kernel size (%d) must be odd, skipping last value\n", __FUNCTION__, ksz);
    2.17 +		--ksz;
    2.18 +	}
    2.19 +	if(scale == 0.0) {
    2.20 +		// calculate scale factor
    2.21 +		float sum = 0.0;
    2.22 +		for(int i=0; i<ksz; i++) {
    2.23 +			sum += kern[i];
    2.24 +		}
    2.25 +		scale = 1.0 / sum;
    2.26 +	}
    2.27 +	int krad = ksz / 2;
    2.28 +	float *buf = (float*)alloca(dest->width * 4 * sizeof *buf);
    2.29 +	unsigned char *sptr = dest->pixels;
    2.30 +
    2.31 +	for(int i=0; i<dest->height; i++) {
    2.32 +		float *bptr = buf;
    2.33 +		for(int j=0; j<dest->width * 4; j++) {
    2.34 +			*bptr++ = (float)(sptr[j] / 255.0);
    2.35 +		}
    2.36 +
    2.37 +		for(int j=0; j<dest->width; j++) {
    2.38 +			float col[] = {0, 0, 0, 0};
    2.39 +
    2.40 +			for(int k=0; k<ksz; k++) {
    2.41 +				int idx = j + k - krad;
    2.42 +				if(idx < 0) idx = 0;
    2.43 +				if(idx >= dest->width) idx = dest->width - 1;
    2.44 +
    2.45 +				col[0] += buf[idx * 4] * kern[k];
    2.46 +				col[1] += buf[idx * 4 + 1] * kern[k];
    2.47 +				col[2] += buf[idx * 4 + 2] * kern[k];
    2.48 +				col[3] += buf[idx * 4 + 3] * kern[k];
    2.49 +			}
    2.50 +
    2.51 +			int ri = (int)(col[0] * scale * 255.0);
    2.52 +			int gi = (int)(col[1] * scale * 255.0);
    2.53 +			int bi = (int)(col[2] * scale * 255.0);
    2.54 +			int ai = (int)(col[3] * scale * 255.0);
    2.55 +
    2.56 +			sptr[0] = ri < 0 ? 0 : (ri > 255 ? 255 : ri);
    2.57 +			sptr[1] = gi < 0 ? 0 : (gi > 255 ? 255 : gi);
    2.58 +			sptr[2] = bi < 0 ? 0 : (bi > 255 ? 255 : bi);
    2.59 +			sptr[3] = ai < 0 ? 0 : (ai > 255 ? 255 : ai);
    2.60 +			sptr += 4;
    2.61 +		}
    2.62 +	}
    2.63 +
    2.64 +	dest->invalidate_texture();
    2.65 +}
    2.66 +
    2.67 +void convolve_vert_image(Image *dest, float *kern, int ksz, float scale)
    2.68 +{
    2.69 +	if((ksz & 1) == 0) {
    2.70 +		fprintf(stderr, "%s: kernel size (%d) must be odd, skipping last value\n", __FUNCTION__, ksz);
    2.71 +		--ksz;
    2.72 +	}
    2.73 +	if(scale == 0.0) {
    2.74 +		// calculate scale factor
    2.75 +		float sum = 0.0;
    2.76 +		for(int i=0; i<ksz; i++) {
    2.77 +			sum += kern[i];
    2.78 +		}
    2.79 +		scale = 1.0 / sum;
    2.80 +	}
    2.81 +	int krad = ksz / 2;
    2.82 +	float *buf = (float*)alloca(dest->height * 4 * sizeof *buf);
    2.83 +	unsigned char *sptr = dest->pixels;
    2.84 +
    2.85 +	for(int i=0; i<dest->width; i++) {
    2.86 +		float *bptr = buf;
    2.87 +		sptr = dest->pixels + i * 4;
    2.88 +
    2.89 +		for(int j=0; j<dest->height; j++) {
    2.90 +			for(int k=0; k<4; k++) {
    2.91 +				*bptr++ = (float)(sptr[k] / 255.0);
    2.92 +			}
    2.93 +			sptr += dest->width * 4;
    2.94 +		}
    2.95 +
    2.96 +		sptr = dest->pixels + i * 4;
    2.97 +
    2.98 +		for(int j=0; j<dest->height; j++) {
    2.99 +			float col[] = {0, 0, 0, 0};
   2.100 +
   2.101 +			for(int k=0; k<ksz; k++) {
   2.102 +				int idx = j + k - krad;
   2.103 +				if(idx < 0) idx = 0;
   2.104 +				if(idx >= dest->height) idx = dest->height - 1;
   2.105 +
   2.106 +				col[0] += buf[idx * 4] * kern[k];
   2.107 +				col[1] += buf[idx * 4 + 1] * kern[k];
   2.108 +				col[2] += buf[idx * 4 + 2] * kern[k];
   2.109 +				col[3] += buf[idx * 4 + 3] * kern[k];
   2.110 +			}
   2.111 +
   2.112 +			int ri = (int)(col[0] * scale * 255.0);
   2.113 +			int gi = (int)(col[1] * scale * 255.0);
   2.114 +			int bi = (int)(col[2] * scale * 255.0);
   2.115 +			int ai = (int)(col[3] * scale * 255.0);
   2.116 +
   2.117 +			sptr[0] = ri < 0 ? 0 : (ri > 255 ? 255 : ri);
   2.118 +			sptr[1] = gi < 0 ? 0 : (gi > 255 ? 255 : gi);
   2.119 +			sptr[2] = bi < 0 ? 0 : (bi > 255 ? 255 : bi);
   2.120 +			sptr[3] = ai < 0 ? 0 : (ai > 255 ? 255 : ai);
   2.121 +			sptr += dest->width * 4;
   2.122 +		}
   2.123 +	}
   2.124 +}
   2.125 +
   2.126  static unsigned int next_pow2(unsigned int x)
   2.127  {
   2.128  	x--;
     3.1 --- a/src/image.h	Tue Jul 07 21:56:37 2015 +0300
     3.2 +++ b/src/image.h	Wed Jul 08 02:31:36 2015 +0300
     3.3 @@ -42,4 +42,7 @@
     3.4  
     3.5  bool combine_image(Image *dest, const Image *aimg, const Image *bimg, ImgCombine op = IMG_OP_LERP, float t = 0.5);
     3.6  
     3.7 +void convolve_horiz_image(Image *dest, float *kern, int ksz, float scale = 0.0);
     3.8 +void convolve_vert_image(Image *dest, float *kern, int ksz, float scale = 0.0);
     3.9 +
    3.10  #endif	// IMAGE_H_