goat3dgfx

view src/texgen.cc @ 5:18879c956eb1

- skycube example - added fatal_log - changed the dataset to keep the whole path while searching for data files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 17 Nov 2013 03:22:40 +0200
parents
children 7d6b667821cf
line source
1 #include "texgen.h"
3 static void intcolor(const Vector4 &color, int *icol);
5 Image *texgen_solid(int xsz, int ysz, const Vector4 &color)
6 {
7 Image *img = new Image;
8 if(!img->create(xsz, ysz, Image::FMT_RGBA)) {
9 delete img;
10 return 0;
11 }
13 int col[4];
14 intcolor(color, col);
16 unsigned char *pix = (unsigned char*)img->get_pixels();
17 for(int i=0; i<xsz * ysz; i++) {
18 *pix++ = col[0];
19 *pix++ = col[1];
20 *pix++ = col[2];
21 *pix++ = col[3];
22 }
23 return img;
24 }
26 Image *texgen_chess(int xsz, int ysz, int usub, int vsub, const Vector4 &col1, const Vector4 &col2)
27 {
28 Image *img = new Image;
29 if(!img->create(xsz, ysz, Image::FMT_RGBA)) {
30 delete img;
31 return 0;
32 }
34 int c1[4], c2[4];
35 intcolor(col1, c1);
36 intcolor(col2, c2);
38 int udiv = xsz / usub;
39 int vdiv = ysz / vsub;
41 unsigned char *pix = (unsigned char*)img->get_pixels();
42 for(int i=0; i<ysz; i++) {
43 for(int j=0; j<xsz; j++) {
44 if(((i / vdiv) & 1) == ((j / udiv) & 1)) {
45 *pix++ = c1[0];
46 *pix++ = c1[1];
47 *pix++ = c1[2];
48 *pix++ = c1[3];
49 } else {
50 *pix++ = c2[0];
51 *pix++ = c2[1];
52 *pix++ = c2[2];
53 *pix++ = c2[3];
54 }
55 }
56 }
57 return img;
58 }
61 Image *texgen(int xsz, int ysz, float usize, float vsize, Vector4 (*eval)(float, float, void*), void *cls)
62 {
63 Image *img = new Image;
64 if(!img->create(xsz, ysz, Image::FMT_RGBA)) {
65 delete img;
66 return 0;
67 }
69 unsigned char *pix = (unsigned char*)img->get_pixels();
70 for(int i=0; i<ysz; i++) {
71 for(int j=0; j<xsz; j++) {
72 float x = usize * (float)j / (float)xsz;
73 float y = vsize * (float)i / (float)ysz;
75 Vector4 color = eval(x, y, cls);
77 int icol[4];
78 intcolor(color, icol);
80 *pix++ = icol[0];
81 *pix++ = icol[1];
82 *pix++ = icol[2];
83 *pix++ = icol[3];
84 }
85 }
86 return img;
87 }
90 struct NoiseArg {
91 int octaves;
92 Vector4 col1, col2;
93 };
95 static Vector4 fbm_eval(float x, float y, void *cls)
96 {
97 NoiseArg *arg = (NoiseArg*)cls;
99 float noise = fbm2(x, y, arg->octaves) * 0.5 + 0.5;
100 return lerp(arg->col1, arg->col2, noise);
101 }
103 static Vector4 fbm_abs_eval(float x, float y, void *cls)
104 {
105 NoiseArg *arg = (NoiseArg*)cls;
107 float noise = turbulence2(x, y, arg->octaves) * 0.5 + 0.5;
108 return lerp(arg->col1, arg->col2, noise);
109 }
112 Image *texgen_fbm(int xsz, int ysz, float usize, float vsize, int octaves, const Vector4 &col1, const Vector4 &col2)
113 {
114 NoiseArg arg = {octaves, col1, col2};
115 if(arg.octaves < 1) {
116 arg.octaves = 1;
117 }
119 return texgen(xsz, ysz, usize, vsize, fbm_eval, &arg);
120 }
122 Image *texgen_fbm_abs(int xsz, int ysz, float usize, float vsize, int octaves, const Vector4 &col1, const Vector4 &col2)
123 {
124 NoiseArg arg = {octaves, col1, col2};
125 if(arg.octaves < 1) {
126 arg.octaves = 1;
127 }
129 return texgen(xsz, ysz, usize, vsize, fbm_abs_eval, &arg);
130 }
132 static inline void intcolor(const Vector4 &color, int *icol)
133 {
134 for(int i=0; i<4; i++) {
135 icol[i] = std::max(std::min((int)(color[i] * 255.0), 255), 0);
136 }
137 }