Panda3D
 All Classes Functions Variables Enumerations
meshDrawer2D.I
00001 // Filename: meshDrawer.I
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 "lpoint2.h"
00016 
00017 ////////////////////////////////////////////////////////////////////
00018 //     Function: MeshDrawer2D::Constructor
00019 //       Access: Published
00020 //  Description: Creates the MeshDrawer2D low level system.
00021 ////////////////////////////////////////////////////////////////////
00022 INLINE MeshDrawer2D::
00023 MeshDrawer2D() {
00024   _root = NodePath("MeshDrawer");  
00025   _bv = NULL;
00026   _vertex = NULL;
00027   _uv = NULL;
00028   _color = NULL;
00029   _budget = 5000;
00030   
00031   _clip_x = -1000000;
00032   _clip_y = -1000000;
00033   _clip_w = 1000000;
00034   _clip_h = 1000000;
00035 }
00036 
00037 ////////////////////////////////////////////////////////////////////
00038 //     Function: MeshDrawer2D::Destructor
00039 //       Access: Published
00040 //  Description: Destroys the MeshDrawer2D low level system.
00041 ////////////////////////////////////////////////////////////////////
00042 INLINE MeshDrawer2D::
00043 ~MeshDrawer2D() {
00044   _root.remove_node();
00045   if (_vertex != NULL) delete _vertex;
00046   if (_uv != NULL)     delete _uv;
00047   if (_color != NULL)  delete _color;
00048 }
00049 
00050 ////////////////////////////////////////////////////////////////////
00051 //     Function: MeshDrawer2D::get_root
00052 //       Access: Published
00053 //  Description: Returns the root NodePath.
00054 ////////////////////////////////////////////////////////////////////
00055 INLINE NodePath MeshDrawer2D::
00056 get_root() {
00057   return _root;
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: MeshDrawer2D::set_budget
00062 //       Access: Published
00063 //  Description: Sets the total triangle budget of the drawer.
00064 ////////////////////////////////////////////////////////////////////
00065 INLINE void MeshDrawer2D::
00066 set_budget(int total_budget) {
00067   _budget = total_budget;
00068   generator(_budget);
00069 }
00070 
00071 ////////////////////////////////////////////////////////////////////
00072 //     Function: MeshDrawer2D::get_budget()
00073 //       Access: Published
00074 //  Description: Gets the total triangle budget of the drawer
00075 ////////////////////////////////////////////////////////////////////
00076 INLINE int MeshDrawer2D::
00077 get_budget() {
00078   return _budget;
00079 }
00080 
00081 ////////////////////////////////////////////////////////////////////
00082 //     Function: MeshDrawer2D::set_budget
00083 //       Access: Published
00084 //  Description: Sets clipping rectangle
00085 ////////////////////////////////////////////////////////////////////
00086 INLINE void MeshDrawer2D::
00087 set_clip(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h) {
00088   _clip_x = x;
00089   _clip_y = y;
00090   _clip_w = w;
00091   _clip_h = h;
00092 }
00093 
00094 ////////////////////////////////////////////////////////////////////
00095 //     Function: MeshDrawer2D::quad
00096 //       Access: Published
00097 //  Description: Draws a 2d rectangle.
00098 //               Ignores the cliping rectangle
00099 ////////////////////////////////////////////////////////////////////
00100 INLINE void MeshDrawer2D::
00101 quad_raw(const LVector3 &v1, const LVector4 &c1, const LVector2 &uv1,
00102      const LVector3 &v2, const LVector4 &c2, const LVector2 &uv2,
00103      const LVector3 &v3, const LVector4 &c3, const LVector2 &uv3,
00104      const LVector3 &v4, const LVector4 &c4, const LVector2 &uv4
00105 ) {
00106 
00107   if( _clear_index > _end_clear_index) return;
00108 
00109   _vertex->add_data3(v1);
00110   _color->add_data4(c1);
00111   _uv->add_data2(uv1);
00112 
00113   _vertex->add_data3(v2);
00114   _color->add_data4(c2);
00115   _uv->add_data2(uv2);
00116 
00117   _vertex->add_data3(v3);
00118   _color->add_data4(c3);
00119   _uv->add_data2(uv3);
00120 
00121   _vertex->add_data3(v4);
00122   _color->add_data4(c4);
00123   _uv->add_data2(uv4);
00124   
00125   _clear_index += 1;
00126 }
00127 
00128 
00129 INLINE void MeshDrawer2D::
00130 rectangle_raw(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h, 
00131      PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs, 
00132      const LVector4 &color
00133 ) {
00134     
00135   quad_raw( 
00136     LVector3(x,   0, y),   color, LVector2(u   , v),
00137     LVector3(x,   0, y+h), color, LVector2(u   , v+vs),
00138     LVector3(x+w, 0, y),   color, LVector2(u+us, v),
00139     LVector3(x+w, 0, y+h), color, LVector2(u+us, v+vs)
00140     );
00141 }
00142 
00143 ////////////////////////////////////////////////////////////////////
00144 //     Function: MeshDrawer2D::quad
00145 //       Access: Published
00146 //  Description: Draws a 2d rectangle, that can be cliped
00147 ////////////////////////////////////////////////////////////////////
00148 INLINE void MeshDrawer2D::
00149 rectangle(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h, 
00150      PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs, 
00151      const LVector4 &color
00152 ) {
00153     
00154   if( w == 0 &&  h == 0 ) return; // no size return 
00155   if (x > _clip_x+_clip_w) return;  // we are left of the clip
00156   if (y > _clip_y+_clip_h) return;  // we are above of the clip
00157   if (x+w < _clip_x) return; // we are right of the clip
00158   if (y+h < _clip_y) return; // we are bellow clip 
00159     
00160   // the rectange fits but it might need to be cliped
00161   
00162   PN_stdfloat x_uv_ratio = us/w;
00163   PN_stdfloat y_uv_ratio = vs/h;
00164   PN_stdfloat dt = 0;
00165       
00166   if (x < _clip_x){
00167     // clip right 
00168     dt = _clip_x-x;
00169     x  += dt;
00170     w  -= dt;              
00171     u  += dt*x_uv_ratio;
00172     us -= dt*x_uv_ratio;
00173   } 
00174                        
00175   if (y < _clip_y){
00176     // clip bottom
00177     dt = _clip_y-y;
00178     y  += dt;
00179     h  -= dt;
00180     v  += dt*y_uv_ratio;
00181     vs -= dt*y_uv_ratio;
00182   }
00183       
00184   if (x+w > _clip_x+_clip_w){
00185     // clip left 
00186     dt = x+w - (_clip_x+_clip_w);
00187     w  -= dt;
00188     us -= dt*x_uv_ratio;
00189   }                
00190      
00191   if (y+h > _clip_y+_clip_h){
00192     // clip top 
00193     dt = y+h - (_clip_y+_clip_h);
00194     h  -= dt; 
00195     vs -= dt*y_uv_ratio;
00196   }
00197 
00198   // we made it lets draw the quad
00199   rectangle_raw(x,y,w,h,u,v,us,vs,color);
00200     
00201 }
00202 
 All Classes Functions Variables Enumerations