Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/builder/builderPrim.cxx

Go to the documentation of this file.
00001 // Filename: builderPrim.cxx
00002 // Created by:  drose (10Sep97)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "builderPrim.h"
00020 #include <notify.h>
00021 // Tell GCC that we'll take care of the instantiation explicitly here.
00022 #ifdef __GNUC__
00023 #pragma implementation
00024 #endif
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: BuilderPrim::nonindexed_copy
00028 //       Access: Public
00029 //  Description: Makes a nonindexed copy of the given indexed prim, by
00030 //               looking up the current values of the indexed
00031 //               coordinates in the given bucket.
00032 ////////////////////////////////////////////////////////////////////
00033 BuilderPrim &BuilderPrim::
00034 nonindexed_copy(const BuilderPrimTempl<BuilderVertexI> &copy,
00035                 const BuilderBucket &bucket) {
00036   clear();
00037 
00038   set_type(copy.get_type());
00039 
00040   if (copy.has_normal()) {
00041     nassertr(bucket.get_normals() != (Normalf *)NULL, *this);
00042     set_normal(bucket.get_normals()[copy.get_normal()]);
00043   }
00044   if (copy.has_color()) {
00045     nassertr(bucket.get_colors() != (Colorf *)NULL, *this);
00046     set_color(bucket.get_colors()[copy.get_color()]);
00047   }
00048   if (copy.has_pixel_size()) {
00049     set_pixel_size(copy.get_pixel_size());
00050   }
00051 
00052   int num_verts = copy.get_num_verts();
00053   int i;
00054   for (i = 0; i < num_verts; i++) {
00055     const BuilderVertexI &cv = copy.get_vertex(i);
00056     BuilderVertex v;
00057     if (cv.has_coord()) {
00058       v.set_coord(cv.get_coord_value(bucket));
00059     }
00060 
00061     if (cv.has_normal()) {
00062       v.set_normal(cv.get_normal_value(bucket));
00063     }
00064 
00065     if (cv.has_texcoord()) {
00066       v.set_texcoord(cv.get_texcoord_value(bucket));
00067     }
00068 
00069     if (cv.has_color()) {
00070       v.set_color(cv.get_color_value(bucket));
00071     }
00072 
00073     if (cv.has_pixel_size()) {
00074       v.set_pixel_size(cv.get_pixel_size());
00075     }
00076     add_vertex(v);
00077   }
00078   return *this;
00079 }
00080 
00081 
00082 
00083 ////////////////////////////////////////////////////////////////////
00084 //     Function: BuilderPrim::flatten_vertex_properties
00085 //       Access: Public
00086 //  Description: If all the vertices of the primitive have the same
00087 //               normal, color, etc., removes those properties from
00088 //               the vertices and assigns them to the primitive
00089 //               instead.
00090 //
00091 //               This can provide better meshing by removing
00092 //               properties from otherwise shared vertices.
00093 ////////////////////////////////////////////////////////////////////
00094 void BuilderPrim::
00095 flatten_vertex_properties() {
00096   int num_verts = get_num_verts();
00097   int i;
00098 
00099   if (has_overall_normal()) {
00100     set_normal(get_normal());
00101 
00102     for (i = 0; i < num_verts; i++) {
00103       get_vertex(i).clear_normal();
00104     }
00105   }
00106 
00107   if (has_overall_color()) {
00108     set_color(get_color());
00109 
00110     for (i = 0; i < num_verts; i++) {
00111       get_vertex(i).clear_color();
00112     }
00113   }
00114 
00115   if (has_overall_pixel_size()) {
00116     set_pixel_size(get_pixel_size());
00117 
00118     for (i = 0; i < num_verts; i++) {
00119       get_vertex(i).clear_pixel_size();
00120     }
00121   }
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: BuilderPrim::fill_geom
00126 //       Access: Public
00127 //  Description: Fills up the attribute values of a Geom with the
00128 //               indicated arrays.  This creates a nonindexed Geom.
00129 ////////////////////////////////////////////////////////////////////
00130 void BuilderPrim::
00131 fill_geom(Geom *geom, const PTA_BuilderV &v_array,
00132           GeomBindType n_attr, const PTA_BuilderN &n_array,
00133           GeomBindType t_attr, const PTA_BuilderTC &t_array,
00134           GeomBindType c_attr, const PTA_BuilderC &c_array,
00135           const BuilderBucket &, int, int, int) {
00136 
00137   // WARNING!  This is questionable practice.  We have a
00138   // PTA_BuilderV etc.; since a BuilderV is just a proxy
00139   // to a Vertexf, we can get away with casting this to a
00140   // PTA_Vertexf.
00141 
00142   geom->set_coords((PTA_Vertexf &)v_array);
00143 
00144   if (n_attr != G_OFF) {
00145     geom->set_normals((PTA_Normalf &)n_array, n_attr);
00146   }
00147 
00148   if (t_attr != G_OFF) {
00149     geom->set_texcoords((PTA_TexCoordf &)t_array, t_attr);
00150   }
00151 
00152   if (c_attr != G_OFF) {
00153     geom->set_colors((PTA_Colorf &)c_array, c_attr);
00154   }
00155 }
00156 
00157 ////////////////////////////////////////////////////////////////////
00158 //     Function: BuilderPrimI::flatten_vertex_properties
00159 //       Access: Public
00160 //  Description: If all the vertices of the primitive have the same
00161 //               normal, color, etc., removes those properties from
00162 //               the vertices and assigns them to the primitive
00163 //               instead.
00164 //
00165 //               This can do nothing in the case of an indexed
00166 //               primitive, because we can't monkey with the vertex
00167 //               properties in this case.
00168 ////////////////////////////////////////////////////////////////////
00169 void BuilderPrimI::
00170 flatten_vertex_properties() {
00171 }
00172 
00173 ////////////////////////////////////////////////////////////////////
00174 //     Function: BuilderPrimI::fill_geom
00175 //       Access: Public
00176 //  Description: Fills up the attribute values of a Geom with the
00177 //               indicated arrays.  This creates an indexed Geom.
00178 ////////////////////////////////////////////////////////////////////
00179 void BuilderPrimI::
00180 fill_geom(Geom *geom, const PTA_ushort &v_array,
00181           GeomBindType n_attr, PTA_ushort n_array,
00182           GeomBindType t_attr, PTA_ushort t_array,
00183           GeomBindType c_attr, PTA_ushort c_array,
00184           const BuilderBucket &bucket,
00185           int num_prims, int num_components, int num_verts) {
00186   PTA_Vertexf v_data = bucket.get_coords();
00187   PTA_Normalf n_data = bucket.get_normals();
00188   PTA_TexCoordf t_data = bucket.get_texcoords();
00189   PTA_Colorf c_data = bucket.get_colors();
00190 
00191   // Make sure the data pointers are NULL if the attribute is off.
00192   if (n_attr == G_OFF) {
00193     n_data = NULL;
00194     n_array = NULL;
00195   }
00196   if (t_attr == G_OFF) {
00197     t_data = NULL;
00198     t_array = NULL;
00199   }
00200   if (c_attr == G_OFF) {
00201     c_data = NULL;
00202     c_array = NULL;
00203   }
00204 
00205   int n_len =
00206     (n_attr==G_PER_VERTEX) ? num_verts :
00207     (n_attr==G_PER_COMPONENT) ? num_components :
00208     (n_attr==G_PER_PRIM) ? num_prims :
00209     (n_attr==G_OVERALL) ? 1 : 0;
00210   int t_len =
00211     (t_attr==G_PER_VERTEX) ? num_verts :
00212     (t_attr==G_PER_COMPONENT) ? num_components :
00213     (t_attr==G_PER_PRIM) ? num_prims :
00214     (t_attr==G_OVERALL) ? 1 : 0;
00215   int c_len =
00216     (c_attr==G_PER_VERTEX) ? num_verts :
00217     (c_attr==G_PER_COMPONENT) ? num_components :
00218     (c_attr==G_PER_PRIM) ? num_prims :
00219     (c_attr==G_OVERALL) ? 1 : 0;
00220 
00221   // See if we can share some of the index lists.
00222   if (n_attr != G_OFF &&
00223       memcmp(v_array, n_array, sizeof(ushort) * n_len)==0) {
00224     n_array = v_array;
00225   }
00226   if (t_attr != G_OFF) {
00227     if (memcmp(v_array, t_array, sizeof(ushort) * t_len)==0) {
00228       t_array = v_array;
00229     } else if (t_len <= n_len &&
00230                memcmp(n_array, t_array, sizeof(ushort) * t_len)==0) {
00231       t_array = n_array;
00232     }
00233   }
00234   if (c_attr != G_OFF) {
00235     if (memcmp(v_array, c_array, sizeof(ushort) * c_len)==0) {
00236       c_array = v_array;
00237     } else if (c_len <= n_len &&
00238                memcmp(n_array, c_array, sizeof(ushort) * c_len)==0) {
00239       c_array = n_array;
00240     } else if (c_len <= t_len &&
00241                memcmp(t_array, c_array, sizeof(ushort) * c_len)==0) {
00242       c_array = t_array;
00243     }
00244   }
00245 
00246   geom->set_coords(v_data, v_array);
00247 
00248   if (n_attr != G_OFF) {
00249     geom->set_normals(n_data, n_attr, n_array);
00250   }
00251 
00252   if (t_attr != G_OFF) {
00253     geom->set_texcoords(t_data, t_attr, t_array);
00254   }
00255 
00256   if (c_attr != G_OFF) {
00257     geom->set_colors(c_data, c_attr, c_array);
00258   }
00259 }
00260 

Generated on Fri May 2 00:34:43 2003 for Panda by doxygen1.3