Panda3D
 All Classes Functions Variables Enumerations
egg_parametrics.cxx
1 // Filename: egg_parametrics.cxx
2 // Created by: drose (13Oct03)
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 "egg_parametrics.h"
16 #include "config_egg2pg.h"
17 
18 ////////////////////////////////////////////////////////////////////
19 // Function: make_nurbs_surface
20 // Description: Returns a new NurbsSurfaceEvaluator that's filled in
21 // with the values from the given EggSurface (and
22 // transformed by the indicated matrix), or NULL if the
23 // object is invalid. If there is vertex color, it will
24 // be applied to values 0 - 3 of the extended vertex
25 // values.
26 ////////////////////////////////////////////////////////////////////
28 make_nurbs_surface(EggNurbsSurface *egg_surface, const LMatrix4d &mat) {
29  if (egg_surface->get_u_order() < 1 || egg_surface->get_u_order() > 4) {
30  egg2pg_cat.error()
31  << "Invalid NURBSSurface U order for " << egg_surface->get_name() << ": "
32  << egg_surface->get_u_order() << "\n";
33  return NULL;
34  }
35 
36  if (egg_surface->get_v_order() < 1 || egg_surface->get_v_order() > 4) {
37  egg2pg_cat.error()
38  << "Invalid NURBSSurface V order for " << egg_surface->get_name() << ": "
39  << egg_surface->get_v_order() << "\n";
40  return NULL;
41  }
42 
44 
45  nurbs->set_u_order(egg_surface->get_u_order());
46  nurbs->set_v_order(egg_surface->get_v_order());
47 
48  int num_u_vertices = egg_surface->get_num_u_cvs();
49  int num_v_vertices = egg_surface->get_num_v_cvs();
50  nurbs->reset(num_u_vertices, num_v_vertices);
51  for (int ui = 0; ui < num_u_vertices; ui++) {
52  for (int vi = 0; vi < num_v_vertices; vi++) {
53  int i = egg_surface->get_vertex_index(ui, vi);
54  EggVertex *egg_vertex = egg_surface->get_vertex(i);
55  nurbs->set_vertex(ui, vi, LCAST(PN_stdfloat, egg_vertex->get_pos4() * mat));
56 
57  LColor color = egg_vertex->get_color();
58  nurbs->set_extended_vertices(ui, vi, 0, color.get_data(), 4);
59  }
60  }
61 
62  int num_u_knots = egg_surface->get_num_u_knots();
63  if (num_u_knots != nurbs->get_num_u_knots()) {
64  egg2pg_cat.error()
65  << "Invalid NURBSSurface number of U knots for "
66  << egg_surface->get_name() << ": got " << num_u_knots
67  << " knots, expected " << nurbs->get_num_u_knots() << "\n";
68  return NULL;
69  }
70 
71  int num_v_knots = egg_surface->get_num_v_knots();
72  if (num_v_knots != nurbs->get_num_v_knots()) {
73  egg2pg_cat.error()
74  << "Invalid NURBSSurface number of U knots for "
75  << egg_surface->get_name() << ": got " << num_v_knots
76  << " knots, expected " << nurbs->get_num_v_knots() << "\n";
77  return NULL;
78  }
79 
80  int i;
81  for (i = 0; i < num_u_knots; i++) {
82  nurbs->set_u_knot(i, egg_surface->get_u_knot(i));
83  }
84  for (i = 0; i < num_v_knots; i++) {
85  nurbs->set_v_knot(i, egg_surface->get_v_knot(i));
86  }
87 
88  return nurbs;
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: make_nurbs_curve
93 // Description: Returns a new NurbsCurveEvaluator that's filled in
94 // with the values from the given EggCurve (and
95 // transformed by the indicated matrix), or NULL if the
96 // object is invalid. If there is vertex color, it will
97 // be applied to values 0 - 3 of the extended vertex
98 // values.
99 ////////////////////////////////////////////////////////////////////
101 make_nurbs_curve(EggNurbsCurve *egg_curve, const LMatrix4d &mat) {
102  if (egg_curve->get_order() < 1 || egg_curve->get_order() > 4) {
103  egg2pg_cat.error()
104  << "Invalid NURBSCurve order for " << egg_curve->get_name() << ": "
105  << egg_curve->get_order() << "\n";
106  return NULL;
107  }
108 
110  nurbs->set_order(egg_curve->get_order());
111 
112  nurbs->reset(egg_curve->size());
113  EggPrimitive::const_iterator pi;
114  int vi = 0;
115  for (pi = egg_curve->begin(); pi != egg_curve->end(); ++pi) {
116  EggVertex *egg_vertex = (*pi);
117  nurbs->set_vertex(vi, LCAST(PN_stdfloat, egg_vertex->get_pos4() * mat));
118  LColor color = egg_vertex->get_color();
119  nurbs->set_extended_vertices(vi, 0, color.get_data(), 4);
120  vi++;
121  }
122 
123  int num_knots = egg_curve->get_num_knots();
124  if (num_knots != nurbs->get_num_knots()) {
125  egg2pg_cat.error()
126  << "Invalid NURBSCurve number of knots for "
127  << egg_curve->get_name() << ": got " << num_knots
128  << " knots, expected " << nurbs->get_num_knots() << "\n";
129  return NULL;
130  }
131 
132  for (int i = 0; i < num_knots; i++) {
133  nurbs->set_knot(i, egg_curve->get_knot(i));
134  }
135 
136  return nurbs;
137 }
A base class for any of a number of kinds of geometry primitives: polygons, point lights...
Definition: eggPrimitive.h:51
const float * get_data() const
Returns the address of the first of the four data elements in the vector.
Definition: lvecBase4.h:745
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
LColor get_color() const
Returns the color set on this particular attribute.
This class is an abstraction for evaluating NURBS curves.
This class is an abstraction for evaluating NURBS surfaces.
LPoint4d get_pos4() const
This is always valid, regardless of the value of get_num_dimensions.
Definition: eggVertex.I:178
Any one-, two-, three-, or four-component vertex, possibly with attributes such as a normal...
Definition: eggVertex.h:41
A parametric NURBS curve.
Definition: eggNurbsCurve.h:28
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
A parametric NURBS surface.