/usr/include/crystalspace-2.0/csgfx/trianglestream.h is in libcrystalspace-dev 2.0+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | /*
Copyright (C) 2006 by Jorrit Tyberghein
(C) 2006 by Frank Richter
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __CS_TRIANGLESTREAM_H__
#define __CS_TRIANGLESTREAM_H__
/**\file
* Triangle extraction from index buffers
*/
#include "csgeom/tri.h"
#include "ivideo/graph3d.h"
#include "ivideo/rndbuf.h"
#ifdef CS_COMPILER_MSVC
#pragma warning(push)
/* Apparently, whatever return type MSVC sees first for GetIndex() below is
taken as "the" return type, at least when it comes to return value precision
checking: sometimes there is a lot of warning spam that assigning size_t to
<something else> may lose precision. */
#pragma warning(disable:4267)
#endif
namespace CS
{
/**
* Helper class to extract triangles from an index buffer. Automatically
* handles tristrips, quads etc. as well.
*/
template<typename T>
class TriangleIndicesStream
{
protected:
size_t stride;
const uint8* index;
const uint8* indexEnd;
T old2, old1;
bool stripFlag;
int quadPart;
iRenderBuffer* buf;
csRenderBufferComponentType compType;
csRenderMeshType meshtype;
/// Fetch the next index from the index buffer
T GetNextIndex()
{
T r;
switch (compType)
{
default:
CS_ASSERT(false);
case CS_BUFCOMP_BYTE:
r = *(char*)index;
break;
case CS_BUFCOMP_UNSIGNED_BYTE:
r = *(unsigned char*)index;
break;
case CS_BUFCOMP_SHORT:
r = *(short*)index;
break;
case CS_BUFCOMP_UNSIGNED_SHORT:
r = *(unsigned short*)index;
break;
case CS_BUFCOMP_INT:
r = *(int*)index;
break;
case CS_BUFCOMP_UNSIGNED_INT:
r = *(unsigned int*)index;
break;
case CS_BUFCOMP_FLOAT:
r = uint (*(float*)index);
break;
case CS_BUFCOMP_DOUBLE:
r = uint (*(double*)index);
break;
}
index += stride;
return r;
}
/// Get element \a idx, based on \a index
T GetIndex (size_t idx, const uint8* index) const
{
switch (compType)
{
default:
CS_ASSERT(false);
case CS_BUFCOMP_BYTE:
return T (*(char*)(index+idx*stride));
case CS_BUFCOMP_UNSIGNED_BYTE:
return T (*(unsigned char*)(index+idx*stride));
case CS_BUFCOMP_SHORT:
return T (*(short*)(index+idx*stride));
case CS_BUFCOMP_UNSIGNED_SHORT:
return T (*(unsigned short*)(index+idx*stride));
case CS_BUFCOMP_INT:
return T (*(int*)(index+idx*stride));
case CS_BUFCOMP_UNSIGNED_INT:
return T (*(unsigned int*)(index+idx*stride));
case CS_BUFCOMP_FLOAT:
return T (*(float*)(index+idx*stride));
case CS_BUFCOMP_DOUBLE:
return T (*(double*)(index+idx*stride));
}
return 0;
}
public:
/**
* Construct uninitialized triangle stream.
* \remark Don't forget to call BeginTriangulate() before using
* HasNext() or Next()!
*/
TriangleIndicesStream () : old2(0), old1(0), buf (0) { }
/**
* Construct triangle stream with an index buffer.
* \param indices Index buffer to triangulate.
* \param meshtype Mesh type of the index data.
* \param indexStart Element of the index buffer to start iterating at.
* \param indexEnd Element of the index buffer to stop iterating at.
* (size_t)~0 means last element.
*/
TriangleIndicesStream (iRenderBuffer* indices,
csRenderMeshType meshtype,
size_t indexStart = 0,
size_t indexEnd = (size_t)~0) : old2(0), old1(0)
{
BeginTriangulate (indices, meshtype, indexStart, indexEnd);
}
~TriangleIndicesStream()
{
if (buf != 0) buf->Release ();
}
/**
* Begin triangulation of an index buffer.
* \param index Pointer to start of the indices.
* \param indexEnd Pointer to end of the indices.
* \param stride Distance between index elements in bytes.
* \param compType Type of component contained in the data.
* \param meshtype Mesh type of the index data.
*/
void BeginTriangulate (const uint8* indexStart, const uint8* indexEnd,
size_t stride, csRenderBufferComponentType compType,
csRenderMeshType meshtype)
{
this->index = indexStart;
this->indexEnd = indexEnd;
this->stride = stride;
stripFlag = false;
quadPart = 0;
this->compType = compType;
this->meshtype = meshtype;
switch (meshtype)
{
case CS_MESHTYPE_TRIANGLESTRIP:
case CS_MESHTYPE_TRIANGLEFAN:
{
old2 = GetNextIndex();
old1 = GetNextIndex();
break;
}
default:
;
}
}
/**
* Begin triangulation of an index buffer.
* \param indices Index buffer to triangulate.
* \param meshtype Mesh type of the index data.
* \param indexStart Element of the index buffer to start iterating at.
* \param indexEnd Element of the index buffer to stop iterating at.
* (size_t)~0 means last element.
*/
void BeginTriangulate (iRenderBuffer* indices,
csRenderMeshType meshtype,
size_t indexStart = 0,
size_t indexEnd = (size_t)~0)
{
if (indexEnd == (size_t)~0) indexEnd = indices->GetElementCount();
buf = indices;
uint8* indexLock = (uint8*)buf->Lock (CS_BUF_LOCK_READ);
size_t stride = indices->GetElementDistance();
uint8* tri = indexLock + indexStart*stride;
const uint8* triEnd = indexLock + indexEnd*stride;
BeginTriangulate (tri, triEnd, stride, indices->GetComponentType(),
meshtype);
}
/// Returns whether a triangle is available.
bool HasNext() const
{
return (index < indexEnd);
}
CS_DEPRECATED_METHOD_MSG("Use HasNext() instead")
bool HasNextTri() const { return HasNext(); }
/// Fetches the next triangle from the buffer.
TriangleT<T> Next ()
{
CS_ASSERT (index < indexEnd);
TriangleT<T> t;
switch (meshtype)
{
case CS_MESHTYPE_TRIANGLES:
{
t.a = GetIndex (0, index);
t.b = GetIndex (1, index);
t.c = GetIndex (2, index);
index += 3*csRenderBufferComponentSizes[compType];
}
break;
case CS_MESHTYPE_TRIANGLESTRIP:
{
const T cur = GetNextIndex();
t.a = old1;
t.b = old2;
t.c = cur;
if (stripFlag)
old2 = cur;
else
old1 = cur;
stripFlag = !stripFlag;
}
break;
case CS_MESHTYPE_TRIANGLEFAN:
{
const T cur = GetNextIndex();
t.a = old2;
t.b = old1;
t.c = cur;
old1 = cur;
}
break;
case CS_MESHTYPE_QUADS:
{
if (quadPart == 0)
{
t.a = GetIndex (0, index);
t.b = GetIndex (1, index);
t.c = GetIndex (2, index);
}
else
{
t.a = GetIndex (0, index);
t.b = GetIndex (2, index);
t.c = GetIndex (3, index);
index += 4*csRenderBufferComponentSizes[compType];
}
quadPart ^= 1;
}
break;
default:
CS_ASSERT_MSG("Unsupported mesh type", false);
;
}
return t;
}
CS_DEPRECATED_METHOD_MSG("Use Next() instead")
bool NextTriangle (T& a, T& b, T& c)
{
TriangleT<T> tri = Next ();
a = tri.a; b = tri.b; c = tri.c;
return true;
}
/**
* Get the remaining components in the buffer.
* Note that this number does't have to correspond with the remaining
* number of triangles, especially when dealing with strips or fans.
*/
size_t GetRemainingComponents() const
{
size_t size;
switch (compType)
{
default:
CS_ASSERT(false);
case CS_BUFCOMP_BYTE: size = sizeof (char); break;
case CS_BUFCOMP_UNSIGNED_BYTE: size = sizeof (unsigned char); break;
case CS_BUFCOMP_SHORT: size = sizeof (short); break;
case CS_BUFCOMP_UNSIGNED_SHORT: size = sizeof (unsigned short); break;
case CS_BUFCOMP_INT: size = sizeof (int); break;
case CS_BUFCOMP_UNSIGNED_INT: size = sizeof (unsigned int); break;
case CS_BUFCOMP_FLOAT: size = sizeof (float); break;
case CS_BUFCOMP_DOUBLE: size = sizeof (double); break;
}
return (indexEnd - index) / size;
}
};
/**
* Extracts triangles like TriangleIndicesStream, but also provides random
* access to individual triangles and can be resetted.
* \remarks There is a slight overhead due to the housekeeping required for
* the random access. Hence for pure triangle streaming the "plain"
* TriangleIndicesStream is more appropriate.
*/
template<typename T>
class TriangleIndicesStreamRandom : public TriangleIndicesStream<T>
{
protected:
const uint8* indexStart;
size_t triangleNum;
const uint8* streamIndex;
size_t streamTriangleNum;
// Update internal stream position from TriangleIndicesStream position
void SwitchToInternalStreaming ()
{
if (streamIndex != 0) return;
streamIndex = this->index;
streamTriangleNum = triangleNum;
}
/* Restore TriangleIndicesStream with the position set before using
* "internal" streaming */
void SwitchToExternalStreaming ()
{
if (streamIndex == 0) return;
this->index = streamIndex;
triangleNum = streamTriangleNum;
streamIndex = 0;
}
template<typename T2>
void GetTriangleFastDefault (TriangleT<T2>& tri, size_t index) const
{
tri.a = T2 (this->GetIndex (index*3+0, indexStart));
tri.b = T2 (this->GetIndex (index*3+1, indexStart));
tri.c = T2 (this->GetIndex (index*3+2, indexStart));
}
void GetTriangleFast (TriangleT<char>& tri, size_t index) const
{
if (this->compType == CS_BUFCOMP_BYTE)
memcpy (&tri, indexStart + (index*3), sizeof (char)*3);
else
GetTriangleFastDefault (tri, index);
}
void GetTriangleFast (TriangleT<unsigned char>& tri, size_t index) const
{
if (this->compType == CS_BUFCOMP_UNSIGNED_BYTE)
memcpy (&tri, indexStart + (index*3), sizeof (unsigned char)*3);
else
GetTriangleFastDefault (tri, index);
}
void GetTriangleFast (TriangleT<short>& tri, size_t index) const
{
if (this->compType == CS_BUFCOMP_SHORT)
memcpy (&tri, indexStart + (index*3), sizeof (short)*3);
else
GetTriangleFastDefault (tri, index);
}
void GetTriangleFast (TriangleT<unsigned short>& tri, size_t index) const
{
if (this->compType == CS_BUFCOMP_UNSIGNED_SHORT)
memcpy (&tri, indexStart + (index*3), sizeof (unsigned short)*3);
else
GetTriangleFastDefault (tri, index);
}
void GetTriangleFast (TriangleT<int>& tri, size_t index) const
{
if (this->compType == CS_BUFCOMP_INT)
memcpy (&tri, indexStart + (index*3), sizeof (int)*3);
else
GetTriangleFastDefault (tri, index);
}
void GetTriangleFast (TriangleT<unsigned int>& tri, size_t index) const
{
if (this->compType == CS_BUFCOMP_UNSIGNED_INT)
memcpy (&tri, indexStart + (index*3), sizeof (unsigned int)*3);
else
GetTriangleFastDefault (tri, index);
}
void GetTriangleFast (TriangleT<float>& tri, size_t index) const
{
if (this->compType == CS_BUFCOMP_FLOAT)
memcpy (&tri, indexStart + (index*3), sizeof (float)*3);
else
GetTriangleFastDefault (tri, index);
}
void GetTriangleFast (TriangleT<double>& tri, size_t index) const
{
if (this->compType == CS_BUFCOMP_DOUBLE)
memcpy (&tri, indexStart + (index*3), sizeof (double)*3);
else
GetTriangleFastDefault (tri, index);
}
TriangleT<T> InternalNext ()
{
SwitchToInternalStreaming ();
TriangleT<T> tri (TriangleIndicesStream<T>::Next ());
++triangleNum;
return tri;
}
public:
/**
* Construct uninitialized triangle stream.
* \remarks Don't forget to call BeginTriangulate() before using
* HasNext() or Next()!
*/
TriangleIndicesStreamRandom () : TriangleIndicesStream<T> () { }
/**
* Construct triangle stream with an index buffer.
* \param indices Index buffer to triangulate.
* \param meshtype Mesh type of the index data.
* \param indexStart Element of the index buffer to start iterating at.
* \param indexEnd Element of the index buffer to stop iterating at.
* (size_t)~0 means last element.
*/
TriangleIndicesStreamRandom (iRenderBuffer* indices,
csRenderMeshType meshtype, size_t indexStart = 0,
size_t indexEnd = (size_t)~0) :
TriangleIndicesStream<T> (indices, meshtype, indexStart, indexEnd)
{
streamIndex = this->indexStart = this->index;
streamTriangleNum = triangleNum = 0;
}
~TriangleIndicesStreamRandom()
{
}
/**
* Begin triangulation of an index buffer.
* \param index Pointer to start of the indices.
* \param indexEnd Pointer to end of the indices.
* \param stride Distance between index elements in bytes.
* \param compType Type of component contained in the data.
* \param meshtype Mesh type of the index data.
*/
void BeginTriangulate (const uint8* indexStart, const uint8* indexEnd,
size_t stride, csRenderBufferComponentType compType,
csRenderMeshType meshtype)
{
TriangleIndicesStream<T>::BeginTriangulate (indexStart, indexEnd, stride,
compType, meshtype);
streamIndex = this->indexStart = this->index;
streamTriangleNum = triangleNum = 0;
}
/**
* Begin triangulation of an index buffer.
* \param indices Index buffer to triangulate.
* \param meshtype Mesh type of the index data.
* \param indexStart Element of the index buffer to start iterating at.
* \param indexEnd Element of the index buffer to stop iterating at.
* (size_t)~0 means last element.
*/
void BeginTriangulate (iRenderBuffer* indices,
csRenderMeshType meshtype,
size_t indexStart = 0,
size_t indexEnd = (size_t)~0)
{
TriangleIndicesStream<T>::BeginTriangulate (indices, meshtype,
indexStart, indexEnd);
streamIndex = this->indexStart = this->index;
streamTriangleNum = triangleNum = 0;
}
/// Reset the stream to the start.
void Reset()
{
this->index = indexStart;
triangleNum = 0;
streamIndex = 0;
}
/// Returns whether a triangle is available.
bool HasNext() const
{
SwitchToExternalStreaming ();
return TriangleIndicesStream<T>::HasNext ();
}
/// Fetches the next triangle from the buffer.
TriangleT<T> Next ()
{
SwitchToExternalStreaming ();
return TriangleIndicesStream<T>::Next ();
}
/// Get a specific triangle.
TriangleT<T> operator[] (size_t index)
{
if (this->meshtype == CS_MESHTYPE_TRIANGLES)
{
// Simple triangles: direct access
TriangleT<T> tri;
GetTriangleFast (tri, index);
return tri;
}
else
{
// Strips, fans...: need to iterate over all to find a specific tri
if (index < triangleNum) Reset();
while (index > triangleNum) InternalNext ();
return InternalNext ();
}
}
};
} // namespace CS
#ifdef CS_COMPILER_MSVC
#pragma warning(pop)
#endif
#endif // __CS_TRIANGLESTREAM_H__
|