goat3dgfx

annotate src/texgen.cc @ 27:be9754def417

added missing post-build copy event in the release configuration
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 27 Feb 2014 02:19:16 +0200
parents 1873dfd13f2d
children
rev   line source
nuclear@0 1 #include "texgen.h"
nuclear@0 2
nuclear@15 3 using namespace goatgfx;
nuclear@15 4
nuclear@0 5 static void intcolor(const Vector4 &color, int *icol);
nuclear@0 6
nuclear@15 7 Image *goatgfx::texgen_solid(int xsz, int ysz, const Vector4 &color)
nuclear@0 8 {
nuclear@0 9 Image *img = new Image;
nuclear@0 10 if(!img->create(xsz, ysz, Image::FMT_RGBA)) {
nuclear@0 11 delete img;
nuclear@0 12 return 0;
nuclear@0 13 }
nuclear@0 14
nuclear@0 15 int col[4];
nuclear@0 16 intcolor(color, col);
nuclear@0 17
nuclear@0 18 unsigned char *pix = (unsigned char*)img->get_pixels();
nuclear@0 19 for(int i=0; i<xsz * ysz; i++) {
nuclear@0 20 *pix++ = col[0];
nuclear@0 21 *pix++ = col[1];
nuclear@0 22 *pix++ = col[2];
nuclear@0 23 *pix++ = col[3];
nuclear@0 24 }
nuclear@0 25 return img;
nuclear@0 26 }
nuclear@0 27
nuclear@15 28 Image *goatgfx::texgen_chess(int xsz, int ysz, int usub, int vsub, const Vector4 &col1, const Vector4 &col2)
nuclear@0 29 {
nuclear@0 30 Image *img = new Image;
nuclear@0 31 if(!img->create(xsz, ysz, Image::FMT_RGBA)) {
nuclear@0 32 delete img;
nuclear@0 33 return 0;
nuclear@0 34 }
nuclear@0 35
nuclear@0 36 int c1[4], c2[4];
nuclear@0 37 intcolor(col1, c1);
nuclear@0 38 intcolor(col2, c2);
nuclear@0 39
nuclear@0 40 int udiv = xsz / usub;
nuclear@0 41 int vdiv = ysz / vsub;
nuclear@0 42
nuclear@0 43 unsigned char *pix = (unsigned char*)img->get_pixels();
nuclear@0 44 for(int i=0; i<ysz; i++) {
nuclear@0 45 for(int j=0; j<xsz; j++) {
nuclear@0 46 if(((i / vdiv) & 1) == ((j / udiv) & 1)) {
nuclear@0 47 *pix++ = c1[0];
nuclear@0 48 *pix++ = c1[1];
nuclear@0 49 *pix++ = c1[2];
nuclear@0 50 *pix++ = c1[3];
nuclear@0 51 } else {
nuclear@0 52 *pix++ = c2[0];
nuclear@0 53 *pix++ = c2[1];
nuclear@0 54 *pix++ = c2[2];
nuclear@0 55 *pix++ = c2[3];
nuclear@0 56 }
nuclear@0 57 }
nuclear@0 58 }
nuclear@0 59 return img;
nuclear@0 60 }
nuclear@0 61
nuclear@0 62
nuclear@15 63 Image *goatgfx::texgen(int xsz, int ysz, float usize, float vsize, Vector4 (*eval)(float, float, void*), void *cls)
nuclear@0 64 {
nuclear@0 65 Image *img = new Image;
nuclear@0 66 if(!img->create(xsz, ysz, Image::FMT_RGBA)) {
nuclear@0 67 delete img;
nuclear@0 68 return 0;
nuclear@0 69 }
nuclear@0 70
nuclear@0 71 unsigned char *pix = (unsigned char*)img->get_pixels();
nuclear@0 72 for(int i=0; i<ysz; i++) {
nuclear@0 73 for(int j=0; j<xsz; j++) {
nuclear@0 74 float x = usize * (float)j / (float)xsz;
nuclear@0 75 float y = vsize * (float)i / (float)ysz;
nuclear@0 76
nuclear@0 77 Vector4 color = eval(x, y, cls);
nuclear@0 78
nuclear@0 79 int icol[4];
nuclear@0 80 intcolor(color, icol);
nuclear@0 81
nuclear@0 82 *pix++ = icol[0];
nuclear@0 83 *pix++ = icol[1];
nuclear@0 84 *pix++ = icol[2];
nuclear@0 85 *pix++ = icol[3];
nuclear@0 86 }
nuclear@0 87 }
nuclear@0 88 return img;
nuclear@0 89 }
nuclear@0 90
nuclear@0 91
nuclear@0 92 struct NoiseArg {
nuclear@0 93 int octaves;
nuclear@0 94 Vector4 col1, col2;
nuclear@0 95 };
nuclear@0 96
nuclear@0 97 static Vector4 fbm_eval(float x, float y, void *cls)
nuclear@0 98 {
nuclear@0 99 NoiseArg *arg = (NoiseArg*)cls;
nuclear@0 100
nuclear@0 101 float noise = fbm2(x, y, arg->octaves) * 0.5 + 0.5;
nuclear@0 102 return lerp(arg->col1, arg->col2, noise);
nuclear@0 103 }
nuclear@0 104
nuclear@0 105 static Vector4 fbm_abs_eval(float x, float y, void *cls)
nuclear@0 106 {
nuclear@0 107 NoiseArg *arg = (NoiseArg*)cls;
nuclear@0 108
nuclear@0 109 float noise = turbulence2(x, y, arg->octaves) * 0.5 + 0.5;
nuclear@0 110 return lerp(arg->col1, arg->col2, noise);
nuclear@0 111 }
nuclear@0 112
nuclear@0 113
nuclear@15 114 Image *goatgfx::texgen_fbm(int xsz, int ysz, float usize, float vsize, int octaves, const Vector4 &col1, const Vector4 &col2)
nuclear@0 115 {
nuclear@0 116 NoiseArg arg = {octaves, col1, col2};
nuclear@0 117 if(arg.octaves < 1) {
nuclear@0 118 arg.octaves = 1;
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 return texgen(xsz, ysz, usize, vsize, fbm_eval, &arg);
nuclear@0 122 }
nuclear@0 123
nuclear@15 124 Image *goatgfx::texgen_fbm_abs(int xsz, int ysz, float usize, float vsize, int octaves, const Vector4 &col1, const Vector4 &col2)
nuclear@0 125 {
nuclear@0 126 NoiseArg arg = {octaves, col1, col2};
nuclear@0 127 if(arg.octaves < 1) {
nuclear@0 128 arg.octaves = 1;
nuclear@0 129 }
nuclear@0 130
nuclear@0 131 return texgen(xsz, ysz, usize, vsize, fbm_abs_eval, &arg);
nuclear@0 132 }
nuclear@0 133
nuclear@0 134 static inline void intcolor(const Vector4 &color, int *icol)
nuclear@0 135 {
nuclear@0 136 for(int i=0; i<4; i++) {
nuclear@0 137 icol[i] = std::max(std::min((int)(color[i] * 255.0), 255), 0);
nuclear@0 138 }
nuclear@0 139 }