shithub: libgraphics

Download patch

ref: ac662557dc246b04ad1b9fa755a40ed0ec05d386
parent: 5f10c82aad318fc9091c9bd612e89fda1a77009f
author: rodri <rgl@antares-labs.eu>
date: Mon Jul 15 16:06:47 EDT 2024

fix stack overflow, skybox fov sensibility and scene duplication.

putting the duplicate camera in a local variable turned
out to be a bad idea. bring the dynamic version back.

the skybox rendering was affected by the camera fov, which
made it look like it was not really at infinity, so set it
always to 90°.

--- a/camera.c
+++ b/camera.c
@@ -19,9 +19,9 @@
 
 	addvattr(sp->v, "dir", VAPoint, &sp->v->p);
 	/* only rotate along with the camera */
-	p = sp->v->p; p.w = 0;
-	p = world2vcs(sp->su->camera, p); p.w = 1;
-	p = vcs2clip(sp->su->camera, p);
+	p = sp->v->p;
+	p.w = 0; p = world2vcs(sp->su->camera, p);
+	p.w = 1; p = vcs2clip(sp->su->camera, p);
 	/* force the cube to always be on the far plane */
 	p.z = -p.w;
 	return p;
@@ -208,7 +208,6 @@
 {
 	static Scene *skyboxscene;
 	static Shadertab skyboxshader = { nil, skyboxvs, skyboxfs };
-	Camera cam;
 	Model *mdl;
 	Renderjob *job;
 	uvlong t0, t1;
@@ -218,8 +217,8 @@
 	job = emalloc(sizeof *job);
 	memset(job, 0, sizeof *job);
 	job->fb = c->view->fbctl->getbb(c->view->fbctl);
-	cam = *c;
-	job->camera = &cam;
+	job->camera = emalloc(sizeof *c);
+	*job->camera = *c;
 	job->scene = dupscene(c->scene);	/* take a snapshot */	
 	job->shaders = s;
 	job->donec = chancreate(sizeof(void*), 0);
@@ -239,8 +238,9 @@
 			mdl = mkskyboxmodel();
 			skyboxscene->addent(skyboxscene, newentity("skybox", mdl));
 		}
-		skyboxscene->skybox = c->scene->skybox;
 		job->camera->cullmode = CullNone;
+		job->camera->fov = 90*DEG;
+		reloadcamera(job->camera);
 		job->scene = dupscene(skyboxscene);
 		job->shaders = &skyboxshader;
 		sendp(c->rctl->c, job);
@@ -254,5 +254,6 @@
 	updatetimes(c, job);
 
 	chanfree(job->donec);
+	free(job->camera);
 	free(job);
 }
--- a/graphics.h
+++ b/graphics.h
@@ -360,6 +360,7 @@
 Color bilitexsampler(Texture*, Point2);
 Color sampletexture(Texture*, Point2, Color(*)(Texture*, Point2));
 Cubemap *readcubemap(char*[6]);
+Cubemap *dupcubemap(Cubemap*);
 void freecubemap(Cubemap*);
 Color samplecubemap(Cubemap*, Point3, Color(*)(Texture*, Point2));
 
--- a/scene.c
+++ b/scene.c
@@ -322,8 +322,7 @@
 		return nil;
 
 	nm = newmodel();
-	if(m->tex != nil)
-		nm->tex = duptexture(m->tex);
+	nm->tex = duptexture(m->tex);
 	if(m->nmaterials > 0){
 		nm->nmaterials = m->nmaterials;
 		nm->materials = emalloc(nm->nmaterials*sizeof(*nm->materials));
@@ -456,6 +455,7 @@
 	if(s->nents > 0)
 		for(e = s->ents.next; e != &s->ents; e = e->next)
 			ns->addent(ns, dupentity(e));
+	ns->skybox = dupcubemap(s->skybox);
 	return ns;
 }
 
--- a/texture.c
+++ b/texture.c
@@ -125,14 +125,9 @@
 Texture *
 duptexture(Texture *t)
 {
-	Texture *nt;
-
 	if(t == nil)
 		return nil;
-
-	nt = alloctexture(t->type, nil);
-	nt->image = dupmemimage(t->image);
-	return nt;
+	return alloctexture(t->type, dupmemimage(t->image));
 }
 
 void
@@ -170,6 +165,24 @@
 		close(fd);
 	}
 	return cm;
+}
+
+Cubemap *
+dupcubemap(Cubemap *cm)
+{
+	Cubemap *ncm;
+	int i;
+
+	if(cm == nil)
+		return nil;
+
+	ncm = emalloc(sizeof *ncm);
+	memset(ncm, 0, sizeof *ncm);
+	if(cm->name != nil)
+		ncm->name = strdup(cm->name);
+	for(i = 0; i < 6; i++)
+		ncm->faces[i] = duptexture(cm->faces[i]);
+	return ncm;
 }
 
 void