Panda3D

geomVertexWriter.cxx

00001 // Filename: geomVertexWriter.cxx
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 #include "geomVertexWriter.h"
00016 
00017 
00018 #ifndef NDEBUG
00019   // This is defined just for the benefit of having something non-NULL
00020   // to return from a nassertr() call.
00021 unsigned char GeomVertexWriter::empty_buffer[100] = { 0 };
00022 #endif
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //     Function: GeomVertexWriter::set_column
00026 //       Access: Published
00027 //  Description: Sets up the writer to use the indicated column
00028 //               description on the given array.
00029 //
00030 //               This also resets the current write row number to the
00031 //               start row (the same value passed to a previous call
00032 //               to set_row(), or 0 if set_row() was never called.)
00033 //
00034 //               The return value is true if the data type is valid,
00035 //               false otherwise.
00036 ////////////////////////////////////////////////////////////////////
00037 bool GeomVertexWriter::
00038 set_column(int array, const GeomVertexColumn *column) {
00039   if (_vertex_data == (GeomVertexData *)NULL &&
00040       _array_data == (GeomVertexArrayData *)NULL) {
00041     return false;
00042   }
00043 
00044   if (column == (const GeomVertexColumn *)NULL) {
00045     // Clear the data type.
00046     _array = -1;
00047     _packer = NULL;
00048     _stride = 0;
00049     _pointer = NULL;
00050     _pointer_end = NULL;
00051 
00052     return false;
00053   }
00054 
00055   if (_vertex_data != (GeomVertexData *)NULL) {
00056     GeomVertexDataPipelineWriter writer(_vertex_data, true, _current_thread);
00057     writer.check_array_writers();
00058     return set_vertex_column(array, column, &writer);
00059   }
00060   if (_array_data != (GeomVertexArrayData *)NULL) {
00061     return set_array_column(column);
00062   }
00063 
00064   // No data is associated with the Writer.
00065   return false;
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: GeomVertexWriter::reserve_num_rows
00070 //       Access: Published
00071 //  Description: This ensures that enough memory space for num_rows is
00072 //               allocated, so that you may add up to num_rows rows
00073 //               without causing a new memory allocation.  This is a
00074 //               performance optimization only; it is especially
00075 //               useful when you know the number of rows you will be
00076 //               adding ahead of time.
00077 ////////////////////////////////////////////////////////////////////
00078 bool GeomVertexWriter::
00079 reserve_num_rows(int num_rows) {
00080   bool result;
00081 
00082   if (_vertex_data != (GeomVertexData *)NULL) {
00083     // If we have a whole GeomVertexData, we must set the length of
00084     // all its arrays at once.
00085     GeomVertexDataPipelineWriter writer(_vertex_data, true, _current_thread);
00086     writer.check_array_writers();
00087     result = writer.reserve_num_rows(num_rows);
00088     _handle = writer.get_array_writer(_array);
00089     
00090   } else {
00091     // Otherwise, we can get away with modifying only the one array
00092     // we're using.
00093     result = _handle->reserve_num_rows(num_rows);
00094   }
00095 
00096   return result;
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function: GeomVertexWriter::output
00101 //       Access: Published
00102 //  Description: 
00103 ////////////////////////////////////////////////////////////////////
00104 void GeomVertexWriter::
00105 output(ostream &out) const {
00106   const GeomVertexColumn *column = get_column();
00107   if (column == (GeomVertexColumn *)NULL) {
00108     out << "GeomVertexWriter()";
00109     
00110   } else {
00111     out << "GeomVertexWriter, array = " << get_array_data()
00112         << ", column = " << column->get_name()
00113         << " (" << get_packer()->get_name()
00114         << "), write row " << get_write_row();
00115   }
00116 }
00117 
00118 ////////////////////////////////////////////////////////////////////
00119 //     Function: GeomVertexWriter::initialize
00120 //       Access: Private
00121 //  Description: Called only by the constructor.
00122 ////////////////////////////////////////////////////////////////////
00123 void GeomVertexWriter::
00124 initialize() {
00125   _array = 0;
00126   _packer = NULL;
00127   _pointer_begin = NULL;
00128   _pointer_end = NULL;
00129   _pointer = NULL;
00130   _start_row = 0;
00131 }
00132 
00133 ////////////////////////////////////////////////////////////////////
00134 //     Function: GeomVertexWriter::set_vertex_column
00135 //       Access: Private
00136 //  Description: Internal method to set the column to column from the
00137 //               indicated array, assuming we have a GeomVertexData
00138 ////////////////////////////////////////////////////////////////////
00139 bool GeomVertexWriter::
00140 set_vertex_column(int array, const GeomVertexColumn *column,
00141                   GeomVertexDataPipelineWriter *data_writer) {
00142   if (column == (const GeomVertexColumn *)NULL) {
00143     return set_column(0, NULL);
00144   }
00145 
00146   nassertr(_vertex_data != (GeomVertexData *)NULL, false);
00147 
00148 #ifndef NDEBUG
00149   _array = -1;
00150   _packer = NULL;
00151   nassertr(array >= 0 && array < _vertex_data->get_num_arrays(), false);
00152 #endif
00153 
00154   _array = array;
00155   _handle = data_writer->get_array_writer(_array);
00156   _stride = _handle->get_array_format()->get_stride();
00157 
00158   _packer = column->_packer;
00159   set_pointer(_start_row);
00160   
00161   return true;
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////
00165 //     Function: GeomVertexWriter::set_array_column
00166 //       Access: Private
00167 //  Description: Internal method to set the column to column from the
00168 //               indicated array, assuming we have a
00169 //               GeomVertexArrayData.
00170 ////////////////////////////////////////////////////////////////////
00171 bool GeomVertexWriter::
00172 set_array_column(const GeomVertexColumn *column) {
00173   if (column == (const GeomVertexColumn *)NULL) {
00174     return set_column(0, NULL);
00175   }
00176 
00177   nassertr(_array_data != (GeomVertexArrayData *)NULL, false);
00178 
00179   _handle = _array_data->modify_handle();
00180   _stride = _handle->get_array_format()->get_stride();
00181 
00182   _packer = column->_packer;
00183   set_pointer(_start_row);
00184   
00185   return true;
00186 }
 All Classes Functions Variables Enumerations