-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTextPointsLoader.cpp
More file actions
130 lines (103 loc) · 3.09 KB
/
Copy pathTextPointsLoader.cpp
File metadata and controls
130 lines (103 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "TextPointsLoader.h"
#include <osg/Geode>
#include <osg/Point>
using namespace omega;
using namespace cyclops;
///////////////////////////////////////////////////////////////////////////////
TextPointsLoader::TextPointsLoader(): ModelLoader("points-text")
{
}
///////////////////////////////////////////////////////////////////////////////
TextPointsLoader::~TextPointsLoader()
{
}
///////////////////////////////////////////////////////////////////////////////
bool TextPointsLoader::supportsExtension(const String& ext)
{
if(ext == "xyz") return true;
return false;
}
///////////////////////////////////////////////////////////////////////////////
bool TextPointsLoader::load(ModelAsset* model)
{
osg::ref_ptr<osg::Group> group = new osg::Group();
bool result = loadFile(model->info->path, model->info->options, group);
// if successful get last child and add to sceneobject
if(result)
{
osg::Geode* points;
points = group->getChild(0)->asGeode();
model->nodes.push_back(points);
group->removeChild(0, 1);
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
bool TextPointsLoader::loadFile(const String& filename, const String& options, osg::Group * grp)
{
if(!grp)
{
return false;
}
osg::Vec3Array* verticesP = new osg::Vec3Array();
osg::Vec4Array* verticesC = new osg::Vec4Array();
String path;
if(DataManager::findFile(filename, path))
{
readXYZ(path, options, verticesP, verticesC);
// create geometry and geodes to hold the data
osg::Geode* geode = new osg::Geode();
geode->setCullingActive(false);
osg::Geometry* nodeGeom = new osg::Geometry();
osg::StateSet *state = nodeGeom->getOrCreateStateSet();
nodeGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0, verticesP->size()));
osg::VertexBufferObject* vboP = nodeGeom->getOrCreateVertexBufferObject();
vboP->setUsage (GL_STREAM_DRAW);
nodeGeom->setUseDisplayList (false);
nodeGeom->setUseVertexBufferObjects(true);
nodeGeom->setVertexArray(verticesP);
nodeGeom->setColorArray(verticesC);
nodeGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
geode->addDrawable(nodeGeom);
geode->dirtyBound();
grp->addChild(geode);
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
void TextPointsLoader::readXYZ(
const String& filename, const String& options, osg::Vec3Array* points, osg::Vec4Array* colors)
{
osg::Vec3f point;
osg::Vec4f color(1.0f, 1.0f, 1.0f, 1.0f);
// create a stream to read in file
ifstream ifs( filename.c_str() );
string value, values;
stringstream ss;
stringstream ssdouble;
while( getline( ifs, values ) )
{
ss << values;
int index = 0;
while(ss >> value)
{
ssdouble << value;
if( index < 3 )
{
ssdouble >> point[index];
}
else
{
ssdouble >> color[index - 3];
//color[index - 3]/=255.0;
}
ssdouble.clear();
index++;
}
points->push_back(point);
colors->push_back(color);
ss.clear();
}
ifs.close();
}