15 #include "meshDrawer.h" 17 #include "geomVertexFormat.h" 18 #include "geomVertexArrayFormat.h" 19 #include "geomVertexData.h" 20 #include "geomVertexWriter.h" 21 #include "geomVertexRewriter.h" 23 #include "boundingSphere.h" 24 #include "geomTristrips.h" 25 #include "geomTriangles.h" 28 #include "pnmPainter.h" 30 #include "lvecBase4.h" 32 #include "pandaNode.h" 36 PN_stdfloat randFloat() {
37 return ((PN_stdfloat)rand() / (PN_stdfloat)RAND_MAX);
45 void MeshDrawer::generator(
int budget) {
58 for (
int i = 0; i < budget; i++) {
59 for (
int vert = 0; vert < 3; vert++) {
63 tvertex.set_data3(vec3);
64 tcolor.set_data4(vec4);
66 tnormal.set_data3(vec3);
73 _prim->add_next_vertices(budget * 3);
74 _prim->close_primitive();
75 _geom =
new Geom(_vdata);
76 _geom->add_primitive(_prim);
77 if (_geomnode == NULL) {
78 _geomnode =
new GeomNode(
"__MeshDrawer_GeomNode");
81 _geomnode->remove_all_geoms();
83 _geomnode->add_geom(_geom);
84 _last_clear_index = budget;
107 _b1 = - _right - _up;
110 _b4 = - _right + _up;
113 if (_vertex != NULL)
delete _vertex;
114 if (_normal != NULL)
delete _normal;
115 if (_uv != NULL)
delete _uv;
116 if (_color != NULL)
delete _color;
118 if (_vdata == NULL) {
126 _dprim = _prim->decompose();
129 _start_clear_index = 0;
130 _end_clear_index = _budget;
131 _clear_index = _start_clear_index;
144 for(
int i = _clear_index ; i < _last_clear_index; i ++ ) {
150 _last_clear_index = _clear_index;
153 delete _vertex; _vertex = NULL;
154 delete _uv; _uv = NULL;
155 delete _normal; _normal = NULL;
156 delete _color; _color = NULL;
170 rotation = rotation / 57.29578;
172 LVector3 v1 = pos + _b1*size*sin(rotation) + _b2*size*cos(rotation);
173 LVector3 v2 = pos + _b2*size*sin(rotation) + _b3*size*cos(rotation);
174 LVector3 v3 = pos + _b3*size*sin(rotation) + _b4*size*cos(rotation);
175 LVector3 v4 = pos + _b4*size*sin(rotation) + _b1*size*cos(rotation);
177 PN_stdfloat u = frame.get_x();
178 PN_stdfloat v = frame.get_y();
179 PN_stdfloat us = frame.get_z();
180 PN_stdfloat vs = frame.get_w();
200 const LVector4 &frame2, PN_stdfloat blend, PN_stdfloat size,
const LVector4 &
color, PN_stdfloat rotation) {
203 PN_stdfloat original_w = c2.get_w();
204 c2.set_w((1.f-blend)*original_w);
205 particle(pos,frame1,size,c2,rotation);
206 c2.set_w(blend*original_w);
207 particle(pos,frame2,size,c2,rotation);
226 PN_stdfloat u = frame.get_x();
227 PN_stdfloat v = frame.get_y();
228 PN_stdfloat us = frame.get_z();
229 PN_stdfloat vs = frame.get_w();
267 PN_stdfloat u = frame.get_x();
268 PN_stdfloat v = frame.get_y();
269 PN_stdfloat us = frame.get_z();
270 PN_stdfloat vs = frame.get_w();
272 LVector3 v1 = start - _up*thickness;
275 LVector3 v4 = start + _up*thickness;
284 v1 = start - _right*thickness;
285 v2 = stop - _right*thickness;
286 v3 = stop + _right*thickness;
287 v4 = start + _right*thickness;
310 const LVector4 &frame, PN_stdfloat thickness_start,
const LVector4 &color_start,
311 PN_stdfloat thickness_stop,
const LVector4 &color_stop) {
313 PN_stdfloat u = frame.get_x();
314 PN_stdfloat v = frame.get_y();
315 PN_stdfloat us = frame.get_z();
316 PN_stdfloat vs = frame.get_w();
318 LVector3 v1 = start - _up*thickness_start;
319 LVector3 v2 = stop - _up*thickness_stop;
320 LVector3 v3 = stop + _up*thickness_stop;
321 LVector3 v4 = start + _up*thickness_start;
325 v3, color_stop,
LVector2(u+us,v+vs));
330 v1 = start - _right*thickness_start;
331 v2 = stop - _right*thickness_stop;
332 v3 = stop + _right*thickness_stop;
333 v4 = start + _right*thickness_start;
337 v3, color_stop,
LVector2(u+us,v+vs));
351 int seed,
int number, PN_stdfloat distance) {
354 for(
int i = 0; i < number; i++) {
355 relative_pos =
LVector3(randFloat()-.5f,randFloat()-.5f,randFloat()-.5f);
357 relative_pos *= randFloat()*distance;
358 particle(relative_pos+pos,frame,size,_color,randFloat()*360.0f);
370 int number, PN_stdfloat offset) {
372 offset = offset-floor(offset);
375 PN_stdfloat distance = vec.
length();
377 for(
int i = 0; i < number; i++) {
378 relative_pos = start + vec * ((i+offset)*(distance/PN_stdfloat(number)));
379 billboard(relative_pos,frame,size,_color);
409 for(
int j=0; j<geomNode->get_num_geoms(); j++) {
410 CPT(
Geom) geom = geomNode->get_geom(j);
414 for(
int k=0; k <geom->get_num_primitives(); k++) {
419 for(
int p=0; p < _prim->get_num_primitives();p++) {
420 int s = _prim->get_primitive_start(p);
421 int e = _prim->get_primitive_end(p);
425 for(
int idx=s; idx<e; idx++) {
426 int vidx = _prim->get_vertex(idx);
430 current_node_path,prim_vertex_reader->
get_data3());
431 uv[indx_over] = prim_uv_reader->
get_data2();
433 if (indx_over > 2)
break;
437 tri(vec[0],color,uv[0],
442 if( _clear_index > _end_clear_index)
return;
445 delete prim_vertex_reader;
446 delete prim_uv_reader;
485 _last_thickness = thickness;
500 DCAST_INTO_V(camera, _camera.
node());
503 lens->
project(cam_start3d, cam_start2d);
504 lens->project(cam_stop3d, cam_stop2d);
506 LVector2 dif = cam_stop2d - cam_start2d;
507 PN_stdfloat rotation = atan2(dif.get_x(),dif.get_y());
509 LVector3 now_v1 = start + _b1*(PN_stdfloat)(thickness*sin(rotation)) + _b2*(PN_stdfloat)(thickness*cos(rotation));
510 LVector3 now_v4 = start + _b4*(PN_stdfloat)(thickness*sin(rotation)) + _b1*(PN_stdfloat)(thickness*cos(rotation));
511 LVector3 now_v2 = stop + _b2*(PN_stdfloat)(thickness*sin(rotation)) + _b3*(PN_stdfloat)(thickness*cos(rotation));
512 LVector3 now_v3 = stop + _b3*(PN_stdfloat)(thickness*sin(rotation)) + _b4*(PN_stdfloat)(thickness*cos(rotation));
528 LVector3 v2 = (_last_v2+now_v1)/2.0f;
529 LVector3 v3 = (_last_v3+now_v4)/2.0f;
533 PN_stdfloat u = frame.get_x();
534 PN_stdfloat v = frame.get_y();
535 PN_stdfloat us = frame.get_z();
536 PN_stdfloat vs = frame.get_w();
553 _last_thickness = thickness;
567 PN_stdfloat u = frame.get_x();
568 PN_stdfloat v = frame.get_y();
569 PN_stdfloat us = frame.get_z();
570 PN_stdfloat vs = frame.get_w();
574 _last_v3, color,
LVector2(u+us,v+vs));
576 _last_v4, _last_color,
LVector2(u,v+vs),
577 _last_v1, _last_color,
LVector2(u,v));
void segment(const LVector3 &start, const LVector3 &stop, const LVector4 &frame, PN_stdfloat thickness, const LVector4 &color)
Draws a segment a line with a thickness.
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
void explosion(const LVector3 &pos, const LVector4 &frame, PN_stdfloat size, const LVector4 &color, int seed, int number, PN_stdfloat distance)
Draws number of particles in a sphere like emitter.
bool unclean_set_num_rows(int n)
This method behaves like set_num_rows(), except the new data is not initialized.
LVector3 get_relative_vector(const NodePath &other, const LVecBase3 &vec) const
Given that the indicated vector is in the coordinate system of the other node, returns the same vecto...
A base class for any number of different kinds of lenses, linear and otherwise.
void billboard(const LVector3 &pos, const LVector4 &frame, PN_stdfloat size, const LVector4 &color)
Draws a billboard - particle with no rotation.
void end()
Finish the drawing and clearing off the remaining vertexes.
const LVecBase2 & get_data2()
Returns the data associated with the read row, expressed as a 2-component value, and advances the rea...
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Lens * get_lens(int index=0) const
Returns a pointer to the particular Lens associated with this LensNode, or NULL if there is not yet a...
bool project(const LPoint3 &point3d, LPoint3 &point2d) const
Given a 3-d point in space, determine the 2-d point this maps to, in the range (-1,1) in both dimensions, where (0,0) is the center of the lens and (-1,-1) is the lower-left corner.
ErrorType get_error_type() const
If is_empty() is true, this returns a code that represents the reason why the NodePath is empty...
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...
void stream(const LVector3 &start, const LVector3 &stop, const LVector4 &frame, PN_stdfloat size, const LVector4 &color, int number, PN_stdfloat offset)
Draws a number of particles in a big line with a shift dictated by the offset.
string get_name() const
Returns the name of the referenced node.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
const LVecBase3 & get_data3()
Returns the data associated with the read row, expressed as a 3-component value, and advances the rea...
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. ...
NodePath get_path(int index) const
Returns the nth NodePath in the collection.
This is a four-component vector distance.
int get_num_paths() const
Returns the number of NodePaths in the collection.
void blended_particle(const LVector3 &pos, const LVector4 &frame1, const LVector4 &frame2, PN_stdfloat blend, PN_stdfloat size, const LVector4 &color, PN_stdfloat rotation)
Works just like particle but accepts 2 frames and a blend (from 0 to 1) component between them Frame ...
LPoint3 get_pos() const
Retrieves the translation component of the transform.
void cross_segment(const LVector3 &start, const LVector3 &stop, const LVector4 &frame, PN_stdfloat thickness, const LVector4 &color)
Draws a segment a line with a thickness.
float length() const
Returns the length of the vector, by the Pythagorean theorem.
void tri(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)
Draws a triangle with the given parameters.
This is a two-component vector offset.
void particle(const LVector3 &pos, const LVector4 &frame, PN_stdfloat size, const LVector4 &color, PN_stdfloat rotation)
Draws a particle that is sort of like a bill board but has an extra rotation component.
PandaNode * node() const
Returns the referenced node of the path.
This object provides a high-level interface for quickly reading a sequence of numeric values from a v...
Defines a series of disconnected triangles.
void link_segment_end(const LVector4 &frame, const LVector4 &color)
Finish drawing linked segments, needs at least two calls to link_segment before it can end the linked...
void set_row_unsafe(int row)
Sets the start row to the indicated value, without internal checks.
LPoint3 get_relative_point(const NodePath &other, const LVecBase3 &point) const
Given that the indicated point is in the coordinate system of the other node, returns the same point ...
This is a two-component point in space.
TypeHandle is the identifier used to differentiate C++ class types.
A node that can be positioned around in the scene graph to represent a point of view for rendering a ...
void begin(NodePath camera, NodePath render)
Pass the current camera node and the root node.
void uneven_segment(const LVector3 &start, const LVector3 &stop, const LVector4 &frame, PN_stdfloat thickness_start, const LVector4 &color_start, PN_stdfloat thickness_stop, const LVector4 &color_stop)
Draws a segment a line with different thickness and color on both sides.
void geometry(NodePath node)
Draws the geometry that is inside this node path into the MeshDrawer object.
NodePathCollection find_all_matches(const string &path) const
Returns the complete set of all NodePaths that begin with this NodePath and can be extended by path...
bool normalize()
Normalizes the vector in place.
void link_segment(const LVector3 &pos, const LVector4 &frame, PN_stdfloat thickness, const LVector4 &color)
Stars or continues linked segment.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
A node that holds Geom objects, renderable pieces of geometry.
This object provides the functionality of both a GeomVertexReader and a GeomVertexWriter, combined together into one convenient package.
This is a set of zero or more NodePaths.