Panda3D
 All Classes Functions Variables Enumerations
meshDrawer2D.I
1 // Filename: meshDrawer.I
2 // Created by: treeform (19dec08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "lpoint2.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function: MeshDrawer2D::Constructor
19 // Access: Published
20 // Description: Creates the MeshDrawer2D low level system.
21 ////////////////////////////////////////////////////////////////////
22 INLINE MeshDrawer2D::
24  _root = NodePath("MeshDrawer");
25  _bv = NULL;
26  _vertex = NULL;
27  _uv = NULL;
28  _color = NULL;
29  _budget = 5000;
30 
31  _clip_x = -1000000;
32  _clip_y = -1000000;
33  _clip_w = 1000000;
34  _clip_h = 1000000;
35 }
36 
37 ////////////////////////////////////////////////////////////////////
38 // Function: MeshDrawer2D::Destructor
39 // Access: Published
40 // Description: Destroys the MeshDrawer2D low level system.
41 ////////////////////////////////////////////////////////////////////
42 INLINE MeshDrawer2D::
44  _root.remove_node();
45  if (_vertex != NULL) delete _vertex;
46  if (_uv != NULL) delete _uv;
47  if (_color != NULL) delete _color;
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: MeshDrawer2D::get_root
52 // Access: Published
53 // Description: Returns the root NodePath.
54 ////////////////////////////////////////////////////////////////////
57  return _root;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: MeshDrawer2D::set_budget
62 // Access: Published
63 // Description: Sets the total triangle budget of the drawer.
64 ////////////////////////////////////////////////////////////////////
65 INLINE void MeshDrawer2D::
66 set_budget(int total_budget) {
67  _budget = total_budget;
68  generator(_budget);
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: MeshDrawer2D::get_budget()
73 // Access: Published
74 // Description: Gets the total triangle budget of the drawer
75 ////////////////////////////////////////////////////////////////////
76 INLINE int MeshDrawer2D::
78  return _budget;
79 }
80 
81 ////////////////////////////////////////////////////////////////////
82 // Function: MeshDrawer2D::set_budget
83 // Access: Published
84 // Description: Sets clipping rectangle
85 ////////////////////////////////////////////////////////////////////
86 INLINE void MeshDrawer2D::
87 set_clip(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h) {
88  _clip_x = x;
89  _clip_y = y;
90  _clip_w = w;
91  _clip_h = h;
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function: MeshDrawer2D::quad
96 // Access: Published
97 // Description: Draws a 2d rectangle.
98 // Ignores the cliping rectangle
99 ////////////////////////////////////////////////////////////////////
100 INLINE void MeshDrawer2D::
101 quad_raw(const LVector3 &v1, const LVector4 &c1, const LVector2 &uv1,
102  const LVector3 &v2, const LVector4 &c2, const LVector2 &uv2,
103  const LVector3 &v3, const LVector4 &c3, const LVector2 &uv3,
104  const LVector3 &v4, const LVector4 &c4, const LVector2 &uv4
105 ) {
106 
107  if( _clear_index > _end_clear_index) return;
108 
109  _vertex->add_data3(v1);
110  _color->add_data4(c1);
111  _uv->add_data2(uv1);
112 
113  _vertex->add_data3(v2);
114  _color->add_data4(c2);
115  _uv->add_data2(uv2);
116 
117  _vertex->add_data3(v3);
118  _color->add_data4(c3);
119  _uv->add_data2(uv3);
120 
121  _vertex->add_data3(v4);
122  _color->add_data4(c4);
123  _uv->add_data2(uv4);
124 
125  _clear_index += 1;
126 }
127 
128 
129 INLINE void MeshDrawer2D::
130 rectangle_raw(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
131  PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs,
132  const LVector4 &color
133 ) {
134 
135  quad_raw(
136  LVector3(x, 0, y), color, LVector2(u , v),
137  LVector3(x, 0, y+h), color, LVector2(u , v+vs),
138  LVector3(x+w, 0, y), color, LVector2(u+us, v),
139  LVector3(x+w, 0, y+h), color, LVector2(u+us, v+vs)
140  );
141 }
142 
143 ////////////////////////////////////////////////////////////////////
144 // Function: MeshDrawer2D::quad
145 // Access: Published
146 // Description: Draws a 2d rectangle, that can be cliped
147 ////////////////////////////////////////////////////////////////////
148 INLINE void MeshDrawer2D::
149 rectangle(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
150  PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs,
151  const LVector4 &color
152 ) {
153 
154  if( w == 0 && h == 0 ) return; // no size return
155  if (x > _clip_x+_clip_w) return; // we are left of the clip
156  if (y > _clip_y+_clip_h) return; // we are above of the clip
157  if (x+w < _clip_x) return; // we are right of the clip
158  if (y+h < _clip_y) return; // we are bellow clip
159 
160  // the rectange fits but it might need to be cliped
161 
162  PN_stdfloat x_uv_ratio = us/w;
163  PN_stdfloat y_uv_ratio = vs/h;
164  PN_stdfloat dt = 0;
165 
166  if (x < _clip_x){
167  // clip right
168  dt = _clip_x-x;
169  x += dt;
170  w -= dt;
171  u += dt*x_uv_ratio;
172  us -= dt*x_uv_ratio;
173  }
174 
175  if (y < _clip_y){
176  // clip bottom
177  dt = _clip_y-y;
178  y += dt;
179  h -= dt;
180  v += dt*y_uv_ratio;
181  vs -= dt*y_uv_ratio;
182  }
183 
184  if (x+w > _clip_x+_clip_w){
185  // clip left
186  dt = x+w - (_clip_x+_clip_w);
187  w -= dt;
188  us -= dt*x_uv_ratio;
189  }
190 
191  if (y+h > _clip_y+_clip_h){
192  // clip top
193  dt = y+h - (_clip_y+_clip_h);
194  h -= dt;
195  vs -= dt*y_uv_ratio;
196  }
197 
198  // we made it lets draw the quad
199  rectangle_raw(x,y,w,h,u,v,us,vs,color);
200 
201 }
202 
void set_clip(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h)
Sets clipping rectangle.
Definition: meshDrawer2D.I:87
void quad_raw(const LVector3 &v1, const LVector4 &c1, const LVector2 &uv1, const LVector3 &v2, const LVector4 &c2, const LVector2 &uv2, const LVector3 &v3, const LVector4 &c3, const LVector2 &uv3, const LVector3 &v4, const LVector4 &c4, const LVector2 &uv4)
Draws a 2d rectangle.
Definition: meshDrawer2D.I:101
~MeshDrawer2D()
Destroys the MeshDrawer2D low level system.
Definition: meshDrawer2D.I:43
void add_data2(PN_stdfloat x, PN_stdfloat y)
Sets the write row to a particular 2-component value, and advances the write row. ...
void rectangle(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h, PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs, const LVector4 &color)
Draws a 2d rectangle, that can be cliped.
Definition: meshDrawer2D.I:149
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
NodePath get_root()
Returns the root NodePath.
Definition: meshDrawer2D.I:56
void add_data4(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z, PN_stdfloat w)
Sets the write row to a particular 4-component value, and advances the write row. ...
void add_data3(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z)
Sets the write row to a particular 3-component value, and advances the write row. ...
This is a four-component vector distance.
Definition: lvector4.h:91
int get_budget()
Gets the total triangle budget of the drawer.
Definition: meshDrawer2D.I:77
This is a two-component vector offset.
Definition: lvector2.h:91
void set_budget(int budget)
Sets the total triangle budget of the drawer.
Definition: meshDrawer2D.I:66
void remove_node(Thread *current_thread=Thread::get_current_thread())
Disconnects the referenced node from the scene graph.
Definition: nodePath.cxx:757
MeshDrawer2D()
Creates the MeshDrawer2D low level system.
Definition: meshDrawer2D.I:23
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165