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