diff options
author | rodri <rgl@antares-labs.eu> | 2024-01-28 19:21:13 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2024-01-28 19:21:13 +0000 |
commit | d9fc92f3d11360bbce2a5c853bfe21d5f76aaf1c (patch) | |
tree | 06264d5e14f58b0c1127d201cfd4033ea4841714 /main.c | |
parent | ef9b5a48bd91757a3ebc0a4f0006c9f98737c205 (diff) | |
download | tinyrend-d9fc92f3d11360bbce2a5c853bfe21d5f76aaf1c.tar.gz tinyrend-d9fc92f3d11360bbce2a5c853bfe21d5f76aaf1c.tar.bz2 tinyrend-d9fc92f3d11360bbce2a5c853bfe21d5f76aaf1c.zip |
do the quad triangulation on a separate procedure.
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 121 |
1 files changed, 67 insertions, 54 deletions
@@ -390,11 +390,72 @@ shaderunit(void *arg) threadexits(nil); } +/* + * it only processes quads for now. + */ +static int +triangulate(OBJElem **newe, OBJElem *e) +{ + OBJIndexArray *newidxtab; + OBJIndexArray *idxtab; + + idxtab = &e->indextab[OBJVGeometric]; + newe[0] = emalloc(sizeof *newe[0]); + newe[0]->type = OBJEFace; + newidxtab = &newe[0]->indextab[OBJVGeometric]; + newidxtab->nindex = 3; + newidxtab->indices = emalloc(newidxtab->nindex*sizeof(*newidxtab->indices)); + newidxtab->indices[0] = idxtab->indices[0]; + newidxtab->indices[1] = idxtab->indices[1]; + newidxtab->indices[2] = idxtab->indices[2]; + idxtab = &e->indextab[OBJVTexture]; + newidxtab = &newe[0]->indextab[OBJVTexture]; + newidxtab->nindex = 3; + newidxtab->indices = emalloc(newidxtab->nindex*sizeof(*newidxtab->indices)); + newidxtab->indices[0] = idxtab->indices[0]; + newidxtab->indices[1] = idxtab->indices[1]; + newidxtab->indices[2] = idxtab->indices[2]; + idxtab = &e->indextab[OBJVNormal]; + newidxtab = &newe[0]->indextab[OBJVNormal]; + newidxtab->nindex = 3; + newidxtab->indices = emalloc(newidxtab->nindex*sizeof(*newidxtab->indices)); + newidxtab->indices[0] = idxtab->indices[0]; + newidxtab->indices[1] = idxtab->indices[1]; + newidxtab->indices[2] = idxtab->indices[2]; + + idxtab = &e->indextab[OBJVGeometric]; + newe[1] = emalloc(sizeof *newe[1]); + newe[1]->type = OBJEFace; + newidxtab = &newe[1]->indextab[OBJVGeometric]; + newidxtab->nindex = 3; + newidxtab->indices = emalloc(newidxtab->nindex*sizeof(*newidxtab->indices)); + newidxtab->indices[0] = idxtab->indices[0]; + newidxtab->indices[1] = idxtab->indices[2]; + newidxtab->indices[2] = idxtab->indices[3]; + idxtab = &e->indextab[OBJVTexture]; + newidxtab = &newe[1]->indextab[OBJVTexture]; + newidxtab->nindex = 3; + newidxtab->indices = emalloc(newidxtab->nindex*sizeof(*newidxtab->indices)); + newidxtab->indices[0] = idxtab->indices[0]; + newidxtab->indices[1] = idxtab->indices[2]; + newidxtab->indices[2] = idxtab->indices[3]; + idxtab = &e->indextab[OBJVNormal]; + newidxtab = &newe[1]->indextab[OBJVNormal]; + newidxtab->nindex = 3; + newidxtab->indices = emalloc(newidxtab->nindex*sizeof(*newidxtab->indices)); + newidxtab->indices[0] = idxtab->indices[0]; + newidxtab->indices[1] = idxtab->indices[2]; + newidxtab->indices[2] = idxtab->indices[3]; + + return 2; +} + void shade(Framebuf *fb, Shader *s) { static int nparts, nworkers; static OBJElem **elems = nil; + OBJElem *trielems[2]; int i, nelems; uvlong time; OBJObject *o; @@ -412,60 +473,12 @@ shade(Framebuf *fb, Shader *s) /* discard non-triangles */ if(e->type != OBJEFace || (idxtab->nindex != 3 && idxtab->nindex != 4)) continue; - if(idxtab->nindex == 4){ /* triangulate */ - OBJElem *auxe; - OBJIndexArray *auxia; - - auxe = emalloc(sizeof *auxe); - auxe->type = OBJEFace; - auxia = &auxe->indextab[OBJVGeometric]; - auxia->nindex = 3; - auxia->indices = emalloc(auxia->nindex*sizeof(*auxia->indices)); - auxia->indices[0] = idxtab->indices[0]; - auxia->indices[1] = idxtab->indices[1]; - auxia->indices[2] = idxtab->indices[2]; - idxtab = &e->indextab[OBJVTexture]; - auxia = &auxe->indextab[OBJVTexture]; - auxia->nindex = 3; - auxia->indices = emalloc(auxia->nindex*sizeof(*auxia->indices)); - auxia->indices[0] = idxtab->indices[0]; - auxia->indices[1] = idxtab->indices[1]; - auxia->indices[2] = idxtab->indices[2]; - idxtab = &e->indextab[OBJVNormal]; - auxia = &auxe->indextab[OBJVNormal]; - auxia->nindex = 3; - auxia->indices = emalloc(auxia->nindex*sizeof(*auxia->indices)); - auxia->indices[0] = idxtab->indices[0]; - auxia->indices[1] = idxtab->indices[1]; - auxia->indices[2] = idxtab->indices[2]; - elems = erealloc(elems, ++nelems*sizeof(*elems)); - elems[nelems-1] = auxe; - - idxtab = &e->indextab[OBJVGeometric]; - auxe = emalloc(sizeof *auxe); - auxe->type = OBJEFace; - auxia = &auxe->indextab[OBJVGeometric]; - auxia->nindex = 3; - auxia->indices = emalloc(auxia->nindex*sizeof(*auxia->indices)); - auxia->indices[0] = idxtab->indices[0]; - auxia->indices[1] = idxtab->indices[2]; - auxia->indices[2] = idxtab->indices[3]; - idxtab = &e->indextab[OBJVTexture]; - auxia = &auxe->indextab[OBJVTexture]; - auxia->nindex = 3; - auxia->indices = emalloc(auxia->nindex*sizeof(*auxia->indices)); - auxia->indices[0] = idxtab->indices[0]; - auxia->indices[1] = idxtab->indices[2]; - auxia->indices[2] = idxtab->indices[3]; - idxtab = &e->indextab[OBJVNormal]; - auxia = &auxe->indextab[OBJVNormal]; - auxia->nindex = 3; - auxia->indices = emalloc(auxia->nindex*sizeof(*auxia->indices)); - auxia->indices[0] = idxtab->indices[0]; - auxia->indices[1] = idxtab->indices[2]; - auxia->indices[2] = idxtab->indices[3]; - elems = erealloc(elems, ++nelems*sizeof(*elems)); - elems[nelems-1] = auxe; + if(idxtab->nindex == 4){ + triangulate(trielems, e); + nelems += 2; + elems = erealloc(elems, nelems*sizeof(*elems)); + elems[nelems-2] = trielems[0]; + elems[nelems-1] = trielems[1]; }else{ elems = erealloc(elems, ++nelems*sizeof(*elems)); elems[nelems-1] = e; |