goat3dgfx

view src/texgen.cc @ 18:6f82b9b6d6c3

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