goat3d
diff src/goat3d_scene.cc @ 14:188c697b3b49
- added a document describing the goat3d file format chunk hierarchy
- started an alternative XML-based file format
- added the openctm library
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Sep 2013 04:47:05 +0300 |
parents | src/goat3d.cc@be15ba7c5483 |
children | f1b4c27382ce |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/goat3d_scene.cc Thu Sep 26 04:47:05 2013 +0300 1.3 @@ -0,0 +1,253 @@ 1.4 +#include <stdarg.h> 1.5 +#include "goat3d.h" 1.6 +#include "goat3d_impl.h" 1.7 +#include "chunk.h" 1.8 + 1.9 +Scene::Scene() 1.10 + : name("unnamed"), ambient(0.05, 0.05, 0.05) 1.11 +{ 1.12 +} 1.13 + 1.14 +Scene::~Scene() 1.15 +{ 1.16 + clear(); 1.17 +} 1.18 + 1.19 +void Scene::clear() 1.20 +{ 1.21 + for(size_t i=0; i<materials.size(); i++) { 1.22 + delete materials[i]; 1.23 + } 1.24 + materials.clear(); 1.25 + 1.26 + for(size_t i=0; i<meshes.size(); i++) { 1.27 + delete meshes[i]; 1.28 + } 1.29 + meshes.clear(); 1.30 + 1.31 + for(size_t i=0; i<lights.size(); i++) { 1.32 + delete lights[i]; 1.33 + } 1.34 + lights.clear(); 1.35 + 1.36 + for(size_t i=0; i<cameras.size(); i++) { 1.37 + delete cameras[i]; 1.38 + } 1.39 + cameras.clear(); 1.40 + 1.41 + for(size_t i=0; i<nodes.size(); i++) { 1.42 + delete_node_tree(nodes[i]); 1.43 + } 1.44 + nodes.clear(); 1.45 + 1.46 + name = "unnamed"; 1.47 +} 1.48 + 1.49 +void Scene::set_name(const char *name) 1.50 +{ 1.51 + this->name = name; 1.52 +} 1.53 + 1.54 +const char *Scene::get_name() const 1.55 +{ 1.56 + return name.c_str(); 1.57 +} 1.58 + 1.59 +void Scene::set_ambient(const Vector3 &amb) 1.60 +{ 1.61 + ambient = amb; 1.62 +} 1.63 + 1.64 +const Vector3 &Scene::get_ambient() const 1.65 +{ 1.66 + return ambient; 1.67 +} 1.68 + 1.69 +void Scene::add_material(Material *mat) 1.70 +{ 1.71 + materials.push_back(mat); 1.72 +} 1.73 + 1.74 +Material *Scene::get_material(int idx) const 1.75 +{ 1.76 + return idx >=0 && idx < (int)materials.size() ? materials[idx] : 0; 1.77 +} 1.78 + 1.79 +Material *Scene::get_material(const char *name) const 1.80 +{ 1.81 + for(size_t i=0; i<materials.size(); i++) { 1.82 + if(materials[i]->name == std::string(name)) { 1.83 + return materials[i]; 1.84 + } 1.85 + } 1.86 + return 0; 1.87 +} 1.88 + 1.89 +int Scene::get_material_count() const 1.90 +{ 1.91 + return (int)materials.size(); 1.92 +} 1.93 + 1.94 + 1.95 +void Scene::add_mesh(Mesh *mesh) 1.96 +{ 1.97 + meshes.push_back(mesh); 1.98 +} 1.99 + 1.100 +Mesh *Scene::get_mesh(int idx) const 1.101 +{ 1.102 + return idx >= 0 && idx < (int)meshes.size() ? meshes[idx] : 0; 1.103 +} 1.104 + 1.105 +Mesh *Scene::get_mesh(const char *name) const 1.106 +{ 1.107 + for(size_t i=0; i<meshes.size(); i++) { 1.108 + if(meshes[i]->name == std::string(name)) { 1.109 + return meshes[i]; 1.110 + } 1.111 + } 1.112 + return 0; 1.113 +} 1.114 + 1.115 +int Scene::get_mesh_count() const 1.116 +{ 1.117 + return (int)meshes.size(); 1.118 +} 1.119 + 1.120 + 1.121 +void Scene::add_light(Light *light) 1.122 +{ 1.123 + lights.push_back(light); 1.124 +} 1.125 + 1.126 +Light *Scene::get_light(int idx) const 1.127 +{ 1.128 + return idx >= 0 && idx < (int)lights.size() ? lights[idx] : 0; 1.129 +} 1.130 + 1.131 +Light *Scene::get_light(const char *name) const 1.132 +{ 1.133 + for(size_t i=0; i<lights.size(); i++) { 1.134 + if(lights[i]->name == std::string(name)) { 1.135 + return lights[i]; 1.136 + } 1.137 + } 1.138 + return 0; 1.139 +} 1.140 + 1.141 +int Scene::get_light_count() const 1.142 +{ 1.143 + return (int)lights.size(); 1.144 +} 1.145 + 1.146 + 1.147 +void Scene::add_camera(Camera *cam) 1.148 +{ 1.149 + cameras.push_back(cam); 1.150 +} 1.151 + 1.152 +Camera *Scene::get_camera(int idx) const 1.153 +{ 1.154 + return idx >= 0 && idx < (int)cameras.size() ? cameras[idx] : 0; 1.155 +} 1.156 + 1.157 +Camera *Scene::get_camera(const char *name) const 1.158 +{ 1.159 + for(size_t i=0; i<cameras.size(); i++) { 1.160 + if(cameras[i]->name == std::string(name)) { 1.161 + return cameras[i]; 1.162 + } 1.163 + } 1.164 + return 0; 1.165 +} 1.166 + 1.167 +int Scene::get_camera_count() const 1.168 +{ 1.169 + return (int)cameras.size(); 1.170 +} 1.171 + 1.172 + 1.173 +void Scene::add_node(Node *node) 1.174 +{ 1.175 + nodes.push_back(node); 1.176 +} 1.177 + 1.178 +Node *Scene::get_node(int idx) const 1.179 +{ 1.180 + return idx >= 0 && idx < (int)nodes.size() ? nodes[idx] : 0; 1.181 +} 1.182 + 1.183 +Node *Scene::get_node(const char *name) const 1.184 +{ 1.185 + for(size_t i=0; i<nodes.size(); i++) { 1.186 + if(strcmp(nodes[i]->get_name(), name) == 0) { 1.187 + return nodes[i]; 1.188 + } 1.189 + } 1.190 + return 0; 1.191 +} 1.192 + 1.193 +int Scene::get_node_count() const 1.194 +{ 1.195 + return (int)nodes.size(); 1.196 +} 1.197 + 1.198 + 1.199 +bool Scene::load(goat3d_io *io) 1.200 +{ 1.201 + return false; 1.202 +} 1.203 + 1.204 +// Scene::save is defined in goat3d_write.cc 1.205 + 1.206 + 1.207 +void io_fprintf(goat3d_io *io, const char *fmt, ...) 1.208 +{ 1.209 + va_list ap; 1.210 + 1.211 + va_start(ap, fmt); 1.212 + io_vfprintf(io, fmt, ap); 1.213 + va_end(ap); 1.214 +} 1.215 + 1.216 + 1.217 +void io_vfprintf(goat3d_io *io, const char *fmt, va_list ap) 1.218 +{ 1.219 + char smallbuf[256]; 1.220 + char *buf = smallbuf; 1.221 + int sz = sizeof smallbuf; 1.222 + 1.223 + int retsz = vsnprintf(buf, sz - 1, fmt, ap); 1.224 + 1.225 + if(retsz >= sz) { 1.226 + /* C99 mandates that snprintf with a short count should return the 1.227 + * number of characters that *would* be printed. 1.228 + */ 1.229 + buf = new char[retsz + 1]; 1.230 + 1.231 + vsnprintf(buf, retsz, fmt, ap); 1.232 + 1.233 + } else if(retsz <= 0) { 1.234 + /* SUSv2 and microsoft specify that snprintf with a short count 1.235 + * returns an arbitrary value <= 0. So let's try allocating 1.236 + * bigger and bigger arrays until we find the correct size. 1.237 + */ 1.238 + sz = sizeof smallbuf; 1.239 + do { 1.240 + sz *= 2; 1.241 + if(buf != smallbuf) { 1.242 + delete [] buf; 1.243 + } 1.244 + buf = new char[sz + 1]; 1.245 + 1.246 + retsz = vsnprintf(buf, sz, fmt, ap); 1.247 + } while(retsz <= 0); 1.248 + } 1.249 + 1.250 + io->write(buf, sz, io->cls); 1.251 + 1.252 + if(buf != smallbuf) { 1.253 + delete [] buf; 1.254 + } 1.255 + 1.256 +}