Skip to content

Commit f6e8e08

Browse files
committed
add vrml node to specify location and load the correct Sky from within a vrml file
1 parent 1fad5fb commit f6e8e08

5 files changed

Lines changed: 288 additions & 20 deletions

File tree

src/OpenCOVER/plugins/general/GeoData/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

2-
using(OpenSceneGraph)
3-
using(GDAL)
2+
USING(OpenSceneGraph)
3+
USING(GDAL)
44
USING(PROJ)
55
USING(CURL)
6+
USING(VRML)
67

78
covise_find_package(OpenSceneGraph 3.2.0 QUIET COMPONENTS osgTerrain)
89
COVISE_FIND_PACKAGE(osgTerrain)
@@ -14,11 +15,13 @@ ENDIF()
1415
set(HEADERS
1516
CutGeometry.h
1617
GeoDataLoader.h
18+
VrmlNodeGeoData.h
1719
)
1820

1921
set(SOURCES
2022
CutGeometry.cpp
2123
GeoDataLoader.cpp
24+
VrmlNodeGeoData.cpp
2225
)
2326

2427

src/OpenCOVER/plugins/general/GeoData/GeoDataLoader.cpp

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ namespace opencover
7373
using namespace covise;
7474
using namespace opencover;
7575

76+
skyEntry::skyEntry(const std::string& n, const std::string& fn)
77+
{
78+
name = n;
79+
fileName = fn;
80+
}
81+
skyEntry::~skyEntry()
82+
{
83+
84+
}
85+
skyEntry::skyEntry(const skyEntry& se)
86+
{
87+
name = se.name;
88+
fileName = se.fileName;
89+
skyNode = se.skyNode;
90+
}
91+
92+
7693
std::string getCoordinates(const std::string& address) {
7794
using namespace opencover::httpclient::curl;
7895
std::string readBuffer;
@@ -246,10 +263,10 @@ bool GeoDataLoader::init()
246263
skyButton->setState(true);
247264
skyButton->setCallback([this](bool state)
248265
{
249-
if (state && skyNode.get()!=nullptr && skyNode->getNumParents()==0)
250-
skyRootNode->addChild(skyNode.get());
251-
else if(!state && skyNode.get()!=nullptr)
252-
skyRootNode->removeChild(skyNode.get());
266+
if (state && currentSkyNode.get()!=nullptr && currentSkyNode->getNumParents()==0)
267+
skyRootNode->addChild(currentSkyNode.get());
268+
else if(!state && currentSkyNode.get()!=nullptr)
269+
skyRootNode->removeChild(currentSkyNode.get());
253270
});
254271
skys = new ui::SelectionList(geoDataMenu, "Skys");
255272
skys->append("None");
@@ -261,12 +278,15 @@ bool GeoDataLoader::init()
261278
{
262279
if (entry.is_regular_file() && entry.path().extension() == ".wrl") {
263280
std::string name = entry.path().filename().string();
281+
skyEntry se(name.substr(0, name.length() - 4),entry.path().string());
282+
skyEntries.push_back(se);
283+
skys->append(se.name);
264284
if (skyNumber == defaultSky)
265285
{
266-
skyNode = coVRFileManager::instance()->loadFile(entry.path().string().c_str(), nullptr, skyRootNode);
286+
currentSkyNode = coVRFileManager::instance()->loadFile(entry.path().string().c_str(), nullptr, skyRootNode);
287+
se.skyNode = currentSkyNode;
267288
}
268289
skyNumber++;
269-
skys->append(name.substr(0,name.length()-4));
270290
}
271291
}
272292
}
@@ -320,20 +340,59 @@ void GeoDataLoader::message(int toWhom, int type, int length, const void* data)
320340
}
321341
void GeoDataLoader::setSky(int selection)
322342
{
323-
if (skyNode.get() != nullptr)
324-
skyRootNode->removeChild(skyNode.get());
325-
if (selection == 0)
343+
while (skyRootNode->getNumChildren())
344+
skyRootNode->removeChild(skyRootNode->getChild(0));
345+
346+
currentSkyNode = nullptr;
347+
if (selection != 0)
326348
{
327-
skyNode = nullptr;
349+
350+
351+
int n = 1;
352+
for( auto& sky : skyEntries)
353+
{
354+
if (n == selection)
355+
{
356+
if (sky.skyNode != nullptr)
357+
{
358+
skyRootNode->addChild(sky.skyNode);
359+
currentSkyNode = sky.skyNode;
360+
}
361+
else
362+
{
363+
sky.skyNode = coVRFileManager::instance()->loadFile((skyPath + "/" + skys->items()[selection] + ".wrl").c_str(), nullptr, skyRootNode);
364+
365+
currentSkyNode = sky.skyNode;
366+
}
367+
}
368+
n++;
369+
}
370+
328371
}
329-
else
330-
{
331-
if (skyNode.get() != nullptr)
332-
skyRootNode->removeChild(skyNode.get());
333-
skyNode = coVRFileManager::instance()->loadFile((skyPath + "/" + skys->items()[selection] + ".wrl").c_str(), nullptr, skyRootNode);
334-
cover->getScene()->addChild(skyRootNode);
372+
}
335373

374+
void GeoDataLoader::setSky(std::string fileName)
375+
{
376+
int n = 0;
377+
for (const auto& sky : skyEntries)
378+
{
379+
if (sky.fileName == fileName)// already have this file in the list
380+
{
381+
setSky(n+1);
382+
}
383+
n++;
336384
}
385+
std::filesystem::path path(fileName);
386+
std::string fn = path.filename().string();
387+
skyEntry se(fn.substr(0, fn.length() - 4), fileName);
388+
skyEntries.push_back(se);
389+
skys->append(se.name);
390+
}
391+
392+
void GeoDataLoader::setOffset(osg::Vec3 off)
393+
{
394+
offset = off;
395+
rootNode->setMatrix(osg::Matrix::translate(-offset));
337396
}
338397

339398
bool GeoDataLoader::update()

src/OpenCOVER/plugins/general/GeoData/GeoDataLoader.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ namespace fs = std::filesystem;
4141
#define RAD_TO_DEG 57.295779513082321
4242
#define DEG_TO_RAD .017453292519943296
4343
#endif
44-
44+
class skyEntry
45+
{
46+
public:
47+
skyEntry(const std::string& n, const std::string& fn);
48+
~skyEntry();
49+
skyEntry(const skyEntry &se);
50+
std::string name;
51+
std::string fileName;
52+
osg::ref_ptr<osg::Node> skyNode;
53+
};
4554

4655
class GeoDataLoader: public opencover::coVRPlugin, public opencover::ui::Owner
4756
{
@@ -60,6 +69,8 @@ class GeoDataLoader: public opencover::coVRPlugin, public opencover::ui::Owner
6069
virtual bool update();
6170
virtual void message(int toWhom, int type, int length, const void* data);
6271
void setSky(int num);
72+
void setSky(std::string fileName);
73+
void setOffset(osg::Vec3 off);
6374

6475

6576
private:
@@ -69,7 +80,7 @@ class GeoDataLoader: public opencover::coVRPlugin, public opencover::ui::Owner
6980

7081
osg::ref_ptr<osg::MatrixTransform> rootNode;
7182
osg::ref_ptr<osg::MatrixTransform> skyRootNode;
72-
osg::ref_ptr<osg::Node> skyNode;
83+
osg::ref_ptr<osg::Node> currentSkyNode;
7384
std::map<std::string, osg::ref_ptr<osg::Node>> loadedTerrains;
7485
std::map<std::string, osg::ref_ptr<osg::Node>> loadedBuildings;
7586
opencover::ui::Menu* geoDataMenu;
@@ -78,6 +89,7 @@ class GeoDataLoader: public opencover::coVRPlugin, public opencover::ui::Owner
7889
opencover::ui::Button* skyButton;
7990
opencover::ui::EditField* location;
8091
opencover::ui::SelectionList* skys;
92+
std::list<skyEntry> skyEntries;
8193
float northAngle;
8294
std::string terrainFile;
8395
std::string skyPath;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* This file is part of COVISE.
2+
3+
You can use it under the terms of the GNU Lesser General Public License
4+
version 2.1 or later, see lgpl-2.1.txt.
5+
6+
* License: LGPL 2+ */
7+
8+
//
9+
// Vrml 97 library
10+
// Copyright (C) 2001 Uwe Woessner
11+
//
12+
// %W% %G%
13+
// VrmlNodeGeoData.cpp
14+
#ifdef _WIN32
15+
#if (_MSC_VER >= 1300) && !(defined(MIDL_PASS) || defined(RC_INVOKED))
16+
#define POINTER_64 __ptr64
17+
#else
18+
#define POINTER_64
19+
#endif
20+
#include <winsock2.h>
21+
#include <windows.h>
22+
#endif
23+
#include <util/common.h>
24+
#include <vrml97/vrml/config.h>
25+
#include <vrml97/vrml/VrmlNodeType.h>
26+
#include <vrml97/vrml/coEventQueue.h>
27+
28+
#include <vrml97/vrml/MathUtils.h>
29+
#include <vrml97/vrml/System.h>
30+
#include <vrml97/vrml/Viewer.h>
31+
#include <vrml97/vrml/VrmlScene.h>
32+
#include <cover/VRViewer.h>
33+
#include <cover/VRSceneGraph.h>
34+
#include <cover/coVRAnimationManager.h>
35+
#include <cover/coVRPluginSupport.h>
36+
#include <OpenVRUI/osg/mathUtils.h>
37+
#include <math.h>
38+
39+
#include <util/byteswap.h>
40+
41+
#include "GeoDataLoader.h"
42+
#include "VrmlNodeGeoData.h"
43+
#include <osg/Quat>
44+
45+
static list<VrmlNodeGeoData *> allGeoData;
46+
47+
// GeoData factory.
48+
49+
static VrmlNode *creator(VrmlScene *scene)
50+
{
51+
return new VrmlNodeGeoData(scene);
52+
}
53+
54+
void VrmlNodeGeoData::update()
55+
{
56+
list<VrmlNodeGeoData *>::iterator ts;
57+
for (ts = allGeoData.begin(); ts != allGeoData.end(); ++ts)
58+
{
59+
}
60+
}
61+
62+
void VrmlNodeGeoData::initFields(VrmlNodeGeoData *node, VrmlNodeType *t)
63+
{
64+
VrmlNodeChild::initFields(node, t); // Parent class
65+
initFieldsHelper(node, t,
66+
field("offset", node->d_offset, [node](auto f){
67+
GeoDataLoader::instance()->setOffset(osg::Vec3(node->d_offset.get()[0], node->d_offset.get()[1], node->d_offset.get()[2]));
68+
}),
69+
field("skyName", node->d_skyName, [node](auto f) {
70+
GeoDataLoader::instance()->setSky(node->d_skyName.get());
71+
}),
72+
field("enabled", node->d_enabled, [node](auto f) {
73+
}));
74+
75+
76+
}
77+
78+
const char *VrmlNodeGeoData::typeName()
79+
{
80+
return "GeoData";
81+
}
82+
83+
VrmlNodeGeoData::VrmlNodeGeoData(VrmlScene *scene)
84+
: VrmlNodeChild(scene, typeName())
85+
, d_offset(0,0,0)
86+
, d_enabled(true)
87+
, d_skyName("")
88+
{
89+
coVRAnimationManager::instance()->showAnimMenu(true);
90+
setModified();
91+
}
92+
93+
void VrmlNodeGeoData::addToScene(VrmlScene *s, const char *relUrl)
94+
{
95+
(void)relUrl;
96+
d_scene = s;
97+
if (s)
98+
{
99+
allGeoData.push_front(this);
100+
}
101+
else
102+
{
103+
cerr << "no Scene" << endl;
104+
}
105+
}
106+
107+
// need copy constructor for new markerName (each instance definitely needs a new marker Name) ...
108+
109+
VrmlNodeGeoData::VrmlNodeGeoData(const VrmlNodeGeoData &n)
110+
: VrmlNodeChild(n)
111+
, d_offset(0, 0, 0)
112+
, d_enabled(true)
113+
, d_skyName("")
114+
{
115+
setModified();
116+
}
117+
118+
VrmlNodeGeoData::~VrmlNodeGeoData()
119+
{
120+
allGeoData.remove(this);
121+
}
122+
123+
VrmlNodeGeoData *VrmlNodeGeoData::toGeoData() const
124+
{
125+
return (VrmlNodeGeoData *)this;
126+
}
127+
128+
void VrmlNodeGeoData::render(Viewer *viewer)
129+
{
130+
(void)viewer;
131+
double timeNow = System::the->time();
132+
133+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* This file is part of COVISE.
2+
3+
You can use it under the terms of the GNU Lesser General Public License
4+
version 2.1 or later, see lgpl-2.1.txt.
5+
6+
* License: LGPL 2+ */
7+
8+
//
9+
// Vrml 97 library
10+
// Copyright (C) 2001 Uwe Woessner
11+
//
12+
// %W% %G%
13+
// VrmlNodeGeoData.h
14+
15+
#ifndef _VRMLNODEGeoData_
16+
#define _VRMLNODEGeoData_
17+
18+
#include <util/coTypes.h>
19+
20+
#include <vrml97/vrml/VrmlNode.h>
21+
#include <vrml97/vrml/VrmlSFBool.h>
22+
#include <vrml97/vrml/VrmlSFInt.h>
23+
#include <vrml97/vrml/VrmlSFFloat.h>
24+
#include <vrml97/vrml/VrmlSFVec3f.h>
25+
#include <vrml97/vrml/VrmlSFString.h>
26+
#include <vrml97/vrml/VrmlSFRotation.h>
27+
#include <vrml97/vrml/VrmlNodeChild.h>
28+
#include <vrml97/vrml/VrmlScene.h>
29+
30+
using namespace opencover;
31+
using namespace vrml;
32+
33+
class VrmlNodeGeoData : public VrmlNodeChild
34+
{
35+
36+
public:
37+
// Define the fields of GeoData nodes
38+
static void initFields(VrmlNodeGeoData *node, vrml::VrmlNodeType *t);
39+
static const char *typeName();
40+
41+
VrmlNodeGeoData(VrmlScene *scene = 0);
42+
VrmlNodeGeoData(const VrmlNodeGeoData &n);
43+
virtual ~VrmlNodeGeoData();
44+
virtual void addToScene(VrmlScene *s, const char *);
45+
46+
virtual VrmlNodeGeoData *toGeoData() const;
47+
virtual void render(Viewer *);
48+
49+
bool isEnabled()
50+
{
51+
return d_enabled.get();
52+
}
53+
static void update();
54+
55+
private:
56+
// Fields
57+
VrmlSFVec3f d_offset;
58+
VrmlSFBool d_enabled;
59+
VrmlSFString d_skyName;
60+
};
61+
#endif //_VRMLNODEGeoData_

0 commit comments

Comments
 (0)