1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version.  The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #include "Exception.h"
15 #include "TerrainTile.h"
16 #include "Group.h"
17 #include "Layer.h"
18 
19 #include <osgDB/Options>
20 #include <osgTerrain/GeometryTechnique>
21 #include <osgTerrain/Terrain>
22 
23 using namespace ive;
24 
write(DataOutputStream * out)25 void TerrainTile::write(DataOutputStream* out)
26 {
27     // Write Terrain's identification.
28     out->writeInt(IVETERRAINTILE);
29     // If the osg class is inherited by any other class we should also write this to file.
30     osg::Group*  group = dynamic_cast<osg::Group*>(this);
31     if(group)
32         ((ive::Group*)(group))->write(out);
33     else
34         out_THROW_EXCEPTION("Terrain::write(): Could not cast this osgTerrain::Terrain to an osg::Group.");
35 
36     if (out->getVersion() >= VERSION_0044)
37     {
38         out->writeInt(getBlendingPolicy());
39     }
40 
41     if (out->getVersion() >= VERSION_0026)
42     {
43         out->writeInt(getTileID().level);
44         out->writeInt(getTileID().x);
45         out->writeInt(getTileID().y);
46     }
47 
48     if (out->getVersion() >= VERSION_0023)
49     {
50         out->writeLocator(getLocator());
51         out->writeLayer(getElevationLayer());
52 
53         out->writeUInt(getNumColorLayers());
54         for(unsigned int i=0; i<getNumColorLayers(); ++i)
55         {
56             out->writeLayer(getColorLayer(i));
57         }
58     }
59     else
60     {
61         LayerHelper helper;
62 
63         helper.writeLocator(out, getLocator());
64 
65         helper.writeLayer(out, getElevationLayer());
66 
67         out->writeUInt(getNumColorLayers());
68 
69         for(unsigned int i=0; i<getNumColorLayers(); ++i)
70         {
71             helper.writeLayer(out, getColorLayer(i));
72         }
73     }
74 
75     writeTerrainTechnique(out, getTerrainTechnique());
76 
77 }
78 
read(DataInputStream * in)79 void TerrainTile::read(DataInputStream* in)
80 {
81     // Peek on Terrain's identification.
82     int id = in->peekInt();
83     if (id != IVETERRAINTILE) in_THROW_EXCEPTION("TerrainTile::read(): Expected Terrain identification.");
84 
85     // Read Terrain's identification.
86     id = in->readInt();
87     // If the osg class is inherited by any other class we should also read this from file.
88     osg::Group*  group = dynamic_cast<osg::Group*>(this);
89     if(group)
90         ((ive::Group*)(group))->read(in);
91     else
92         in_THROW_EXCEPTION("Terrain::read(): Could not cast this osgTerrain::Terrain to an osg::Group.");
93 
94     if (in->getVersion() >= VERSION_0044)
95     {
96         setBlendingPolicy(static_cast<osgTerrain::TerrainTile::BlendingPolicy>(in->readInt()));
97     }
98 
99     if (in->getVersion() >= VERSION_0026)
100     {
101 
102         int level = in->readInt();
103         int x = in->readInt();
104         int y = in->readInt();
105         setTileID(osgTerrain::TileID(level,x,y));
106 
107         // OSG_NOTICE<<"Read TileID("<<level<<", "<<x<<", "<<y<<")"<<std::endl;
108     }
109 
110     if (in->getVersion() >= VERSION_0023)
111     {
112         setLocator(in->readLocator());
113         setElevationLayer(in->readLayer());
114         unsigned int numColorLayers = in->readUInt();
115         for(unsigned int i=0; i<numColorLayers; ++i)
116         {
117             setColorLayer(i, in->readLayer());
118         }
119     }
120     else
121     {
122         LayerHelper helper;
123 
124         setLocator(helper.readLocator(in));
125 
126         setElevationLayer(helper.readLayer(in));
127 
128         unsigned int numColorLayers = in->readUInt();
129         for(unsigned int i=0; i<numColorLayers; ++i)
130         {
131             setColorLayer(i, helper.readLayer(in));
132         }
133     }
134 
135     setTerrainTechnique(readTerrainTechnique(in));
136 
137     if (in->getOptions())
138     {
139         osg::ref_ptr<osg::Node> node;
140         if (in->getOptions()->getTerrain().lock(node))
141         {
142             setTerrain(node->asTerrain());
143         }
144     }
145 
146     if (osgTerrain::TerrainTile::getTileLoadedCallback().valid())
147         osgTerrain::TerrainTile::getTileLoadedCallback()->loaded(this, in->getOptions());
148 }
149 
writeTerrainTechnique(DataOutputStream * out,osgTerrain::TerrainTechnique * technique)150 void TerrainTile::writeTerrainTechnique(DataOutputStream* out, osgTerrain::TerrainTechnique* technique)
151 {
152     if (dynamic_cast<osgTerrain::GeometryTechnique*>(technique))
153     {
154         out->writeBool(true);
155         out->writeInt(IVEGEOMETRYTECHNIQUE);
156     }
157     else
158     {
159         out->writeBool(false);
160     }
161 }
162 
readTerrainTechnique(DataInputStream * in)163 osgTerrain::TerrainTechnique* TerrainTile::readTerrainTechnique(DataInputStream* in)
164 {
165     bool hasTechnique = in->readBool();
166     if (!hasTechnique) return 0;
167 
168     int id = in->readInt();
169     if (id==IVEGEOMETRYTECHNIQUE)
170     {
171         return new osgTerrain::GeometryTechnique;
172     }
173     else
174     {
175         return 0;
176     }
177 }
178