Panda3D
|
00001 // Filename: egg_parametrics.cxx 00002 // Created by: drose (13Oct03) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "egg_parametrics.h" 00016 #include "config_egg2pg.h" 00017 00018 //////////////////////////////////////////////////////////////////// 00019 // Function: make_nurbs_surface 00020 // Description: Returns a new NurbsSurfaceEvaluator that's filled in 00021 // with the values from the given EggSurface (and 00022 // transformed by the indicated matrix), or NULL if the 00023 // object is invalid. If there is vertex color, it will 00024 // be applied to values 0 - 3 of the extended vertex 00025 // values. 00026 //////////////////////////////////////////////////////////////////// 00027 PT(NurbsSurfaceEvaluator) 00028 make_nurbs_surface(EggNurbsSurface *egg_surface, const LMatrix4d &mat) { 00029 if (egg_surface->get_u_order() < 1 || egg_surface->get_u_order() > 4) { 00030 egg2pg_cat.error() 00031 << "Invalid NURBSSurface U order for " << egg_surface->get_name() << ": " 00032 << egg_surface->get_u_order() << "\n"; 00033 return NULL; 00034 } 00035 00036 if (egg_surface->get_v_order() < 1 || egg_surface->get_v_order() > 4) { 00037 egg2pg_cat.error() 00038 << "Invalid NURBSSurface V order for " << egg_surface->get_name() << ": " 00039 << egg_surface->get_v_order() << "\n"; 00040 return NULL; 00041 } 00042 00043 PT(NurbsSurfaceEvaluator) nurbs = new NurbsSurfaceEvaluator; 00044 00045 nurbs->set_u_order(egg_surface->get_u_order()); 00046 nurbs->set_v_order(egg_surface->get_v_order()); 00047 00048 int num_u_vertices = egg_surface->get_num_u_cvs(); 00049 int num_v_vertices = egg_surface->get_num_v_cvs(); 00050 nurbs->reset(num_u_vertices, num_v_vertices); 00051 for (int ui = 0; ui < num_u_vertices; ui++) { 00052 for (int vi = 0; vi < num_v_vertices; vi++) { 00053 int i = egg_surface->get_vertex_index(ui, vi); 00054 EggVertex *egg_vertex = egg_surface->get_vertex(i); 00055 nurbs->set_vertex(ui, vi, LCAST(PN_stdfloat, egg_vertex->get_pos4() * mat)); 00056 00057 LColor color = egg_vertex->get_color(); 00058 nurbs->set_extended_vertices(ui, vi, 0, color.get_data(), 4); 00059 } 00060 } 00061 00062 int num_u_knots = egg_surface->get_num_u_knots(); 00063 if (num_u_knots != nurbs->get_num_u_knots()) { 00064 egg2pg_cat.error() 00065 << "Invalid NURBSSurface number of U knots for " 00066 << egg_surface->get_name() << ": got " << num_u_knots 00067 << " knots, expected " << nurbs->get_num_u_knots() << "\n"; 00068 return NULL; 00069 } 00070 00071 int num_v_knots = egg_surface->get_num_v_knots(); 00072 if (num_v_knots != nurbs->get_num_v_knots()) { 00073 egg2pg_cat.error() 00074 << "Invalid NURBSSurface number of U knots for " 00075 << egg_surface->get_name() << ": got " << num_v_knots 00076 << " knots, expected " << nurbs->get_num_v_knots() << "\n"; 00077 return NULL; 00078 } 00079 00080 int i; 00081 for (i = 0; i < num_u_knots; i++) { 00082 nurbs->set_u_knot(i, egg_surface->get_u_knot(i)); 00083 } 00084 for (i = 0; i < num_v_knots; i++) { 00085 nurbs->set_v_knot(i, egg_surface->get_v_knot(i)); 00086 } 00087 00088 return nurbs; 00089 } 00090 00091 //////////////////////////////////////////////////////////////////// 00092 // Function: make_nurbs_curve 00093 // Description: Returns a new NurbsCurveEvaluator that's filled in 00094 // with the values from the given EggCurve (and 00095 // transformed by the indicated matrix), or NULL if the 00096 // object is invalid. If there is vertex color, it will 00097 // be applied to values 0 - 3 of the extended vertex 00098 // values. 00099 //////////////////////////////////////////////////////////////////// 00100 PT(NurbsCurveEvaluator) 00101 make_nurbs_curve(EggNurbsCurve *egg_curve, const LMatrix4d &mat) { 00102 if (egg_curve->get_order() < 1 || egg_curve->get_order() > 4) { 00103 egg2pg_cat.error() 00104 << "Invalid NURBSCurve order for " << egg_curve->get_name() << ": " 00105 << egg_curve->get_order() << "\n"; 00106 return NULL; 00107 } 00108 00109 PT(NurbsCurveEvaluator) nurbs = new NurbsCurveEvaluator; 00110 nurbs->set_order(egg_curve->get_order()); 00111 00112 nurbs->reset(egg_curve->size()); 00113 EggPrimitive::const_iterator pi; 00114 int vi = 0; 00115 for (pi = egg_curve->begin(); pi != egg_curve->end(); ++pi) { 00116 EggVertex *egg_vertex = (*pi); 00117 nurbs->set_vertex(vi, LCAST(PN_stdfloat, egg_vertex->get_pos4() * mat)); 00118 LColor color = egg_vertex->get_color(); 00119 nurbs->set_extended_vertices(vi, 0, color.get_data(), 4); 00120 vi++; 00121 } 00122 00123 int num_knots = egg_curve->get_num_knots(); 00124 if (num_knots != nurbs->get_num_knots()) { 00125 egg2pg_cat.error() 00126 << "Invalid NURBSCurve number of knots for " 00127 << egg_curve->get_name() << ": got " << num_knots 00128 << " knots, expected " << nurbs->get_num_knots() << "\n"; 00129 return NULL; 00130 } 00131 00132 for (int i = 0; i < num_knots; i++) { 00133 nurbs->set_knot(i, egg_curve->get_knot(i)); 00134 } 00135 00136 return nurbs; 00137 }