00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00029
00030
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
00047
00048
00049
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
00098
00099
00100
00101
00102 PT(PandaNode) CardMaker::
00103 rescale_source_geometry() {
00104 PT(PandaNode) root = _source_geometry->copy_subgraph();
00105
00106
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
00132 SceneGraphReducer reducer;
00133 reducer.apply_attribs(root);
00134 reducer.flatten(root, true);
00135
00136 return root;
00137 }