Panda3D

geomVertexReader.I

00001 // Filename: geomVertexReader.I
00002 // Created by:  drose (25Mar05)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: GeomVertexReader::Default Constructor
00018 //       Access: Published
00019 //  Description: Constructs an invalid GeomVertexReader.  You must use
00020 //               the assignment operator to assign a valid
00021 //               GeomVertexReader to this object before you can use
00022 //               it.
00023 ////////////////////////////////////////////////////////////////////
00024 INLINE GeomVertexReader::
00025 GeomVertexReader(Thread *current_thread) :
00026   _vertex_data(NULL),
00027   _current_thread(current_thread)
00028 {
00029   initialize();
00030 }
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //     Function: GeomVertexReader::Constructor
00034 //       Access: Published
00035 //  Description: Constructs a new reader to process the vertices of
00036 //               the indicated data object.
00037 ////////////////////////////////////////////////////////////////////
00038 INLINE GeomVertexReader::
00039 GeomVertexReader(const GeomVertexData *vertex_data,
00040                  Thread *current_thread) :
00041   _vertex_data(vertex_data),
00042   _current_thread(current_thread)
00043 {
00044   initialize();
00045 }
00046 
00047 ////////////////////////////////////////////////////////////////////
00048 //     Function: GeomVertexReader::Constructor
00049 //       Access: Published
00050 //  Description: Constructs a new reader to process the vertices of
00051 //               the indicated data object.  This flavor creates the
00052 //               reader specifically to process the named data type.
00053 ////////////////////////////////////////////////////////////////////
00054 INLINE GeomVertexReader::
00055 GeomVertexReader(const GeomVertexData *vertex_data, const string &name,
00056                  Thread *current_thread) :
00057   _vertex_data(vertex_data),
00058   _current_thread(current_thread)
00059 {
00060   initialize();
00061   set_column(name);
00062 }
00063 
00064 ////////////////////////////////////////////////////////////////////
00065 //     Function: GeomVertexReader::Constructor
00066 //       Access: Published
00067 //  Description: Constructs a new reader to process the vertices of
00068 //               the indicated data object.  This flavor creates the
00069 //               reader specifically to process the named data type.
00070 ////////////////////////////////////////////////////////////////////
00071 INLINE GeomVertexReader::
00072 GeomVertexReader(const GeomVertexData *vertex_data, 
00073                  const InternalName *name,
00074                  Thread *current_thread) :
00075   _vertex_data(vertex_data),
00076   _current_thread(current_thread)
00077 {
00078   initialize();
00079   set_column(name);
00080 }
00081 
00082 ////////////////////////////////////////////////////////////////////
00083 //     Function: GeomVertexReader::Constructor
00084 //       Access: Published
00085 //  Description: Constructs a new reader to process the vertices of
00086 //               the indicated array only.
00087 ////////////////////////////////////////////////////////////////////
00088 INLINE GeomVertexReader::
00089 GeomVertexReader(const GeomVertexArrayData *array_data,
00090                  Thread *current_thread) :
00091   _array_data(array_data),
00092   _current_thread(current_thread)
00093 {
00094   initialize();
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function: GeomVertexReader::Constructor
00099 //       Access: Published
00100 //  Description: Constructs a new reader to process the vertices of
00101 //               the indicated array only.
00102 ////////////////////////////////////////////////////////////////////
00103 INLINE GeomVertexReader::
00104 GeomVertexReader(const GeomVertexArrayData *array_data, int column,
00105                  Thread *current_thread) :
00106   _array_data(array_data),
00107   _current_thread(current_thread)
00108 {
00109   initialize();
00110   set_column(column);
00111 }
00112 
00113 ////////////////////////////////////////////////////////////////////
00114 //     Function: GeomVertexReader::Constructor
00115 //       Access: Public
00116 //  Description: Constructs a new reader to process the vertices of
00117 //               the indicated data object.  This flavor creates the
00118 //               reader specifically to process the named data type.
00119 ////////////////////////////////////////////////////////////////////
00120 INLINE GeomVertexReader::
00121 GeomVertexReader(const GeomVertexDataPipelineReader *data_reader,
00122                  const InternalName *name, bool force) :
00123   _vertex_data(data_reader->get_object()),
00124   _current_thread(data_reader->get_current_thread())
00125 {
00126   initialize();
00127   _force = force;
00128   const GeomVertexFormat *format = data_reader->get_format();
00129   set_vertex_column(format->get_array_with(name),
00130                     format->get_column(name),
00131                     data_reader);
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: GeomVertexReader::Copy Constructor
00136 //       Access: Published
00137 //  Description: 
00138 ////////////////////////////////////////////////////////////////////
00139 INLINE GeomVertexReader::
00140 GeomVertexReader(const GeomVertexReader &copy) :
00141   _vertex_data(copy._vertex_data),
00142   _array(copy._array),
00143   _array_data(copy._array_data),
00144   _current_thread(copy._current_thread),
00145   _packer(copy._packer),
00146   _stride(copy._stride),
00147   _handle(copy._handle),
00148   _pointer_begin(copy._pointer_begin),
00149   _pointer_end(copy._pointer_end),
00150   _pointer(copy._pointer),
00151   _start_row(copy._start_row),
00152   _force(copy._force)
00153 {
00154 }
00155 
00156 ////////////////////////////////////////////////////////////////////
00157 //     Function: GeomVertexReader::Copy Assignment Operator
00158 //       Access: Published
00159 //  Description: 
00160 ////////////////////////////////////////////////////////////////////
00161 INLINE void GeomVertexReader::
00162 operator = (const GeomVertexReader &copy) {
00163   _vertex_data = copy._vertex_data;
00164   _array = copy._array;
00165   _array_data = copy._array_data;
00166   _current_thread = copy._current_thread;
00167   _packer = copy._packer;
00168   _stride = copy._stride;
00169   _handle = copy._handle;
00170   _pointer_begin = copy._pointer_begin;
00171   _pointer_end = copy._pointer_end;
00172   _pointer = copy._pointer;
00173   _start_row = copy._start_row;
00174   _force = copy._force;
00175 }
00176 
00177 ////////////////////////////////////////////////////////////////////
00178 //     Function: GeomVertexReader::Destructor
00179 //       Access: Published
00180 //  Description: 
00181 ////////////////////////////////////////////////////////////////////
00182 INLINE GeomVertexReader::
00183 ~GeomVertexReader() {
00184 }
00185 
00186 ////////////////////////////////////////////////////////////////////
00187 //     Function: GeomVertexReader::get_vertex_data
00188 //       Access: Published
00189 //  Description: Returns the vertex data object that the
00190 //               reader is processing.  This may return NULL if the
00191 //               reader was constructed with just an array pointer.
00192 ////////////////////////////////////////////////////////////////////
00193 INLINE const GeomVertexData *GeomVertexReader::
00194 get_vertex_data() const {
00195   return _vertex_data;
00196 }
00197 
00198 ////////////////////////////////////////////////////////////////////
00199 //     Function: GeomVertexReader::get_array_data
00200 //       Access: Published
00201 //  Description: Returns the particular array object that the
00202 //               reader is currently processing.
00203 ////////////////////////////////////////////////////////////////////
00204 INLINE const GeomVertexArrayData *GeomVertexReader::
00205 get_array_data() const {
00206   return _array_data;
00207 }
00208 
00209 ////////////////////////////////////////////////////////////////////
00210 //     Function: GeomVertexReader::get_current_thread
00211 //       Access: Published
00212 //  Description: Returns the Thread pointer of the currently-executing
00213 //               thread, as passed to the constructor of this object.
00214 ////////////////////////////////////////////////////////////////////
00215 INLINE Thread *GeomVertexReader::
00216 get_current_thread() const {
00217   return _current_thread;
00218 }
00219 
00220 ////////////////////////////////////////////////////////////////////
00221 //     Function: GeomVertexReader::set_force
00222 //       Access: Published
00223 //  Description: Sets the value of the force flag.  When this is true
00224 //               (the default), vertex data will be paged in from disk
00225 //               if necessary.  When this is false, the GeomVertexData
00226 //               will simply return a failure code when attempting to
00227 //               read vertex data that is not resident (but will put
00228 //               it on the queue to become resident later).
00229 //
00230 //               Normally, vertex data is always resident, so this
00231 //               will not be an issue.  It is only possible for vertex
00232 //               data to be nonresident if you have enabled vertex
00233 //               paging via the GeomVertexArrayData and VertexDataPage
00234 //               interfaces.
00235 ////////////////////////////////////////////////////////////////////
00236 INLINE void GeomVertexReader::
00237 set_force(bool force) {
00238   _force = force;
00239 }
00240 
00241 ////////////////////////////////////////////////////////////////////
00242 //     Function: GeomVertexReader::get_force
00243 //       Access: Published
00244 //  Description: Returns the value of the force flag.  See
00245 //               set_force().
00246 ////////////////////////////////////////////////////////////////////
00247 INLINE bool GeomVertexReader::
00248 get_force() const {
00249   return _force;
00250 }
00251 
00252 ////////////////////////////////////////////////////////////////////
00253 //     Function: GeomVertexReader::set_column
00254 //       Access: Published
00255 //  Description: Sets up the reader to use the nth data type of the
00256 //               GeomVertexFormat, numbering from 0.
00257 //
00258 //               This also resets the read row number to the start row
00259 //               (the same value passed to a previous call to
00260 //               set_row(), or 0 if set_row() was never called.)
00261 //
00262 //               The return value is true if the data type is valid,
00263 //               false otherwise.
00264 ////////////////////////////////////////////////////////////////////
00265 INLINE bool GeomVertexReader::
00266 set_column(int column) {
00267   if (_vertex_data != (const GeomVertexData *)NULL) {
00268     GeomVertexDataPipelineReader reader(_vertex_data, _current_thread);
00269     reader.check_array_readers();
00270     const GeomVertexFormat *format = reader.get_format();
00271     return set_vertex_column(format->get_array_with(column),
00272                              format->get_column(column),
00273                              &reader);
00274   }
00275   if (_array_data != (const GeomVertexArrayData *)NULL) {
00276     return set_array_column(_array_data->get_array_format()->get_column(column));
00277   }
00278   return false;
00279 }
00280 
00281 ////////////////////////////////////////////////////////////////////
00282 //     Function: GeomVertexReader::set_column
00283 //       Access: Published
00284 //  Description: Sets up the reader to use the data type with the
00285 //               indicated name.
00286 //
00287 //               This also resets the read row number to the start row
00288 //               (the same value passed to a previous call to
00289 //               set_row(), or 0 if set_row() was never called.)
00290 //
00291 //               The return value is true if the data type is valid,
00292 //               false otherwise.
00293 ////////////////////////////////////////////////////////////////////
00294 INLINE bool GeomVertexReader::
00295 set_column(const string &name) {
00296   return set_column(InternalName::make(name));
00297 }
00298 
00299 ////////////////////////////////////////////////////////////////////
00300 //     Function: GeomVertexReader::set_column
00301 //       Access: Published
00302 //  Description: Sets up the reader to use the data type with the
00303 //               indicated name.
00304 //
00305 //               This also resets the read row number to the start row
00306 //               (the same value passed to a previous call to
00307 //               set_row(), or 0 if set_row() was never called.)
00308 //
00309 //               The return value is true if the data type is valid,
00310 //               false otherwise.
00311 ////////////////////////////////////////////////////////////////////
00312 INLINE bool GeomVertexReader::
00313 set_column(const InternalName *name) {
00314   if (_vertex_data != (const GeomVertexData *)NULL) {
00315     GeomVertexDataPipelineReader reader(_vertex_data, _current_thread);
00316     reader.check_array_readers();
00317     const GeomVertexFormat *format = reader.get_format();
00318     return set_vertex_column(format->get_array_with(name),
00319                              format->get_column(name),
00320                              &reader);
00321   }
00322   if (_array_data != (const GeomVertexArrayData *)NULL) {
00323     return set_array_column(_array_data->get_array_format()->get_column(name));
00324   }
00325 
00326   return false;
00327 }
00328 
00329 ////////////////////////////////////////////////////////////////////
00330 //     Function: GeomVertexReader::clear
00331 //       Access: Published
00332 //  Description: Resets the GeomVertexReader to the initial state.
00333 ////////////////////////////////////////////////////////////////////
00334 INLINE void GeomVertexReader::
00335 clear() {
00336   (*this) = GeomVertexReader(_current_thread);
00337 }
00338 
00339 ////////////////////////////////////////////////////////////////////
00340 //     Function: GeomVertexReader::has_column
00341 //       Access: Published
00342 //  Description: Returns true if a valid data type has been
00343 //               successfully set, or false if the data type does not
00344 //               exist (or if get_force() is false and the vertex data
00345 //               is nonresident).
00346 ////////////////////////////////////////////////////////////////////
00347 INLINE bool GeomVertexReader::
00348 has_column() const {
00349   return (_packer != (GeomVertexColumn::Packer *)NULL);
00350 }
00351 
00352 ////////////////////////////////////////////////////////////////////
00353 //     Function: GeomVertexReader::get_array
00354 //       Access: Published
00355 //  Description: Returns the array index containing the data type that
00356 //               the reader is working on.
00357 ////////////////////////////////////////////////////////////////////
00358 INLINE int GeomVertexReader::
00359 get_array() const {
00360   return _array;
00361 }
00362 
00363 ////////////////////////////////////////////////////////////////////
00364 //     Function: GeomVertexReader::get_column
00365 //       Access: Published
00366 //  Description: Returns the description of the data type that the
00367 //               reader is working on.
00368 ////////////////////////////////////////////////////////////////////
00369 INLINE const GeomVertexColumn *GeomVertexReader::
00370 get_column() const {
00371   if (_packer != (GeomVertexColumn::Packer *)NULL) {
00372     return _packer->_column;
00373   }
00374   return NULL;
00375 }
00376 
00377 ////////////////////////////////////////////////////////////////////
00378 //     Function: GeomVertexReader::set_row
00379 //       Access: Published
00380 //  Description: Sets the start row to the indicated value.  The
00381 //               reader will begin reading from the indicated row;
00382 //               each subsequent get_data*() call will return the data
00383 //               from the subsequent row.  If set_column() is called,
00384 //               the reader will return to this row.
00385 ////////////////////////////////////////////////////////////////////
00386 INLINE void GeomVertexReader::
00387 set_row(int row) {
00388   _start_row = row;
00389   if (has_column()) {
00390     quick_set_pointer(_start_row);
00391   }
00392 }
00393 
00394 ////////////////////////////////////////////////////////////////////
00395 //     Function: GeomVertexReader::get_start_row
00396 //       Access: Published
00397 //  Description: Returns the row index at which the reader started.
00398 //               It will return to this row if you reset the current
00399 //               column.
00400 ////////////////////////////////////////////////////////////////////
00401 INLINE int GeomVertexReader::
00402 get_start_row() const {
00403   return _start_row;
00404 }
00405 
00406 ////////////////////////////////////////////////////////////////////
00407 //     Function: GeomVertexReader::get_read_row
00408 //       Access: Published
00409 //  Description: Returns the row index from which the data will be
00410 //               retrieved by the next call to get_data*().
00411 ////////////////////////////////////////////////////////////////////
00412 INLINE int GeomVertexReader::
00413 get_read_row() const {
00414   return (int)(_pointer - _pointer_begin) / _stride;
00415 }
00416 
00417 ////////////////////////////////////////////////////////////////////
00418 //     Function: GeomVertexReader::is_at_end
00419 //       Access: Published
00420 //  Description: Returns true if the reader is currently at the end of
00421 //               the list of vertices, false otherwise.  If this is
00422 //               true, another call to get_data*() will result in a
00423 //               crash.
00424 ////////////////////////////////////////////////////////////////////
00425 INLINE bool GeomVertexReader::
00426 is_at_end() const {
00427   return _pointer >= _pointer_end;
00428 }
00429 
00430 ////////////////////////////////////////////////////////////////////
00431 //     Function: GeomVertexReader::get_data1f
00432 //       Access: Published
00433 //  Description: Returns the data associated with the read row,
00434 //               expressed as a 1-component value, and advances the
00435 //               read row.
00436 ////////////////////////////////////////////////////////////////////
00437 INLINE float GeomVertexReader::
00438 get_data1f() {
00439   nassertr(has_column(), 0.0f);
00440   return _packer->get_data1f(inc_pointer());
00441 }
00442 
00443 ////////////////////////////////////////////////////////////////////
00444 //     Function: GeomVertexReader::get_data2f
00445 //       Access: Published
00446 //  Description: Returns the data associated with the read row,
00447 //               expressed as a 2-component value, and advances the
00448 //               read row.
00449 ////////////////////////////////////////////////////////////////////
00450 INLINE const LVecBase2f &GeomVertexReader::
00451 get_data2f() {
00452   nassertr(has_column(), LVecBase2f::zero());
00453   return _packer->get_data2f(inc_pointer());
00454 }
00455 
00456 ////////////////////////////////////////////////////////////////////
00457 //     Function: GeomVertexReader::get_data3f
00458 //       Access: Published
00459 //  Description: Returns the data associated with the read row,
00460 //               expressed as a 3-component value, and advances the
00461 //               read row.
00462 ////////////////////////////////////////////////////////////////////
00463 INLINE const LVecBase3f &GeomVertexReader::
00464 get_data3f() {
00465   nassertr(has_column(), LVecBase3f::zero());
00466   return _packer->get_data3f(inc_pointer());
00467 }
00468 
00469 ////////////////////////////////////////////////////////////////////
00470 //     Function: GeomVertexReader::get_data4f
00471 //       Access: Published
00472 //  Description: Returns the data associated with the read row,
00473 //               expressed as a 4-component value, and advances the
00474 //               read row.
00475 ////////////////////////////////////////////////////////////////////
00476 INLINE const LVecBase4f &GeomVertexReader::
00477 get_data4f() {
00478   nassertr(has_column(), LVecBase4f::zero());
00479   return _packer->get_data4f(inc_pointer());
00480 }
00481 
00482 ////////////////////////////////////////////////////////////////////
00483 //     Function: GeomVertexReader::get_data1i
00484 //       Access: Published
00485 //  Description: Returns the data associated with the read row,
00486 //               expressed as a 1-component value, and advances the
00487 //               read row.
00488 ////////////////////////////////////////////////////////////////////
00489 INLINE int GeomVertexReader::
00490 get_data1i() {
00491   nassertr(has_column(), 0);
00492   return _packer->get_data1i(inc_pointer());
00493 }
00494 
00495 ////////////////////////////////////////////////////////////////////
00496 //     Function: GeomVertexReader::get_data2i
00497 //       Access: Published
00498 //  Description: Returns the data associated with the read row,
00499 //               expressed as a 2-component value, and advances the
00500 //               read row.
00501 ////////////////////////////////////////////////////////////////////
00502 INLINE const int *GeomVertexReader::
00503 get_data2i() {
00504   nassertr(has_column(), 0);
00505   return _packer->get_data2i(inc_pointer());
00506 }
00507 
00508 ////////////////////////////////////////////////////////////////////
00509 //     Function: GeomVertexReader::get_data3i
00510 //       Access: Published
00511 //  Description: Returns the data associated with the read row,
00512 //               expressed as a 3-component value, and advances the
00513 //               read row.
00514 ////////////////////////////////////////////////////////////////////
00515 INLINE const int *GeomVertexReader::
00516 get_data3i() {
00517   nassertr(has_column(), 0);
00518   return _packer->get_data3i(inc_pointer());
00519 }
00520 
00521 ////////////////////////////////////////////////////////////////////
00522 //     Function: GeomVertexReader::get_data4i
00523 //       Access: Published
00524 //  Description: Returns the data associated with the read row,
00525 //               expressed as a 4-component value, and advances the
00526 //               read row.
00527 ////////////////////////////////////////////////////////////////////
00528 INLINE const int *GeomVertexReader::
00529 get_data4i() {
00530   nassertr(has_column(), 0);
00531   return _packer->get_data4i(inc_pointer());
00532 }
00533 
00534 ////////////////////////////////////////////////////////////////////
00535 //     Function: GeomVertexReader::get_packer
00536 //       Access: Protected
00537 //  Description: Returns the reader's Packer object.
00538 ////////////////////////////////////////////////////////////////////
00539 INLINE GeomVertexColumn::Packer *GeomVertexReader::
00540 get_packer() const {
00541   return _packer;
00542 }
00543 
00544 ////////////////////////////////////////////////////////////////////
00545 //     Function: GeomVertexReader::set_pointer
00546 //       Access: Private
00547 //  Description: Sets up the array pointers freshly from the source
00548 //               object (in case they have been reallocated recently),
00549 //               and sets the internal pointer to the indicated row.
00550 //
00551 //               Returns true if successful, or false if the vertex
00552 //               data is not resident.  If it returns false, the
00553 //               reader's internal column spec is cleared.  It is only
00554 //               possible to return false if get_force() is false.
00555 ////////////////////////////////////////////////////////////////////
00556 INLINE bool GeomVertexReader::
00557 set_pointer(int row) {
00558   _pointer_begin = _handle->get_read_pointer(_force);
00559   if (_pointer_begin == NULL) {
00560     // Vertex data is not resident.
00561     set_column(0, NULL);
00562     return false;
00563   }
00564 
00565   _pointer_end = _pointer_begin + _handle->get_data_size_bytes();
00566   quick_set_pointer(row);
00567   return true;
00568 }
00569 
00570 ////////////////////////////////////////////////////////////////////
00571 //     Function: GeomVertexReader::quick_set_pointer
00572 //       Access: Private
00573 //  Description: Sets up the internal pointer to the indicated row,
00574 //               without first verifying that arrays haven't been
00575 //               reallocated.
00576 ////////////////////////////////////////////////////////////////////
00577 INLINE void GeomVertexReader::
00578 quick_set_pointer(int row) {
00579   nassertv(has_column() && _pointer_begin != NULL);
00580 
00581 #if defined(_DEBUG)
00582   // Make sure we still have the same pointer as stored in the array.
00583   nassertv(_pointer_begin == _handle->get_read_pointer(true));
00584 #endif
00585 
00586   _pointer = _pointer_begin + _packer->_column->get_start() + _stride * row;
00587 
00588 #if defined(_DEBUG)
00589   nassertv(_pointer_begin == _pointer_end || _pointer < _pointer_end);
00590 #endif
00591 }
00592 
00593 ////////////////////////////////////////////////////////////////////
00594 //     Function: GeomVertexReader::inc_pointer
00595 //       Access: Private
00596 //  Description: Increments to the next row, and returns the data
00597 //               pointer as it was before incrementing.
00598 ////////////////////////////////////////////////////////////////////
00599 INLINE const unsigned char *GeomVertexReader::
00600 inc_pointer() {
00601 #if defined(_DEBUG)
00602   nassertr(_pointer < _pointer_end, empty_buffer);
00603   // Make sure we still have the same pointer as stored in the array.
00604   nassertr(_pointer_begin == _handle->get_read_pointer(true), empty_buffer);
00605   nassertr(_pointer < _pointer_begin + _handle->get_data_size_bytes(), empty_buffer);
00606 #endif
00607 
00608   const unsigned char *orig_pointer = _pointer;
00609   _pointer += _stride;
00610   return orig_pointer;
00611 }
 All Classes Functions Variables Enumerations