00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include <OSGGL.h>
00047
00048 #include <OSGConfig.h>
00049 #include <OSGAction.h>
00050 #include <OSGTypedFunctors.h>
00051
00052 #include <OSGCamera.h>
00053
00054 #include <OSGDrawAction.h>
00055 #include <OSGRenderAction.h>
00056 #include <OSGIntersectAction.h>
00057 #include <OSGWindow.h>
00058 #include <OSGGLEXT.h>
00059
00060 #include "OSGSlices.h"
00061
00062 OSG_USING_NAMESPACE
00063
00067 UInt32 Slices::_arbMultitexture = Window::invalidFunctionID;
00068 UInt32 Slices::_funcMultiTexCoord3fARB = Window::invalidFunctionID;
00069
00070 typedef void (OSG_APIENTRY * PFNGLMULTITEXCOORD3FARBPROC)(GLenum target,
00071 GLfloat s,
00072 GLfloat t,
00073 GLfloat r);
00074
00075
00076
00078
00079 Slices::Slices(void) :
00080 Inherited()
00081 {
00082 _arbMultitexture =
00083 Window::registerExtension("GL_ARB_multitexture");
00084
00085 _funcMultiTexCoord3fARB =
00086 Window::registerFunction(OSG_DLSYM_UNDERSCORE"glMultiTexCoord3fARB",
00087 _arbMultitexture);
00088 }
00089
00091
00092 Slices::Slices(const Slices &source) :
00093 Inherited(source)
00094 {
00095 }
00096
00098
00099 Slices::~Slices(void)
00100 {
00101 subRefCP(_sfMaterial.getValue());
00102 }
00103
00104
00105
00107
00108 void Slices::initMethod (void)
00109 {
00110 DrawAction::registerEnterDefault( getClassType(),
00111 osgTypedMethodFunctor2BaseCPtrRef<Action::ResultE,
00112 MaterialDrawablePtr ,
00113 CNodePtr ,
00114 Action *>(&MaterialDrawable::drawActionHandler));
00115
00116 RenderAction::registerEnterDefault( getClassType(),
00117 osgTypedMethodFunctor2BaseCPtrRef<Action::ResultE,
00118 MaterialDrawablePtr ,
00119 CNodePtr ,
00120 Action *>(&MaterialDrawable::renderActionHandler));
00121
00122 IntersectAction::registerEnterDefault( getClassType(),
00123 osgTypedMethodFunctor2BaseCPtrRef<Action::ResultE,
00124 SlicesPtr ,
00125 CNodePtr ,
00126 Action *>(&Slices::intersect));
00127
00128
00129 }
00130
00132
00133 void Slices::changed(BitVector whichField,
00134 UInt32 origin )
00135 {
00136 if(whichField & MaterialFieldMask)
00137 {
00138 if(origin & ChangedOrigin::Abstract)
00139 {
00140 if(origin & ChangedOrigin::AbstrIncRefCount)
00141 {
00142 addRefCP(_sfMaterial.getValue());
00143 }
00144 else
00145 {
00146 MaterialPtr pMat = _sfMaterial.getValue();
00147
00148 _sfMaterial.setValue(NullFC);
00149
00150 setMaterial(pMat);
00151 }
00152 }
00153 }
00154
00155 if(whichField & SizeFieldMask)
00156 {
00157 initEdgeVec();
00158 invalidateVolume();
00159 }
00160
00161 }
00162
00164
00165 void Slices::dump( UInt32 ,
00166 const BitVector ) const
00167 {
00168 SLOG << "Dump Slices NI" << std::endl;
00169 }
00170
00171 Action::ResultE Slices::intersect(Action * OSG_CHECK_ARG(action) )
00172 {
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 return Action::Continue;
00199 }
00200
00201 Action::ResultE Slices::drawPrimitives(DrawActionBase *action)
00202 {
00203 Matrix camera,toworld;
00204 UInt32 triCount, vertexCount;
00205 Vec3f planeNormal(0,0,1);
00206 StatCollector *coll = action->getStatistics();
00207 StatIntElem *el = 0;
00208
00209 RenderAction *ra = dynamic_cast<RenderAction *>(action);
00210
00211 camera = action->getCameraToWorld();
00212
00213 if(ra != NULL)
00214 {
00215 toworld = ra->top_matrix();
00216 }
00217 else
00218 {
00219 action->getActNode()->getToWorld(toworld);
00220 }
00221
00222
00223 toworld[0].normalize();
00224 toworld[1].normalize();
00225 toworld[2].normalize();
00226 toworld.invert();
00227 camera.multLeft(toworld);
00228
00229
00230 camera.multMatrixVec(planeNormal,planeNormal);
00231 planeNormal.normalize();
00232
00233 drawSlices(action->getWindow(), planeNormal,triCount,vertexCount);
00234
00235 if (coll)
00236 {
00237 if((el = coll->getElem(Drawable::statNTriangles,false)))
00238 el->add(triCount);
00239 if((el = coll->getElem(Drawable::statNVertices,false)))
00240 el->add(vertexCount);
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 return Action::Continue;
00263 }
00264
00265 void Slices::adjustVolume( Volume & volume )
00266 {
00267 Vec3f v(_sfSize.getValue());
00268 Pnt3f p(v[0],v[1],v[2]);
00269
00270 volume.setValid();
00271 volume.setEmpty();
00272
00273 p /= 2;
00274 volume.extendBy ( p );
00275
00276 p.negate();
00277 volume.extendBy ( p );
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 void Slices::initEdgeVec( void )
00289 {
00290 UInt32 i;
00291 std::vector<Pnt3f> & p(_pointVec);
00292 const Vec3f size(getSize());
00293
00294 FDEBUG (("Run Slices::initEdgeVec()\n"));
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 _pointVec.resize(8);
00310
00311 p[0][0] = p[3][0] = p[4][0] = p[7][0] = -0.5f * size.x();
00312 p[1][0] = p[2][0] = p[5][0] = p[6][0] = 0.5f * size.x();
00313 p[0][1] = p[1][1] = p[2][1] = p[3][1] = -0.5f * size.y();
00314 p[4][1] = p[5][1] = p[6][1] = p[7][1] = 0.5f * size.y();
00315 p[0][2] = p[1][2] = p[4][2] = p[5][2] = -0.5f * size.z();
00316 p[2][2] = p[3][2] = p[6][2] = p[7][2] = 0.5f * size.z();
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 _edgeVec.resize(12);
00333
00334 _edgeVec[0].pointIndexA = 0;
00335 _edgeVec[0].pointIndexB = 1;
00336 _edgeVec[0].edgeTag = 5;
00337
00338 _edgeVec[1].pointIndexA = 1;
00339 _edgeVec[1].pointIndexB = 2;
00340 _edgeVec[1].edgeTag = 36;
00341
00342 _edgeVec[2].pointIndexA = 2;
00343 _edgeVec[2].pointIndexB = 3;
00344 _edgeVec[2].edgeTag = 6;
00345
00346 _edgeVec[3].pointIndexA = 3;
00347 _edgeVec[3].pointIndexB = 0;
00348 _edgeVec[3].edgeTag = 20;
00349
00350 _edgeVec[4].pointIndexA = 4;
00351 _edgeVec[4].pointIndexB = 5;
00352 _edgeVec[4].edgeTag = 9;
00353
00354 _edgeVec[5].pointIndexA = 5;
00355 _edgeVec[5].pointIndexB = 6;
00356 _edgeVec[5].edgeTag = 40;
00357
00358 _edgeVec[6].pointIndexA = 6;
00359 _edgeVec[6].pointIndexB = 7;
00360 _edgeVec[6].edgeTag = 10;
00361
00362 _edgeVec[7].pointIndexA = 7;
00363 _edgeVec[7].pointIndexB = 4;
00364 _edgeVec[7].edgeTag = 24;
00365
00366 _edgeVec[8].pointIndexA = 0;
00367 _edgeVec[8].pointIndexB = 4;
00368 _edgeVec[8].edgeTag = 17;
00369
00370 _edgeVec[9].pointIndexA = 1;
00371 _edgeVec[9].pointIndexB = 5;
00372 _edgeVec[9].edgeTag = 33;
00373
00374 _edgeVec[10].pointIndexA = 2;
00375 _edgeVec[10].pointIndexB = 6;
00376 _edgeVec[10].edgeTag = 34;
00377
00378 _edgeVec[11].pointIndexA = 3;
00379 _edgeVec[11].pointIndexB = 7;
00380 _edgeVec[11].edgeTag = 18;
00381
00382
00383 for (i = 0; i < 12; i++)
00384 _edgeVec[i].line.setValue( _pointVec[_edgeVec[i].pointIndexA],
00385 _pointVec[_edgeVec[i].pointIndexB]);
00386 }
00387
00388 UInt32 Slices::createSlice ( const OSG::Plane &plane, Slice &slice )
00389 {
00390 UInt32 i,j,k, lastButOne;
00391 Vec3f v1, v2, winding;
00392 bool pointInHalfSpace[8];
00393 unsigned swapCount = 0;
00394
00395
00396 for(i = 0; i < 8; i++)
00397 pointInHalfSpace[i] = plane.isInHalfSpace(_pointVec[i]);
00398
00399
00400 for(slice.numOfIntersection = i = 0; i < 12; i++)
00401 {
00402 if((pointInHalfSpace[_edgeVec[i].pointIndexA] !=
00403 pointInHalfSpace[_edgeVec[i].pointIndexB] ))
00404 {
00405 plane.intersect(_edgeVec[i].line,
00406 slice.pointVec[slice.numOfIntersection] );
00407 slice.edgeVec[slice.numOfIntersection] = i;
00408 slice.numOfIntersection++;
00409 }
00410 }
00411
00412 if (slice.numOfIntersection > 2)
00413 {
00414 lastButOne = slice.numOfIntersection - 2;
00415 i=j=k=0;
00416
00417
00418 for (; j++ <= lastButOne; i++)
00419 {
00420
00421 while((j <= lastButOne) &&
00422 ((_edgeVec[slice.edgeVec[i]].edgeTag &
00423 _edgeVec[slice.edgeVec[j]].edgeTag ) != 0))
00424 {
00425 i++;
00426 j++;
00427 }
00428
00429
00430 if (j <= lastButOne)
00431 {
00432
00433 k = j+1;
00434 while((k <= lastButOne) &&
00435 ((_edgeVec[slice.edgeVec[i]].edgeTag &
00436 _edgeVec[slice.edgeVec[k]].edgeTag ) == 0))
00437 {
00438 k++;
00439 }
00440
00441
00442 osgSwap (slice.edgeVec[j], slice.edgeVec[k]);
00443 osgSwap (slice.pointVec[j], slice.pointVec[k]);
00444 swapCount++;
00445 }
00446 }
00447
00448
00449
00450 v1 = slice.pointVec[1];
00451 v1 -= slice.pointVec[0];
00452 v2 = slice.pointVec[2];
00453 v2 -= slice.pointVec[0];
00454 winding = v1.cross(v2);
00455
00456 slice.ccw = ( fabs (winding.x() + plane.getNormal().x())
00457 >= fabs(plane.getNormal().x()) );
00458 }
00459
00460 return slice.numOfIntersection;
00461 }
00462
00463 void Slices::drawSlices(Window *win, const Vec3f &planeNormal,
00464 UInt32 & triCount, UInt32 & vertexCount)
00465 {
00466 bool has_multitexture = win->hasExtension(_arbMultitexture);
00467
00468 PFNGLMULTITEXCOORD3FARBPROC multiTexCoord3f = NULL;
00469 if(has_multitexture)
00470 multiTexCoord3f = (PFNGLMULTITEXCOORD3FARBPROC)
00471 win->getFunction(_funcMultiTexCoord3fARB);
00472
00473 Real32 hsx = getSize().x()/2, hsy = getSize().y()/2, hsz = getSize().z()/2;
00474 Real32 ssx = 1/getSize().x(), ssy = 1/getSize().y(), ssz = 1/getSize().z();
00475 Slice slice;
00476 Plane plane;
00477 Real32 distance, sliceDistance, volumeDiagonal = getSize().length();
00478 Int32 i, si, numOfSlices;
00479 Vec3f texPos0, texPos1, texPos2, texOff, texSliceNormal;
00480
00481 if(_edgeVec.empty())
00482 initEdgeVec();
00483
00484 sliceDistance = getSliceDistance();
00485 numOfSlices = int (volumeDiagonal / sliceDistance) + 1;
00486 distance = - volumeDiagonal / 2;
00487 texOff = planeNormal;
00488 texOff.normalize();
00489 texOff *= sliceDistance * 0.5;
00490
00491 texSliceNormal.setValues(planeNormal.x() * sliceDistance * ssx,
00492 planeNormal.y() * sliceDistance * ssy,
00493 planeNormal.z() * sliceDistance * ssz );
00494
00495 triCount = 0;
00496 vertexCount = 0;
00497
00498 if (numOfSlices)
00499 {
00500 glNormal3fv ( planeNormal.getValues() );
00501
00502 for(si = 0; si < numOfSlices; si++)
00503 {
00504 plane.set ( planeNormal, distance += sliceDistance );
00505
00506 if(createSlice(plane,slice) > 2)
00507 {
00508 triCount += slice.numOfIntersection - 2;
00509 vertexCount += slice.numOfIntersection;
00510
00511 ::glBegin(GL_POLYGON);
00512 if(slice.ccw)
00513 {
00514 for(i = 0; i < slice.numOfIntersection; i++)
00515 {
00516 texPos0 = texPos1 = texPos2 = slice.pointVec[i];
00517 texPos1 += texOff;
00518 texPos2 -= texOff;
00519
00520 texSliceNormal = texPos1;
00521 texSliceNormal -= texPos2;
00522
00523 glTexCoord3f((hsx + texPos0.x()) * ssx,
00524 (hsy + texPos0.y()) * ssy,
00525 (hsz + texPos0.z()) * ssz );
00526
00527 if(has_multitexture)
00528 {
00529 multiTexCoord3f(GL_TEXTURE1_ARB,
00530 (hsx + texPos1.x()) * ssx,
00531 (hsy + texPos1.y()) * ssy,
00532 (hsz + texPos1.z()) * ssz );
00533
00534 multiTexCoord3f(GL_TEXTURE2_ARB,
00535 (hsx + texPos2.x()) * ssx,
00536 (hsy + texPos2.y()) * ssy,
00537 (hsz + texPos2.z()) * ssz );
00538 }
00539 glVertex3fv ( slice.pointVec[i].getValues() );
00540 }
00541 }
00542 else
00543 {
00544 for(i = slice.numOfIntersection - 1; i >= 0; i--)
00545 {
00546 texPos0 = texPos1 = texPos2 = slice.pointVec[i];
00547 texPos1 += texOff;
00548 texPos2 -= texOff;
00549
00550 glTexCoord3f ((hsx + texPos0.x()) * ssx,
00551 (hsy + texPos0.y()) * ssy,
00552 (hsz + texPos0.z()) * ssz );
00553
00554 if(has_multitexture)
00555 {
00556 multiTexCoord3f(GL_TEXTURE1_ARB,
00557 (hsx + texPos1.x()) * ssx,
00558 (hsy + texPos1.y()) * ssy,
00559 (hsz + texPos1.z()) * ssz );
00560
00561 multiTexCoord3f(GL_TEXTURE2_ARB,
00562 (hsx + texPos2.x()) * ssx,
00563 (hsy + texPos2.y()) * ssy,
00564 (hsz + texPos2.z()) * ssz );
00565 }
00566 glVertex3fv ( slice.pointVec[i].getValues() );
00567 }
00568 }
00569 ::glEnd();
00570 }
00571 }
00572 }
00573 }
00574
00575
00576
00577
00578 #ifdef OSG_SGI_CC
00579 #pragma set woff 1174
00580 #endif
00581
00582 #ifdef OSG_LINUX_ICC
00583 #pragma warning( disable : 177 )
00584 #endif
00585
00586 namespace
00587 {
00588 static Char8 cvsid_cpp [] = "@(#)$Id: FCTemplate_cpp.h,v 1.12 2002/04/30 09:29:13 vossg Exp $";
00589 static Char8 cvsid_hpp [] = OSGSLICESBASE_HEADER_CVSID;
00590 static Char8 cvsid_inl [] = OSGSLICESBASE_INLINE_CVSID;
00591
00592 static Char8 cvsid_fields_hpp[] = OSGSLICESFIELDS_HEADER_CVSID;
00593 }
00594
00595 #ifdef __sgi
00596 #pragma reset woff 1174
00597 #endif
00598
00599