Panda3D
geomVertexWriter.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file geomVertexWriter.cxx
10  * @author drose
11  * @date 2005-03-25
12  */
13 
14 #include "geomVertexWriter.h"
15 
16 #ifdef _DEBUG
17  // This is defined just for the benefit of having something non-NULL to
18  // return from a nassertr() call.
19 unsigned char GeomVertexWriter::empty_buffer[100] = { 0 };
20 #endif
21 
22 /**
23  * Sets up the writer to use the indicated column description on the given
24  * array.
25  *
26  * This also resets the current write row number to the start row (the same
27  * value passed to a previous call to set_row(), or 0 if set_row() was never
28  * called.)
29  *
30  * The return value is true if the data type is valid, false otherwise.
31  */
33 set_column(int array, const GeomVertexColumn *column) {
34  if (_vertex_data == nullptr &&
35  _array_data == nullptr) {
36  return false;
37  }
38 
39  if (column == nullptr) {
40  // Clear the data type.
41  _array = -1;
42  _packer = nullptr;
43  _stride = 0;
44  _pointer = nullptr;
45  _pointer_end = nullptr;
46 
47  return false;
48  }
49 
50  if (_vertex_data != nullptr) {
51  GeomVertexDataPipelineWriter writer(_vertex_data, true, _current_thread);
52  writer.check_array_writers();
53  return set_vertex_column(array, column, &writer);
54  }
55  if (_array_data != nullptr) {
56  return set_array_column(column);
57  }
58 
59  // No data is associated with the Writer.
60  return false;
61 }
62 
63 /**
64  * This ensures that enough memory space for num_rows is allocated, so that
65  * you may add up to num_rows rows without causing a new memory allocation.
66  * This is a performance optimization only; it is especially useful when you
67  * know the number of rows you will be adding ahead of time.
68  */
70 reserve_num_rows(int num_rows) {
71  bool result;
72 
73  if (_vertex_data != nullptr) {
74  // If we have a whole GeomVertexData, we must set the length of all its
75  // arrays at once.
76  GeomVertexDataPipelineWriter writer(_vertex_data, true, _current_thread);
77  writer.check_array_writers();
78  result = writer.reserve_num_rows(num_rows);
79  _handle = writer.get_array_writer(_array);
80 
81  } else {
82  // Otherwise, we can get away with modifying only the one array we're
83  // using.
84  result = _handle->reserve_num_rows(num_rows);
85  }
86 
87  return result;
88 }
89 
90 /**
91  *
92  */
93 void GeomVertexWriter::
94 output(std::ostream &out) const {
95  const GeomVertexColumn *column = get_column();
96  if (column == nullptr) {
97  out << "GeomVertexWriter()";
98 
99  } else {
100  out << "GeomVertexWriter, array = " << get_array_data()
101  << ", column = " << column->get_name()
102  << " (" << get_packer()->get_name()
103  << "), write row " << get_write_row();
104  }
105 }
106 
107 /**
108  * Called only by the constructor.
109  */
110 void GeomVertexWriter::
111 initialize() {
112  _array = 0;
113  _packer = nullptr;
114  _pointer_begin = nullptr;
115  _pointer_end = nullptr;
116  _pointer = nullptr;
117  _start_row = 0;
118 }
119 
120 /**
121  * Internal method to set the column to column from the indicated array,
122  * assuming we have a GeomVertexData
123  */
124 bool GeomVertexWriter::
125 set_vertex_column(int array, const GeomVertexColumn *column,
126  GeomVertexDataPipelineWriter *data_writer) {
127  if (column == nullptr) {
128  return set_column(0, nullptr);
129  }
130 
131  nassertr(_vertex_data != nullptr, false);
132 
133 #ifndef NDEBUG
134  _array = -1;
135  _packer = nullptr;
136  nassertr(array >= 0 && (size_t)array < _vertex_data->get_num_arrays(), false);
137 #endif
138 
139  _array = array;
140  _handle = data_writer->get_array_writer(_array);
141  _stride = _handle->get_array_format()->get_stride();
142 
143  _packer = column->_packer;
144  set_pointer(_start_row);
145 
146  return true;
147 }
148 
149 /**
150  * Internal method to set the column to column from the indicated array,
151  * assuming we have a GeomVertexArrayData.
152  */
153 bool GeomVertexWriter::
154 set_array_column(const GeomVertexColumn *column) {
155  if (column == nullptr) {
156  return set_column(0, nullptr);
157  }
158 
159  nassertr(_array_data != nullptr, false);
160 
161  _handle = _array_data->modify_handle();
162  _stride = _handle->get_array_format()->get_stride();
163 
164  _packer = column->_packer;
165  set_pointer(_start_row);
166 
167  return true;
168 }
Encapsulates the data from a GeomVertexData, pre-fetched for one stage of the pipeline.
This defines how a single column is interleaved within a vertex array stored within a Geom.
bool reserve_num_rows(int num_rows)
This ensures that enough memory space for num_rows is allocated, so that you may add up to num_rows r...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomVertexArrayData * get_array_data() const
Returns the particular array object that the writer is currently processing.
bool set_column(int column)
Sets up the writer to use the nth data type of the GeomVertexFormat, numbering from 0.
const InternalName * get_name() const
Returns the name of this particular data field, e.g.
int get_write_row() const
Returns the row index to which the data will be written at the next call to set_data*() or add_data*(...
const GeomVertexColumn * get_column() const
Returns the description of the data type that the writer is working on.