aboutsummaryrefslogtreecommitdiff
path: root/obj.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-01-29 22:27:33 +0000
committerrodri <rgl@antares-labs.eu>2023-01-29 22:27:33 +0000
commite70ecd9d84578dad435a39b40592f9a7cd1908d3 (patch)
tree0abadddf4f131a1aa30fed5a3a434805868fa1e6 /obj.c
downloadqball-e70ecd9d84578dad435a39b40592f9a7cd1908d3.tar.gz
qball-e70ecd9d84578dad435a39b40592f9a7cd1908d3.tar.bz2
qball-e70ecd9d84578dad435a39b40592f9a7cd1908d3.zip
initial public release.
the original qball doesn't seem to work at all. burnzez's qb adaptation has proven to be very close, with some minor modifications, but still doesn't get it.
Diffstat (limited to 'obj.c')
-rw-r--r--obj.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/obj.c b/obj.c
new file mode 100644
index 0000000..9f2a9d9
--- /dev/null
+++ b/obj.c
@@ -0,0 +1,65 @@
+#include <u.h>
+#include <libc.h>
+#include <ctype.h>
+#include <bio.h>
+#include <draw.h>
+#include "dat.h"
+#include "fns.h"
+
+Vector3 *verts;
+Triangle3 *tris;
+int nvert, ntri;
+int lineno;
+
+int
+objread(char *f, Triangle3 **mesh)
+{
+ Biobuf *bin;
+ char *s;
+ int idx;
+ double *coord;
+ Vector3 *vert;
+
+ bin = Bopen(f, OREAD);
+ if(bin == nil)
+ sysfatal("Bopen: %r");
+ while((s = Brdline(bin, '\n')) != nil){
+ lineno++;
+ if(*s == 'v'){
+ verts = erealloc(verts, ++nvert*sizeof(Vector3));
+ coord = (double *)(verts+nvert-1);
+ while(*s != '\n' && (Vector3 *)coord-verts < nvert){
+ while(isspace(*++s))
+ ;
+ *coord++ = strtod(s, &s);
+ }
+ if(coord-(double *)(verts+nvert-1) < 3){
+ werrstr("%s:%d insufficient coordinates", f, lineno);
+ goto error;
+ }
+ }
+ if(*s == 'f'){
+ tris = erealloc(tris, ++ntri*sizeof(Triangle3));
+ vert = (Vector3 *)(tris+ntri-1);
+ while(*s != '\n' && (Triangle3 *)vert-tris < ntri){
+ while(isspace(*++s))
+ ;
+ idx = strtol(s, &s, 0);
+ if(idx > nvert){
+ werrstr("%s:%d insufficient vertices", f, lineno);
+ goto error;
+ }
+ *vert++ = verts[idx-1];
+ }
+ }
+ }
+ *mesh = tris;
+ free(verts);
+ Bterm(bin);
+ return ntri;
+error:
+ free(verts);
+ free(tris);
+ Bterm(bin);
+ return -1;
+}