goat3d

changeset 29:3d669155709d

- switched the unix build to use the internal vmath/anim as well - fixed a memory corruption issue which was caused by including the system-wide installed version of the anim.h header file - started the ass2goat assimp->goat3d converter
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 21:53:03 +0300
parents 9ba3e2fb8a33
children 0fe02696fb1e f43f4849c86a
files .hgignore Makefile converters/ass2goat/.clang_complete converters/ass2goat/Makefile converters/ass2goat/src/main.c libs/anim/Makefile libs/anim/anim.c libs/anim/anim.h libs/anim/config.h libs/anim/dynarr.c libs/anim/dynarr.h libs/anim/track.c libs/anim/track.h libs/vmath/Makefile libs/vmath/basis.cc libs/vmath/basis.h libs/vmath/basis_c.c libs/vmath/geom.c libs/vmath/geom.h libs/vmath/matrix.cc libs/vmath/matrix.h libs/vmath/matrix.inl libs/vmath/matrix_c.c libs/vmath/quat.cc libs/vmath/quat.h libs/vmath/quat.inl libs/vmath/quat_c.c libs/vmath/ray.cc libs/vmath/ray.h libs/vmath/ray.inl libs/vmath/ray_c.c libs/vmath/sphvec.cc libs/vmath/sphvec.h libs/vmath/vector.cc libs/vmath/vector.h libs/vmath/vector.inl libs/vmath/vmath.c libs/vmath/vmath.h libs/vmath/vmath.inl libs/vmath/vmath_config.h libs/vmath/vmath_types.h src/goat3d.cc src/xform_node.cc
diffstat 9 files changed, 249 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Sun Sep 29 08:46:19 2013 +0300
     1.2 +++ b/.hgignore	Sun Sep 29 21:53:03 2013 +0300
     1.3 @@ -14,3 +14,4 @@
     1.4  \.ctm$
     1.5  goatprim$
     1.6  goatview$
     1.7 +ass2goat$
     2.1 --- a/Makefile	Sun Sep 29 08:46:19 2013 +0300
     2.2 +++ b/Makefile	Sun Sep 29 21:53:03 2013 +0300
     2.3 @@ -10,9 +10,11 @@
     2.4  
     2.5  openctm = libs/openctm/libopenctm.a
     2.6  tinyxml2 = libs/tinyxml2/libtinyxml2.a
     2.7 +vmath = libs/vmath/libvmath.a
     2.8 +anim = libs/anim/libanim.a
     2.9  
    2.10 -extinc = -Ilibs/openctm -Ilibs/tinyxml2
    2.11 -extlibs = $(openctm) $(tinyxml2)
    2.12 +extinc = -Ilibs/openctm -Ilibs/tinyxml2 -Ilibs/anim
    2.13 +extlibs = $(openctm) $(tinyxml2) $(anim) $(vmath)
    2.14  
    2.15  name = goat3d
    2.16  so_major = 0
    2.17 @@ -35,7 +37,7 @@
    2.18  CC = clang
    2.19  CXX = clang++
    2.20  CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(pic) $(extinc)
    2.21 -LDFLAGS = $(extlibs) -lvmath -lanim
    2.22 +LDFLAGS = $(extlibs)
    2.23  
    2.24  .PHONY: all
    2.25  all: $(lib_so) $(lib_a)
    2.26 @@ -44,7 +46,7 @@
    2.27  	$(CXX) -o $@ $(shared) $(obj) $(LDFLAGS)
    2.28  
    2.29  $(lib_a): $(obj) $(extlibs)
    2.30 -	$(AR) rcs $@ $(obj) $(openctm)
    2.31 +	$(AR) rcs $@ $(obj) $(extlibs)
    2.32  
    2.33  $(openctm):
    2.34  	$(MAKE) -C libs/openctm
    2.35 @@ -52,6 +54,12 @@
    2.36  $(tinyxml2):
    2.37  	$(MAKE) -C libs/tinyxml2
    2.38  
    2.39 +$(vmath):
    2.40 +	$(MAKE) -C libs/vmath
    2.41 +
    2.42 +$(anim):
    2.43 +	$(MAKE) -C libs/anim
    2.44 +
    2.45  -include $(dep)
    2.46  
    2.47  %.d: %.cc
    2.48 @@ -64,3 +72,26 @@
    2.49  .PHONY: cleandep
    2.50  cleandep:
    2.51  	rm -f $(dep)
    2.52 +
    2.53 +.PHONY: install
    2.54 +install: $(lib_so) $(lib_a)
    2.55 +	mkdir -p $(DESTDIR)$(PREFIX)/lib $(DESTDIR)$(PREFIX)/include
    2.56 +	cp src/goat3d.h $(DESTDIR)$(PREFIX)/include/goat3d.h
    2.57 +	cp $(lib_a) $(DESTDIR)$(PREFIX)/lib/$(lib_a)
    2.58 +	cp $(lib_so) $(DESTDIR)$(PREFIX)/lib/$(lib_so)
    2.59 +	[ -n "$(devlink)" ] && \
    2.60 +		cd $(DESTDIR)$(PREFIX)/lib && \
    2.61 +		rm -f $(soname) $(devlink) && \
    2.62 +		ln -s $(lib_so) $(soname) && \
    2.63 +		ln -s $(soname) $(devlink) || \
    2.64 +		true
    2.65 +
    2.66 +.PHONY: uninstall
    2.67 +uninstall:
    2.68 +	rm -f $(DESTDIR)$(PREFIX)/include/goat3d.h
    2.69 +	rm -f $(DESTDIR)$(PREFIX)/lib/$(lib_so)
    2.70 +	rm -f $(DESTDIR)$(PREFIX)/lib/$(lib_a)
    2.71 +	[ -n "$(devlink)" ] && \
    2.72 +		rm -f $(DESTDIR)$(PREFIX)/lib/$(soname) && \
    2.73 +		rm -f $(DESTDIR)$(PREFIX)/lib/$(devlink) || \
    2.74 +		true
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/converters/ass2goat/.clang_complete	Sun Sep 29 21:53:03 2013 +0300
     3.3 @@ -0,0 +1,2 @@
     3.4 +-I../../src
     3.5 +-I/usr/local/include
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/converters/ass2goat/Makefile	Sun Sep 29 21:53:03 2013 +0300
     4.3 @@ -0,0 +1,25 @@
     4.4 +src = $(wildcard src/*.c)
     4.5 +obj = $(src:.c=.o)
     4.6 +dep = $(obj:.o=.d)
     4.7 +bin = ass2goat
     4.8 +
     4.9 +CC = clang
    4.10 +CPP = clang -E
    4.11 +CFLAGS = -pedantic -Wall -g $(goatinc)
    4.12 +LDFLAGS = -lgoat3d -lassimp
    4.13 +
    4.14 +$(bin): $(obj)
    4.15 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    4.16 +
    4.17 +-include $(dep)
    4.18 +
    4.19 +%.d: %.c
    4.20 +	@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
    4.21 +
    4.22 +.PHONY: clean
    4.23 +clean:
    4.24 +	rm -f $(obj) $(bin)
    4.25 +
    4.26 +.PHONY: cleandep
    4.27 +cleandep:
    4.28 +	rm -f $(dep)
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/converters/ass2goat/src/main.c	Sun Sep 29 21:53:03 2013 +0300
     5.3 @@ -0,0 +1,147 @@
     5.4 +#include <stdio.h>
     5.5 +#include <stdlib.h>
     5.6 +#include "goat3d.h"
     5.7 +#include "assimp/cimport.h"
     5.8 +#include "assimp/postprocess.h"
     5.9 +#include "assimp/scene.h"
    5.10 +
    5.11 +int convert(const char *infname, const char *outfname);
    5.12 +void process_material(struct goat3d_material *mtl, struct aiMaterial *aimtl);
    5.13 +void process_node(struct goat3d *goat, struct goat3d_node *parent, struct aiNode *ainode);
    5.14 +
    5.15 +int main(int argc, char **argv)
    5.16 +{
    5.17 +	int i, num_done = 0;
    5.18 +
    5.19 +	for(i=1; i<argc; i++) {
    5.20 +		if(argv[i][0] == '-') {
    5.21 +		} else {
    5.22 +			char *lastdot;
    5.23 +			char *outfname = malloc(strlen(argv[i]) + 4);
    5.24 +			strcpy(outfname, argv[i]);
    5.25 +
    5.26 +			if((lastdot = strrchr(outfname, '.'))) {
    5.27 +				*lastdot = 0;
    5.28 +			}
    5.29 +			strcat(outfname, ".xml");
    5.30 +
    5.31 +			printf("converting %s -> %s\n", argv[i], outfname);
    5.32 +			convert(argv[i], outfname);
    5.33 +			num_done++;
    5.34 +		}
    5.35 +	}
    5.36 +
    5.37 +	if(!num_done) {
    5.38 +		fprintf(stderr, "you must specify a 3D scene file to convert\n");
    5.39 +		return 1;
    5.40 +	}
    5.41 +
    5.42 +	return 0;
    5.43 +}
    5.44 +
    5.45 +#define PPFLAGS	\
    5.46 +	(aiProcess_Triangulate | \
    5.47 +	 aiProcess_GenNormals | \
    5.48 +	 aiProcess_JoinIdenticalVertices | \
    5.49 +	 aiProcess_CalcTangentSpace | \
    5.50 +	 aiProcess_LimitBoneWeights | \
    5.51 +	 aiProcess_GenUVCoords)
    5.52 +
    5.53 +int convert(const char *infname, const char *outfname)
    5.54 +{
    5.55 +	int i;
    5.56 +	const struct aiScene *aiscn;
    5.57 +	struct goat3d *goat;
    5.58 +
    5.59 +	if(!(aiscn = aiImportFile(infname, PPFLAGS))) {
    5.60 +		fprintf(stderr, "failed to import %s\n", infname);
    5.61 +		return -1;
    5.62 +	}
    5.63 +
    5.64 +	goat = goat3d_create();
    5.65 +
    5.66 +	for(i=0; i<(int)aiscn->mNumMaterials; i++) {
    5.67 +		struct aiMaterial *aimat = aiscn->mMaterials[i];
    5.68 +		struct goat3d_material *mat = goat3d_create_mtl();
    5.69 +
    5.70 +		process_material(mat, aimat);
    5.71 +		goat3d_add_mtl(goat, mat);
    5.72 +	}
    5.73 +
    5.74 +	for(i=0; i<(int)aiscn->mRootNode->mNumChildren; i++) {
    5.75 +		process_node(goat, 0, aiscn->mRootNode->mChildren[i]);
    5.76 +	}
    5.77 +
    5.78 +	goat3d_save(goat, outfname);
    5.79 +	goat3d_free(goat);
    5.80 +	aiReleaseImport(aiscn);
    5.81 +	return 0;
    5.82 +}
    5.83 +
    5.84 +void process_material(struct goat3d_material *mtl, struct aiMaterial *aimtl)
    5.85 +{
    5.86 +	struct aiString aistr;
    5.87 +	struct aiColor4D color;
    5.88 +	float val;
    5.89 +
    5.90 +	if(aiGetMaterialString(aimtl, AI_MATKEY_NAME, &aistr) == aiReturn_SUCCESS) {
    5.91 +		goat3d_set_mtl_name(mtl, aistr.data);
    5.92 +	}
    5.93 +
    5.94 +	if(aiGetMaterialColor(aimtl, AI_MATKEY_COLOR_DIFFUSE, &color) == aiReturn_SUCCESS) {
    5.95 +		goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, color.r, color.g, color.b);
    5.96 +	}
    5.97 +
    5.98 +	if(aiGetMaterialColor(aimtl, AI_MATKEY_COLOR_SPECULAR, &color) == aiReturn_SUCCESS) {
    5.99 +		float sstr = 1.0;
   5.100 +		aiGetMaterialFloatArray(aimtl, AI_MATKEY_SHININESS_STRENGTH, &sstr, 0);
   5.101 +		goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_SPECULAR, color.r * sstr, color.g * sstr, color.b * sstr);
   5.102 +	}
   5.103 +
   5.104 +	if(aiGetMaterialFloatArray(aimtl, AI_MATKEY_BUMPSCALING, &val, 0) == aiReturn_SUCCESS) {
   5.105 +		goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_BUMP, val, val, val);
   5.106 +	}
   5.107 +
   5.108 +	if(aiGetMaterialFloatArray(aimtl, AI_MATKEY_REFLECTIVITY, &val, 0) == aiReturn_SUCCESS) {
   5.109 +		goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_REFLECTION, val);
   5.110 +	}
   5.111 +
   5.112 +	if(aiGetMaterialFloatArray(aimtl, AI_MATKEY_OPACITY, &val, 0) == aiReturn_SUCCESS) {
   5.113 +		goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, 1.0 - val);
   5.114 +	}
   5.115 +
   5.116 +	if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_DIFFUSE(0), &aistr) == aiReturn_SUCCESS) {
   5.117 +		goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_DIFFUSE, aistr.data);
   5.118 +	}
   5.119 +	if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_SPECULAR(0), &aistr) == aiReturn_SUCCESS) {
   5.120 +		goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SPECULAR, aistr.data);
   5.121 +	}
   5.122 +	if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_SHININESS(0), &aistr) == aiReturn_SUCCESS) {
   5.123 +		goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SHININESS, aistr.data);
   5.124 +	}
   5.125 +	if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_NORMALS(0), &aistr) == aiReturn_SUCCESS) {
   5.126 +		goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_NORMAL, aistr.data);
   5.127 +	}
   5.128 +	if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_REFLECTION(0), &aistr) == aiReturn_SUCCESS) {
   5.129 +		goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, aistr.data);
   5.130 +	}
   5.131 +	if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_OPACITY(0), &aistr) == aiReturn_SUCCESS) {
   5.132 +		// TODO this is semantically inverted... maybe add an alpha attribute?
   5.133 +		goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, aistr.data);
   5.134 +	}
   5.135 +}
   5.136 +
   5.137 +void process_node(struct goat3d *goat, struct goat3d_node *parent, struct aiNode *ainode)
   5.138 +{
   5.139 +	int i;
   5.140 +	struct goat3d_node *node;
   5.141 +
   5.142 +	node = goat3d_create_node();
   5.143 +	goat3d_set_node_name(node, ainode->mName.data);
   5.144 +
   5.145 +	for(i=0; i<ainode->mNumChildren; i++) {
   5.146 +		process_node(goat, node, ainode->mChildren[i]);
   5.147 +	}
   5.148 +
   5.149 +	goat3d_add_node(goat, node);
   5.150 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/libs/anim/Makefile	Sun Sep 29 21:53:03 2013 +0300
     6.3 @@ -0,0 +1,17 @@
     6.4 +src = $(wildcard *.c)
     6.5 +obj = $(src:.c=.o)
     6.6 +lib = libanim.a
     6.7 +
     6.8 +ifneq ($(shell uname -s), Darwin)
     6.9 +	pic = -fPIC
    6.10 +endif
    6.11 +
    6.12 +CFLAGS = -pedantic -Wall -g $(pic)
    6.13 +CXXFLAGS = -pedantic -Wall -g $(pic)
    6.14 +
    6.15 +$(lib): $(obj)
    6.16 +	$(AR) rcs $@ $(obj)
    6.17 +
    6.18 +.PHONY: clean
    6.19 +clean:
    6.20 +	rm -f $(obj) $(lib)
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/libs/vmath/Makefile	Sun Sep 29 21:53:03 2013 +0300
     7.3 @@ -0,0 +1,18 @@
     7.4 +csrc = $(wildcard *.c)
     7.5 +ccsrc = $(wildcard *.cc)
     7.6 +obj = $(csrc:.c=.o) $(ccsrc:.cc=.o)
     7.7 +lib = libvmath.a
     7.8 +
     7.9 +ifneq ($(shell uname -s), Darwin)
    7.10 +	pic = -fPIC
    7.11 +endif
    7.12 +
    7.13 +CFLAGS = -pedantic -Wall -g $(pic)
    7.14 +CXXFLAGS = -pedantic -Wall -g $(pic)
    7.15 +
    7.16 +$(lib): $(obj)
    7.17 +	$(AR) rcs $@ $(obj)
    7.18 +
    7.19 +.PHONY: clean
    7.20 +clean:
    7.21 +	rm -f $(obj) $(lib)
     8.1 --- a/src/goat3d.cc	Sun Sep 29 08:46:19 2013 +0300
     8.2 +++ b/src/goat3d.cc	Sun Sep 29 21:53:03 2013 +0300
     8.3 @@ -27,6 +27,8 @@
     8.4  	goat3d *goat = new goat3d;
     8.5  	goat->flags = 0;
     8.6  	goat->scn = new Scene;
     8.7 +
     8.8 +	goat3d_setopt(goat, GOAT3D_OPT_SAVEXML, 1);
     8.9  	return goat;
    8.10  }
    8.11  
     9.1 --- a/src/xform_node.cc	Sun Sep 29 08:46:19 2013 +0300
     9.2 +++ b/src/xform_node.cc	Sun Sep 29 21:53:03 2013 +0300
     9.3 @@ -1,8 +1,8 @@
     9.4  #include <assert.h>
     9.5  #include <algorithm>
     9.6  #include "xform_node.h"
     9.7 -#include "anim/anim.h"
     9.8 -#include "anim/track.h"
     9.9 +#include "anim.h"
    9.10 +#include "track.h"
    9.11  
    9.12  static inline anm_interpolator track_interpolator(Interp in);
    9.13  static inline anm_extrapolator track_extrapolator(Extrap ex);