2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023 John Tsiombikas <nuclear@mutantstargoat.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
25 static cgm_vec3 lookup_pixmap(const struct texture *btex, const struct rayhit *hit);
26 static cgm_vec3 lookup_chess(const struct texture *btex, const struct rayhit *hit);
27 static cgm_vec3 lookup_fbm2d(const struct texture *btex, const struct rayhit *hit);
28 static cgm_vec3 lookup_fbm3d(const struct texture *btex, const struct rayhit *hit);
29 static cgm_vec3 lookup_marble2d(const struct texture *btex, const struct rayhit *hit);
30 static cgm_vec3 lookup_marble3d(const struct texture *btex, const struct rayhit *hit);
32 static const cgm_vec3 (*lookup[])(const struct texture*, const struct rayhit*) = {
41 static const char *defname_fmt[] = {
42 "pixmap%03d", "chess%03d",
43 "fbm%03d", "sfbm%03d",
44 "marble%03d", "smarble%03d"
47 struct texture *create_texture(int type)
50 struct tex_pixmap *tpix;
51 struct tex_chess *tchess;
58 if(!(tpix = malloc(sizeof *tpix))) {
61 tex = (struct texture*)tpix;
66 if(!(tchess = malloc(sizeof *tchess))) {
69 tex = (struct texture*)tchess;
70 cgm_vcons(tchess->color, 0, 0, 0);
71 cgm_vcons(tchess->color + 1, 1, 1, 1);
76 if(!(tfbm = malloc(sizeof *tfbm))) {
79 tex = (struct texture*)tfbm;
81 cgm_vcons(tfbm->color, 0, 0, 0);
82 cgm_vcons(tfbm->color + 1, 1, 1, 1);
86 errormsg("tried to allocate invalid texture type (%d)\n", type);
92 cgm_vcons(&tex->offs, 0, 0, 0);
93 cgm_vcons(&tex->scale, 1, 1, 1);
94 tex->lookup = lookup[type];
96 sprintf(namebuf, defname_fmt[type], texidx++);
97 tex_set_name(tex, namebuf);
101 errormsg("failed to allocate texture\n");
105 void free_texture(struct texture *tex)
112 void tex_set_name(struct texture *tex, const char *name)
114 char *tmp = strdup(name);
120 #define XFORM_UV(tex, u, v) \
122 u *= (tex)->scale.x + (tex)->offs.x; \
123 v *= (tex)->scale.y + (tex)->offs.y; \
126 #define XFORM_UVW(tex, u, v, w) \
128 u *= (tex)->scale.x + (tex)->offs.x; \
129 v *= (tex)->scale.y + (tex)->offs.y; \
130 w *= (tex)->scale.z * (tex)->offs.z; \
133 static cgm_vec3 lookup_pixmap(const struct texture *btex, const struct rayhit *hit)
135 struct tex_pixmap *tex = (struct tex_pixmap*)btex;
136 int px, py, twidth, theight;
142 twidth = tex->img->width;
143 theight = tex->img->height;
147 px = cround64((float)twidth * u) % twidth;
148 py = cround64((float)theight * v) % theight;
149 if(px < 0) px += twidth;
150 if(py < 0) py += theight;
152 texel = ((uint32_t*)tex->img->pixels)[py * twidth + px];
154 g = (texel >> 8) & 0xff;
155 b = (texel >> 16) & 0xff;
157 return cgm_vvec(r / 255.0f, g / 255.0f, b / 255.0f);
160 static cgm_vec3 lookup_chess(const struct texture *btex, const struct rayhit *hit)
162 struct tex_chess *tex = (struct tex_chess*)btex;
169 cx = cround64(u) & 1;
170 cy = cround64(v) & 1;
172 return tex->color[chess];
175 static cgm_vec3 lookup_fbm2d(const struct texture *btex, const struct rayhit *hit)
177 return cgm_vvec(1, 0, 0); /* TODO */
180 static cgm_vec3 lookup_fbm3d(const struct texture *btex, const struct rayhit *hit)
182 return cgm_vvec(1, 0, 0); /* TODO */
185 static cgm_vec3 lookup_marble2d(const struct texture *btex, const struct rayhit *hit)
187 return cgm_vvec(1, 0, 0); /* TODO */
190 static cgm_vec3 lookup_marble3d(const struct texture *btex, const struct rayhit *hit)
192 return cgm_vvec(1, 0, 0); /* TODO */