[2798] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[406] | 2 | /* |
---|
| 3 | * ---------------------------------------------------------------------- |
---|
| 4 | * VolumeRenderer.cpp : VolumeRenderer class for volume visualization |
---|
| 5 | * |
---|
| 6 | * ====================================================================== |
---|
| 7 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
| 8 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
| 9 | * |
---|
[3502] | 10 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
[406] | 11 | * |
---|
| 12 | * See the file "license.terms" for information on usage and |
---|
| 13 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 14 | * ====================================================================== |
---|
| 15 | */ |
---|
[2822] | 16 | #include <stdlib.h> |
---|
[2973] | 17 | #include <float.h> |
---|
[2804] | 18 | |
---|
[2972] | 19 | #include <vector> |
---|
| 20 | |
---|
[2822] | 21 | #include <GL/glew.h> |
---|
| 22 | |
---|
[3492] | 23 | #include <vrmath/Vector3f.h> |
---|
[3630] | 24 | #include <vrmath/Vector4f.h> |
---|
[3492] | 25 | #include <vrmath/Matrix4x4d.h> |
---|
[3630] | 26 | #include <vrmath/BBox.h> |
---|
[3492] | 27 | |
---|
[2804] | 28 | #include "nanovis.h" |
---|
[2822] | 29 | #include "VolumeRenderer.h" |
---|
[3492] | 30 | #include "Plane.h" |
---|
[2822] | 31 | #include "ConvexPolygon.h" |
---|
[3612] | 32 | #include "StdVertexShader.h" |
---|
[884] | 33 | #include "Trace.h" |
---|
[406] | 34 | |
---|
[3611] | 35 | using namespace nv; |
---|
[3492] | 36 | using namespace vrmath; |
---|
| 37 | |
---|
[2974] | 38 | VolumeRenderer::VolumeRenderer() |
---|
[406] | 39 | { |
---|
[2877] | 40 | initShaders(); |
---|
[617] | 41 | |
---|
[884] | 42 | _volumeInterpolator = new VolumeInterpolator(); |
---|
[418] | 43 | } |
---|
| 44 | |
---|
[580] | 45 | VolumeRenderer::~VolumeRenderer() |
---|
| 46 | { |
---|
[3362] | 47 | delete _cutplaneShader; |
---|
[617] | 48 | delete _zincBlendeShader; |
---|
| 49 | delete _regularVolumeShader; |
---|
| 50 | delete _stdVertexShader; |
---|
[884] | 51 | delete _volumeInterpolator; |
---|
[580] | 52 | } |
---|
[418] | 53 | |
---|
| 54 | //initialize the volume shaders |
---|
[2877] | 55 | void VolumeRenderer::initShaders() |
---|
[2822] | 56 | { |
---|
[3612] | 57 | _cutplaneShader = new Shader(); |
---|
[3630] | 58 | _cutplaneShader->loadVertexProgram("cutplane_vp.cg"); |
---|
| 59 | _cutplaneShader->loadFragmentProgram("cutplane_fp.cg"); |
---|
[3362] | 60 | |
---|
[2822] | 61 | //standard vertex program |
---|
[3612] | 62 | _stdVertexShader = new StdVertexShader(); |
---|
[406] | 63 | |
---|
[2822] | 64 | //volume rendering shader: one cubic volume |
---|
[3612] | 65 | _regularVolumeShader = new RegularVolumeShader(); |
---|
[524] | 66 | |
---|
[2822] | 67 | //volume rendering shader: one zincblende orbital volume. |
---|
| 68 | //This shader renders one orbital of the simulation. |
---|
| 69 | //A sim has S, P, D, SS orbitals. thus a full rendering requires 4 zincblende orbital volumes. |
---|
| 70 | //A zincblende orbital volume is decomposed into 2 "interlocking" cubic 4-component volumes and passed to the shader. |
---|
[3630] | 71 | //We render each orbital with independent transfer functions then blend the result. |
---|
[2822] | 72 | // |
---|
| 73 | //The engine is already capable of rendering multiple volumes and combine them. Thus, we just invoke this shader on |
---|
| 74 | //S, P, D and SS orbitals with different transfor functions. The result is a multi-orbital rendering. |
---|
[3612] | 75 | _zincBlendeShader = new ZincBlendeVolumeShader(); |
---|
[406] | 76 | } |
---|
| 77 | |
---|
[1258] | 78 | struct SortElement { |
---|
| 79 | float z; |
---|
[2877] | 80 | int volumeId; |
---|
| 81 | int sliceId; |
---|
[2822] | 82 | |
---|
[2804] | 83 | SortElement(float _z, int _v, int _s) : |
---|
[2877] | 84 | z(_z), |
---|
| 85 | volumeId(_v), |
---|
| 86 | sliceId(_s) |
---|
[2804] | 87 | {} |
---|
[415] | 88 | }; |
---|
| 89 | |
---|
[2877] | 90 | static int sliceSort(const void *a, const void *b) |
---|
[2822] | 91 | { |
---|
| 92 | if ((*((SortElement*)a)).z > (*((SortElement*)b)).z) |
---|
[2804] | 93 | return 1; |
---|
| 94 | else |
---|
| 95 | return -1; |
---|
[415] | 96 | } |
---|
| 97 | |
---|
[1258] | 98 | void |
---|
[2877] | 99 | VolumeRenderer::renderAll() |
---|
[580] | 100 | { |
---|
[1478] | 101 | size_t total_rendered_slices = 0; |
---|
[900] | 102 | |
---|
[2877] | 103 | if (_volumeInterpolator->isStarted()) { |
---|
[1478] | 104 | #ifdef notdef |
---|
[884] | 105 | ani_vol = _volumeInterpolator->getVolume(); |
---|
[1478] | 106 | ani_tf = ani_vol->transferFunction(); |
---|
| 107 | #endif |
---|
[2853] | 108 | TRACE("VOLUME INTERPOLATOR IS STARTED ----------------------------"); |
---|
[884] | 109 | } |
---|
[1478] | 110 | // Determine the volumes that are to be rendered. |
---|
[2804] | 111 | std::vector<Volume *> volumes; |
---|
[3567] | 112 | for (NanoVis::VolumeHashmap::iterator itr = NanoVis::volumeTable.begin(); |
---|
| 113 | itr != NanoVis::volumeTable.end(); ++itr) { |
---|
| 114 | Volume *volume = itr->second; |
---|
| 115 | if (!volume->visible()) { |
---|
[2853] | 116 | continue; // Skip this volume |
---|
| 117 | } |
---|
| 118 | // BE CAREFUL: Set the number of slices to something slightly |
---|
| 119 | // different for each volume. If we have identical volumes at exactly |
---|
| 120 | // the same position with exactly the same number of slices, the |
---|
| 121 | // second volume will overwrite the first, so the first won't appear |
---|
| 122 | // at all. |
---|
[3567] | 123 | volumes.push_back(volume); |
---|
| 124 | volume->numSlices(256 - volumes.size()); |
---|
[1478] | 125 | } |
---|
[884] | 126 | |
---|
[2932] | 127 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 128 | |
---|
[1258] | 129 | //two dimension pointer array |
---|
[2853] | 130 | ConvexPolygon ***polys = new ConvexPolygon**[volumes.size()]; |
---|
[1258] | 131 | //number of actual slices for each volume |
---|
[2932] | 132 | size_t *actual_slices = new size_t[volumes.size()]; |
---|
[3362] | 133 | float *z_steps = new float[volumes.size()]; |
---|
[3630] | 134 | float *sampleRatios = new float[volumes.size()]; |
---|
| 135 | Vector4f *objPlaneS = new Vector4f[volumes.size()]; |
---|
| 136 | Vector4f *objPlaneT = new Vector4f[volumes.size()]; |
---|
| 137 | Vector4f *objPlaneR = new Vector4f[volumes.size()]; |
---|
[884] | 138 | |
---|
[3452] | 139 | TRACE("start loop %d", volumes.size()); |
---|
[1478] | 140 | for (size_t i = 0; i < volumes.size(); i++) { |
---|
[3597] | 141 | Volume *volume = volumes[i]; |
---|
[1478] | 142 | polys[i] = NULL; |
---|
| 143 | actual_slices[i] = 0; |
---|
[1429] | 144 | |
---|
[3597] | 145 | int n_slices = volume->numSlices(); |
---|
| 146 | if (volume->isosurface()) { |
---|
[2853] | 147 | // double the number of slices |
---|
| 148 | n_slices <<= 1; |
---|
| 149 | } |
---|
[2804] | 150 | |
---|
[3630] | 151 | // Get any additional transforms on Volume |
---|
| 152 | Vector3f volPos = volume->getPosition(); |
---|
| 153 | Vector3f volScale = volume->getScale(); |
---|
| 154 | // Get world coords of volume bbox |
---|
| 155 | double x0 = volume->xAxis.min(); |
---|
| 156 | double y0 = volume->yAxis.min(); |
---|
| 157 | double z0 = volume->zAxis.min(); |
---|
| 158 | double x1 = volume->xAxis.max(); |
---|
| 159 | double y1 = volume->yAxis.max(); |
---|
| 160 | double z1 = volume->zAxis.max(); |
---|
| 161 | Vector3f worldMin(x0, y0, z0); |
---|
| 162 | Vector3f worldMax(x1, y1, z1); |
---|
[2804] | 163 | |
---|
[3630] | 164 | float edgeLengthX = volume->xAxis.length(); |
---|
| 165 | float edgeLengthY = volume->yAxis.length(); |
---|
| 166 | float edgeLengthZ = volume->zAxis.length(); |
---|
| 167 | // Texgen planes |
---|
| 168 | objPlaneS[i].set(1./edgeLengthX, 0, 0, -volume->xAxis.min()/edgeLengthX); |
---|
| 169 | objPlaneT[i].set(0, 1./edgeLengthY, 0, -volume->yAxis.min()/edgeLengthY); |
---|
| 170 | objPlaneR[i].set(0, 0, 1./edgeLengthZ, -volume->zAxis.min()/edgeLengthZ); |
---|
[3362] | 171 | |
---|
[3492] | 172 | Matrix4x4d model_view_no_trans, model_view_trans; |
---|
| 173 | Matrix4x4d model_view_no_trans_inverse, model_view_trans_inverse; |
---|
[2804] | 174 | |
---|
[2853] | 175 | //initialize volume plane with world coordinates |
---|
[3492] | 176 | nv::Plane volume_planes[6]; |
---|
[2906] | 177 | volume_planes[0].setCoeffs( 1, 0, 0, -x0); |
---|
[3630] | 178 | volume_planes[1].setCoeffs(-1, 0, 0, x1); |
---|
[2906] | 179 | volume_planes[2].setCoeffs( 0, 1, 0, -y0); |
---|
[3630] | 180 | volume_planes[3].setCoeffs( 0, -1, 0, y1); |
---|
[2906] | 181 | volume_planes[4].setCoeffs( 0, 0, 1, -z0); |
---|
[3630] | 182 | volume_planes[5].setCoeffs( 0, 0, -1, z1); |
---|
[2804] | 183 | |
---|
[3630] | 184 | TRACE("VOL POS: %g %g %g", |
---|
| 185 | volPos.x, volPos.y, volPos.z); |
---|
| 186 | TRACE("VOL SCALE: %g %g %g", |
---|
| 187 | volScale.x, volScale.y, volScale.z); |
---|
| 188 | |
---|
[2853] | 189 | //get modelview matrix with no translation |
---|
[900] | 190 | glPushMatrix(); |
---|
[3630] | 191 | glScalef(volScale.x, volScale.y, volScale.z); |
---|
[2804] | 192 | |
---|
[900] | 193 | glEnable(GL_DEPTH_TEST); |
---|
[2804] | 194 | |
---|
[3492] | 195 | GLdouble mv_no_trans[16]; |
---|
| 196 | glGetDoublev(GL_MODELVIEW_MATRIX, mv_no_trans); |
---|
[2804] | 197 | |
---|
[3492] | 198 | model_view_no_trans = Matrix4x4d(mv_no_trans); |
---|
[900] | 199 | model_view_no_trans_inverse = model_view_no_trans.inverse(); |
---|
[2804] | 200 | |
---|
[900] | 201 | glPopMatrix(); |
---|
[2804] | 202 | |
---|
[2853] | 203 | //get modelview matrix with translation |
---|
[900] | 204 | glPushMatrix(); |
---|
[3362] | 205 | glTranslatef(volPos.x, volPos.y, volPos.z); |
---|
[3630] | 206 | glScalef(volScale.x, volScale.y, volScale.z); |
---|
[3362] | 207 | |
---|
[3492] | 208 | GLdouble mv_trans[16]; |
---|
| 209 | glGetDoublev(GL_MODELVIEW_MATRIX, mv_trans); |
---|
[2804] | 210 | |
---|
[3492] | 211 | model_view_trans = Matrix4x4d(mv_trans); |
---|
[900] | 212 | model_view_trans_inverse = model_view_trans.inverse(); |
---|
[2804] | 213 | |
---|
[3492] | 214 | model_view_trans.print(); |
---|
| 215 | |
---|
[2853] | 216 | //draw volume bounding box with translation (the correct location in |
---|
| 217 | //space) |
---|
[3597] | 218 | if (volume->outline()) { |
---|
[900] | 219 | float olcolor[3]; |
---|
[3597] | 220 | volume->getOutlineColor(olcolor); |
---|
[3630] | 221 | drawBoundingBox(x0, y0, z0, x1, y1, z1, |
---|
| 222 | olcolor[0], olcolor[1], olcolor[2], |
---|
| 223 | 1.5); |
---|
[900] | 224 | } |
---|
| 225 | glPopMatrix(); |
---|
[2804] | 226 | |
---|
[3362] | 227 | // transform volume_planes to eye coordinates. |
---|
| 228 | // Need to transform without translation since we don't want |
---|
| 229 | // to translate plane normals, just rotate them |
---|
[2804] | 230 | for (size_t j = 0; j < 6; j++) { |
---|
[1478] | 231 | volume_planes[j].transform(model_view_no_trans); |
---|
[2853] | 232 | } |
---|
[3630] | 233 | Vector3f eyeMin, eyeMax; |
---|
| 234 | double zNear, zFar; |
---|
| 235 | getEyeSpaceBounds(worldMin, worldMax, |
---|
| 236 | model_view_no_trans, |
---|
| 237 | eyeMin, eyeMax); |
---|
| 238 | zNear = eyeMax.z; |
---|
| 239 | zFar = eyeMin.z; |
---|
[2804] | 240 | |
---|
[2853] | 241 | //compute actual rendering slices |
---|
| 242 | float z_step = fabs(zNear-zFar)/n_slices; |
---|
[3362] | 243 | z_steps[i] = z_step; |
---|
[1478] | 244 | size_t n_actual_slices; |
---|
[2804] | 245 | |
---|
[3597] | 246 | if (volume->dataEnabled()) { |
---|
[3362] | 247 | if (z_step == 0.0f) |
---|
| 248 | n_actual_slices = 1; |
---|
| 249 | else |
---|
| 250 | n_actual_slices = (int)(fabs(zNear-zFar)/z_step + 1); |
---|
[1478] | 251 | polys[i] = new ConvexPolygon*[n_actual_slices]; |
---|
[900] | 252 | } else { |
---|
| 253 | n_actual_slices = 0; |
---|
[1478] | 254 | polys[i] = NULL; |
---|
[900] | 255 | } |
---|
[1478] | 256 | actual_slices[i] = n_actual_slices; |
---|
[2804] | 257 | |
---|
[3362] | 258 | TRACE("near: %g far: %g eye space bounds: (%g,%g)-(%g,%g) z_step: %g slices: %d", |
---|
[3630] | 259 | zNear, zFar, eyeMin.x, eyeMax.x, eyeMin.y, eyeMax.y, z_step, n_actual_slices); |
---|
[2804] | 260 | |
---|
[3630] | 261 | // Compute opacity correction sample ratios |
---|
| 262 | float defDist = z_step == 0.0f ? 1.0 : z_step; |
---|
| 263 | float sampleDistX = (volume->width() > 1) ? edgeLengthX / (volume->width()-1) : defDist; |
---|
| 264 | float sampleDistY = (volume->height() > 1) ? edgeLengthY / (volume->height()-1) : defDist; |
---|
| 265 | float sampleDistZ = (volume->depth() > 1) ? edgeLengthZ / (volume->depth()-1) : defDist; |
---|
| 266 | sampleDistX *= volScale.x; |
---|
| 267 | sampleDistY *= volScale.y; |
---|
| 268 | sampleDistZ *= volScale.z; |
---|
| 269 | BBox voxelIn, voxelOut; |
---|
| 270 | voxelIn.min.set(0,0,0); |
---|
| 271 | voxelIn.max.set(sampleDistX, sampleDistY, sampleDistZ); |
---|
| 272 | getEyeSpaceBounds(voxelIn.min, voxelIn.max, model_view_no_trans, voxelOut.min, voxelOut.max); |
---|
| 273 | sampleRatios[i] = z_step / (voxelOut.max.z - voxelOut.min.z); |
---|
| 274 | |
---|
[3492] | 275 | Vector4f vert1, vert2, vert3, vert4; |
---|
[3362] | 276 | |
---|
[2853] | 277 | // Render cutplanes first with depth test enabled. They will mark the |
---|
| 278 | // image with their depth values. Then we render other volume slices. |
---|
| 279 | // These volume slices will be occluded correctly by the cutplanes and |
---|
| 280 | // vice versa. |
---|
[1825] | 281 | |
---|
[3597] | 282 | for (int j = 0; j < volume->getCutplaneCount(); j++) { |
---|
[3883] | 283 | if (!volume->cutplanesVisible() || !volume->isCutplaneEnabled(j)) { |
---|
[2853] | 284 | continue; |
---|
| 285 | } |
---|
[3630] | 286 | Vector4f texcoord1, texcoord2, texcoord3, texcoord4; |
---|
[3597] | 287 | float offset = volume->getCutplane(j)->offset; |
---|
| 288 | int axis = volume->getCutplane(j)->orient; |
---|
[3362] | 289 | |
---|
| 290 | switch (axis) { |
---|
[3630] | 291 | case CutPlane::X_AXIS: // YZ plane |
---|
| 292 | vert1 = Vector4f(x0 + offset * (x1 - x0), y0, z0, 1); |
---|
| 293 | vert2 = Vector4f(x0 + offset * (x1 - x0), y1, z0, 1); |
---|
| 294 | vert3 = Vector4f(x0 + offset * (x1 - x0), y1, z1, 1); |
---|
| 295 | vert4 = Vector4f(x0 + offset * (x1 - x0), y0, z1, 1); |
---|
| 296 | texcoord1 = Vector4f(offset, 0, 0, 1); |
---|
| 297 | texcoord2 = Vector4f(offset, 1, 0, 1); |
---|
| 298 | texcoord3 = Vector4f(offset, 1, 1, 1); |
---|
| 299 | texcoord4 = Vector4f(offset, 0, 1, 1); |
---|
[3362] | 300 | break; |
---|
[3630] | 301 | case CutPlane::Y_AXIS: // XZ plane |
---|
| 302 | vert1 = Vector4f(x0, y0 + offset * (y1 - y0), z0, 1); |
---|
| 303 | vert2 = Vector4f(x1, y0 + offset * (y1 - y0), z0, 1); |
---|
| 304 | vert3 = Vector4f(x1, y0 + offset * (y1 - y0), z1, 1); |
---|
| 305 | vert4 = Vector4f(x0, y0 + offset * (y1 - y0), z1, 1); |
---|
| 306 | texcoord1 = Vector4f(0, offset, 0, 1); |
---|
| 307 | texcoord2 = Vector4f(1, offset, 0, 1); |
---|
| 308 | texcoord3 = Vector4f(1, offset, 1, 1); |
---|
| 309 | texcoord4 = Vector4f(0, offset, 1, 1); |
---|
[3362] | 310 | break; |
---|
[3630] | 311 | case CutPlane::Z_AXIS: // XY plane |
---|
[3362] | 312 | default: |
---|
[3630] | 313 | vert1 = Vector4f(x0, y0, z0 + offset * (z1 - z0), 1); |
---|
| 314 | vert2 = Vector4f(x1, y0, z0 + offset * (z1 - z0), 1); |
---|
| 315 | vert3 = Vector4f(x1, y1, z0 + offset * (z1 - z0), 1); |
---|
| 316 | vert4 = Vector4f(x0, y1, z0 + offset * (z1 - z0), 1); |
---|
| 317 | texcoord1 = Vector4f(0, 0, offset, 1); |
---|
| 318 | texcoord2 = Vector4f(1, 0, offset, 1); |
---|
| 319 | texcoord3 = Vector4f(1, 1, offset, 1); |
---|
| 320 | texcoord4 = Vector4f(0, 1, offset, 1); |
---|
[3362] | 321 | break; |
---|
[2853] | 322 | } |
---|
[2804] | 323 | |
---|
[3362] | 324 | _cutplaneShader->bind(); |
---|
[3597] | 325 | _cutplaneShader->setFPTextureParameter("volume", volume->textureID()); |
---|
| 326 | _cutplaneShader->setFPTextureParameter("tf", volume->transferFunction()->id()); |
---|
[2804] | 327 | |
---|
[2853] | 328 | glPushMatrix(); |
---|
[3362] | 329 | glTranslatef(volPos.x, volPos.y, volPos.z); |
---|
[3630] | 330 | glScalef(volScale.x, volScale.y, volScale.z); |
---|
[3362] | 331 | _cutplaneShader->setGLStateMatrixVPParameter("modelViewProjMatrix", |
---|
[3612] | 332 | Shader::MODELVIEW_PROJECTION_MATRIX); |
---|
[2853] | 333 | glPopMatrix(); |
---|
[2804] | 334 | |
---|
[2853] | 335 | glEnable(GL_DEPTH_TEST); |
---|
| 336 | glDisable(GL_BLEND); |
---|
[2804] | 337 | |
---|
[3362] | 338 | glBegin(GL_QUADS); |
---|
| 339 | glTexCoord3f(texcoord1.x, texcoord1.y, texcoord1.z); |
---|
| 340 | glVertex3f(vert1.x, vert1.y, vert1.z); |
---|
| 341 | glTexCoord3f(texcoord2.x, texcoord2.y, texcoord2.z); |
---|
| 342 | glVertex3f(vert2.x, vert2.y, vert2.z); |
---|
| 343 | glTexCoord3f(texcoord3.x, texcoord3.y, texcoord3.z); |
---|
| 344 | glVertex3f(vert3.x, vert3.y, vert3.z); |
---|
| 345 | glTexCoord3f(texcoord4.x, texcoord4.y, texcoord4.z); |
---|
| 346 | glVertex3f(vert4.x, vert4.y, vert4.z); |
---|
[2853] | 347 | glEnd(); |
---|
[3362] | 348 | |
---|
[2853] | 349 | glDisable(GL_DEPTH_TEST); |
---|
[3362] | 350 | _cutplaneShader->disableFPTextureParameter("tf"); |
---|
| 351 | _cutplaneShader->disableFPTextureParameter("volume"); |
---|
| 352 | _cutplaneShader->unbind(); |
---|
[2853] | 353 | } //done cutplanes |
---|
[2804] | 354 | |
---|
[3362] | 355 | // Now prepare proxy geometry slices |
---|
[2853] | 356 | |
---|
[2973] | 357 | // Initialize view-aligned quads with eye space bounds of |
---|
| 358 | // volume |
---|
[3630] | 359 | vert1 = Vector4f(eyeMin.x, eyeMin.y, zFar, 1); |
---|
| 360 | vert2 = Vector4f(eyeMax.x, eyeMin.y, zFar, 1); |
---|
| 361 | vert3 = Vector4f(eyeMax.x, eyeMax.y, zFar, 1); |
---|
| 362 | vert4 = Vector4f(eyeMin.x, eyeMax.y, zFar, 1); |
---|
[2804] | 363 | |
---|
[1478] | 364 | size_t counter = 0; |
---|
[2804] | 365 | |
---|
[3362] | 366 | // Transform slices and store them |
---|
[900] | 367 | float slice_z; |
---|
[1478] | 368 | for (size_t j = 0; j < n_actual_slices; j++) { |
---|
[2853] | 369 | slice_z = zFar + j * z_step; //back to front |
---|
[2804] | 370 | |
---|
[900] | 371 | ConvexPolygon *poly = new ConvexPolygon(); |
---|
[1478] | 372 | polys[i][counter] = poly; |
---|
[900] | 373 | counter++; |
---|
[2804] | 374 | |
---|
[900] | 375 | poly->vertices.clear(); |
---|
[2877] | 376 | poly->setId(i); |
---|
[2804] | 377 | |
---|
[2973] | 378 | // Set eye space Z-coordinate of slice |
---|
[900] | 379 | vert1.z = slice_z; |
---|
| 380 | vert2.z = slice_z; |
---|
| 381 | vert3.z = slice_z; |
---|
| 382 | vert4.z = slice_z; |
---|
[2804] | 383 | |
---|
[2877] | 384 | poly->appendVertex(vert1); |
---|
| 385 | poly->appendVertex(vert2); |
---|
| 386 | poly->appendVertex(vert3); |
---|
| 387 | poly->appendVertex(vert4); |
---|
[2804] | 388 | |
---|
| 389 | for (size_t k = 0; k < 6; k++) { |
---|
[3630] | 390 | if (!poly->clip(volume_planes[k], false)) |
---|
[3362] | 391 | break; |
---|
[2853] | 392 | } |
---|
[2804] | 393 | |
---|
[3362] | 394 | if (poly->vertices.size() >= 3) { |
---|
| 395 | poly->transform(model_view_no_trans_inverse); |
---|
| 396 | poly->transform(model_view_trans); |
---|
[2853] | 397 | total_rendered_slices++; |
---|
[3362] | 398 | } |
---|
[900] | 399 | } |
---|
[1258] | 400 | } //iterate all volumes |
---|
[3452] | 401 | TRACE("end loop"); |
---|
[2804] | 402 | |
---|
[1493] | 403 | // We sort all the polygons according to their eye-space depth, from |
---|
| 404 | // farthest to the closest. This step is critical for correct blending |
---|
[2804] | 405 | |
---|
[2853] | 406 | SortElement *slices = (SortElement *) |
---|
| 407 | malloc(sizeof(SortElement) * total_rendered_slices); |
---|
[2804] | 408 | |
---|
[1478] | 409 | size_t counter = 0; |
---|
[2804] | 410 | for (size_t i = 0; i < volumes.size(); i++) { |
---|
| 411 | for (size_t j = 0; j < actual_slices[i]; j++) { |
---|
| 412 | if (polys[i][j]->vertices.size() >= 3) { |
---|
[900] | 413 | slices[counter] = SortElement(polys[i][j]->vertices[0].z, i, j); |
---|
| 414 | counter++; |
---|
| 415 | } |
---|
| 416 | } |
---|
[884] | 417 | } |
---|
[2804] | 418 | |
---|
[1258] | 419 | //sort them |
---|
[2877] | 420 | qsort(slices, total_rendered_slices, sizeof(SortElement), sliceSort); |
---|
[2804] | 421 | |
---|
[1258] | 422 | //Now we are ready to render all the slices from back to front |
---|
[900] | 423 | glEnable(GL_DEPTH_TEST); |
---|
[2932] | 424 | // Non pre-multiplied alpha |
---|
| 425 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
[900] | 426 | glEnable(GL_BLEND); |
---|
[2804] | 427 | |
---|
| 428 | for (size_t i = 0; i < total_rendered_slices; i++) { |
---|
[3630] | 429 | int volIdx = slices[i].volumeId; |
---|
| 430 | int sliceIdx = slices[i].sliceId; |
---|
| 431 | ConvexPolygon *currentSlice = polys[volIdx][sliceIdx]; |
---|
[1478] | 432 | |
---|
[3630] | 433 | Volume *volume = volumes[volIdx]; |
---|
| 434 | Vector3f volPos = volume->getPosition(); |
---|
| 435 | Vector3f volScale = volume->getScale(); |
---|
[2804] | 436 | |
---|
[900] | 437 | glPushMatrix(); |
---|
[3630] | 438 | glTranslatef(volPos.x, volPos.y, volPos.z); |
---|
| 439 | glScalef(volScale.x, volScale.y, volScale.z); |
---|
[2804] | 440 | |
---|
[1493] | 441 | #ifdef notdef |
---|
[3630] | 442 | float z_step = z_steps[volIdx]; |
---|
| 443 | TRACE("shading slice: volume %s addr=%x slice=%d, volume=%d z_step=%g sampleRatio=%g", |
---|
| 444 | volume->name(), volume, sliceIdx, volIdx, z_step, sampleRatios[volIdx]); |
---|
[1493] | 445 | #endif |
---|
[3630] | 446 | activateVolumeShader(volume, objPlaneS[volIdx], objPlaneT[volIdx], objPlaneR[volIdx], false, sampleRatios[volIdx]); |
---|
[900] | 447 | glPopMatrix(); |
---|
[2804] | 448 | |
---|
[900] | 449 | glBegin(GL_POLYGON); |
---|
[3630] | 450 | currentSlice->emit(false); |
---|
[900] | 451 | glEnd(); |
---|
[884] | 452 | |
---|
[2877] | 453 | deactivateVolumeShader(); |
---|
[884] | 454 | } |
---|
[2804] | 455 | |
---|
[2932] | 456 | glPopAttrib(); |
---|
[2804] | 457 | |
---|
[900] | 458 | //Deallocate all the memory used |
---|
[2804] | 459 | for (size_t i = 0; i < volumes.size(); i++) { |
---|
| 460 | for (size_t j = 0; j <actual_slices[i]; j++) { |
---|
[900] | 461 | delete polys[i][j]; |
---|
| 462 | } |
---|
[1258] | 463 | if (polys[i]) { |
---|
[900] | 464 | delete[] polys[i]; |
---|
| 465 | } |
---|
[452] | 466 | } |
---|
[900] | 467 | delete[] polys; |
---|
| 468 | delete[] actual_slices; |
---|
[3362] | 469 | delete[] z_steps; |
---|
[3630] | 470 | delete[] objPlaneS; |
---|
| 471 | delete[] objPlaneT; |
---|
| 472 | delete[] objPlaneR; |
---|
| 473 | delete[] sampleRatios; |
---|
[900] | 474 | free(slices); |
---|
[415] | 475 | } |
---|
| 476 | |
---|
[1478] | 477 | void |
---|
[2877] | 478 | VolumeRenderer::drawBoundingBox(float x0, float y0, float z0, |
---|
| 479 | float x1, float y1, float z1, |
---|
| 480 | float r, float g, float b, |
---|
[3630] | 481 | float lineWidth) |
---|
[406] | 482 | { |
---|
[2932] | 483 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 484 | |
---|
[1028] | 485 | glEnable(GL_DEPTH_TEST); |
---|
| 486 | glDisable(GL_TEXTURE_2D); |
---|
| 487 | glEnable(GL_BLEND); |
---|
[406] | 488 | |
---|
[2932] | 489 | glMatrixMode(GL_MODELVIEW); |
---|
| 490 | glPushMatrix(); |
---|
| 491 | |
---|
[1028] | 492 | glColor4d(r, g, b, 1.0); |
---|
[3630] | 493 | glLineWidth(lineWidth); |
---|
[2804] | 494 | |
---|
[1028] | 495 | glBegin(GL_LINE_LOOP); |
---|
| 496 | { |
---|
[2853] | 497 | glVertex3d(x0, y0, z0); |
---|
| 498 | glVertex3d(x1, y0, z0); |
---|
| 499 | glVertex3d(x1, y1, z0); |
---|
| 500 | glVertex3d(x0, y1, z0); |
---|
[1028] | 501 | } |
---|
| 502 | glEnd(); |
---|
[2804] | 503 | |
---|
[1028] | 504 | glBegin(GL_LINE_LOOP); |
---|
| 505 | { |
---|
[2853] | 506 | glVertex3d(x0, y0, z1); |
---|
| 507 | glVertex3d(x1, y0, z1); |
---|
| 508 | glVertex3d(x1, y1, z1); |
---|
| 509 | glVertex3d(x0, y1, z1); |
---|
[1028] | 510 | } |
---|
| 511 | glEnd(); |
---|
[2853] | 512 | |
---|
[1028] | 513 | glBegin(GL_LINE_LOOP); |
---|
| 514 | { |
---|
[2853] | 515 | glVertex3d(x0, y0, z0); |
---|
| 516 | glVertex3d(x0, y0, z1); |
---|
| 517 | glVertex3d(x0, y1, z1); |
---|
| 518 | glVertex3d(x0, y1, z0); |
---|
[1028] | 519 | } |
---|
| 520 | glEnd(); |
---|
[2804] | 521 | |
---|
[1028] | 522 | glBegin(GL_LINE_LOOP); |
---|
| 523 | { |
---|
[2853] | 524 | glVertex3d(x1, y0, z0); |
---|
| 525 | glVertex3d(x1, y0, z1); |
---|
| 526 | glVertex3d(x1, y1, z1); |
---|
| 527 | glVertex3d(x1, y1, z0); |
---|
[1028] | 528 | } |
---|
| 529 | glEnd(); |
---|
[406] | 530 | |
---|
[1028] | 531 | glPopMatrix(); |
---|
[2932] | 532 | glPopAttrib(); |
---|
[406] | 533 | } |
---|
| 534 | |
---|
[1478] | 535 | void |
---|
[3630] | 536 | VolumeRenderer::activateVolumeShader(Volume *volume, |
---|
| 537 | Vector4f& objPlaneS, |
---|
| 538 | Vector4f& objPlaneT, |
---|
| 539 | Vector4f& objPlaneR, |
---|
| 540 | bool sliceMode, |
---|
[3362] | 541 | float sampleRatio) |
---|
[617] | 542 | { |
---|
[1478] | 543 | //vertex shader |
---|
[3630] | 544 | _stdVertexShader->bind(objPlaneS, objPlaneT, objPlaneR); |
---|
[3597] | 545 | TransferFunction *transferFunc = volume->transferFunction(); |
---|
| 546 | if (volume->volumeType() == Volume::CUBIC) { |
---|
| 547 | _regularVolumeShader->bind(transferFunc->id(), volume, sliceMode, sampleRatio); |
---|
| 548 | } else if (volume->volumeType() == Volume::ZINCBLENDE) { |
---|
| 549 | _zincBlendeShader->bind(transferFunc->id(), volume, sliceMode, sampleRatio); |
---|
[1478] | 550 | } |
---|
[406] | 551 | } |
---|
[2877] | 552 | |
---|
| 553 | void VolumeRenderer::deactivateVolumeShader() |
---|
[580] | 554 | { |
---|
[617] | 555 | _stdVertexShader->unbind(); |
---|
| 556 | _regularVolumeShader->unbind(); |
---|
| 557 | _zincBlendeShader->unbind(); |
---|
[406] | 558 | } |
---|
| 559 | |
---|
[3630] | 560 | void VolumeRenderer::getEyeSpaceBounds(const Vector3f& worldMin, |
---|
| 561 | const Vector3f& worldMax, |
---|
| 562 | const Matrix4x4d& modelViewMatrix, |
---|
| 563 | Vector3f& eyeMin, Vector3f& eyeMax) |
---|
[406] | 564 | { |
---|
[3630] | 565 | BBox bbox(worldMin, worldMax); |
---|
[406] | 566 | |
---|
[3630] | 567 | bbox.transform(bbox, modelViewMatrix); |
---|
[406] | 568 | |
---|
[3630] | 569 | eyeMin = bbox.min; |
---|
| 570 | eyeMax = bbox.max; |
---|
[406] | 571 | } |
---|