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