Browse Source

gobj: Add methods for adding/removing GeomVertexData arrays

rdb 5 years ago
parent
commit
3d5f0b7fe1

+ 23 - 0
panda/src/gobj/geomVertexData.I

@@ -208,6 +208,29 @@ set_array(size_t i, const GeomVertexArrayData *array) {
   writer.set_array(i, array);
 }
 
+/**
+ * Removes the array wit hthe given index from the GeomVertexData.
+ */
+INLINE void GeomVertexData::
+remove_array(size_t i) {
+  GeomVertexDataPipelineWriter writer(this, true, Thread::get_current_thread());
+  writer.remove_array(i);
+}
+
+/**
+ * Inserts the indicated vertex data array into the list of arrays, which also
+ * modifies the format.  You should be careful that the new array has the same
+ * number of rows as the vertex data.
+ *
+ * Don't call this in a downstream thread unless you don't mind it blowing
+ * away other changes you might have recently made in an upstream thread.
+ */
+INLINE void GeomVertexData::
+insert_array(size_t i, const GeomVertexArrayData *array) {
+  GeomVertexDataPipelineWriter writer(this, true, Thread::get_current_thread());
+  writer.insert_array(i, array);
+}
+
 /**
  * Returns a const pointer to the TransformTable assigned to this data.
  * Vertices within the table will index into this table to indicate their

+ 46 - 0
panda/src/gobj/geomVertexData.cxx

@@ -2582,6 +2582,52 @@ set_array(size_t i, const GeomVertexArrayData *array) {
   }
 }
 
+/**
+ *
+ */
+void GeomVertexDataPipelineWriter::
+remove_array(size_t i) {
+  nassertv(i < _cdata->_arrays.size());
+
+  GeomVertexFormat *new_format = new GeomVertexFormat(*_cdata->_format);
+  new_format->remove_array(i);
+  _cdata->_format = GeomVertexFormat::register_format(new_format);
+  _cdata->_arrays.erase(_cdata->_arrays.begin() + i);
+
+  _object->clear_cache_stage();
+  _cdata->_modified = Geom::get_next_modified();
+  _cdata->_animated_vertices.clear();
+
+  if (_got_array_writers) {
+    _array_writers.erase(_array_writers.begin() + i);
+  }
+}
+
+/**
+ *
+ */
+void GeomVertexDataPipelineWriter::
+insert_array(size_t i, const GeomVertexArrayData *array) {
+  const GeomVertexArrayFormat *array_format = array->get_array_format();
+
+  if (i > _cdata->_arrays.size()) {
+    i = _cdata->_arrays.size();
+  }
+
+  GeomVertexFormat *new_format = new GeomVertexFormat(*_cdata->_format);
+  new_format->insert_array(i, array_format);
+  _cdata->_format = GeomVertexFormat::register_format(new_format);
+  _cdata->_arrays.insert(_cdata->_arrays.begin() + i, (GeomVertexArrayData *)array);
+
+  _object->clear_cache_stage();
+  _cdata->_modified = Geom::get_next_modified();
+  _cdata->_animated_vertices.clear();
+
+  if (_got_array_writers) {
+    _array_writers.insert(_array_writers.begin() + i, new GeomVertexArrayDataHandle(_cdata->_arrays[i].get_write_pointer(), _current_thread));
+  }
+}
+
 /**
  * Copies a single row of the data from the other array into the indicated row
  * of this array.  In this case, the source format must exactly match the

+ 5 - 1
panda/src/gobj/geomVertexData.h

@@ -112,7 +112,9 @@ PUBLISHED:
   INLINE PT(GeomVertexArrayData) modify_array(size_t i);
   INLINE PT(GeomVertexArrayDataHandle) modify_array_handle(size_t i);
   INLINE void set_array(size_t i, const GeomVertexArrayData *array);
-  MAKE_SEQ_PROPERTY(arrays, get_num_arrays, get_array, set_array);
+  INLINE void remove_array(size_t i);
+  INLINE void insert_array(size_t i, const GeomVertexArrayData *array);
+  MAKE_SEQ_PROPERTY(arrays, get_num_arrays, get_array, set_array, remove_array, insert_array);
 
   INLINE const TransformTable *get_transform_table() const;
   void set_transform_table(const TransformTable *table);
@@ -520,6 +522,8 @@ public:
 
   PT(GeomVertexArrayData) modify_array(size_t i);
   void set_array(size_t i, const GeomVertexArrayData *array);
+  void remove_array(size_t i);
+  void insert_array(size_t i, const GeomVertexArrayData *array);
 
   int get_num_rows() const;
   bool set_num_rows(int n);