[2798] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[3502] | 2 | /* |
---|
| 3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
| 4 | * |
---|
| 5 | */ |
---|
[2835] | 6 | #include "nanovis.h" |
---|
| 7 | |
---|
[3492] | 8 | #include <vrmath/Vector3f.h> |
---|
| 9 | #include <vrmath/Vector4f.h> |
---|
| 10 | |
---|
[1369] | 11 | #include "NvVectorField.h" |
---|
| 12 | #include "NvParticleRenderer.h" |
---|
| 13 | |
---|
[3492] | 14 | using namespace vrmath; |
---|
| 15 | |
---|
[1431] | 16 | NvVectorField::NvVectorField() : |
---|
[2835] | 17 | _vectorFieldId(0), |
---|
[2909] | 18 | _volPtr(NULL), |
---|
[2835] | 19 | _activated(true), |
---|
[2909] | 20 | _origin(0, 0, 0), |
---|
[2835] | 21 | _scaleX(1), |
---|
| 22 | _scaleY(1), |
---|
| 23 | _scaleZ(1), |
---|
[2909] | 24 | _max(1), |
---|
| 25 | _deviceVisible(false) |
---|
[1369] | 26 | { |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | NvVectorField::~NvVectorField() |
---|
| 30 | { |
---|
[2835] | 31 | std::map<std::string, NvParticleRenderer *>::iterator iter; |
---|
[1431] | 32 | for (iter = _particleRendererMap.begin(); |
---|
| 33 | iter != _particleRendererMap.end(); iter++) { |
---|
| 34 | delete (*iter).second; |
---|
| 35 | } |
---|
[1369] | 36 | } |
---|
| 37 | |
---|
[1431] | 38 | void |
---|
[3492] | 39 | NvVectorField::setVectorField(Volume *volPtr, const Vector3f& origin, |
---|
[1431] | 40 | float scaleX, float scaleY, float scaleZ, |
---|
| 41 | float max) |
---|
[1369] | 42 | { |
---|
[1431] | 43 | _volPtr = volPtr; |
---|
[2909] | 44 | _origin = origin; |
---|
[1431] | 45 | _scaleX = scaleX; |
---|
| 46 | _scaleY = scaleY; |
---|
| 47 | _scaleZ = scaleZ; |
---|
| 48 | _max = max; |
---|
[3362] | 49 | _vectorFieldId = volPtr->textureID(); |
---|
[1369] | 50 | } |
---|
| 51 | |
---|
[1431] | 52 | void |
---|
| 53 | NvVectorField::addDeviceShape(const std::string& name, |
---|
| 54 | const NvDeviceShape& shape) |
---|
[1369] | 55 | { |
---|
[1431] | 56 | _shapeMap[name] = shape; |
---|
[1369] | 57 | } |
---|
| 58 | |
---|
| 59 | void NvVectorField::removeDeviceShape(const std::string& name) |
---|
| 60 | { |
---|
[1431] | 61 | std::map<std::string, NvDeviceShape>::iterator iter = _shapeMap.find(name); |
---|
| 62 | if (iter != _shapeMap.end()) _shapeMap.erase(iter); |
---|
[1369] | 63 | } |
---|
| 64 | |
---|
| 65 | void NvVectorField::initialize() |
---|
| 66 | { |
---|
[2835] | 67 | std::map<std::string, NvParticleRenderer *>::iterator iter; |
---|
[1431] | 68 | for (iter = _particleRendererMap.begin(); |
---|
| 69 | iter != _particleRendererMap.end(); iter++) { |
---|
| 70 | if ((*iter).second) (*iter).second->initialize(); |
---|
| 71 | } |
---|
[1370] | 72 | } |
---|
| 73 | |
---|
| 74 | void NvVectorField::reset() |
---|
| 75 | { |
---|
[2835] | 76 | std::map<std::string, NvParticleRenderer *>::iterator iter; |
---|
[1431] | 77 | for (iter = _particleRendererMap.begin(); |
---|
| 78 | iter != _particleRendererMap.end(); iter++) { |
---|
| 79 | if ((*iter).second) (*iter).second->reset(); |
---|
| 80 | } |
---|
[1369] | 81 | } |
---|
| 82 | |
---|
| 83 | void NvVectorField::setPlaneAxis(const std::string& name, int axis) |
---|
| 84 | { |
---|
[2835] | 85 | std::map<std::string, NvParticleRenderer *>::iterator iter = _particleRendererMap.find(name); |
---|
[1431] | 86 | if (iter != _particleRendererMap.end()) { |
---|
| 87 | (*iter).second->setAxis(axis); |
---|
| 88 | } |
---|
| 89 | |
---|
[1369] | 90 | } |
---|
| 91 | |
---|
| 92 | void NvVectorField::setPlanePos(const std::string& name, float pos) |
---|
| 93 | { |
---|
[2835] | 94 | std::map<std::string, NvParticleRenderer *>::iterator iter = _particleRendererMap.find(name); |
---|
[1431] | 95 | if (iter != _particleRendererMap.end()) { |
---|
| 96 | (*iter).second->setPos(pos); |
---|
| 97 | } |
---|
[1369] | 98 | } |
---|
| 99 | |
---|
| 100 | void NvVectorField::addPlane(const std::string& name) |
---|
| 101 | { |
---|
[2835] | 102 | std::map<std::string, NvParticleRenderer *>::iterator iter = _particleRendererMap.find(name); |
---|
| 103 | NvParticleRenderer *renderer = NULL; |
---|
[1431] | 104 | if (iter != _particleRendererMap.end()) { |
---|
| 105 | if ((*iter).second != 0) { |
---|
| 106 | renderer = (*iter).second; |
---|
| 107 | } else { |
---|
[2863] | 108 | renderer = (*iter).second = new NvParticleRenderer(NMESH, NMESH); |
---|
[1369] | 109 | } |
---|
[1431] | 110 | } else { |
---|
[2863] | 111 | renderer = new NvParticleRenderer(NMESH, NMESH); |
---|
[1431] | 112 | _particleRendererMap[name] = renderer; |
---|
| 113 | } |
---|
[2835] | 114 | |
---|
[1515] | 115 | renderer->setVectorField(_vectorFieldId, _origin, _scaleX, _scaleY, _scaleZ, _max); |
---|
[1431] | 116 | if (renderer) { |
---|
| 117 | renderer->initialize(); |
---|
| 118 | } |
---|
[1369] | 119 | } |
---|
| 120 | |
---|
| 121 | void NvVectorField::removePlane(const std::string& name) |
---|
| 122 | { |
---|
[2835] | 123 | std::map<std::string, NvParticleRenderer* >::iterator iter = _particleRendererMap.find(name); |
---|
[1431] | 124 | if (iter != _particleRendererMap.end()) { |
---|
| 125 | delete (*iter).second; |
---|
| 126 | _particleRendererMap.erase(iter); |
---|
| 127 | } |
---|
[1369] | 128 | } |
---|
| 129 | |
---|
| 130 | void NvVectorField::activatePlane(const std::string& name) |
---|
| 131 | { |
---|
[2835] | 132 | std::map<std::string, NvParticleRenderer *>::iterator iter = _particleRendererMap.find(name); |
---|
[1431] | 133 | if (iter != _particleRendererMap.end()) { |
---|
| 134 | (*iter).second->active(true); |
---|
| 135 | } |
---|
[1369] | 136 | } |
---|
| 137 | |
---|
| 138 | void NvVectorField::deactivatePlane(const std::string& name) |
---|
| 139 | { |
---|
[2835] | 140 | std::map<std::string, NvParticleRenderer *>::iterator iter = _particleRendererMap.find(name); |
---|
[1431] | 141 | if (iter != _particleRendererMap.end()) { |
---|
| 142 | (*iter).second->active(false); |
---|
| 143 | } |
---|
[1369] | 144 | } |
---|
| 145 | |
---|
[3492] | 146 | void NvVectorField::setParticleColor(const std::string& name, const vrmath::Vector4f& color) |
---|
[1369] | 147 | { |
---|
[2835] | 148 | std::map<std::string, NvParticleRenderer *>::iterator iter = _particleRendererMap.find(name); |
---|
[1431] | 149 | if (iter != _particleRendererMap.end()) { |
---|
| 150 | (*iter).second->setColor(color); |
---|
| 151 | } |
---|
[1369] | 152 | } |
---|
| 153 | |
---|
| 154 | void NvVectorField::setParticleColor(const std::string& name, float r, float g, float b, float a) |
---|
| 155 | { |
---|
[2835] | 156 | std::map<std::string, NvParticleRenderer *>::iterator iter = _particleRendererMap.find(name); |
---|
[1431] | 157 | if (iter != _particleRendererMap.end()) { |
---|
[3492] | 158 | if ((*iter).second) (*iter).second->setColor(vrmath::Vector4f(r,g,b,a)); |
---|
[1431] | 159 | } |
---|
[1369] | 160 | } |
---|
| 161 | |
---|
| 162 | void NvVectorField::advect() |
---|
| 163 | { |
---|
[2835] | 164 | std::map<std::string, NvParticleRenderer *>::iterator iter; |
---|
[1431] | 165 | for (iter = _particleRendererMap.begin(); |
---|
| 166 | iter != _particleRendererMap.end(); ++iter) { |
---|
| 167 | if ((*iter).second && (*iter).second->active()) |
---|
| 168 | (*iter).second->advect(); |
---|
| 169 | } |
---|
[1369] | 170 | } |
---|
| 171 | |
---|
| 172 | void NvVectorField::render() |
---|
| 173 | { |
---|
[2835] | 174 | std::map<std::string, NvParticleRenderer *>::iterator iter; |
---|
[1431] | 175 | for (iter = _particleRendererMap.begin(); |
---|
| 176 | iter != _particleRendererMap.end(); ++iter) { |
---|
| 177 | if ((*iter).second && (*iter).second->active()) { |
---|
| 178 | (*iter).second->render(); |
---|
[1369] | 179 | } |
---|
[1431] | 180 | } |
---|
| 181 | if (_deviceVisible) { |
---|
| 182 | drawDeviceShape(); |
---|
| 183 | } |
---|
[1369] | 184 | } |
---|
| 185 | |
---|
| 186 | void |
---|
| 187 | NvVectorField::drawDeviceShape() |
---|
| 188 | { |
---|
[2909] | 189 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 190 | |
---|
[3362] | 191 | glEnable(GL_DEPTH_TEST); |
---|
| 192 | glDisable(GL_TEXTURE_2D); |
---|
| 193 | glDisable(GL_BLEND); |
---|
| 194 | |
---|
[2909] | 195 | glMatrixMode(GL_MODELVIEW); |
---|
[1431] | 196 | glPushMatrix(); |
---|
[2909] | 197 | |
---|
[3492] | 198 | Vector3f origin = _volPtr->location(); |
---|
[3362] | 199 | glTranslatef(origin.x, origin.y, origin.z); |
---|
[1369] | 200 | |
---|
[3492] | 201 | Vector3f scale = _volPtr->getPhysicalScaling(); |
---|
[3362] | 202 | glScaled(scale.x, scale.y, scale.z); |
---|
| 203 | |
---|
[3492] | 204 | Vector3f min, max; |
---|
[3362] | 205 | min.x = _volPtr->xAxis.min(); |
---|
| 206 | min.y = _volPtr->yAxis.min(); |
---|
| 207 | min.z = _volPtr->zAxis.min(); |
---|
| 208 | max.x = _volPtr->xAxis.max(); |
---|
| 209 | max.y = _volPtr->yAxis.max(); |
---|
| 210 | max.z = _volPtr->zAxis.max(); |
---|
| 211 | |
---|
[1431] | 212 | std::map<std::string, NvDeviceShape>::iterator iterShape; |
---|
[2909] | 213 | |
---|
[1431] | 214 | for (iterShape = _shapeMap.begin(); iterShape != _shapeMap.end(); |
---|
| 215 | ++iterShape) { |
---|
| 216 | NvDeviceShape& shape = (*iterShape).second; |
---|
[3362] | 217 | |
---|
[1431] | 218 | if (!shape.visible) continue; |
---|
[3362] | 219 | |
---|
| 220 | float x0, y0, z0, x1, y1, z1; |
---|
| 221 | x0 = y0 = z0 = 0.0f; |
---|
| 222 | x1 = y1 = z1 = 0.0f; |
---|
| 223 | if (max.x > min.x) { |
---|
| 224 | x0 = (shape.min.x - min.x) / (max.x - min.x); |
---|
| 225 | x1 = (shape.max.x - min.x) / (max.x - min.x); |
---|
| 226 | } |
---|
| 227 | if (max.y > min.y) { |
---|
| 228 | y0 = (shape.min.y - min.y) / (max.y - min.y); |
---|
| 229 | y1 = (shape.max.y - min.y) / (max.y - min.y); |
---|
| 230 | } |
---|
| 231 | if (max.z > min.z) { |
---|
| 232 | z0 = (shape.min.z - min.z) / (max.z - min.z); |
---|
| 233 | z1 = (shape.max.z - min.z) / (max.z - min.z); |
---|
| 234 | } |
---|
| 235 | |
---|
[3452] | 236 | TRACE("rendering box %g,%g %g,%g %g,%g", x0, x1, y0, y1, z0, z1); |
---|
[3362] | 237 | |
---|
[1431] | 238 | glColor4d(shape.color.x, shape.color.y, shape.color.z, shape.color.w); |
---|
| 239 | glLineWidth(1.2); |
---|
| 240 | glBegin(GL_LINE_LOOP); |
---|
| 241 | { |
---|
| 242 | glVertex3d(x0, y0, z0); |
---|
| 243 | glVertex3d(x1, y0, z0); |
---|
| 244 | glVertex3d(x1, y1, z0); |
---|
| 245 | glVertex3d(x0, y1, z0); |
---|
| 246 | } |
---|
| 247 | glEnd(); |
---|
| 248 | glBegin(GL_LINE_LOOP); |
---|
| 249 | { |
---|
| 250 | glVertex3d(x0, y0, z1); |
---|
| 251 | glVertex3d(x1, y0, z1); |
---|
| 252 | glVertex3d(x1, y1, z1); |
---|
| 253 | glVertex3d(x0, y1, z1); |
---|
| 254 | } |
---|
| 255 | glEnd(); |
---|
| 256 | |
---|
| 257 | glBegin(GL_LINE_LOOP); |
---|
| 258 | { |
---|
| 259 | glVertex3d(x0, y0, z0); |
---|
| 260 | glVertex3d(x0, y0, z1); |
---|
| 261 | glVertex3d(x0, y1, z1); |
---|
| 262 | glVertex3d(x0, y1, z0); |
---|
| 263 | } |
---|
| 264 | glEnd(); |
---|
| 265 | |
---|
| 266 | glBegin(GL_LINE_LOOP); |
---|
| 267 | { |
---|
| 268 | glVertex3d(x1, y0, z0); |
---|
| 269 | glVertex3d(x1, y0, z1); |
---|
| 270 | glVertex3d(x1, y1, z1); |
---|
| 271 | glVertex3d(x1, y1, z0); |
---|
| 272 | } |
---|
| 273 | glEnd(); |
---|
| 274 | } |
---|
[1369] | 275 | |
---|
[1431] | 276 | glPopMatrix(); |
---|
[2909] | 277 | glPopAttrib(); |
---|
[1369] | 278 | } |
---|
| 279 | |
---|
| 280 | void |
---|
| 281 | NvVectorField::activateDeviceShape(const std::string& name) |
---|
| 282 | { |
---|
[1431] | 283 | std::map<std::string, NvDeviceShape>::iterator iter = _shapeMap.find(name); |
---|
| 284 | if (iter != _shapeMap.end()) { |
---|
| 285 | (*iter).second.visible = true; |
---|
| 286 | } |
---|
[1369] | 287 | } |
---|
| 288 | |
---|
| 289 | void |
---|
| 290 | NvVectorField::deactivateDeviceShape(const std::string& name) |
---|
| 291 | { |
---|
[1431] | 292 | std::map<std::string, NvDeviceShape>::iterator iter = _shapeMap.find(name); |
---|
| 293 | if (iter != _shapeMap.end()) { |
---|
| 294 | (*iter).second.visible = false; |
---|
| 295 | } |
---|
[1369] | 296 | } |
---|