goat3d
changeset 15:f1b4c27382ce
blah
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Sep 2013 14:06:14 +0300 |
parents | 188c697b3b49 |
children | cb6c1a945a11 |
files | Makefile doc/goatfmt src/goat3d.h src/goat3d_impl.h src/goat3d_scene.cc src/goat3d_writexml.cc src/material.cc src/material.h src/mesh.cc src/mesh.h |
diffstat | 10 files changed, 141 insertions(+), 39 deletions(-) [+] |
line diff
1.1 --- a/Makefile Thu Sep 26 04:47:05 2013 +0300 1.2 +++ b/Makefile Thu Sep 26 14:06:14 2013 +0300 1.3 @@ -31,6 +31,8 @@ 1.4 pic = -fPIC 1.5 endif 1.6 1.7 +CC = clang 1.8 +CXX = clang++ 1.9 CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(pic) $(extinc) 1.10 LDFLAGS = $(extlibs) -lvmath -lanim 1.11 1.12 @@ -41,7 +43,7 @@ 1.13 $(CXX) -o $@ $(shared) $(obj) $(LDFLAGS) 1.14 1.15 $(lib_a): $(obj) $(extlibs) 1.16 - $(AR) rcs $@ $(obj) 1.17 + $(AR) rcs $@ $(obj) $(openctm) 1.18 1.19 $(openctm): 1.20 $(MAKE) -C libs/openctm
2.1 --- a/doc/goatfmt Thu Sep 26 04:47:05 2013 +0300 2.2 +++ b/doc/goatfmt Thu Sep 26 14:06:14 2013 +0300 2.3 @@ -28,7 +28,7 @@ 2.4 | +--MESH_NAME 2.5 | | +--<STRING> 2.6 | +--MESH_MATERIAL 2.7 - | | +--<STRING> (material name) 2.8 + | | +--<STRING|INT> (material name or index) 2.9 | +--MESH_VERTEX_LIST 2.10 | | +--[FLOAT3] 2.11 | +--MESH_NORMAL_LIST
3.1 --- a/src/goat3d.h Thu Sep 26 04:47:05 2013 +0300 3.2 +++ b/src/goat3d.h Thu Sep 26 14:06:14 2013 +0300 3.3 @@ -4,7 +4,46 @@ 3.4 #include <stdio.h> 3.5 #include <stdlib.h> 3.6 3.7 +#define GOAT3D_MAT_ATTR_DIFFUSE "diffuse" 3.8 +#define GOAT3D_MAT_ATTR_SPECULAR "specular" 3.9 +#define GOAT3D_MAT_ATTR_SHININESS "shininess" 3.10 +#define GOAT3D_MAT_ATTR_NORMAL "normal" 3.11 +#define GOAT3D_MAT_ATTR_BUMP "bump" 3.12 +#define GOAT3D_MAT_ATTR_REFLECTION "reflection" 3.13 +#define GOAT3D_MAT_ATTR_TRANSMISSION "transmission" 3.14 +#define GOAT3D_MAT_ATTR_IOR "ior" 3.15 + 3.16 +enum goat3d_mesh_attrib { 3.17 + GOAT3D_MESH_ATTR_VERTEX, 3.18 + GOAT3D_MESH_ATTR_NORMAL, 3.19 + GOAT3D_MESH_ATTR_TANGENT, 3.20 + GOAT3D_MESH_ATTR_TEXCOORD, 3.21 + GOAT3D_MESH_ATTR_SKIN_WEIGHT, 3.22 + GOAT3D_MESH_ATTR_SKIN_MATRIX, 3.23 + GOAT3D_MESH_ATTR_COLOR, 3.24 + 3.25 + NUM_GOAT3D_MESH_ATTRIBS 3.26 +}; 3.27 + 3.28 +/* immediate mode mesh construction primitive type */ 3.29 +enum goat3d_im_primitive { 3.30 + GOAT3D_TRIANGLES, 3.31 + GOAT3D_QUADS 3.32 +}; 3.33 + 3.34 + 3.35 +enum goat3d_option { 3.36 + GOAT3D_OPT_SAVEXML, /* save in XML format */ 3.37 + 3.38 + NUM_GOAT3D_OPTIONS 3.39 +}; 3.40 + 3.41 struct goat3d; 3.42 +struct goat3d_material; 3.43 +struct goat3d_mesh; 3.44 +struct goat3d_light; 3.45 +struct goat3d_camera; 3.46 +struct goat3d_node; 3.47 3.48 struct goat3d_io { 3.49 void *cls; /* closure data */ 3.50 @@ -14,18 +53,17 @@ 3.51 long (*seek)(long offs, int whence, void *uptr); 3.52 }; 3.53 3.54 -struct goat3d_vec3 { float x, y, z; }; 3.55 -struct goat3d_vec4 { float x, y, z, w; }; 3.56 - 3.57 - 3.58 #ifdef __cplusplus 3.59 extern "C" { 3.60 #endif 3.61 3.62 /* construction/destruction */ 3.63 -struct goat3d *goat3d_create(); 3.64 +struct goat3d *goat3d_create(void); 3.65 void goat3d_free(struct goat3d *g); 3.66 3.67 +void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val); 3.68 +int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt); 3.69 + 3.70 /* load/save */ 3.71 int goat3d_load(struct goat3d *g, const char *fname); 3.72 int goat3d_save(const struct goat3d *g, const char *fname); 3.73 @@ -40,12 +78,82 @@ 3.74 int goat3d_set_name(struct goat3d *g, const char *name); 3.75 const char *goat3d_get_name(const struct goat3d *g); 3.76 3.77 -void goat3d_set_ambient(struct goat3d *g, float x, float y, float z); 3.78 +void goat3d_set_ambient(struct goat3d *g, const float *ambient); 3.79 +void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab); 3.80 +const float *goat3d_get_ambient(const struct goat3d *g); 3.81 3.82 -/* helpers */ 3.83 -struct goat3d_vec3 goat3d_vec3(float x, float y, float z); 3.84 -struct goat3d_vec4 goat3d_vec4(float x, float y, float z, float w); 3.85 +/* materials */ 3.86 +struct goat3d_material *goat3d_create_mtl(void); 3.87 +void goat3d_destroy_mtl(struct goat3d_material *mtl); 3.88 3.89 +void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name); 3.90 +const char *goat3d_get_mtl_name(const struct goat3d_material *mtl); 3.91 + 3.92 +void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val); 3.93 +void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val); 3.94 +void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b); 3.95 +void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a); 3.96 +const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib); 3.97 + 3.98 +void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname); 3.99 +const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib); 3.100 + 3.101 +void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl); 3.102 + 3.103 +/* meshes */ 3.104 +struct goat3d_mesh *goat3d_create_mesh(void); 3.105 +void goat3d_destroy_mesh(struct goat3d_mesh *mesh); 3.106 + 3.107 +void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name); 3.108 +const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh); 3.109 + 3.110 +void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl); 3.111 +struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh); 3.112 + 3.113 +int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib); 3.114 +int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh); 3.115 + 3.116 +/* sets all the data for a single vertex attribute array in one go. 3.117 + * vnum is the number of *vertices* to be set, not the number of floats, ints or whatever 3.118 + * data is expected to be something different depending on the attribute: 3.119 + * - GOAT3D_MESH_ATTR_VERTEX - 3 floats per vertex 3.120 + * - GOAT3D_MESH_ATTR_NORMAL - 3 floats per vertex 3.121 + * - GOAT3D_MESH_ATTR_TANGENT - 3 floats per vertex 3.122 + * - GOAT3D_MESH_ATTR_TEXCOORD - 2 floats per vertex 3.123 + * - GOAT3D_MESH_ATTR_SKIN_WEIGHT - 4 floats per vertex 3.124 + * - GOAT3D_MESH_ATTR_SKIN_MATRIX - 4 ints per vertex 3.125 + * - GOAT3D_MESH_ATTR_COLOR - 4 floats per vertex 3.126 + */ 3.127 +void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum); 3.128 +/* returns a pointer to the beginning of the requested mesh attribute array */ 3.129 +void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib); 3.130 +/* returns a pointer to the requested mesh attribute */ 3.131 +void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx); 3.132 + 3.133 +/* sets all the faces in one go. data is an array of 3 int vertex indices per face */ 3.134 +void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int fnum); 3.135 +/* returns a pointer to the beginning of the face index array */ 3.136 +int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh); 3.137 +/* returns a pointer to a face index */ 3.138 +int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx); 3.139 + 3.140 +/* immediate mode OpenGL-like interface for setting mesh data 3.141 + * NOTE: using this interface will result in no vertex sharing between faces 3.142 + * NOTE2: the immedate mode interface is not thread-safe, either use locks, or don't 3.143 + * use it at all in multithreaded situations. 3.144 + */ 3.145 +void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim); 3.146 +void goat3d_end(void); 3.147 +void goat3d_vertex3f(float x, float y, float z); 3.148 +void goat3d_normal3f(float x, float y, float z); 3.149 +void goat3d_tangent3f(float x, float y, float z); 3.150 +void goat3d_texcoord2f(float x, float y); 3.151 +void goat3d_skin_weight4f(float x, float y, float z, float w); 3.152 +void goat3d_skin_matrix4i(int x, int y, int z, int w); 3.153 +void goat3d_color3f(float x, float y, float z); 3.154 +void goat3d_color4f(float x, float y, float z, float w); 3.155 + 3.156 +void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh); 3.157 3.158 #ifdef __cplusplus 3.159 }
4.1 --- a/src/goat3d_impl.h Thu Sep 26 04:47:05 2013 +0300 4.2 +++ b/src/goat3d_impl.h Thu Sep 26 14:06:14 2013 +0300 4.3 @@ -10,6 +10,8 @@ 4.4 #include "material.h" 4.5 #include "node.h" 4.6 4.7 +extern int goat_log_level; 4.8 + 4.9 class Scene { 4.10 private: 4.11 std::string name;
5.1 --- a/src/goat3d_scene.cc Thu Sep 26 04:47:05 2013 +0300 5.2 +++ b/src/goat3d_scene.cc Thu Sep 26 14:06:14 2013 +0300 5.3 @@ -198,6 +198,11 @@ 5.4 return false; 5.5 } 5.6 5.7 +bool Scene::loadxml(goat3d_io *io) 5.8 +{ 5.9 + return false; 5.10 +} 5.11 + 5.12 // Scene::save is defined in goat3d_write.cc 5.13 5.14 5.15 @@ -244,7 +249,7 @@ 5.16 } while(retsz <= 0); 5.17 } 5.18 5.19 - io->write(buf, sz, io->cls); 5.20 + io->write(buf, retsz, io->cls); 5.21 5.22 if(buf != smallbuf) { 5.23 delete [] buf;
6.1 --- a/src/goat3d_writexml.cc Thu Sep 26 04:47:05 2013 +0300 6.2 +++ b/src/goat3d_writexml.cc Thu Sep 26 14:06:14 2013 +0300 6.3 @@ -49,7 +49,7 @@ 6.4 xmlout(io, level + 2, "<name string=\"%s\"/>\n", mat->get_attrib_name(i)); 6.5 6.6 const MaterialAttrib &attr = (*mat)[i]; 6.7 - xmlout(io, level + 2, "<val float4=\"%.3f %.3f %.3f\"/>\n", attr.value.x, 6.8 + xmlout(io, level + 2, "<val float4=\"%.3f %.3f %.3f %.3f\"/>\n", attr.value.x, 6.9 attr.value.y, attr.value.z, attr.value.w); 6.10 if(!attr.map.empty()) { 6.11 xmlout(io, level + 2, "<map string=\"%s\"/>\n", attr.map.c_str()); 6.12 @@ -94,15 +94,7 @@ 6.13 6.14 // texture coordinates 6.15 if(!mesh->texcoords.empty()) { 6.16 - CTMfloat *uvarray = new CTMfloat[vnum * 2 * sizeof *uvarray]; 6.17 - CTMfloat *uvptr = uvarray; 6.18 - 6.19 - for(int i=0; i<vnum; i++) { 6.20 - *uvptr++ = mesh->texcoords[i].x; 6.21 - *uvptr++ = mesh->texcoords[i].y; 6.22 - } 6.23 - ctmAddUVMap(ctm, uvarray, "texcoord", 0); 6.24 - delete [] uvarray; 6.25 + ctmAddUVMap(ctm, &mesh->texcoords[0].x, "texcoord", 0); 6.26 } 6.27 6.28 // vertex colors
7.1 --- a/src/material.cc Thu Sep 26 04:47:05 2013 +0300 7.2 +++ b/src/material.cc Thu Sep 26 14:06:14 2013 +0300 7.3 @@ -1,5 +1,7 @@ 7.4 #include "material.h" 7.5 7.6 +MaterialAttrib Material::def_attr; 7.7 + 7.8 int Material::get_attrib_count() const 7.9 { 7.10 return (int)attrib.size();
8.1 --- a/src/material.h Thu Sep 26 04:47:05 2013 +0300 8.2 +++ b/src/material.h Thu Sep 26 14:06:14 2013 +0300 8.3 @@ -8,17 +8,10 @@ 8.4 struct MaterialAttrib { 8.5 Vector4 value; 8.6 std::string map; 8.7 + 8.8 + MaterialAttrib() : value(1, 1, 1, 1) {} 8.9 }; 8.10 8.11 -#define MAT_ATTR_DIFFUSE "diffuse" 8.12 -#define MAT_ATTR_SPECULAR "specular" 8.13 -#define MAT_ATTR_SHININESS "shininess" 8.14 -#define MAT_ATTR_NORMAL "normal" 8.15 -#define MAT_ATTR_BUMP "bump" 8.16 -#define MAT_ATTR_REFLECTION "reflection" 8.17 -#define MAT_ATTR_TRANSMISSION "transmission" 8.18 -#define MAT_ATTR_IOR "ior" 8.19 - 8.20 class Material { 8.21 private: 8.22 static MaterialAttrib def_attr;
9.1 --- a/src/mesh.cc Thu Sep 26 04:47:05 2013 +0300 9.2 +++ b/src/mesh.cc Thu Sep 26 14:06:14 2013 +0300 9.3 @@ -5,8 +5,6 @@ 9.4 material = 0; 9.5 } 9.6 9.7 -Mesh::~Mesh() {} 9.8 - 9.9 void Mesh::set_material(Material *mat) 9.10 { 9.11 material = mat;
10.1 --- a/src/mesh.h Thu Sep 26 04:47:05 2013 +0300 10.2 +++ b/src/mesh.h Thu Sep 26 14:06:14 2013 +0300 10.3 @@ -22,19 +22,19 @@ 10.4 std::vector<Vector3> vertices; 10.5 std::vector<Vector3> normals; 10.6 std::vector<Vector3> tangents; 10.7 - std::vector<Vector3> texcoords; 10.8 + std::vector<Vector2> texcoords; 10.9 std::vector<Vector4> skin_weights; 10.10 std::vector<Int4> skin_matrices; 10.11 std::vector<Vector4> colors; 10.12 - std::vector<Node*> bones; 10.13 std::vector<Face> faces; 10.14 10.15 + std::vector<Node*> bones; 10.16 + 10.17 Mesh(); 10.18 - virtual ~Mesh(); 10.19 10.20 - virtual void set_material(Material *mat); 10.21 - virtual Material *get_material(); 10.22 - virtual const Material *get_material() const; 10.23 + void set_material(Material *mat); 10.24 + Material *get_material(); 10.25 + const Material *get_material() const; 10.26 }; 10.27 10.28 #endif // MESH_H_