goat3dgfx

diff src/texgen.cc @ 0:1873dfd13f2d

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Nov 2013 05:27:09 +0200
parents
children 7d6b667821cf
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/texgen.cc	Thu Nov 14 05:27:09 2013 +0200
     1.3 @@ -0,0 +1,137 @@
     1.4 +#include "texgen.h"
     1.5 +
     1.6 +static void intcolor(const Vector4 &color, int *icol);
     1.7 +
     1.8 +Image *texgen_solid(int xsz, int ysz, const Vector4 &color)
     1.9 +{
    1.10 +	Image *img = new Image;
    1.11 +	if(!img->create(xsz, ysz, Image::FMT_RGBA)) {
    1.12 +		delete img;
    1.13 +		return 0;
    1.14 +	}
    1.15 +
    1.16 +	int col[4];
    1.17 +	intcolor(color, col);
    1.18 +
    1.19 +	unsigned char *pix = (unsigned char*)img->get_pixels();
    1.20 +	for(int i=0; i<xsz * ysz; i++) {
    1.21 +		*pix++ = col[0];
    1.22 +		*pix++ = col[1];
    1.23 +		*pix++ = col[2];
    1.24 +		*pix++ = col[3];
    1.25 +	}
    1.26 +	return img;
    1.27 +}
    1.28 +
    1.29 +Image *texgen_chess(int xsz, int ysz, int usub, int vsub, const Vector4 &col1, const Vector4 &col2)
    1.30 +{
    1.31 +	Image *img = new Image;
    1.32 +	if(!img->create(xsz, ysz, Image::FMT_RGBA)) {
    1.33 +		delete img;
    1.34 +		return 0;
    1.35 +	}
    1.36 +
    1.37 +	int c1[4], c2[4];
    1.38 +	intcolor(col1, c1);
    1.39 +	intcolor(col2, c2);
    1.40 +
    1.41 +	int udiv = xsz / usub;
    1.42 +	int vdiv = ysz / vsub;
    1.43 +
    1.44 +	unsigned char *pix = (unsigned char*)img->get_pixels();
    1.45 +	for(int i=0; i<ysz; i++) {
    1.46 +		for(int j=0; j<xsz; j++) {
    1.47 +			if(((i / vdiv) & 1) == ((j / udiv) & 1)) {
    1.48 +				*pix++ = c1[0];
    1.49 +				*pix++ = c1[1];
    1.50 +				*pix++ = c1[2];
    1.51 +				*pix++ = c1[3];
    1.52 +			} else {
    1.53 +				*pix++ = c2[0];
    1.54 +				*pix++ = c2[1];
    1.55 +				*pix++ = c2[2];
    1.56 +				*pix++ = c2[3];
    1.57 +			}
    1.58 +		}
    1.59 +	}
    1.60 +	return img;
    1.61 +}
    1.62 +
    1.63 +
    1.64 +Image *texgen(int xsz, int ysz, float usize, float vsize, Vector4 (*eval)(float, float, void*), void *cls)
    1.65 +{
    1.66 +	Image *img = new Image;
    1.67 +	if(!img->create(xsz, ysz, Image::FMT_RGBA)) {
    1.68 +		delete img;
    1.69 +		return 0;
    1.70 +	}
    1.71 +
    1.72 +	unsigned char *pix = (unsigned char*)img->get_pixels();
    1.73 +	for(int i=0; i<ysz; i++) {
    1.74 +		for(int j=0; j<xsz; j++) {
    1.75 +			float x = usize * (float)j / (float)xsz;
    1.76 +			float y = vsize * (float)i / (float)ysz;
    1.77 +
    1.78 +			Vector4 color = eval(x, y, cls);
    1.79 +
    1.80 +			int icol[4];
    1.81 +			intcolor(color, icol);
    1.82 +
    1.83 +			*pix++ = icol[0];
    1.84 +			*pix++ = icol[1];
    1.85 +			*pix++ = icol[2];
    1.86 +			*pix++ = icol[3];
    1.87 +		}
    1.88 +	}
    1.89 +	return img;
    1.90 +}
    1.91 +
    1.92 +
    1.93 +struct NoiseArg {
    1.94 +	int octaves;
    1.95 +	Vector4 col1, col2;
    1.96 +};
    1.97 +
    1.98 +static Vector4 fbm_eval(float x, float y, void *cls)
    1.99 +{
   1.100 +	NoiseArg *arg = (NoiseArg*)cls;
   1.101 +
   1.102 +	float noise = fbm2(x, y, arg->octaves) * 0.5 + 0.5;
   1.103 +	return lerp(arg->col1, arg->col2, noise);
   1.104 +}
   1.105 +
   1.106 +static Vector4 fbm_abs_eval(float x, float y, void *cls)
   1.107 +{
   1.108 +	NoiseArg *arg = (NoiseArg*)cls;
   1.109 +
   1.110 +	float noise = turbulence2(x, y, arg->octaves) * 0.5 + 0.5;
   1.111 +	return lerp(arg->col1, arg->col2, noise);
   1.112 +}
   1.113 +
   1.114 +
   1.115 +Image *texgen_fbm(int xsz, int ysz, float usize, float vsize, int octaves, const Vector4 &col1, const Vector4 &col2)
   1.116 +{
   1.117 +	NoiseArg arg = {octaves, col1, col2};
   1.118 +	if(arg.octaves < 1) {
   1.119 +		arg.octaves = 1;
   1.120 +	}
   1.121 +
   1.122 +	return texgen(xsz, ysz, usize, vsize, fbm_eval, &arg);
   1.123 +}
   1.124 +
   1.125 +Image *texgen_fbm_abs(int xsz, int ysz, float usize, float vsize, int octaves, const Vector4 &col1, const Vector4 &col2)
   1.126 +{
   1.127 +	NoiseArg arg = {octaves, col1, col2};
   1.128 +	if(arg.octaves < 1) {
   1.129 +		arg.octaves = 1;
   1.130 +	}
   1.131 +
   1.132 +	return texgen(xsz, ysz, usize, vsize, fbm_abs_eval, &arg);
   1.133 +}
   1.134 +
   1.135 +static inline void intcolor(const Vector4 &color, int *icol)
   1.136 +{
   1.137 +	for(int i=0; i<4; i++) {
   1.138 +		icol[i] = std::max(std::min((int)(color[i] * 255.0), 255), 0);
   1.139 +	}
   1.140 +}