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 scene *create_scene(void)
29 if(!(scn = malloc(sizeof *scn))) {
30 errormsg("failed to allocate scene\n");
33 scn->objects = darr_alloc(0, sizeof *scn->objects);
38 void free_scene(struct scene *scn)
44 for(i=0; i<darr_size(scn->objects); i++) {
45 free_object(scn->objects[i]);
47 darr_free(scn->objects);
51 int scn_add_object(struct scene *scn, struct object *obj)
53 darr_push(scn->objects, &obj);
57 int scn_rm_object(struct scene *scn, int idx)
59 int numobj = darr_size(scn->objects);
61 if(idx < 0 || idx >= numobj) {
65 free_object(scn->objects[idx]);
67 if(idx < numobj - 1) {
68 scn->objects[idx] = scn->objects[numobj - 1];
70 darr_pop(scn->objects);
74 int scn_num_objects(const struct scene *scn)
76 return darr_size(scn->objects);
79 int scn_object_index(const struct scene *scn, const struct object *obj)
81 int i, num = darr_size(scn->objects);
82 for(i=0; i<num; i++) {
83 if(scn->objects[i] == obj) {
90 int scn_intersect(const struct scene *scn, const cgm_ray *ray, struct rayhit *hit)
99 numobj = darr_size(scn->objects);
100 for(i=0; i<numobj; i++) {
101 if(ray_object(ray, scn->objects[i], &chit) && chit.ivlist[0].a.t < hit0.t) {
102 hit0 = chit.ivlist[0].a;
113 struct object *create_object(int type)
120 if(!(obj = calloc(1, sizeof *obj))) {
121 errormsg("failed to allocate object\n");
126 cgm_vcons(&obj->pos, 0, 0, 0);
127 cgm_qcons(&obj->rot, 0, 0, 0, 1);
128 cgm_vcons(&obj->scale, 1, 1, 1);
129 cgm_vcons(&obj->pivot, 0, 0, 0);
130 cgm_midentity(obj->xform);
131 cgm_midentity(obj->inv_xform);
135 sph = (struct sphere*)obj;
137 sprintf(buf, "sphere%03d", objid);
141 sprintf(buf, "object%03d", objid);
145 set_object_name(obj, buf);
150 void free_object(struct object *obj)
158 int set_object_name(struct object *obj, const char *name)
160 char *str = strdup(name);
168 void calc_object_matrix(struct object *obj)
172 float *mat = obj->xform;
174 cgm_mtranslation(mat, obj->pivot.x, obj->pivot.y, obj->pivot.z);
175 cgm_mrotation_quat(rmat, &obj->rot);
179 mat[4 + i] = rmat[4 + i];
180 mat[8 + i] = rmat[8 + i];
183 mat[0] *= obj->scale.x; mat[4] *= obj->scale.y; mat[8] *= obj->scale.z; mat[12] += obj->pos.x;
184 mat[1] *= obj->scale.x; mat[5] *= obj->scale.y; mat[9] *= obj->scale.z; mat[13] += obj->pos.y;
185 mat[2] *= obj->scale.x; mat[6] *= obj->scale.y; mat[10] *= obj->scale.z; mat[14] += obj->pos.z;
187 cgm_mpretranslate(mat, -obj->pivot.x, -obj->pivot.y, -obj->pivot.z);
188 /* that's basically: pivot * rotation * translation * scaling * -pivot */
190 cgm_mcopy(obj->inv_xform, mat);
191 cgm_minverse(obj->inv_xform);