Panda3D

geomVertexWriter.h

00001 // Filename: geomVertexWriter.h
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 #ifndef GEOMVERTEXWRITER_H
00016 #define GEOMVERTEXWRITER_H
00017 
00018 #include "pandabase.h"
00019 #include "geomVertexData.h"
00020 #include "geomVertexColumn.h"
00021 #include "internalName.h"
00022 #include "luse.h"
00023 #include "pointerTo.h"
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //       Class : GeomVertexWriter
00027 // Description : This object provides a high-level interface for
00028 //               quickly writing a sequence of numeric values from a
00029 //               vertex table. 
00030 //
00031 //               This object can be used both to replace existing
00032 //               vertices in the table, or to extend the table with
00033 //               new vertices.  The set_data*() family of methods can
00034 //               only be used to replace existing data; it is an error
00035 //               to allow these to run past the end of the data.  The
00036 //               add_data*() family of methods, on the other hand, can
00037 //               be used to replace existing data or add new data; if
00038 //               you call set_row() into the middle of existing
00039 //               data the add_data*() methods will behave like the
00040 //               corresponding set_data*(), but if they run past the
00041 //               end of existing data they will quietly add new
00042 //               vertices.
00043 //
00044 //               Like GeomVertexReader, the writer is particularly
00045 //               optimized for writing a single column of data values
00046 //               for a series of vertices, without changing columns
00047 //               between each number.  Although you can also use one
00048 //               GeomVertexWriter to write across the columns if it is
00049 //               convenient, by calling set_column() repeatedly at
00050 //               each vertex, it is faster to write down the columns,
00051 //               and to use a different GeomVertexWriter for each
00052 //               column.
00053 //
00054 //               Note that, like a GeomVertexReader, a
00055 //               GeomVertexWriter does not keep a reference count to
00056 //               the actual vertex data buffer.  This means that it is
00057 //               important not to keep a GeomVertexWriter object
00058 //               around over a long period of time in which the data
00059 //               buffer is likely to be deallocated; it is intended
00060 //               for making a quick pass over the data in one session.
00061 //
00062 //               It also means that you should create any
00063 //               GeomVertexWriters *before* creating GeomVertexReaders
00064 //               on the same data, since the writer itself might cause
00065 //               the vertex buffer to be deallocated.  Better yet, use
00066 //               a GeomVertexRewriter if you are going to create both
00067 //               of them anyway.
00068 ////////////////////////////////////////////////////////////////////
00069 class EXPCL_PANDA_GOBJ GeomVertexWriter : public GeomEnums {
00070 PUBLISHED:
00071   INLINE GeomVertexWriter(Thread *current_thread = Thread::get_current_thread());
00072   INLINE GeomVertexWriter(GeomVertexData *vertex_data,
00073                           Thread *current_thread = Thread::get_current_thread());
00074   INLINE GeomVertexWriter(GeomVertexData *vertex_data,
00075                           const string &name,
00076                           Thread *current_thread = Thread::get_current_thread());
00077   INLINE GeomVertexWriter(GeomVertexData *vertex_data,
00078                           const InternalName *name,
00079                           Thread *current_thread = Thread::get_current_thread());
00080   INLINE GeomVertexWriter(GeomVertexArrayData *array_data,
00081                           Thread *current_thread = Thread::get_current_thread());
00082   INLINE GeomVertexWriter(GeomVertexArrayData *array_data, 
00083                           int column,
00084                           Thread *current_thread = Thread::get_current_thread());
00085 
00086 public:
00087   INLINE GeomVertexWriter(GeomVertexDataPipelineWriter *data_writer,
00088                           const InternalName *name);
00089 
00090 PUBLISHED:
00091   INLINE GeomVertexWriter(const GeomVertexWriter &copy);
00092   INLINE void operator = (const GeomVertexWriter &copy);
00093   INLINE ~GeomVertexWriter();
00094 
00095   INLINE GeomVertexData *get_vertex_data() const;
00096   INLINE GeomVertexArrayData *get_array_data() const;
00097   INLINE Thread *get_current_thread() const;
00098 
00099   INLINE bool set_column(int column);
00100   INLINE bool set_column(const string &name);
00101   INLINE bool set_column(const InternalName *name);
00102   bool set_column(int array, const GeomVertexColumn *column);
00103   INLINE void clear();
00104 
00105   INLINE bool has_column() const;
00106   INLINE int get_array() const;
00107   INLINE const GeomVertexColumn *get_column() const;
00108 
00109   INLINE void set_row(int row);
00110 
00111   INLINE int get_start_row() const;
00112   INLINE int get_write_row() const;
00113   INLINE bool is_at_end() const;
00114 
00115   INLINE void set_data1f(float data);
00116   INLINE void set_data2f(float x, float y);
00117   INLINE void set_data2f(const LVecBase2f &data);
00118   INLINE void set_data3f(float x, float y, float z);
00119   INLINE void set_data3f(const LVecBase3f &data);
00120   INLINE void set_data4f(float x, float y, float z, float w);
00121   INLINE void set_data4f(const LVecBase4f &data);
00122 
00123   INLINE void set_data1i(int data);
00124   INLINE void set_data2i(int a, int b);
00125   INLINE void set_data2i(const int data[2]);
00126   INLINE void set_data3i(int a, int b, int c);
00127   INLINE void set_data3i(const int data[3]);
00128   INLINE void set_data4i(int a, int b, int c, int d);
00129   INLINE void set_data4i(const int data[4]);
00130 
00131   INLINE void add_data1f(float data);
00132   INLINE void add_data2f(float x, float y);
00133   INLINE void add_data2f(const LVecBase2f &data);
00134   INLINE void add_data3f(float x, float y, float z);
00135   INLINE void add_data3f(const LVecBase3f &data);
00136   INLINE void add_data4f(float x, float y, float z, float w);
00137   INLINE void add_data4f(const LVecBase4f &data);
00138 
00139   INLINE void add_data1i(int data);
00140   INLINE void add_data2i(int a, int b);
00141   INLINE void add_data2i(const int data[2]);
00142   INLINE void add_data3i(int a, int b, int c);
00143   INLINE void add_data3i(const int data[3]);
00144   INLINE void add_data4i(int a, int b, int c, int d);
00145   INLINE void add_data4i(const int data[4]);
00146 
00147   void output(ostream &out) const;
00148 
00149 protected:
00150   INLINE GeomVertexColumn::Packer *get_packer() const;
00151 
00152 private:
00153   class Writer;
00154 
00155   void initialize();
00156 
00157   INLINE void set_pointer(int row);
00158   INLINE void quick_set_pointer(int row);
00159   INLINE unsigned char *inc_pointer();
00160   INLINE unsigned char *inc_add_pointer();
00161 
00162   bool set_vertex_column(int array, const GeomVertexColumn *column,
00163                          GeomVertexDataPipelineWriter *data_writer);
00164   bool set_array_column(const GeomVertexColumn *column);
00165 
00166   // It is important that we only store *one* of the following two
00167   // pointers.  If we are storing a GeomVertexData/array index, we
00168   // must not keep a pointer to the particular ArrayData we are
00169   // working on (if we do, it may result in an extra copy of the data
00170   // due to holding the reference count).
00171   PT(GeomVertexData) _vertex_data;
00172   int _array;
00173   PT(GeomVertexArrayData) _array_data;
00174 
00175   Thread *_current_thread;
00176   GeomVertexColumn::Packer *_packer;
00177   int _stride;
00178 
00179   PT(GeomVertexArrayDataHandle) _handle;
00180   unsigned char *_pointer_begin;
00181   unsigned char *_pointer_end;
00182   unsigned char *_pointer;
00183 
00184   int _start_row;
00185 
00186 #ifndef NDEBUG
00187   // This is defined just for the benefit of having something non-NULL
00188   // to return from a nassertr() call.
00189   static unsigned char empty_buffer[100];
00190 #endif
00191 };
00192 
00193 INLINE ostream &
00194 operator << (ostream &out, const GeomVertexWriter &writer) {
00195   writer.output(out);
00196   return out;
00197 }
00198 
00199 #include "geomVertexWriter.I"
00200 
00201 #endif
 All Classes Functions Variables Enumerations