goat3d

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