goat3d

diff src/scene.cc @ 17:1d85d7dd0038

goatprim done
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Sep 2013 02:29:52 +0300
parents src/goat3d_scene.cc@f1b4c27382ce
children b35427826b60
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/scene.cc	Fri Sep 27 02:29:52 2013 +0300
     1.3 @@ -0,0 +1,268 @@
     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 +	if(mat->name.empty()) {
    1.72 +		char buf[64];
    1.73 +		sprintf(buf, "material%04d", (int)materials.size());
    1.74 +		mat->name = std::string(buf);
    1.75 +	}
    1.76 +	materials.push_back(mat);
    1.77 +}
    1.78 +
    1.79 +Material *Scene::get_material(int idx) const
    1.80 +{
    1.81 +	return idx >=0 && idx < (int)materials.size() ? materials[idx] : 0;
    1.82 +}
    1.83 +
    1.84 +Material *Scene::get_material(const char *name) const
    1.85 +{
    1.86 +	for(size_t i=0; i<materials.size(); i++) {
    1.87 +		if(materials[i]->name == std::string(name)) {
    1.88 +			return materials[i];
    1.89 +		}
    1.90 +	}
    1.91 +	return 0;
    1.92 +}
    1.93 +
    1.94 +int Scene::get_material_count() const
    1.95 +{
    1.96 +	return (int)materials.size();
    1.97 +}
    1.98 +
    1.99 +
   1.100 +void Scene::add_mesh(Mesh *mesh)
   1.101 +{
   1.102 +	if(mesh->name.empty()) {
   1.103 +		char buf[64];
   1.104 +		sprintf(buf, "mesh%04d", (int)meshes.size());
   1.105 +		mesh->name = std::string(buf);
   1.106 +	}
   1.107 +	meshes.push_back(mesh);
   1.108 +}
   1.109 +
   1.110 +Mesh *Scene::get_mesh(int idx) const
   1.111 +{
   1.112 +	return idx >= 0 && idx < (int)meshes.size() ? meshes[idx] : 0;
   1.113 +}
   1.114 +
   1.115 +Mesh *Scene::get_mesh(const char *name) const
   1.116 +{
   1.117 +	for(size_t i=0; i<meshes.size(); i++) {
   1.118 +		if(meshes[i]->name == std::string(name)) {
   1.119 +			return meshes[i];
   1.120 +		}
   1.121 +	}
   1.122 +	return 0;
   1.123 +}
   1.124 +
   1.125 +int Scene::get_mesh_count() const
   1.126 +{
   1.127 +	return (int)meshes.size();
   1.128 +}
   1.129 +
   1.130 +
   1.131 +void Scene::add_light(Light *light)
   1.132 +{
   1.133 +	lights.push_back(light);
   1.134 +}
   1.135 +
   1.136 +Light *Scene::get_light(int idx) const
   1.137 +{
   1.138 +	return idx >= 0 && idx < (int)lights.size() ? lights[idx] : 0;
   1.139 +}
   1.140 +
   1.141 +Light *Scene::get_light(const char *name) const
   1.142 +{
   1.143 +	for(size_t i=0; i<lights.size(); i++) {
   1.144 +		if(lights[i]->name == std::string(name)) {
   1.145 +			return lights[i];
   1.146 +		}
   1.147 +	}
   1.148 +	return 0;
   1.149 +}
   1.150 +
   1.151 +int Scene::get_light_count() const
   1.152 +{
   1.153 +	return (int)lights.size();
   1.154 +}
   1.155 +
   1.156 +
   1.157 +void Scene::add_camera(Camera *cam)
   1.158 +{
   1.159 +	cameras.push_back(cam);
   1.160 +}
   1.161 +
   1.162 +Camera *Scene::get_camera(int idx) const
   1.163 +{
   1.164 +	return idx >= 0 && idx < (int)cameras.size() ? cameras[idx] : 0;
   1.165 +}
   1.166 +
   1.167 +Camera *Scene::get_camera(const char *name) const
   1.168 +{
   1.169 +	for(size_t i=0; i<cameras.size(); i++) {
   1.170 +		if(cameras[i]->name == std::string(name)) {
   1.171 +			return cameras[i];
   1.172 +		}
   1.173 +	}
   1.174 +	return 0;
   1.175 +}
   1.176 +
   1.177 +int Scene::get_camera_count() const
   1.178 +{
   1.179 +	return (int)cameras.size();
   1.180 +}
   1.181 +
   1.182 +
   1.183 +void Scene::add_node(Node *node)
   1.184 +{
   1.185 +	nodes.push_back(node);
   1.186 +}
   1.187 +
   1.188 +Node *Scene::get_node(int idx) const
   1.189 +{
   1.190 +	return idx >= 0 && idx < (int)nodes.size() ? nodes[idx] : 0;
   1.191 +}
   1.192 +
   1.193 +Node *Scene::get_node(const char *name) const
   1.194 +{
   1.195 +	for(size_t i=0; i<nodes.size(); i++) {
   1.196 +		if(strcmp(nodes[i]->get_name(), name) == 0) {
   1.197 +			return nodes[i];
   1.198 +		}
   1.199 +	}
   1.200 +	return 0;
   1.201 +}
   1.202 +
   1.203 +int Scene::get_node_count() const
   1.204 +{
   1.205 +	return (int)nodes.size();
   1.206 +}
   1.207 +
   1.208 +
   1.209 +bool Scene::load(goat3d_io *io)
   1.210 +{
   1.211 +	return false;
   1.212 +}
   1.213 +
   1.214 +bool Scene::loadxml(goat3d_io *io)
   1.215 +{
   1.216 +	return false;
   1.217 +}
   1.218 +
   1.219 +// Scene::save is defined in goat3d_write.cc
   1.220 +
   1.221 +
   1.222 +void io_fprintf(goat3d_io *io, const char *fmt, ...)
   1.223 +{
   1.224 +	va_list ap;
   1.225 +
   1.226 +	va_start(ap, fmt);
   1.227 +	io_vfprintf(io, fmt, ap);
   1.228 +	va_end(ap);
   1.229 +}
   1.230 +
   1.231 +
   1.232 +void io_vfprintf(goat3d_io *io, const char *fmt, va_list ap)
   1.233 +{
   1.234 +	char smallbuf[256];
   1.235 +	char *buf = smallbuf;
   1.236 +	int sz = sizeof smallbuf;
   1.237 +
   1.238 +	int retsz = vsnprintf(buf, sz - 1, fmt, ap);
   1.239 +
   1.240 +	if(retsz >= sz) {
   1.241 +		/* C99 mandates that snprintf with a short count should return the
   1.242 +		 * number of characters that *would* be printed.
   1.243 +		 */
   1.244 +		buf = new char[retsz + 1];
   1.245 +
   1.246 +		vsnprintf(buf, retsz, fmt, ap);
   1.247 +
   1.248 +	} else if(retsz <= 0) {
   1.249 +		/* SUSv2 and microsoft specify that snprintf with a short count
   1.250 +		 * returns an arbitrary value <= 0. So let's try allocating
   1.251 +		 * bigger and bigger arrays until we find the correct size.
   1.252 +		 */
   1.253 +		sz = sizeof smallbuf;
   1.254 +		do {
   1.255 +			sz *= 2;
   1.256 +			if(buf != smallbuf) {
   1.257 +				delete [] buf;
   1.258 +			}
   1.259 +			buf = new char[sz + 1];
   1.260 +
   1.261 +			retsz = vsnprintf(buf, sz, fmt, ap);
   1.262 +		} while(retsz <= 0);
   1.263 +	}
   1.264 +
   1.265 +	io->write(buf, retsz, io->cls);
   1.266 +
   1.267 +	if(buf != smallbuf) {
   1.268 +		delete [] buf;
   1.269 +	}
   1.270 +
   1.271 +}