|
| 1 | +#include "CloudManager.h" |
| 2 | + |
| 3 | +#include <cvrKernel/ComController.h> |
| 4 | +#include <sstream> |
| 5 | +#include <algorithm> |
| 6 | +#include <cstdio> |
| 7 | + |
| 8 | +#include <sys/syscall.h> |
| 9 | +#include <sys/stat.h> |
| 10 | + |
| 11 | +using namespace cvr; |
| 12 | +using namespace std; |
| 13 | + |
| 14 | +CloudManager::CloudManager() |
| 15 | +{ |
| 16 | + useKColor = true; |
| 17 | + should_quit = false; |
| 18 | + |
| 19 | + //initialPointScale = 0.001; |
| 20 | + initialPointScale = ConfigManager::getFloat("Plugin.KinectDemo.KinectDefaultOn.KinectPointSize",0.0f); |
| 21 | + pgm1 = new osg::Program; |
| 22 | + pgm1->setName("Sphere"); |
| 23 | + std::string shaderPath = ConfigManager::getEntry("Plugin.Points.ShaderPath"); |
| 24 | + pgm1->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile(shaderPath + "/Sphere.vert"))); |
| 25 | + pgm1->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile(shaderPath + "/Sphere.frag"))); |
| 26 | + pgm1->addShader(osg::Shader::readShaderFile(osg::Shader::GEOMETRY, osgDB::findDataFile(shaderPath + "/Sphere.geom"))); |
| 27 | + pgm1->setParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 4); |
| 28 | + pgm1->setParameter(GL_GEOMETRY_INPUT_TYPE_EXT, GL_POINTS); |
| 29 | + pgm1->setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP); |
| 30 | + minDistHSV = 700; |
| 31 | + maxDistHSV = 5000; |
| 32 | + |
| 33 | + kinectgrp = new osg::Group(); |
| 34 | + osg::StateSet* state = kinectgrp->getOrCreateStateSet(); |
| 35 | + state->setAttribute(pgm1); |
| 36 | + state->addUniform(new osg::Uniform("pointScale", initialPointScale)); |
| 37 | + state->addUniform(new osg::Uniform("globalAlpha", 1.0f)); |
| 38 | + float pscale = 1.0; |
| 39 | + osg::Uniform* _scaleUni = new osg::Uniform("pointScale", 1.0f * pscale); |
| 40 | + kinectgrp->getOrCreateStateSet()->addUniform(_scaleUni); |
| 41 | + |
| 42 | + _root = new osg::MatrixTransform(); |
| 43 | + SceneManager::instance()->getObjectsRoot()->addChild(_root); |
| 44 | + _root->addChild(kinectgrp); |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +CloudManager::~CloudManager() |
| 49 | +{ |
| 50 | +} |
| 51 | + |
| 52 | +bool CloudManager::isCacheDone() |
| 53 | +{ |
| 54 | + return _cacheDone; |
| 55 | +} |
| 56 | + |
| 57 | +void CloudManager::update() |
| 58 | +{ |
| 59 | + static bool frameLoading = false; |
| 60 | + bool cDone; |
| 61 | +if(true) |
| 62 | +{ |
| 63 | + if(cvr::ComController::instance()->isMaster()) |
| 64 | + { |
| 65 | + cDone = _cacheDone; |
| 66 | + int numSlaves = cvr::ComController::instance()->getNumSlaves(); |
| 67 | + bool sDone[numSlaves]; |
| 68 | + cvr::ComController::instance()->readSlaves(sDone,sizeof(bool)); |
| 69 | + for(int i = 0; i < numSlaves; i++) |
| 70 | + { |
| 71 | + cDone = cDone && sDone[i]; |
| 72 | + } |
| 73 | + cvr::ComController::instance()->sendSlaves(&cDone,sizeof(bool)); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + cDone = _cacheDone; |
| 78 | + cvr::ComController::instance()->sendMaster(&cDone,sizeof(bool)); |
| 79 | + cvr::ComController::instance()->readMaster(&cDone,sizeof(bool)); |
| 80 | + } |
| 81 | + |
| 82 | + if(!cDone) |
| 83 | + { |
| 84 | + //std::cerr << "Waiting for load to finish." << std::endl; |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + cvr::ComController::instance()->sync(); |
| 89 | +//Add not here? |
| 90 | + |
| 91 | + |
| 92 | + return; |
| 93 | +} |
| 94 | + if(cDone) |
| 95 | + { |
| 96 | + std::cerr << "Load Finished." << std::endl; |
| 97 | + //Add loaded node to root |
| 98 | + |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | +// std::cerr << "Waiting for GPU load finish." << std::endl; |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +void CloudManager::run() |
| 108 | +{ |
| 109 | +//Do functions |
| 110 | +cerr << "."; |
| 111 | +if(kinectgrp != NULL) |
| 112 | +{ |
| 113 | + |
| 114 | + zmq::context_t context2(1); |
| 115 | + cloudT_socket = NULL; |
| 116 | + cloudT_socket = new SubSocket<RemoteKinect::PointCloud> (context2, ConfigManager::getEntry("Plugin.KinectDemo.KinectServer.PointCloud")); |
| 117 | + |
| 118 | + packet = new RemoteKinect::PointCloud(); |
| 119 | + while(!should_quit) |
| 120 | + { |
| 121 | + |
| 122 | + //printf("."); |
| 123 | + if(cloudT_socket != NULL) |
| 124 | +{ |
| 125 | +// printf("NotNull"); |
| 126 | + |
| 127 | + if (cloudT_socket->recv(*packet)) |
| 128 | + { |
| 129 | + float r, g, b, a; |
| 130 | + osg::Vec3Array* kinectVertices = new osg::Vec3Array; |
| 131 | +// kinectVertices->empty(); |
| 132 | + osg::Vec3Array* normals = new osg::Vec3Array; |
| 133 | + osg::Vec4Array* kinectColours = new osg::Vec4Array; |
| 134 | + // kinectColours->empty(); |
| 135 | + // cerr << "."; |
| 136 | + //int size = packet->points_size(); |
| 137 | + //printf("Points %i\n",size); |
| 138 | + if(true) |
| 139 | + { |
| 140 | + for (int i = 0; i < packet->points_size(); i++) |
| 141 | + { |
| 142 | +/* |
| 143 | + osg::Vec3f ppos((packet->points(i).x() / 1000) + Skeleton::camPos.x(), |
| 144 | + (packet->points(i).z() / -1000) + Skeleton::camPos.y(), |
| 145 | + (packet->points(i).y() / 1000) + Skeleton::camPos.z()); |
| 146 | +*/ |
| 147 | + osg::Vec3f ppos((packet->points(i).x() / 1000), |
| 148 | + (packet->points(i).z() / -1000), |
| 149 | + (packet->points(i).y() / 1000)); |
| 150 | + kinectVertices->push_back(ppos); |
| 151 | + //useKColor |
| 152 | + if (useKColor) |
| 153 | + { |
| 154 | + r = (packet->points(i).r() / 255.); |
| 155 | + g = (packet->points(i).g() / 255.); |
| 156 | + b = (packet->points(i).b() / 255.); |
| 157 | + a = 1; |
| 158 | + kinectColours->push_back(osg::Vec4f(r, g, b, a)); |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + kinectColours->push_back(getColorRGB(packet->points(i).z())); |
| 163 | + } |
| 164 | + } |
| 165 | +//cerr << kinectVertices->size(); |
| 166 | + |
| 167 | + osg::Geode* kgeode = new osg::Geode(); |
| 168 | + kgeode->setCullingActive(false); |
| 169 | + osg::Geometry* nodeGeom = new osg::Geometry(); |
| 170 | + osg::StateSet* state = nodeGeom->getOrCreateStateSet(); |
| 171 | + nodeGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, kinectVertices->size())); |
| 172 | + osg::VertexBufferObject* vboP = nodeGeom->getOrCreateVertexBufferObject(); |
| 173 | + vboP->setUsage(GL_STREAM_DRAW); |
| 174 | + nodeGeom->setUseDisplayList(true); |
| 175 | + nodeGeom->setUseVertexBufferObjects(true); |
| 176 | + nodeGeom->setVertexArray(kinectVertices); |
| 177 | + nodeGeom->setColorArray(kinectColours); |
| 178 | + nodeGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
| 179 | + kgeode->addDrawable(nodeGeom); |
| 180 | + kgeode->dirtyBound(); |
| 181 | + //if (kinectgrp != NULL) _root->removeChild(kinectgrp); |
| 182 | + kinectgrp->removeChild(0, 1); |
| 183 | + kinectgrp->addChild(kgeode); |
| 184 | + |
| 185 | + |
| 186 | + } |
| 187 | + } |
| 188 | +if(should_quit) |
| 189 | +{ |
| 190 | + pgm1->ref(); |
| 191 | + kinectgrp->removeChild(0, 1); |
| 192 | + _root->removeChild(kinectgrp); |
| 193 | + kinectgrp = NULL; |
| 194 | + |
| 195 | +} |
| 196 | +} |
| 197 | +} |
| 198 | +} |
| 199 | +//When Finished |
| 200 | + _cacheDone = true; |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + std::cerr << "All frames loaded." << std::endl; |
| 206 | + |
| 207 | +} |
| 208 | + |
| 209 | +void CloudManager::quit() |
| 210 | +{ |
| 211 | + |
| 212 | + should_quit = true; |
| 213 | + // pgm1->ref(); |
| 214 | + // kinectgrp->removeChild(0, 1); |
| 215 | + // _root->removeChild(kinectgrp); |
| 216 | + // kinectgrp = NULL; |
| 217 | + |
| 218 | +} |
| 219 | + |
| 220 | +osg::Vec4f CloudManager::getColorRGB(int dist) |
| 221 | +{ |
| 222 | + if (distanceColorMap.count(dist) == 0) // that can be commented out after precomputing completely if the range of Z is known (and it is set on the server side) |
| 223 | + { |
| 224 | + float r, g, b; |
| 225 | + float h = depth_to_hue(minDistHSV, dist, maxDistHSV); |
| 226 | + HSVtoRGB(&r, &g, &b, h, 1, 1); |
| 227 | + distanceColorMap[dist] = osg::Vec4f(r, g, b, 1); |
| 228 | + } |
| 229 | + |
| 230 | + return distanceColorMap[dist]; |
| 231 | +} |
0 commit comments