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