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 struct material *default_material(void);
27 struct scene *create_scene(void)
31 if(!(scn = malloc(sizeof *scn))) {
32 errormsg("failed to allocate scene\n");
35 scn->objects = darr_alloc(0, sizeof *scn->objects);
36 scn->lights = darr_alloc(0, sizeof *scn->lights);
37 scn->mtl = darr_alloc(0, sizeof *scn->mtl);
41 void free_scene(struct scene *scn)
47 for(i=0; i<darr_size(scn->objects); i++) {
48 free_object(scn->objects[i]);
50 darr_free(scn->objects);
52 for(i=0; i<darr_size(scn->lights); i++) {
53 free_light(scn->lights[i]);
55 darr_free(scn->lights);
57 for(i=0; i<darr_size(scn->mtl); i++) {
58 mtl_destroy(scn->mtl[i]);
66 int scn_add_object(struct scene *scn, struct object *obj)
68 darr_push(scn->objects, &obj);
72 int scn_rm_object(struct scene *scn, int idx)
74 int numobj = darr_size(scn->objects);
76 if(idx < 0 || idx >= numobj) {
80 free_object(scn->objects[idx]);
82 if(idx < numobj - 1) {
83 scn->objects[idx] = scn->objects[numobj - 1];
85 darr_pop(scn->objects);
89 int scn_num_objects(const struct scene *scn)
91 return darr_size(scn->objects);
94 int scn_object_index(const struct scene *scn, const struct object *obj)
96 int i, num = darr_size(scn->objects);
97 for(i=0; i<num; i++) {
98 if(scn->objects[i] == obj) {
107 int scn_add_material(struct scene *scn, struct material *mtl)
109 darr_push(scn->mtl, &mtl);
113 int scn_rm_material(struct scene *scn, struct material *mtl)
117 if((idx = scn_material_index(scn, mtl)) == -1) {
121 num_mtl = darr_size(scn->mtl);
123 if(idx < num_mtl - 1) {
124 scn->mtl[idx] = scn->mtl[num_mtl - 1];
130 int scn_num_materials(const struct scene *scn)
132 return darr_size(scn->mtl);
135 int scn_material_index(const struct scene *scn, const struct material *mtl)
139 num_mtl = darr_size(scn->mtl);
140 for(i=0; i<num_mtl; i++) {
141 if(scn->mtl[i] == mtl) {
148 struct material *scn_find_material(const struct scene *scn, const char *mname)
152 num_mtl = darr_size(scn->mtl);
153 for(i=0; i<num_mtl; i++) {
154 if(strcmp(scn->mtl[i]->name, mname) == 0) {
163 int scn_add_light(struct scene *scn, struct light *light)
165 darr_push(scn->lights, &light);
169 int scn_rm_light(struct scene *scn, struct light *light)
173 if((idx = scn_light_index(scn, light)) == -1) {
177 num_lights = darr_size(scn->lights);
179 if(idx < num_lights - 1) {
180 scn->lights[idx] = scn->lights[num_lights - 1];
182 darr_pop(scn->lights);
186 int scn_num_lights(const struct scene *scn)
188 return darr_size(scn->lights);
191 int scn_light_index(const struct scene *scn, const struct light *light)
195 num_lights = darr_size(scn->lights);
196 for(i=0; i<num_lights; i++) {
197 if(scn->lights[i] == light) {
204 struct light *scn_find_light(const struct scene *scn, const char *mname)
208 num_lights = darr_size(scn->lights);
209 for(i=0; i<num_lights; i++) {
210 if(strcmp(scn->lights[i]->name, mname) == 0) {
211 return scn->lights[i];
218 int scn_intersect(const struct scene *scn, const cgm_ray *ray, struct rayhit *hit)
221 struct rayhit hit0, tmphit;
226 numobj = darr_size(scn->objects);
227 for(i=0; i<numobj; i++) {
228 if(ray_object(ray, scn->objects[i], &tmphit) && tmphit.t < hit0.t) {
241 /* --- object functions --- */
243 struct object *create_object(int type)
252 if(!(obj = calloc(1, sizeof *sph))) {
255 sph = (struct sphere*)obj;
257 sprintf(buf, "sphere%03d", objid);
261 if(!(obj = calloc(1, sizeof *obj))) {
264 sprintf(buf, "object%03d", objid);
270 cgm_vcons(&obj->pos, 0, 0, 0);
271 cgm_qcons(&obj->rot, 0, 0, 0, 1);
272 cgm_vcons(&obj->scale, 1, 1, 1);
273 cgm_vcons(&obj->pivot, 0, 0, 0);
274 cgm_midentity(obj->xform);
275 cgm_midentity(obj->inv_xform);
276 obj->xform_valid = 1;
277 obj->mtl = default_material();
279 set_object_name(obj, buf);
284 errormsg("failed to allocate object\n");
288 void free_object(struct object *obj)
296 int set_object_name(struct object *obj, const char *name)
298 char *str = strdup(name);
306 void calc_object_matrix(struct object *obj)
310 float *mat = obj->xform;
312 cgm_mtranslation(mat, obj->pivot.x, obj->pivot.y, obj->pivot.z);
313 cgm_mrotation_quat(rmat, &obj->rot);
317 mat[4 + i] = rmat[4 + i];
318 mat[8 + i] = rmat[8 + i];
321 mat[0] *= obj->scale.x; mat[4] *= obj->scale.y; mat[8] *= obj->scale.z; mat[12] += obj->pos.x;
322 mat[1] *= obj->scale.x; mat[5] *= obj->scale.y; mat[9] *= obj->scale.z; mat[13] += obj->pos.y;
323 mat[2] *= obj->scale.x; mat[6] *= obj->scale.y; mat[10] *= obj->scale.z; mat[14] += obj->pos.z;
325 cgm_mpretranslate(mat, -obj->pivot.x, -obj->pivot.y, -obj->pivot.z);
326 /* that's basically: pivot * rotation * translation * scaling * -pivot */
328 cgm_mcopy(obj->inv_xform, mat);
329 cgm_minverse(obj->inv_xform);
331 obj->xform_valid = 1;
335 struct light *create_light(void)
341 if(!(lt = malloc(sizeof *lt))) {
344 cgm_vcons(<->pos, 0, 0, 0);
346 set_light_color(lt, 1, 1, 1);
347 set_light_energy(lt, 1);
349 sprintf(buf, "light%03d", ltidx++);
350 set_light_name(lt, buf);
355 void free_light(struct light *lt)
362 int set_light_name(struct light *lt, const char *name)
364 char *tmp = strdup(name);
371 void set_light_color(struct light *lt, float r, float g, float b)
373 cgm_vcons(<->orig_color, r, g, b);
374 lt->color = lt->orig_color;
375 cgm_vscale(<->color, lt->energy);
378 void set_light_energy(struct light *lt, float e)
381 lt->color = lt->orig_color;
382 cgm_vscale(<->color, e);
385 static struct material *default_material(void)
387 static struct material defmtl;
391 mtl_set_name(&defmtl, "default_mtl");