Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/grutil/cardMaker.cxx

Go to the documentation of this file.
00001 // Filename: cardMaker.cxx
00002 // Created by:  drose (16Mar02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "cardMaker.h"
00020 #include "geomNode.h"
00021 #include "geomTristrip.h"
00022 #include "transformState.h"
00023 #include "colorAttrib.h"
00024 #include "sceneGraphReducer.h"
00025 
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: CardMaker::reset
00029 //       Access: Public
00030 //  Description: Resets all the parameters to their initial defaults.
00031 ////////////////////////////////////////////////////////////////////
00032 void CardMaker::
00033 reset() {
00034   _has_uvs = true;
00035   _ll.set(0.0f, 0.0f);
00036   _ur.set(1.0f, 1.0f);
00037   _frame.set(0.0f, 1.0f, 0.0f, 1.0f);
00038   _has_color = false;
00039   _color.set(1.0f, 1.0f, 1.0f, 1.0f);
00040   _source_geometry = (PandaNode *)NULL;
00041   _source_frame.set(0.0f, 0.0f, 0.0f, 0.0f);
00042 }
00043 
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: CardMaker::generate
00047 //       Access: Public
00048 //  Description: Generates a GeomNode that renders the specified
00049 //               geometry.
00050 ////////////////////////////////////////////////////////////////////
00051 PT(PandaNode) CardMaker::
00052 generate() {
00053   if (_source_geometry != (PandaNode *)NULL) {
00054     return rescale_source_geometry();
00055   }
00056 
00057   PT(GeomNode) gnode = new GeomNode(get_name());
00058   Geom *geom = new GeomTristrip;
00059   gnode->add_geom(geom);
00060 
00061   float left = _frame[0];
00062   float right = _frame[1];
00063   float bottom = _frame[2];
00064   float top = _frame[3];
00065 
00066   PTA_int lengths=PTA_int::empty_array(0);
00067   lengths.push_back(4);
00068 
00069   PTA_Vertexf verts;
00070   verts.push_back(Vertexf::rfu(left, 0.0f, top));
00071   verts.push_back(Vertexf::rfu(left, 0.0f, bottom));
00072   verts.push_back(Vertexf::rfu(right, 0.0f, top));
00073   verts.push_back(Vertexf::rfu(right, 0.0f, bottom));
00074 
00075   geom->set_num_prims(1);
00076   geom->set_lengths(lengths);
00077 
00078   geom->set_coords(verts);
00079 
00080   PTA_Colorf colors;
00081   colors.push_back(_color);
00082   geom->set_colors(colors, G_OVERALL);
00083 
00084   if (_has_uvs) {
00085     PTA_TexCoordf uvs;
00086     uvs.push_back(TexCoordf(_ll[0], _ur[1]));
00087     uvs.push_back(TexCoordf(_ll[0], _ll[1]));
00088     uvs.push_back(TexCoordf(_ur[0], _ur[1]));
00089     uvs.push_back(TexCoordf(_ur[0], _ll[1]));
00090     geom->set_texcoords(uvs, G_PER_VERTEX);
00091   }
00092   
00093   return gnode.p();
00094 }
00095 
00096 ////////////////////////////////////////////////////////////////////
00097 //     Function: CardMaker::rescale_source_geometry
00098 //       Access: Private
00099 //  Description: Generates the card by rescaling the source geometry
00100 //               appropriately.
00101 ////////////////////////////////////////////////////////////////////
00102 PT(PandaNode) CardMaker::
00103 rescale_source_geometry() {
00104   PT(PandaNode) root = _source_geometry->copy_subgraph();
00105 
00106   // Determine the translate and scale appropriate for our geometry.
00107   float geom_center_x = (_source_frame[0] + _source_frame[1]) * 0.5f;
00108   float geom_center_y = (_source_frame[2] + _source_frame[3]) * 0.5f;
00109 
00110   float frame_center_x = (_frame[0] + _frame[1]) * 0.5f;
00111   float frame_center_y = (_frame[2] + _frame[3]) * 0.5f;
00112 
00113   float scale_x = 
00114     (_frame[1] - _frame[0]) / (_source_frame[1] - _source_frame[0]);
00115   float scale_y = 
00116     (_frame[3] - _frame[2]) / (_source_frame[3] - _source_frame[2]);
00117 
00118   LVector3f trans = LVector3f::rfu(frame_center_x - geom_center_x, 0.0f, 
00119                                    frame_center_y - geom_center_y);
00120   LVector3f scale = LVector3f::rfu(scale_x, 1.0f, scale_y);
00121 
00122   CPT(TransformState) transform = 
00123     TransformState::make_pos_hpr_scale(trans, LPoint3f(0.0f, 0.0f, 0.0f),
00124                                        scale);
00125   root->set_transform(transform);
00126 
00127   if (_has_color) {
00128     root->set_attrib(ColorAttrib::make_flat(_color));
00129   }
00130 
00131   // Now flatten out the geometry as much as we can.
00132   SceneGraphReducer reducer;
00133   reducer.apply_attribs(root);
00134   reducer.flatten(root, true);
00135 
00136   return root;
00137 }

Generated on Fri May 2 00:39:45 2003 for Panda by doxygen1.3