Panda3D

meshDrawer2D.cxx

00001 // Filename: MeshDrawer2D.cxx
00002 // Created by:  treeform (19dec08)
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 #include "meshDrawer2D.h"
00016 
00017 #include "geomVertexFormat.h"
00018 #include "geomVertexArrayFormat.h"
00019 #include "geomVertexData.h"
00020 #include "geomVertexWriter.h"
00021 #include "geomVertexRewriter.h"
00022 #include "camera.h"
00023 #include "boundingSphere.h"
00024 #include "geomTristrips.h"
00025 #include "geomTriangles.h"
00026 #include "geom.h"
00027 #include "geomNode.h"
00028 #include "pnmPainter.h"
00029 #include "pnmBrush.h"
00030 #include "lvecBase4.h"
00031 #include "lvector3.h"
00032 #include "pandaNode.h"
00033 
00034 TypeHandle MeshDrawer2D::_type_handle;
00035 
00036 
00037 #define RANDF ((PN_stdfloat) rand() / (PN_stdfloat) 0x7fffffff)
00038 
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: MeshDrawer2D::generator
00042 //       Access: Private
00043 //  Description: Creates a system with a given budget.
00044 ////////////////////////////////////////////////////////////////////
00045 void MeshDrawer2D::generator(int budget) {
00046   // create enough triangles for budget:
00047   _vdata = new GeomVertexData(_root.get_name(), GeomVertexFormat::get_v3c4t2(), Geom::UH_static);//UH_dynamic);
00048   GeomVertexWriter *tvertex = new GeomVertexWriter(_vdata, "vertex");
00049   GeomVertexWriter *tuv = new GeomVertexWriter(_vdata, "texcoord");
00050   GeomVertexWriter *tcolor = new GeomVertexWriter(_vdata, "color");
00051   _prim = new GeomTriangles(Geom::UH_static);
00052  
00053   // iterate and fill _up a geom with random data so that it will
00054   // not be optimized out by panda3d system
00055   for(int i = 0; i < budget; i++) {
00056     for( int vert = 0; vert < 4; vert++) {
00057       
00058       LVector3 vec3 = LVector3(RANDF+10000,RANDF,RANDF);
00059       LVector4 vec4 = LVector4(RANDF,RANDF,RANDF,0);
00060       LVector2 vec2 = LVector2(RANDF,RANDF);
00061       
00062       tvertex->add_data3(vec3);
00063       tcolor->add_data4(vec4);
00064       tuv->add_data2(vec2);
00065       
00066     }
00067     
00068     _prim->add_vertices(i*4+0, i*4+1, i*4+2);
00069     _prim->close_primitive();
00070     
00071     _prim->add_vertices(i*4+1, i*4+2, i*4+3);
00072     _prim->close_primitive();
00073     
00074   }
00075   // create our node and attach it to this node path
00076   _geom = new Geom(_vdata);
00077   _geom->add_primitive(_prim);
00078   _geomnode = new GeomNode("__MeshDrawer_GeomNode");
00079   _geomnode->add_geom(_geom);
00080   _root.attach_new_node(_geomnode);
00081   _last_clear_index = budget;
00082 
00083   delete tvertex;
00084   delete tuv;
00085   delete tcolor;
00086 }
00087 
00088 ////////////////////////////////////////////////////////////////////
00089 //     Function: MeshDrawer2D::begin
00090 //       Access: Published
00091 //  Description: Opens up the geom for drawing, don't forget to call
00092 //               MeshDrawer2D::end()
00093 ////////////////////////////////////////////////////////////////////
00094 void MeshDrawer2D::begin() {
00095   
00096   // recreate our rewriters
00097   if (_vertex != NULL) delete _vertex;  
00098   if (_uv != NULL)     delete _uv;
00099   if (_color != NULL)  delete _color;
00100   
00101   _vertex = new GeomVertexRewriter(_vdata, "vertex");
00102   _uv = new GeomVertexRewriter(_vdata, "texcoord");
00103   _color = new GeomVertexRewriter(_vdata, "color");
00104   _dprim = _prim->decompose();
00105 
00106   // reseta our clearning indexes
00107   _start_clear_index = 0;
00108   _end_clear_index = _budget;
00109   _clear_index = _start_clear_index;
00110 
00111 }
00112 
00113 ////////////////////////////////////////////////////////////////////
00114 //     Function: MeshDrawer2D::end
00115 //       Access: Published
00116 //  Description: Finish the drawing and clearing off the remaining
00117 //               vertexes.
00118 ////////////////////////////////////////////////////////////////////
00119 void MeshDrawer2D::end() {
00120 
00121   // clear the unused triangles at the end of the buffer
00122   for(int i = _clear_index ; i < _last_clear_index; i ++ ) {
00123     _vertex->add_data3(0,0,0);
00124     _vertex->add_data3(0,0,0);
00125     _vertex->add_data3(0,0,0);
00126     _vertex->add_data3(0,0,0);
00127   }
00128   // don't clear more then you have too
00129   _last_clear_index = _clear_index;
00130 
00131   // delete the re writers
00132   delete _vertex; _vertex = NULL;
00133   delete _uv;     _uv     = NULL;
00134   delete _color;  _color  = NULL;
00135 
00136 }
00137 
00138 
00139 
00140 ////////////////////////////////////////////////////////////////////
00141 //     Function: MeshDrawer2D::quad
00142 //       Access: Published
00143 //  Description: Draws a tiled rectangle, size of tiles is in 
00144 //               us and vs
00145 ////////////////////////////////////////////////////////////////////
00146 void MeshDrawer2D::
00147 rectangle_tiled(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h, 
00148      PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs, 
00149      const LVector4 &color
00150 ) {
00151 
00152   PN_stdfloat x_fit = w/us;
00153   PN_stdfloat y_fit = h/vs;
00154   PN_stdfloat x_pos = x;
00155   
00156   while (x_fit > 0){
00157     PN_stdfloat y_pos = y;
00158     y_fit = h/vs;           
00159     while (y_fit > 0){
00160     
00161       PN_stdfloat fixed_us = us;
00162       PN_stdfloat fixed_vs = vs;
00163       
00164       // we are cuttin in the middle of a tile x direction
00165       if (x_fit < 1){
00166         fixed_us = w;
00167         while (fixed_us > us){
00168           fixed_us -= us;
00169         }
00170       } 
00171       
00172       // we are cuttin in the middel of a tile y directon
00173       if (y_fit < 1){
00174         fixed_vs = h;
00175         while (fixed_vs > vs){
00176           fixed_vs -= vs;
00177         }
00178       }
00179       
00180       rectangle(x_pos,y_pos,fixed_us,fixed_vs,u,v,fixed_us,fixed_vs,color);
00181       
00182       y_pos += vs;
00183       y_fit -= 1;
00184     }
00185     x_pos += us;
00186     x_fit -= 1;
00187   }
00188             
00189   
00190 }
00191 
00192 
00193 ////////////////////////////////////////////////////////////////////
00194 //     Function: MeshDrawer2D::quad
00195 //       Access: Published
00196 //  Description: Draws a 2d rectangle, with borders and corders, 
00197 //               taken from the surrounding texture
00198 ////////////////////////////////////////////////////////////////////
00199 void MeshDrawer2D::
00200 rectangle_border(
00201     PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
00202     PN_stdfloat r, PN_stdfloat t, PN_stdfloat l, PN_stdfloat b,
00203     PN_stdfloat tr, PN_stdfloat tt, PN_stdfloat tl, PN_stdfloat tb,
00204     PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs, 
00205     const LVector4 &color){
00206     
00207     rectangle(x,y,w,h,u,v,us,vs,color); // center
00208     
00209     //        --------------   -----------------  ------  
00210     rectangle(x,   y+h, w, t,  u,  v+vs, us,  tt, color); // N
00211     rectangle(x,   y-b, w, b,  u,  v-tb, us,  tb, color); // S
00212     
00213     
00214     rectangle(x-l, y,   l, h,  u-tl, v,    tl,  vs, color); // W
00215     rectangle(x+w, y,   r, h,  r,    v,    tr,  vs, color); // E
00216     
00217 /*
00218     rectangle(x-l, y+h, l, t,  u-tl, v,    tl,  tt, color); // NW
00219     rectangle(x-l, y-b, l, b,  u-tl, v-tb, tl,  tb, color); // SW
00220     rectangle(x+w, y+h, r, t,  u,    v,    tr,  tt, color); // NE
00221     rectangle(x+w, y-b, r, b,  u,    v-tb, tr,  tb, color); // SE
00222 */
00223 }
00224 
00225 ////////////////////////////////////////////////////////////////////
00226 //     Function: MeshDrawer2D::quad
00227 //       Access: Published
00228 //  Description: Draws a 2d rectangle, with borders and corders, 
00229 //               taken from the surrounding texture
00230 ////////////////////////////////////////////////////////////////////
00231 void MeshDrawer2D::
00232 rectangle_border_tiled(
00233     PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
00234     PN_stdfloat r, PN_stdfloat t, PN_stdfloat l, PN_stdfloat b,
00235     PN_stdfloat tr, PN_stdfloat tt, PN_stdfloat tl, PN_stdfloat tb,
00236     PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs, 
00237     const LVector4 &color){
00238     
00239     rectangle_tiled(x,y,w,h,u,v,us,vs,color); // center
00240     
00241     rectangle_tiled(x,   y+h, w, t,  u,   v+t, us, t,  color); // N
00242     rectangle_tiled(x,   y-b, w, b,  u,   v-b, us, b,  color); // S
00243     rectangle_tiled(x-l, y,   l, h,  u-l, v,   l,  vs, color); // W
00244     rectangle_tiled(x+w, y,   r, h,  r,   v,   r,  vs, color); // E
00245 
00246     rectangle_tiled(x-l, y+h, l, t,  u-l, v,   l,  t, color); // NW
00247     rectangle_tiled(x-l, y-b, l, b,  u-l, v-b, l,  b, color); // SW
00248     rectangle_tiled(x+w, y+h, r, t,  u,   v,   r,  t, color); // NE
00249     rectangle_tiled(x+w, y-b, r, b,  u,   v-b, r,  b, color); // SE
00250 }
 All Classes Functions Variables Enumerations