1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
13 * This library is free software; you can redistribute it and/or modify it *
14 * under the terms of the GNU Library General Public License as published *
15 * by the Free Software Foundation, version 2. *
17 * This library is distributed in the hope that it will be useful, but *
18 * WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
20 * Library General Public License for more details. *
22 * You should have received a copy of the GNU Library General Public *
23 * License along with this library; if not, write to the Free Software *
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
35 \*---------------------------------------------------------------------------*/
37 #include <UnitTest++.h>
39 // Unit tests for OSB file io.
43 #include "OSGNameAttachment.h"
44 #include "OSGSceneFileHandler.h"
46 #include "OSGGeometry.h"
47 #include "OSGSimpleGeometry.h"
49 #include "OSGChunkMaterial.h"
50 #include "OSGTextureObjChunk.h"
51 #include "OSGTextureEnvChunk.h"
53 #ifndef OSG_DISABLE_DEPRECATED
54 #include "OSGTextureChunk.h"
57 #include <boost/filesystem/operations.hpp>
59 namespace bf
= boost::filesystem
;
68 test_file
= bf::path("test_file.osb");
69 bf::remove(test_file
);
74 bf::remove(test_file
);
81 TEST_FIXTURE(FileFixture
, CreateOSBFile
)
83 OSG::NodeUnrecPtr n
= OSG::Node::create();
84 n
->setCore(OSG::Group::create());
86 CHECK(!bf::exists(test_file
));
87 OSG::SceneFileHandler::the()->write(n
, test_file
.native_file_string().c_str());
88 CHECK(bf::exists(test_file
));
90 OSG::NodeUnrecPtr new_n
=
91 OSG::SceneFileHandler::the()->read(test_file
.native_file_string().c_str());
95 TEST_FIXTURE(FileFixture
, CreateOSBTree
)
98 OSG::NodeUnrecPtr base_node
= OSG::Node::create();
99 base_node
->setCore(OSG::Group::create());
100 OSG::NodeUnrecPtr child_node
= OSG::Node::create();
101 child_node
->setCore(OSG::Group::create());
103 base_node
->addChild(child_node
);
105 CHECK(!bf::exists(test_file
));
106 OSG::SceneFileHandler::the()->write(base_node
, test_file
.native_file_string().c_str());
107 CHECK(bf::exists(test_file
));
109 OSG::NodeUnrecPtr new_n
=
110 OSG::SceneFileHandler::the()->read(test_file
.native_file_string().c_str());
111 CHECK(new_n
!= NULL
);
112 CHECK(new_n
->getCore() != NULL
);
113 CHECK(new_n
->getNChildren() == 1);
114 CHECK(new_n
->getChild(0) != NULL
);
115 CHECK(new_n
->getChild(0)->getCore() != NULL
);
119 TEST_FIXTURE(FileFixture
, TestNameRetention
)
121 std::string
start_name("node");
122 OSG::NodeUnrecPtr n
= OSG::Node::create();
123 OSG::setName(n
, start_name
);
124 n
->setCore(OSG::Group::create());
126 CHECK(!bf::exists(test_file
));
127 OSG::SceneFileHandler::the()->write(n
, test_file
.native_file_string().c_str());
128 CHECK(bf::exists(test_file
));
130 OSG::NodeUnrecPtr new_n
=
131 OSG::SceneFileHandler::the()->read(test_file
.native_file_string().c_str());
132 CHECK(new_n
!= NULL
);
133 CHECK(OSG::getName(new_n
) != NULL
);
134 std::string cur_name
= std::string(OSG::getName(new_n
));
136 CHECK(cur_name
== start_name
);
139 #ifndef OSG_DISABLE_DEPRECATED
140 TEST_FIXTURE(FileFixture
, TextureChunkIO
)
142 OSG::NodeUnrecPtr n
= OSG::Node::create();
143 OSG::GeometryUnrecPtr geo
= OSG::makeBoxGeo(2.0, 2.0, 2.0, 1, 1, 1);
144 OSG::ChunkMaterialUnrecPtr mat
= OSG::ChunkMaterial::create();
146 OSG::ImageUnrecPtr img
= OSG::Image::create();
147 img
->set(OSG::Image::OSG_RGBA_PF
, 2, 2);
149 img
->editData()[0 * 2 * 4 + 0 * 4 + 0] = 255;
150 img
->editData()[0 * 2 * 4 + 0 * 4 + 1] = 0;
151 img
->editData()[0 * 2 * 4 + 0 * 4 + 2] = 0;
152 img
->editData()[0 * 2 * 4 + 0 * 4 + 3] = 255;
154 img
->editData()[0 * 2 * 4 + 1 * 4 + 0] = 255;
155 img
->editData()[0 * 2 * 4 + 1 * 4 + 1] = 255;
156 img
->editData()[0 * 2 * 4 + 1 * 4 + 2] = 0;
157 img
->editData()[0 * 2 * 4 + 1 * 4 + 3] = 255;
159 img
->editData()[1 * 2 * 4 + 0 * 4 + 0] = 0;
160 img
->editData()[1 * 2 * 4 + 0 * 4 + 1] = 0;
161 img
->editData()[1 * 2 * 4 + 0 * 4 + 2] = 255;
162 img
->editData()[1 * 2 * 4 + 0 * 4 + 3] = 255;
164 img
->editData()[1 * 2 * 4 + 1 * 4 + 0] = 0;
165 img
->editData()[1 * 2 * 4 + 1 * 4 + 1] = 255;
166 img
->editData()[1 * 2 * 4 + 1 * 4 + 2] = 255;
167 img
->editData()[1 * 2 * 4 + 1 * 4 + 3] = 255;
169 OSG::TextureChunkUnrecPtr tex
= OSG::TextureChunk::create();
173 geo
->setMaterial(mat
);
177 OSG::SceneFileHandler::the()->write(n
, test_file
.native_file_string().c_str());
179 OSG::NodeUnrecPtr n2
=
180 OSG::SceneFileHandler::the()->read(test_file
.native_file_string().c_str());
184 CHECK(n2
->getCore() != NULL
);
185 OSG::GeometryUnrecPtr geo2
=
186 dynamic_cast<OSG::Geometry
*>(n2
->getCore());
189 CHECK(geo2
->getMaterial() != NULL
);
190 OSG::ChunkMaterialUnrecPtr mat2
=
191 dynamic_cast<OSG::ChunkMaterial
*>(geo2
->getMaterial());
194 CHECK(mat2
->getChunk(0) != NULL
);
195 const OSG::TextureChunk
*tex2
=
196 dynamic_cast<const OSG::TextureChunk
*>(mat2
->getChunk(0));
197 const OSG::TextureObjChunk
*texObj
=
198 dynamic_cast<const OSG::TextureObjChunk
*>(mat2
->getChunk(0));
199 const OSG::TextureEnvChunk
*texEnv
=
200 dynamic_cast<const OSG::TextureEnvChunk
*>(mat2
->getChunk(1));
202 CHECK(texObj
!= NULL
);
203 CHECK(texEnv
!= NULL
);
205 CHECK(texObj
->getImage() != NULL
);
206 OSG::Image
*img2
= texObj
->getImage();
209 CHECK_EQUAL(255, img2
->getData()[0 * 2 * 4 + 0 * 4 + 0]);
210 CHECK_EQUAL( 0, img2
->getData()[0 * 2 * 4 + 0 * 4 + 1]);
211 CHECK_EQUAL( 0, img2
->getData()[0 * 2 * 4 + 0 * 4 + 2]);
212 CHECK_EQUAL(255, img2
->getData()[0 * 2 * 4 + 0 * 4 + 3]);
214 CHECK_EQUAL(255, img2
->getData()[0 * 2 * 4 + 1 * 4 + 0]);
215 CHECK_EQUAL(255, img2
->getData()[0 * 2 * 4 + 1 * 4 + 1]);
216 CHECK_EQUAL( 0, img2
->getData()[0 * 2 * 4 + 1 * 4 + 2]);
217 CHECK_EQUAL(255, img2
->getData()[0 * 2 * 4 + 1 * 4 + 3]);
219 CHECK_EQUAL( 0, img2
->getData()[1 * 2 * 4 + 0 * 4 + 0]);
220 CHECK_EQUAL( 0, img2
->getData()[1 * 2 * 4 + 0 * 4 + 1]);
221 CHECK_EQUAL(255, img2
->getData()[1 * 2 * 4 + 0 * 4 + 2]);
222 CHECK_EQUAL(255, img2
->getData()[1 * 2 * 4 + 0 * 4 + 3]);
224 CHECK_EQUAL( 0, img2
->getData()[1 * 2 * 4 + 1 * 4 + 0]);
225 CHECK_EQUAL(255, img2
->getData()[1 * 2 * 4 + 1 * 4 + 1]);
226 CHECK_EQUAL(255, img2
->getData()[1 * 2 * 4 + 1 * 4 + 2]);
227 CHECK_EQUAL(255, img2
->getData()[1 * 2 * 4 + 1 * 4 + 3]);
229 #endif // OSG_DISABLE_DEPRECATED