Panda3D
meshDrawer2D.cxx
1 // Filename: MeshDrawer2D.cxx
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 "meshDrawer2D.h"
16 
17 #include "geomVertexFormat.h"
18 #include "geomVertexArrayFormat.h"
19 #include "geomVertexData.h"
20 #include "geomVertexWriter.h"
21 #include "geomVertexRewriter.h"
22 #include "camera.h"
23 #include "boundingSphere.h"
24 #include "geomTristrips.h"
25 #include "geomTriangles.h"
26 #include "geom.h"
27 #include "geomNode.h"
28 #include "pnmPainter.h"
29 #include "pnmBrush.h"
30 #include "lvecBase4.h"
31 #include "lvector3.h"
32 #include "pandaNode.h"
33 
34 TypeHandle MeshDrawer2D::_type_handle;
35 
36 
37 #define RANDF ((PN_stdfloat) rand() / (PN_stdfloat) 0x7fffffff)
38 
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: MeshDrawer2D::generator
42 // Access: Private
43 // Description: Creates a system with a given budget.
44 ////////////////////////////////////////////////////////////////////
45 void MeshDrawer2D::generator(int budget) {
46  // create enough triangles for budget:
47  _vdata = new GeomVertexData(_root.get_name(), GeomVertexFormat::get_v3c4t2(), Geom::UH_static);//UH_dynamic);
48  GeomVertexWriter *tvertex = new GeomVertexWriter(_vdata, "vertex");
49  GeomVertexWriter *tuv = new GeomVertexWriter(_vdata, "texcoord");
50  GeomVertexWriter *tcolor = new GeomVertexWriter(_vdata, "color");
51  _prim = new GeomTriangles(Geom::UH_static);
52 
53  // iterate and fill _up a geom with random data so that it will
54  // not be optimized out by panda3d system
55  for(int i = 0; i < budget; i++) {
56  for( int vert = 0; vert < 4; vert++) {
57 
58  LVector3 vec3 = LVector3(RANDF+10000,RANDF,RANDF);
59  LVector4 vec4 = LVector4(RANDF,RANDF,RANDF,0);
60  LVector2 vec2 = LVector2(RANDF,RANDF);
61 
62  tvertex->add_data3(vec3);
63  tcolor->add_data4(vec4);
64  tuv->add_data2(vec2);
65 
66  }
67 
68  _prim->add_vertices(i*4+0, i*4+1, i*4+2);
69  _prim->close_primitive();
70 
71  _prim->add_vertices(i*4+1, i*4+2, i*4+3);
72  _prim->close_primitive();
73 
74  }
75  // create our node and attach it to this node path
76  _geom = new Geom(_vdata);
77  _geom->add_primitive(_prim);
78  _geomnode = new GeomNode("__MeshDrawer_GeomNode");
79  _geomnode->add_geom(_geom);
80  _root.attach_new_node(_geomnode);
81  _last_clear_index = budget;
82 
83  delete tvertex;
84  delete tuv;
85  delete tcolor;
86 }
87 
88 ////////////////////////////////////////////////////////////////////
89 // Function: MeshDrawer2D::begin
90 // Access: Published
91 // Description: Opens up the geom for drawing, don't forget to call
92 // MeshDrawer2D::end()
93 ////////////////////////////////////////////////////////////////////
95 
96  // recreate our rewriters
97  if (_vertex != NULL) delete _vertex;
98  if (_uv != NULL) delete _uv;
99  if (_color != NULL) delete _color;
100 
101  _vertex = new GeomVertexRewriter(_vdata, "vertex");
102  _uv = new GeomVertexRewriter(_vdata, "texcoord");
103  _color = new GeomVertexRewriter(_vdata, "color");
104  _dprim = _prim->decompose();
105 
106  // reseta our clearning indexes
107  _start_clear_index = 0;
108  _end_clear_index = _budget;
109  _clear_index = _start_clear_index;
110 
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: MeshDrawer2D::end
115 // Access: Published
116 // Description: Finish the drawing and clearing off the remaining
117 // vertexes.
118 ////////////////////////////////////////////////////////////////////
120 
121  // clear the unused triangles at the end of the buffer
122  for(int i = _clear_index ; i < _last_clear_index; i ++ ) {
123  _vertex->add_data3(0,0,0);
124  _vertex->add_data3(0,0,0);
125  _vertex->add_data3(0,0,0);
126  _vertex->add_data3(0,0,0);
127  }
128  // don't clear more then you have too
129  _last_clear_index = _clear_index;
130 
131  // delete the re writers
132  delete _vertex; _vertex = NULL;
133  delete _uv; _uv = NULL;
134  delete _color; _color = NULL;
135 
136 }
137 
138 
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: MeshDrawer2D::quad
142 // Access: Published
143 // Description: Draws a tiled rectangle, size of tiles is in
144 // us and vs
145 ////////////////////////////////////////////////////////////////////
146 void MeshDrawer2D::
147 rectangle_tiled(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
148  PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs,
149  const LVector4 &color
150 ) {
151 
152  PN_stdfloat x_fit = w/us;
153  PN_stdfloat y_fit = h/vs;
154  PN_stdfloat x_pos = x;
155 
156  while (x_fit > 0){
157  PN_stdfloat y_pos = y;
158  y_fit = h/vs;
159  while (y_fit > 0){
160 
161  PN_stdfloat fixed_us = us;
162  PN_stdfloat fixed_vs = vs;
163 
164  // we are cuttin in the middle of a tile x direction
165  if (x_fit < 1){
166  fixed_us = w;
167  while (fixed_us > us){
168  fixed_us -= us;
169  }
170  }
171 
172  // we are cuttin in the middel of a tile y directon
173  if (y_fit < 1){
174  fixed_vs = h;
175  while (fixed_vs > vs){
176  fixed_vs -= vs;
177  }
178  }
179 
180  rectangle(x_pos,y_pos,fixed_us,fixed_vs,u,v,fixed_us,fixed_vs,color);
181 
182  y_pos += vs;
183  y_fit -= 1;
184  }
185  x_pos += us;
186  x_fit -= 1;
187  }
188 
189 
190 }
191 
192 
193 ////////////////////////////////////////////////////////////////////
194 // Function: MeshDrawer2D::quad
195 // Access: Published
196 // Description: Draws a 2d rectangle, with borders and corders,
197 // taken from the surrounding texture
198 ////////////////////////////////////////////////////////////////////
199 void MeshDrawer2D::
201  PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
202  PN_stdfloat r, PN_stdfloat t, PN_stdfloat l, PN_stdfloat b,
203  PN_stdfloat tr, PN_stdfloat tt, PN_stdfloat tl, PN_stdfloat tb,
204  PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs,
205  const LVector4 &color){
206 
207  rectangle(x,y,w,h,u,v,us,vs,color); // center
208 
209  // -------------- ----------------- ------
210  rectangle(x, y+h, w, t, u, v+vs, us, tt, color); // N
211  rectangle(x, y-b, w, b, u, v-tb, us, tb, color); // S
212 
213 
214  rectangle(x-l, y, l, h, u-tl, v, tl, vs, color); // W
215  rectangle(x+w, y, r, h, r, v, tr, vs, color); // E
216 
217 /*
218  rectangle(x-l, y+h, l, t, u-tl, v, tl, tt, color); // NW
219  rectangle(x-l, y-b, l, b, u-tl, v-tb, tl, tb, color); // SW
220  rectangle(x+w, y+h, r, t, u, v, tr, tt, color); // NE
221  rectangle(x+w, y-b, r, b, u, v-tb, tr, tb, color); // SE
222 */
223 }
224 
225 ////////////////////////////////////////////////////////////////////
226 // Function: MeshDrawer2D::quad
227 // Access: Published
228 // Description: Draws a 2d rectangle, with borders and corders,
229 // taken from the surrounding texture
230 ////////////////////////////////////////////////////////////////////
231 void MeshDrawer2D::
233  PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
234  PN_stdfloat r, PN_stdfloat t, PN_stdfloat l, PN_stdfloat b,
235  PN_stdfloat tr, PN_stdfloat tt, PN_stdfloat tl, PN_stdfloat tb,
236  PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs,
237  const LVector4 &color){
238 
239  rectangle_tiled(x,y,w,h,u,v,us,vs,color); // center
240 
241  rectangle_tiled(x, y+h, w, t, u, v+t, us, t, color); // N
242  rectangle_tiled(x, y-b, w, b, u, v-b, us, b, color); // S
243  rectangle_tiled(x-l, y, l, h, u-l, v, l, vs, color); // W
244  rectangle_tiled(x+w, y, r, h, r, v, r, vs, color); // E
245 
246  rectangle_tiled(x-l, y+h, l, t, u-l, v, l, t, color); // NW
247  rectangle_tiled(x-l, y-b, l, b, u-l, v-b, l, b, color); // SW
248  rectangle_tiled(x+w, y+h, r, t, u, v, r, t, color); // NE
249  rectangle_tiled(x+w, y-b, r, b, u, v-b, r, b, color); // SE
250 }
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
void rectangle_tiled(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 tiled rectangle, size of tiles is in us and vs.
void end()
Finish the drawing and clearing off the remaining vertexes.
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
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. ...
NodePath attach_new_node(PandaNode *node, int sort=0, Thread *current_thread=Thread::get_current_thread()) const
Attaches a new node, with or without existing parents, to the scene graph below the referenced node o...
Definition: nodePath.cxx:723
string get_name() const
Returns the name of the referenced node.
Definition: nodePath.I:2580
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
Definition: geom.h:58
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
void rectangle_border_tiled(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h, PN_stdfloat r, PN_stdfloat t, PN_stdfloat l, PN_stdfloat b, PN_stdfloat tr, PN_stdfloat tt, PN_stdfloat tl, PN_stdfloat tb, PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs, const LVector4 &color)
Draws a 2d rectangle, with borders and corders, taken from the surrounding texture.
This is a two-component vector offset.
Definition: lvector2.h:91
void begin()
Opens up the geom for drawing, don&#39;t forget to call MeshDrawer2D::end()
Defines a series of disconnected triangles.
Definition: geomTriangles.h:25
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
void rectangle_border(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h, PN_stdfloat r, PN_stdfloat t, PN_stdfloat l, PN_stdfloat b, PN_stdfloat tr, PN_stdfloat tt, PN_stdfloat tl, PN_stdfloat tb, PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs, const LVector4 &color)
Draws a 2d rectangle, with borders and corders, taken from the surrounding texture.
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:37
This object provides the functionality of both a GeomVertexReader and a GeomVertexWriter, combined together into one convenient package.