summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--camera.c5
-rw-r--r--graphics.h3
-rw-r--r--scene.c20
3 files changed, 25 insertions, 3 deletions
diff --git a/camera.c b/camera.c
index ccd1f57..9734af1 100644
--- a/camera.c
+++ b/camera.c
@@ -68,9 +68,8 @@ mkskyboxmodel(void)
if(i > 0)
t[j].v[k].p = qrotate(t[j].v[k].p, axes[i%3], PI/2);
- m->prims = erealloc(m->prims, (m->nprims += 2)*sizeof(*m->prims));
- m->prims[m->nprims-2] = t[0];
- m->prims[m->nprims-1] = t[1];
+ m->addprim(m, t[0]);
+ m->addprim(m, t[1]);
}
return m;
}
diff --git a/graphics.h b/graphics.h
index 448acbb..a2c42e2 100644
--- a/graphics.h
+++ b/graphics.h
@@ -164,6 +164,9 @@ struct Model
ulong nmaterials;
Texture *tex; /* texture map (TODO get rid of it, use materials) */
+
+ int (*addprim)(Model*, Primitive);
+ int (*addmaterial)(Model*, Material);
};
struct Entity
diff --git a/scene.c b/scene.c
index d4d1872..7cf6868 100644
--- a/scene.c
+++ b/scene.c
@@ -8,6 +8,22 @@
#include "graphics.h"
#include "internal.h"
+static int
+model_addprim(Model *m, Primitive p)
+{
+ m->prims = erealloc(m->prims, ++m->nprims*sizeof(*m->prims));
+ m->prims[m->nprims-1] = p;
+ return 0;
+}
+
+static int
+model_addmaterial(Model *m, Material mtl)
+{
+ m->materials = erealloc(m->materials, ++m->nmaterials*sizeof(*m->materials));
+ m->materials[m->nmaterials-1] = mtl;
+ return 0;
+}
+
Model *
newmodel(void)
{
@@ -15,6 +31,8 @@ newmodel(void)
m = emalloc(sizeof *m);
memset(m, 0, sizeof *m);
+ m->addprim = model_addprim;
+ m->addmaterial = model_addmaterial;
return m;
}
@@ -35,6 +53,7 @@ dupmodel(Model *m)
for(i = 0; i < m->nmaterials; i++){
nm->materials[i] = m->materials[i];
nm->materials[i].diffusemap = duptexture(m->materials[i].diffusemap);
+ nm->materials[i].specularmap = duptexture(m->materials[i].specularmap);
nm->materials[i].normalmap = duptexture(m->materials[i].normalmap);
nm->materials[i].name = strdup(m->materials[i].name);
if(nm->materials[i].name == nil)
@@ -62,6 +81,7 @@ delmodel(Model *m)
freetexture(m->tex);
while(m->nmaterials--){
freetexture(m->materials[m->nmaterials].diffusemap);
+ freetexture(m->materials[m->nmaterials].specularmap);
freetexture(m->materials[m->nmaterials].normalmap);
free(m->materials[m->nmaterials].name);
}