goat3d

annotate 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
rev   line source
nuclear@14 1 #include <stdarg.h>
nuclear@0 2 #include "goat3d.h"
nuclear@0 3 #include "goat3d_impl.h"
nuclear@9 4 #include "chunk.h"
nuclear@0 5
nuclear@0 6 Scene::Scene()
nuclear@0 7 : name("unnamed"), ambient(0.05, 0.05, 0.05)
nuclear@0 8 {
nuclear@0 9 }
nuclear@0 10
nuclear@0 11 Scene::~Scene()
nuclear@0 12 {
nuclear@0 13 clear();
nuclear@0 14 }
nuclear@0 15
nuclear@0 16 void Scene::clear()
nuclear@0 17 {
nuclear@0 18 for(size_t i=0; i<materials.size(); i++) {
nuclear@0 19 delete materials[i];
nuclear@0 20 }
nuclear@0 21 materials.clear();
nuclear@0 22
nuclear@0 23 for(size_t i=0; i<meshes.size(); i++) {
nuclear@0 24 delete meshes[i];
nuclear@0 25 }
nuclear@0 26 meshes.clear();
nuclear@0 27
nuclear@0 28 for(size_t i=0; i<lights.size(); i++) {
nuclear@0 29 delete lights[i];
nuclear@0 30 }
nuclear@0 31 lights.clear();
nuclear@0 32
nuclear@0 33 for(size_t i=0; i<cameras.size(); i++) {
nuclear@0 34 delete cameras[i];
nuclear@0 35 }
nuclear@0 36 cameras.clear();
nuclear@0 37
nuclear@12 38 for(size_t i=0; i<nodes.size(); i++) {
nuclear@12 39 delete_node_tree(nodes[i]);
nuclear@0 40 }
nuclear@12 41 nodes.clear();
nuclear@0 42
nuclear@0 43 name = "unnamed";
nuclear@0 44 }
nuclear@0 45
nuclear@0 46 void Scene::set_name(const char *name)
nuclear@0 47 {
nuclear@0 48 this->name = name;
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 const char *Scene::get_name() const
nuclear@0 52 {
nuclear@0 53 return name.c_str();
nuclear@0 54 }
nuclear@8 55
nuclear@8 56 void Scene::set_ambient(const Vector3 &amb)
nuclear@8 57 {
nuclear@8 58 ambient = amb;
nuclear@8 59 }
nuclear@8 60
nuclear@8 61 const Vector3 &Scene::get_ambient() const
nuclear@8 62 {
nuclear@8 63 return ambient;
nuclear@8 64 }
nuclear@8 65
nuclear@8 66 void Scene::add_material(Material *mat)
nuclear@8 67 {
nuclear@8 68 materials.push_back(mat);
nuclear@8 69 }
nuclear@8 70
nuclear@8 71 Material *Scene::get_material(int idx) const
nuclear@8 72 {
nuclear@8 73 return idx >=0 && idx < (int)materials.size() ? materials[idx] : 0;
nuclear@8 74 }
nuclear@8 75
nuclear@8 76 Material *Scene::get_material(const char *name) const
nuclear@8 77 {
nuclear@8 78 for(size_t i=0; i<materials.size(); i++) {
nuclear@8 79 if(materials[i]->name == std::string(name)) {
nuclear@8 80 return materials[i];
nuclear@8 81 }
nuclear@8 82 }
nuclear@8 83 return 0;
nuclear@8 84 }
nuclear@8 85
nuclear@9 86 int Scene::get_material_count() const
nuclear@9 87 {
nuclear@9 88 return (int)materials.size();
nuclear@9 89 }
nuclear@9 90
nuclear@9 91
nuclear@8 92 void Scene::add_mesh(Mesh *mesh)
nuclear@8 93 {
nuclear@8 94 meshes.push_back(mesh);
nuclear@8 95 }
nuclear@8 96
nuclear@8 97 Mesh *Scene::get_mesh(int idx) const
nuclear@8 98 {
nuclear@8 99 return idx >= 0 && idx < (int)meshes.size() ? meshes[idx] : 0;
nuclear@8 100 }
nuclear@8 101
nuclear@8 102 Mesh *Scene::get_mesh(const char *name) const
nuclear@8 103 {
nuclear@8 104 for(size_t i=0; i<meshes.size(); i++) {
nuclear@8 105 if(meshes[i]->name == std::string(name)) {
nuclear@8 106 return meshes[i];
nuclear@8 107 }
nuclear@8 108 }
nuclear@8 109 return 0;
nuclear@8 110 }
nuclear@8 111
nuclear@9 112 int Scene::get_mesh_count() const
nuclear@9 113 {
nuclear@9 114 return (int)meshes.size();
nuclear@9 115 }
nuclear@9 116
nuclear@9 117
nuclear@8 118 void Scene::add_light(Light *light)
nuclear@8 119 {
nuclear@8 120 lights.push_back(light);
nuclear@8 121 }
nuclear@8 122
nuclear@8 123 Light *Scene::get_light(int idx) const
nuclear@8 124 {
nuclear@8 125 return idx >= 0 && idx < (int)lights.size() ? lights[idx] : 0;
nuclear@8 126 }
nuclear@8 127
nuclear@8 128 Light *Scene::get_light(const char *name) const
nuclear@8 129 {
nuclear@8 130 for(size_t i=0; i<lights.size(); i++) {
nuclear@8 131 if(lights[i]->name == std::string(name)) {
nuclear@8 132 return lights[i];
nuclear@8 133 }
nuclear@8 134 }
nuclear@8 135 return 0;
nuclear@8 136 }
nuclear@8 137
nuclear@9 138 int Scene::get_light_count() const
nuclear@9 139 {
nuclear@9 140 return (int)lights.size();
nuclear@9 141 }
nuclear@9 142
nuclear@9 143
nuclear@8 144 void Scene::add_camera(Camera *cam)
nuclear@8 145 {
nuclear@8 146 cameras.push_back(cam);
nuclear@8 147 }
nuclear@8 148
nuclear@8 149 Camera *Scene::get_camera(int idx) const
nuclear@8 150 {
nuclear@8 151 return idx >= 0 && idx < (int)cameras.size() ? cameras[idx] : 0;
nuclear@8 152 }
nuclear@8 153
nuclear@8 154 Camera *Scene::get_camera(const char *name) const
nuclear@8 155 {
nuclear@8 156 for(size_t i=0; i<cameras.size(); i++) {
nuclear@8 157 if(cameras[i]->name == std::string(name)) {
nuclear@8 158 return cameras[i];
nuclear@8 159 }
nuclear@8 160 }
nuclear@8 161 return 0;
nuclear@8 162 }
nuclear@8 163
nuclear@9 164 int Scene::get_camera_count() const
nuclear@9 165 {
nuclear@9 166 return (int)cameras.size();
nuclear@9 167 }
nuclear@9 168
nuclear@9 169
nuclear@8 170 void Scene::add_node(Node *node)
nuclear@8 171 {
nuclear@8 172 nodes.push_back(node);
nuclear@8 173 }
nuclear@8 174
nuclear@8 175 Node *Scene::get_node(int idx) const
nuclear@8 176 {
nuclear@8 177 return idx >= 0 && idx < (int)nodes.size() ? nodes[idx] : 0;
nuclear@8 178 }
nuclear@8 179
nuclear@8 180 Node *Scene::get_node(const char *name) const
nuclear@8 181 {
nuclear@8 182 for(size_t i=0; i<nodes.size(); i++) {
nuclear@8 183 if(strcmp(nodes[i]->get_name(), name) == 0) {
nuclear@8 184 return nodes[i];
nuclear@8 185 }
nuclear@8 186 }
nuclear@8 187 return 0;
nuclear@8 188 }
nuclear@8 189
nuclear@9 190 int Scene::get_node_count() const
nuclear@9 191 {
nuclear@9 192 return (int)nodes.size();
nuclear@9 193 }
nuclear@9 194
nuclear@9 195
nuclear@8 196 bool Scene::load(goat3d_io *io)
nuclear@8 197 {
nuclear@8 198 return false;
nuclear@8 199 }
nuclear@8 200
nuclear@14 201 // Scene::save is defined in goat3d_write.cc
nuclear@9 202
nuclear@9 203
nuclear@14 204 void io_fprintf(goat3d_io *io, const char *fmt, ...)
nuclear@8 205 {
nuclear@14 206 va_list ap;
nuclear@9 207
nuclear@14 208 va_start(ap, fmt);
nuclear@14 209 io_vfprintf(io, fmt, ap);
nuclear@14 210 va_end(ap);
nuclear@14 211 }
nuclear@9 212
nuclear@9 213
nuclear@14 214 void io_vfprintf(goat3d_io *io, const char *fmt, va_list ap)
nuclear@14 215 {
nuclear@14 216 char smallbuf[256];
nuclear@14 217 char *buf = smallbuf;
nuclear@14 218 int sz = sizeof smallbuf;
nuclear@9 219
nuclear@14 220 int retsz = vsnprintf(buf, sz - 1, fmt, ap);
nuclear@9 221
nuclear@14 222 if(retsz >= sz) {
nuclear@14 223 /* C99 mandates that snprintf with a short count should return the
nuclear@14 224 * number of characters that *would* be printed.
nuclear@14 225 */
nuclear@14 226 buf = new char[retsz + 1];
nuclear@9 227
nuclear@14 228 vsnprintf(buf, retsz, fmt, ap);
nuclear@9 229
nuclear@14 230 } else if(retsz <= 0) {
nuclear@14 231 /* SUSv2 and microsoft specify that snprintf with a short count
nuclear@14 232 * returns an arbitrary value <= 0. So let's try allocating
nuclear@14 233 * bigger and bigger arrays until we find the correct size.
nuclear@14 234 */
nuclear@14 235 sz = sizeof smallbuf;
nuclear@14 236 do {
nuclear@14 237 sz *= 2;
nuclear@14 238 if(buf != smallbuf) {
nuclear@14 239 delete [] buf;
nuclear@14 240 }
nuclear@14 241 buf = new char[sz + 1];
nuclear@9 242
nuclear@14 243 retsz = vsnprintf(buf, sz, fmt, ap);
nuclear@14 244 } while(retsz <= 0);
nuclear@9 245 }
nuclear@9 246
nuclear@14 247 io->write(buf, sz, io->cls);
nuclear@9 248
nuclear@14 249 if(buf != smallbuf) {
nuclear@14 250 delete [] buf;
nuclear@9 251 }
nuclear@9 252
nuclear@9 253 }