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 struct img_pixmap renderbuf;
29 static int rx, ry, rwidth, rheight;
31 static int xstep, ystep;
37 rx = ry = rwidth = rheight = roffs = 0;
43 void rend_destroy(void)
45 img_destroy(&renderbuf);
48 void rend_size(int xsz, int ysz)
50 if(xsz != renderbuf.width || ysz != renderbuf.height) {
51 img_set_pixels(&renderbuf, xsz, ysz, IMG_FMT_RGBA32, 0);
55 void rend_begin(int x, int y, int w, int h)
60 if(w == 0 || h == 0) {
62 rwidth = renderbuf.width;
63 rheight = renderbuf.height;
70 roffs = ry * renderbuf.width + rx;
75 ptr = (uint32_t*)renderbuf.pixels + roffs;
76 for(i=0; i<rheight; i++) {
77 memset(ptr, 0, rwidth * sizeof *ptr);
78 ptr += renderbuf.width;
82 static void fillrect(uint32_t *fb, int x, int y, int w, int h, uint32_t c)
86 fb += y * renderbuf.width + x;
91 fb += renderbuf.width;
95 int render(uint32_t *fb)
97 int i, j, w, h, offs, r, g, b;
102 dest = (uint32_t*)renderbuf.pixels + roffs;
105 if(xstep < 1) xstep = 1;
106 if(ystep < 1) ystep = 1;
108 for(i=0; i<rheight; i+=ystep) {
110 if(i + h > rheight) h = rheight - i;
112 for(j=0; j<rwidth; j+=xstep) {
113 primray(&ray, rx + j, ry + i);
114 ray_trace(&ray, max_ray_depth, &color);
116 r = cround64(color.x * 255.0f);
117 g = cround64(color.y * 255.0f);
118 b = cround64(color.z * 255.0f);
124 pcol = PACK_RGB32(r, g, b);
126 offs = i * renderbuf.width + j;
131 if(j + w > rwidth) w = rwidth - j;
133 fillrect(fb, j, i, w, h, pcol);
141 if((xstep | ystep) >= 1) {
147 int ray_trace(const cgm_ray *ray, int maxiter, cgm_vec3 *res)
151 if(!scn_intersect(scn, ray, &hit)) {
156 *res = shade(ray, &hit, maxiter);
160 cgm_vec3 bgcolor(const cgm_ray *ray)
162 return cgm_vvec(0, 0, 0);
165 cgm_vec3 shade(const cgm_ray *ray, const struct rayhit *hit, int maxiter)
167 return cgm_vvec(1, 0, 0);