[3567] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
| 2 | /* |
---|
| 3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
| 4 | * |
---|
| 5 | * Authors: |
---|
| 6 | * Wei Qiao <qiaow@purdue.edu> |
---|
| 7 | * Insoo Woo <iwoo@purdue.edu> |
---|
| 8 | * George A. Howlett <gah@purdue.edu> |
---|
| 9 | * Leif Delgass <ldelgass@purdue.edu> |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | #include <float.h> |
---|
| 13 | #include <assert.h> |
---|
| 14 | |
---|
| 15 | #include <vrmath/Vector3f.h> |
---|
| 16 | #include <vrmath/Vector4f.h> |
---|
| 17 | #include <vrmath/Matrix4x4d.h> |
---|
| 18 | |
---|
| 19 | #include "FlowBox.h" |
---|
| 20 | #include "Volume.h" |
---|
| 21 | #include "Trace.h" |
---|
| 22 | |
---|
[3611] | 23 | using namespace nv; |
---|
[3567] | 24 | using namespace vrmath; |
---|
| 25 | |
---|
| 26 | FlowBox::FlowBox(const char *name) : |
---|
| 27 | _name(name) |
---|
| 28 | { |
---|
| 29 | _sv.isHidden = false; |
---|
| 30 | _sv.corner1.x = 0.0f; |
---|
| 31 | _sv.corner1.y = 0.0f; |
---|
| 32 | _sv.corner1.z = 0.0f; |
---|
| 33 | _sv.corner2.x = 1.0f; |
---|
| 34 | _sv.corner2.y = 1.0f; |
---|
| 35 | _sv.corner2.z = 1.0f; |
---|
| 36 | _sv.lineWidth = 1.2f; |
---|
| 37 | _sv.color.r = _sv.color.b = _sv.color.g = _sv.color.a = 1.0f; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | FlowBox::~FlowBox() |
---|
| 41 | { |
---|
| 42 | TRACE("Freeing switches"); |
---|
| 43 | Rappture::FreeSwitches(_switches, &_sv, 0); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void |
---|
| 47 | FlowBox::getWorldSpaceBounds(Vector3f& bboxMin, |
---|
| 48 | Vector3f& bboxMax, |
---|
| 49 | const Volume *vol) const |
---|
| 50 | { |
---|
| 51 | bboxMin.set(FLT_MAX, FLT_MAX, FLT_MAX); |
---|
| 52 | bboxMax.set(-FLT_MAX, -FLT_MAX, -FLT_MAX); |
---|
| 53 | |
---|
| 54 | Vector3f origin = vol->location(); |
---|
| 55 | Vector3f scale = vol->getPhysicalScaling(); |
---|
| 56 | |
---|
| 57 | Matrix4x4d mat; |
---|
| 58 | mat.makeTranslation(origin); |
---|
| 59 | Matrix4x4d mat2; |
---|
| 60 | mat2.makeScale(scale); |
---|
| 61 | |
---|
| 62 | mat.multiply(mat2); |
---|
| 63 | |
---|
| 64 | Vector3f min, max; |
---|
| 65 | min.x = vol->xAxis.min(); |
---|
| 66 | min.y = vol->yAxis.min(); |
---|
| 67 | min.z = vol->zAxis.min(); |
---|
| 68 | max.x = vol->xAxis.max(); |
---|
| 69 | max.y = vol->yAxis.max(); |
---|
| 70 | max.z = vol->zAxis.max(); |
---|
| 71 | |
---|
| 72 | float x0, y0, z0, x1, y1, z1; |
---|
| 73 | x0 = y0 = z0 = 0.0f; |
---|
| 74 | x1 = y1 = z1 = 0.0f; |
---|
| 75 | if (max.x > min.x) { |
---|
| 76 | x0 = (_sv.corner1.x - min.x) / (max.x - min.x); |
---|
| 77 | x1 = (_sv.corner2.x - min.x) / (max.x - min.x); |
---|
| 78 | } |
---|
| 79 | if (max.y > min.y) { |
---|
| 80 | y0 = (_sv.corner1.y - min.y) / (max.y - min.y); |
---|
| 81 | y1 = (_sv.corner2.y - min.y) / (max.y - min.y); |
---|
| 82 | } |
---|
| 83 | if (max.z > min.z) { |
---|
| 84 | z0 = (_sv.corner1.z - min.z) / (max.z - min.z); |
---|
| 85 | z1 = (_sv.corner2.z - min.z) / (max.z - min.z); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | TRACE("Box model bounds: (%g,%g,%g) - (%g,%g,%g)", |
---|
| 89 | x0, y0, z0, x1, y1, z1); |
---|
| 90 | |
---|
| 91 | Vector3f modelMin(x0, y0, z0); |
---|
| 92 | Vector3f modelMax(x1, y1, z1); |
---|
| 93 | |
---|
| 94 | Vector4f bvert[8]; |
---|
| 95 | bvert[0] = Vector4f(modelMin.x, modelMin.y, modelMin.z, 1); |
---|
| 96 | bvert[1] = Vector4f(modelMax.x, modelMin.y, modelMin.z, 1); |
---|
| 97 | bvert[2] = Vector4f(modelMin.x, modelMax.y, modelMin.z, 1); |
---|
| 98 | bvert[3] = Vector4f(modelMin.x, modelMin.y, modelMax.z, 1); |
---|
| 99 | bvert[4] = Vector4f(modelMax.x, modelMax.y, modelMin.z, 1); |
---|
| 100 | bvert[5] = Vector4f(modelMax.x, modelMin.y, modelMax.z, 1); |
---|
| 101 | bvert[6] = Vector4f(modelMin.x, modelMax.y, modelMax.z, 1); |
---|
| 102 | bvert[7] = Vector4f(modelMax.x, modelMax.y, modelMax.z, 1); |
---|
| 103 | |
---|
| 104 | for (int i = 0; i < 8; i++) { |
---|
| 105 | Vector4f worldVert = mat.transform(bvert[i]); |
---|
| 106 | if (worldVert.x < bboxMin.x) bboxMin.x = worldVert.x; |
---|
| 107 | if (worldVert.x > bboxMax.x) bboxMax.x = worldVert.x; |
---|
| 108 | if (worldVert.y < bboxMin.y) bboxMin.y = worldVert.y; |
---|
| 109 | if (worldVert.y > bboxMax.y) bboxMax.y = worldVert.y; |
---|
| 110 | if (worldVert.z < bboxMin.z) bboxMin.z = worldVert.z; |
---|
| 111 | if (worldVert.z > bboxMax.z) bboxMax.z = worldVert.z; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | TRACE("Box world bounds: (%g,%g,%g) - (%g,%g,%g)", |
---|
| 115 | bboxMin.x, bboxMin.y, bboxMin.z, |
---|
| 116 | bboxMax.x, bboxMax.y, bboxMax.z); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | void |
---|
| 120 | FlowBox::render(Volume *vol) |
---|
| 121 | { |
---|
| 122 | TRACE("Box: '%s'", _name.c_str()); |
---|
| 123 | |
---|
| 124 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 125 | |
---|
| 126 | glEnable(GL_DEPTH_TEST); |
---|
| 127 | glDisable(GL_TEXTURE_2D); |
---|
| 128 | glDisable(GL_BLEND); |
---|
| 129 | |
---|
| 130 | glMatrixMode(GL_MODELVIEW); |
---|
| 131 | glPushMatrix(); |
---|
| 132 | |
---|
| 133 | Vector3f origin = vol->location(); |
---|
| 134 | glTranslatef(origin.x, origin.y, origin.z); |
---|
| 135 | |
---|
| 136 | Vector3f scale = vol->getPhysicalScaling(); |
---|
| 137 | glScalef(scale.x, scale.y, scale.z); |
---|
| 138 | |
---|
| 139 | Vector3f min, max; |
---|
| 140 | min.x = vol->xAxis.min(); |
---|
| 141 | min.y = vol->yAxis.min(); |
---|
| 142 | min.z = vol->zAxis.min(); |
---|
| 143 | max.x = vol->xAxis.max(); |
---|
| 144 | max.y = vol->yAxis.max(); |
---|
| 145 | max.z = vol->zAxis.max(); |
---|
| 146 | |
---|
| 147 | TRACE("box is %g,%g %g,%g %g,%g", |
---|
| 148 | _sv.corner1.x, _sv.corner2.x, |
---|
| 149 | _sv.corner1.y, _sv.corner2.y, |
---|
| 150 | _sv.corner1.z, _sv.corner2.z); |
---|
| 151 | TRACE("world is %g,%g %g,%g %g,%g", |
---|
| 152 | min.x, max.x, min.y, max.y, min.z, max.z); |
---|
| 153 | |
---|
| 154 | float x0, y0, z0, x1, y1, z1; |
---|
| 155 | x0 = y0 = z0 = 0.0f; |
---|
| 156 | x1 = y1 = z1 = 0.0f; |
---|
| 157 | if (max.x > min.x) { |
---|
| 158 | x0 = (_sv.corner1.x - min.x) / (max.x - min.x); |
---|
| 159 | x1 = (_sv.corner2.x - min.x) / (max.x - min.x); |
---|
| 160 | } |
---|
| 161 | if (max.y > min.y) { |
---|
| 162 | y0 = (_sv.corner1.y - min.y) / (max.y - min.y); |
---|
| 163 | y1 = (_sv.corner2.y - min.y) / (max.y - min.y); |
---|
| 164 | } |
---|
| 165 | if (max.z > min.z) { |
---|
| 166 | z0 = (_sv.corner1.z - min.z) / (max.z - min.z); |
---|
| 167 | z1 = (_sv.corner2.z - min.z) / (max.z - min.z); |
---|
| 168 | } |
---|
| 169 | TRACE("box bounds: %g,%g %g,%g %g,%g", |
---|
| 170 | x0, x1, y0, y1, z0, z1); |
---|
| 171 | |
---|
| 172 | glColor4d(_sv.color.r, _sv.color.g, _sv.color.b, _sv.color.a); |
---|
| 173 | glLineWidth(_sv.lineWidth); |
---|
| 174 | glBegin(GL_LINE_LOOP); |
---|
| 175 | { |
---|
| 176 | glVertex3d(x0, y0, z0); |
---|
| 177 | glVertex3d(x1, y0, z0); |
---|
| 178 | glVertex3d(x1, y1, z0); |
---|
| 179 | glVertex3d(x0, y1, z0); |
---|
| 180 | } |
---|
| 181 | glEnd(); |
---|
| 182 | glBegin(GL_LINE_LOOP); |
---|
| 183 | { |
---|
| 184 | glVertex3d(x0, y0, z1); |
---|
| 185 | glVertex3d(x1, y0, z1); |
---|
| 186 | glVertex3d(x1, y1, z1); |
---|
| 187 | glVertex3d(x0, y1, z1); |
---|
| 188 | } |
---|
| 189 | glEnd(); |
---|
| 190 | |
---|
| 191 | glBegin(GL_LINE_LOOP); |
---|
| 192 | { |
---|
| 193 | glVertex3d(x0, y0, z0); |
---|
| 194 | glVertex3d(x0, y0, z1); |
---|
| 195 | glVertex3d(x0, y1, z1); |
---|
| 196 | glVertex3d(x0, y1, z0); |
---|
| 197 | } |
---|
| 198 | glEnd(); |
---|
| 199 | |
---|
| 200 | glBegin(GL_LINE_LOOP); |
---|
| 201 | { |
---|
| 202 | glVertex3d(x1, y0, z0); |
---|
| 203 | glVertex3d(x1, y0, z1); |
---|
| 204 | glVertex3d(x1, y1, z1); |
---|
| 205 | glVertex3d(x1, y1, z0); |
---|
| 206 | } |
---|
| 207 | glEnd(); |
---|
| 208 | |
---|
| 209 | glPopMatrix(); |
---|
| 210 | glPopAttrib(); |
---|
| 211 | |
---|
| 212 | assert(CheckGL(AT)); |
---|
| 213 | } |
---|