src/serialize.cc
branchpyrit
changeset 80 907929fa9b59
parent 78 9569e9f35374
child 91 9d66d323c354
equal deleted inserted replaced
79:062b1c4143f7 80:907929fa9b59
    23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    24  * THE SOFTWARE.
    24  * THE SOFTWARE.
    25  */
    25  */
    26 
    26 
    27 #include "serialize.h"
    27 #include "serialize.h"
       
    28 #include <vector>
    28 #include <string>
    29 #include <string>
    29 #include <sstream>
    30 #include <sstream>
    30 
    31 
    31 Indexer vertex_index, shape_index;
    32 Indexer vertex_index, shape_index;
       
    33 vector<Vertex*> vertices;
    32 
    34 
    33 void resetSerializer()
    35 void resetSerializer()
    34 {
    36 {
    35 	vertex_index.reset();
    37 	vertex_index.reset();
    36 	shape_index.reset();
    38 	shape_index.reset();
       
    39 	vertices.clear();
    37 }
    40 }
    38 
    41 
    39 bool Indexer::get(void *o, int &retidx)
    42 bool Indexer::get(void *o, int &retidx)
    40 {
    43 {
    41 	map <void *, int>::iterator i;
    44 	map <void *, int>::iterator i;
    51 		retidx = i->second;
    54 		retidx = i->second;
    52 		return true;
    55 		return true;
    53 	}
    56 	}
    54 }
    57 }
    55 
    58 
    56 Shape *loadShape(istream &st)
    59 Shape *loadShape(istream &st, Material *mat)
    57 {
    60 {
    58 	string s;
    61 	string s;
    59 	istringstream is;
    62 	istringstream is;
    60 	getline(st, s, ',');
    63 	for (;;)
    61 	trim(s);
       
    62 	if (s.compare("(box") == 0)
       
    63 	{
    64 	{
    64 		Vector3 L,H;
       
    65 		st >> L;
       
    66 		getline(st, s, ',');
    65 		getline(st, s, ',');
    67 		st >> H;
    66 		trim(s);
    68 		getline(st, s, ')');
    67 
    69 		return new Box(L, H, new Material(Colour(1,1,1)));
    68 		// Vertex
       
    69 		if (s.compare("(v") == 0)
       
    70 		{
       
    71 			Vector3 P;
       
    72 			st >> P;
       
    73 			getline(st, s, ')');
       
    74 			vertices.push_back(new Vertex(P));
       
    75 			getline(st, s, ',');
       
    76 			continue;
       
    77 		}
       
    78 
       
    79 		// NormalVertex
       
    80 		if (s.compare("(vn") == 0)
       
    81 		{
       
    82 			Vector3 P,N;
       
    83 			st >> P;
       
    84 			getline(st, s, ',');
       
    85 			st >> N;
       
    86 			getline(st, s, ')');
       
    87 			vertices.push_back(new NormalVertex(P,N));
       
    88 			getline(st, s, ',');
       
    89 			continue;
       
    90 		}
       
    91 
       
    92 		// Triangle
       
    93 		if (s.compare("(t") == 0)
       
    94 		{
       
    95 			int a,b,c;
       
    96 			st >> a;
       
    97 			getline(st, s, ',');
       
    98 			st >> b;
       
    99 			getline(st, s, ',');
       
   100 			st >> c;
       
   101 			getline(st, s, ')');
       
   102 			return new Triangle(vertices[a], vertices[b], vertices[c], mat);
       
   103 		}
       
   104 
       
   105 		// box
       
   106 		if (s.compare("(box") == 0)
       
   107 		{
       
   108 			Vector3 L,H;
       
   109 			st >> L;
       
   110 			getline(st, s, ',');
       
   111 			st >> H;
       
   112 			getline(st, s, ')');
       
   113 			return new Box(L, H, mat);
       
   114 		}
       
   115 
       
   116 		// Sphere
       
   117 		if (s.compare("(sphere") == 0)
       
   118 		{
       
   119 			Vector3 center;
       
   120 			Float radius;
       
   121 			st >> center;
       
   122 			getline(st, s, ',');
       
   123 			st >> radius;
       
   124 			getline(st, s, ')');
       
   125 			return new Sphere(center, radius, mat);
       
   126 		}
       
   127 
       
   128 		// else...
       
   129 		return NULL;
    70 	}
   130 	}
    71 	if (s.compare("(sphere") == 0)
       
    72 	{
       
    73 		Vector3 center;
       
    74 		Float radius;
       
    75 		st >> center;
       
    76 		getline(st, s, ',');
       
    77 		st >> radius;
       
    78 		getline(st, s, ')');
       
    79 		return new Sphere(center, radius, new Material(Colour(1,1,1)));
       
    80 	}
       
    81 	return NULL;
       
    82 }
   131 }
    83 
   132 
    84 ostream & operator<<(ostream &st, Shape &o)
   133 ostream & operator<<(ostream &st, Shape &o)
    85 {
   134 {
    86 	return o.dump(st);
   135 	return o.dump(st);