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

panda/src/testbed/demo.cxx

Go to the documentation of this file.
00001 // Filename: demo.cxx
00002 // Created by:  
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 "framework.h"
00020 
00021 #include "eventHandler.h"
00022 #include "chancfg.h"
00023 #include "string"
00024 #include "renderModeTransition.h"
00025 #include "colorTransition.h"
00026 #include "colorBlendTransition.h"
00027 #include "cullFaceTransition.h"
00028 #include "depthTestTransition.h"
00029 #include "depthWriteTransition.h"
00030 #include "textureTransition.h"
00031 #include "textureApplyTransition.h"
00032 #include "materialTransition.h"
00033 #include "transformTransition.h"
00034 #include "transparencyTransition.h"
00035 #include "drawBoundsTransition.h"
00036 #include "pruneTransition.h"
00037 #include "get_rel_pos.h"
00038 #include "boundingSphere.h"
00039 #include "geomSphere.h"
00040 #include "geomNode.h"
00041 #include "notify.h"
00042 #include "directionalLight.h"
00043 #include "renderRelation.h"
00044 #include "camera.h"
00045 #include "frustum.h"
00046 #include "textNode.h"
00047 #include "colorMatrixTransition.h"
00048 #include "alphaTransformTransition.h"
00049 #include "lensFlareNode.h"
00050 #include "texture.h"
00051 #include "texturePool.h"
00052 #include "spotlight.h"
00053 #include "nodePath.h"
00054 #include "pta_Colorf.h"
00055 #include "pta_float.h"
00056 #include "pt_Node.h"
00057 #include "panda.h"
00058 
00059 // If we're doing a static link, we should explicitly initialize some
00060 // of our external libraries, or they may not get linked in.
00061 #ifdef LINK_ALL_STATIC
00062   #ifdef HAVE_GL
00063     #include "pandagl.h"
00064   #endif
00065   #ifdef HAVE_DX
00066     #include "pandadx.h"
00067   #endif
00068   #include "pandaegg.h"
00069 #endif
00070 
00071 //From framework
00072 extern PT(GeomNode) geomnode;
00073 extern RenderRelation* first_arc;
00074 
00075 // These variables are used to implement the set_highlight() and
00076 // related functions to allow the user to walk through the scene graph
00077 // with the arrow keys.
00078 
00079 // highlight_render_node is a special node that has a funny render
00080 // mode set on it to draw things in red wireframe, but it has no
00081 // geometry of its own.  The node that we're currently highlighting
00082 // (current_node) is instanced under highlight_render_node via
00083 // current_arc, so that the highlight applies to that node.
00084 RenderRelation *highlight_arc = NULL;
00085 PT_NamedNode highlight_render_node;
00086 RenderRelation *current_arc = NULL;
00087 
00088 NodePath selected_node;
00089 
00090 PT_NamedNode render2d_top;
00091 RenderRelation *render2d_arc = NULL;
00092 PT_NamedNode render2d;
00093 
00094 PT(TextNode) label2d;
00095 
00096 //Global Node used by LensFlare and NodePath color/alpha demos
00097 PT_NamedNode sky = (NamedNode *)NULL;
00098 RenderRelation *sky_arc = (RenderRelation *)NULL;
00099 RenderRelation *flare_arc = (RenderRelation *)NULL;
00100 
00101 
00102 static void
00103 clear_highlight() {
00104   if (selected_node.has_arcs()) {
00105     selected_node.hide_bounds();
00106   }
00107 
00108   if (current_arc != NULL) {
00109     remove_arc(current_arc);
00110     current_arc = NULL;
00111   }
00112 }
00113 
00114 static void
00115 set_highlight() {
00116   // Transform the highlight to the coordinate space of the node.
00117   LMatrix4f mat = selected_node.get_mat(NodePath(render));
00118   highlight_arc->set_transition(new TransformTransition(mat));
00119 
00120   nout << "Highlighting " << selected_node.as_string(1) << "\n";
00121 
00122   nout << "Bounding volume of node is " << selected_node.node()->get_bound() << "\n";
00123 
00124   if (selected_node.has_arcs()) {
00125     nout << "Bounding volume of arc is " << *selected_node.get_bounds() << "\n";
00126 
00127     nout << "Transitions on arc:\n";
00128     selected_node.arc()->write_transitions(nout, 2);
00129     selected_node.show_bounds();
00130   }
00131 
00132   current_arc = new RenderRelation(highlight_render_node, selected_node.node());
00133 }
00134 
00135 static void
00136 event_up(CPT_Event) {
00137   if (selected_node.has_arcs() && selected_node.get_node(1) != root) {
00138     clear_highlight();
00139     selected_node.shorten(1);
00140     set_highlight();
00141   }
00142 }
00143 
00144 static void
00145 event_down(CPT_Event) {
00146   if (!selected_node.is_empty() &&
00147       selected_node.get_num_children() != 0) {
00148     clear_highlight();
00149     selected_node = selected_node.get_child(0);
00150     set_highlight();
00151   }
00152 }
00153 
00154 static void
00155 event_left(CPT_Event) {
00156   // Go to the previous child in the sibling list, if there is one.
00157   if (selected_node.has_arcs()) {
00158     NodePath parent = selected_node;
00159     parent.shorten(1);
00160     int num_children = parent.get_num_children();
00161 
00162     for (int i = 0; i < num_children; i++) {
00163       if (parent.get_child(i) == selected_node) {
00164         // We've currently got child i; now select child i-1.
00165         if (i - 1 >=  0) {
00166           clear_highlight();
00167           selected_node = parent.get_child(i - 1);
00168           set_highlight();
00169         }
00170         return;
00171       }
00172     }
00173   }
00174 }
00175 
00176 static void
00177 event_right(CPT_Event) {
00178   // Go to the next child in the sibling list, if there is one.
00179   if (selected_node.has_arcs()) {
00180     NodePath parent = selected_node;
00181     parent.shorten(1);
00182     int num_children = parent.get_num_children();
00183 
00184     for (int i = 0; i < num_children; i++) {
00185       if (parent.get_child(i) == selected_node) {
00186         // We've currently got child i; now select child i + 11.
00187         if (i + 1 < num_children) {
00188           clear_highlight();
00189           selected_node = parent.get_child(i + 1);
00190           set_highlight();
00191         }
00192         return;
00193       }
00194     }
00195   }
00196 }
00197 
00198 static void
00199 event_fkey(CPT_Event event) {
00200   if (selected_node.has_arcs()) {
00201     string name = event->get_name();
00202     if (name.substr(0, 6) == "shift-") {
00203       // Shift-fkey: work with color scale.
00204       if (name == "shift-f9") {
00205         selected_node.clear_color_scale();
00206         selected_node.clear_transparency();
00207         cerr << "Clearing color scale on " << selected_node << "\n";
00208 
00209       } else {
00210         Colorf color_scale;
00211         if (name == "shift-f1") {
00212           color_scale.set(0.5, 0.5, 1.0, 1.0);
00213         } else if (name == "shift-f2") {
00214           color_scale.set(0.5, 1.0, 0.5, 1.0);
00215         } else if (name == "shift-f3") {
00216           color_scale.set(0.5, 1.0, 1.0, 1.0);
00217         } else if (name == "shift-f4") {
00218           color_scale.set(1.0, 0.5, 0.5, 1.0);
00219         } else if (name == "shift-f5") {
00220           color_scale.set(1.0, 0.5, 1.0, 1.0);
00221         } else if (name == "shift-f6") {
00222           color_scale.set(1.0, 1.0, 0.5, 1.0);
00223         } else if (name == "shift-f7") {
00224           color_scale.set(1.0, 1.0, 1.0, 1.0);
00225         } else if (name == "shift-f8") {
00226           color_scale.set(1.0, 1.0, 1.0, 0.5);
00227         }
00228         selected_node.set_color_scale(color_scale);
00229         if (color_scale[3] != 1.0) { 
00230           selected_node.set_transparency(true);
00231         }
00232         cerr << "Setting color scale on " << selected_node << " to " << color_scale << "\n";
00233       }
00234     } else {
00235       // Non shifted fkey: work with flat color.
00236         
00237       if (name == "f9") {
00238         // F9: restore the natural color.
00239         selected_node.clear_color();
00240         selected_node.clear_transparency();
00241         cerr << "Clearing color on " << selected_node << "\n";
00242         
00243       } else {
00244         Colorf color;
00245         if (name == "f1") {
00246           color.set(0.0, 0.0, 1.0, 1.0);
00247         } else if (name == "f2") {
00248           color.set(0.0, 1.0, 0.0, 1.0);
00249         } else if (name == "f3") {
00250           color.set(0.0, 1.0, 1.0, 1.0);
00251         } else if (name == "f4") {
00252           color.set(1.0, 0.0, 0.0, 1.0);
00253         } else if (name == "f5") {
00254           color.set(1.0, 0.0, 1.0, 1.0);
00255         } else if (name == "f6") {
00256           color.set(1.0, 1.0, 0.0, 1.0);
00257         } else if (name == "f7") {
00258           color.set(1.0, 1.0, 1.0, 1.0);
00259         } else if (name == "f8") {
00260           color.set(1.0, 1.0, 1.0, 0.5);
00261         }
00262         selected_node.set_color(color, 1);
00263         if (color[3] != 1.0) { 
00264           selected_node.set_transparency(true);
00265         }
00266         cerr << "Setting color on " << selected_node << " to " << color << "\n";
00267       }
00268     }
00269   }
00270 }
00271 
00272 static void
00273 event_B(CPT_Event event) {
00274   // List everything under the selected bounding volume and
00275   // recompute the volume.
00276   selected_node.ls();
00277   selected_node.analyze();
00278 
00279   if (selected_node.has_arcs()) {
00280     selected_node.arc()->force_bound_stale();
00281   }
00282 }
00283 
00284 static void
00285 enable_highlight() {
00286   if (highlight_render_node == NULL) {
00287     highlight_render_node = new NamedNode("highlight");
00288     highlight_arc = new RenderRelation(render, highlight_render_node);
00289 
00290     // Set up the funny rendering attributes on the highlighted
00291     // geometry.
00292     RenderModeTransition *rmt =
00293       new RenderModeTransition(RenderModeProperty::M_wireframe);
00294     ColorTransition *ct =
00295       new ColorTransition(1.0, 0.0, 0.0, 1.0);
00296     CullFaceTransition *cft =
00297       new CullFaceTransition(CullFaceProperty::M_cull_none);
00298     TextureTransition *tt =
00299       new TextureTransition;
00300 
00301     rmt->set_priority(100);
00302     ct->set_priority(100);
00303     cft->set_priority(100);
00304     tt->set_priority(100);
00305 
00306     highlight_arc->set_transition(rmt);
00307     highlight_arc->set_transition(ct);
00308     highlight_arc->set_transition(cft);
00309     highlight_arc->set_transition(tt);
00310   }
00311 
00312   // Add a temporary arc from the highlight render node to the node we
00313   // are highlighting.
00314   selected_node = NodePath(root);
00315   if (selected_node.get_num_children() == 0) {
00316     nout << "No nodes.\n";
00317     selected_node.clear();
00318 
00319   } else {
00320     selected_node = selected_node.get_child(0);
00321     set_highlight();
00322   }
00323 }
00324 
00325 static void
00326 disable_highlight() {
00327   nout << "Disabling highlight\n";
00328   clear_highlight();
00329   selected_node.clear();
00330 }
00331 
00332 static void
00333 event_h(CPT_Event) {
00334   if (selected_node.is_empty()) {
00335     enable_highlight();
00336   } else {
00337     disable_highlight();
00338   }
00339 }
00340 
00341 static bool attach_sky() {
00342   // Load the sun and sky
00343   sky = DCAST(NamedNode, loader.load_sync("sky"));
00344   if (sky != (NamedNode *)NULL) {
00345     if (sky_arc == (RenderRelation *)NULL) {
00346       sky_arc = new RenderRelation(render, sky);
00347     }
00348   }
00349   return (sky!=NULL) && (sky_arc!=NULL);
00350 }
00351 
00352 static void
00353 event_k(CPT_Event) {
00354   static bool is_color_mat = false;
00355 
00356   if (!is_color_mat) {
00357     LMatrix4f color_mat = LMatrix4f::scale_mat(0,0,0) * LMatrix4f::translate_mat(1,0,0);
00358 
00359     first_arc->set_transition(new ColorMatrixTransition(color_mat));
00360   }
00361   else {
00362     LMatrix4f color_mat = LMatrix4f::ident_mat();
00363 
00364     first_arc->set_transition(new ColorMatrixTransition(color_mat));
00365   }
00366   is_color_mat = !is_color_mat;
00367 }
00368 
00369 static void
00370 event_a(CPT_Event) {
00371   static bool is_alpha = false;
00372 
00373   if (!is_alpha) {
00374     first_arc->set_transition(new AlphaTransformTransition(-0.5, 1));
00375     first_arc->set_transition(new TransparencyTransition(TransparencyProperty::M_alpha));
00376   }
00377   else {
00378     first_arc->clear_transition(AlphaTransformTransition::get_class_type());
00379     first_arc->clear_transition(TransparencyTransition::get_class_type());
00380   }
00381   is_alpha = !is_alpha;
00382 }
00383 
00384 
00385 static void
00386 event_v(CPT_Event) {
00387   static bool is_color_scale = false;
00388 
00389   if(!attach_sky())
00390     return;
00391 
00392   NodePath search(sky);
00393   NodePath sky_search = search.find("**/sun");
00394 
00395   if (!is_color_scale) {
00396     sky_search.set_color_scale(1, 0.5, 0.5, 0.5);
00397     sky_search.set_transparency(true);
00398   }
00399   else {
00400     sky_search.clear_color_scale();
00401     sky_search.clear_transparency();
00402   }
00403   is_color_scale = !is_color_scale;
00404 }
00405 
00406 static void
00407 event_L(CPT_Event) {
00408   static bool is_flare = false;
00409 
00410   if(!attach_sky())
00411     return;
00412 
00413   if (!is_flare) {
00414     //Texture *shine = TexturePool::load_texture("MyShine.bw");
00415     Texture *shine = TexturePool::load_texture("bigsmileycrop.rgba");
00416 
00417     Texture *f4 = TexturePool::load_texture("Flare4.bw");
00418     Texture *f5 = TexturePool::load_texture("Flare5.bw");
00419     Texture *f6 = TexturePool::load_texture("Flare6.bw");
00420 
00421     LensFlareNode *flare = new LensFlareNode();
00422 
00423     PTA_float scales, offsets, angles;
00424     PTA_Colorf colors;
00425 
00426     scales.push_back(0.5); offsets.push_back(0.2); angles.push_back(0); colors.push_back(Colorf(0.3, 0.6, 0.3, 1));
00427     scales.push_back(0.5); offsets.push_back(0.4); angles.push_back(0); colors.push_back(Colorf(0.6, 0.6, 0.6, 1));
00428     scales.push_back(0.75); offsets.push_back(0.7); angles.push_back(0.2); colors.push_back(Colorf(0.3, 0.6, 0.3, 1));
00429     scales.push_back(1.5); offsets.push_back(1.2); angles.push_back(0); colors.push_back(Colorf(0.6, 0.6, 0.6, 1));
00430     scales.push_back(0.75); offsets.push_back(1.5); angles.push_back(0); colors.push_back(Colorf(0.6, 0.6, 0.6, 1));
00431 
00432     flare->add_flare(f6, scales, offsets, angles, colors);
00433 
00434     scales.clear(); offsets.clear(); angles.clear(); colors.clear();
00435 
00436     scales.push_back(0.3); offsets.push_back(0.55); angles.push_back(0); colors.push_back(Colorf(0.0, 0.0, 0.6, 1));
00437     scales.push_back(0.3); offsets.push_back(0.8); angles.push_back(0); colors.push_back(Colorf(0.0, 0.0, 0.6, 1));
00438     scales.push_back(0.5); offsets.push_back(1.1); angles.push_back(0); colors.push_back(Colorf(0.0, 0.0, 0.6, 1));
00439     scales.push_back(0.15); offsets.push_back(1.35); angles.push_back(0.2); colors.push_back(Colorf(0.3, 0.6, 0.3, 1));
00440 
00441     flare->add_flare(f5, scales, offsets, angles, colors);
00442 
00443     scales.clear(); offsets.clear(); angles.clear(); colors.clear();
00444 
00445     scales.push_back(1); offsets.push_back(0.0); angles.push_back(-0.3); colors.push_back(Colorf(1.0, 0.0, 0.0, 1));
00446     scales.push_back(1.05); offsets.push_back(0.0); angles.push_back(-0.3); colors.push_back(Colorf(0.0, 1.0, 0.0, 1));
00447     scales.push_back(1.1); offsets.push_back(0.0); angles.push_back(-0.3); colors.push_back(Colorf(0.0, 0.0, 1.0, 1));
00448 
00449     flare->add_flare(f4, scales, offsets, angles, colors);
00450 
00451     flare->add_blind(shine);
00452     flare->set_blind_falloff(5);
00453     flare->set_flare_falloff(45);
00454 
00455     NodePath search(sky);
00456     NodePath sky_search = search.find("**/sun");
00457     PT_Node light = sky_search.node();
00458 
00459     flare->set_light_source(light);
00460     flare_arc = new RenderRelation(light, flare, 10);
00461     ColorBlendTransition *cb = new ColorBlendTransition(ColorBlendProperty::M_add);
00462     flare_arc->set_transition(cb);
00463 
00464     TextureApplyTransition *ta =
00465       new TextureApplyTransition(TextureApplyProperty::M_decal);
00466     flare_arc->set_transition(ta);
00467 
00468     DepthTestTransition *dta =
00469       new DepthTestTransition(DepthTestProperty::M_none);
00470     flare_arc->set_transition(dta);
00471   }
00472   else {
00473     remove_arc(flare_arc);
00474   }
00475 
00476   is_flare = !is_flare;
00477 }
00478 
00479 void demo_keys(EventHandler&) {
00480   /*
00481   event_handler.add_hook("mw-in-label2d", event_in_label2d);
00482   event_handler.add_hook("mw-out-label2d", event_out_label2d);
00483   */
00484 
00485   event_handler.add_hook("h", event_h);
00486   event_handler.add_hook("up", event_up);
00487   event_handler.add_hook("down", event_down);
00488   event_handler.add_hook("left", event_left);
00489   event_handler.add_hook("right", event_right);
00490   event_handler.add_hook("f1", event_fkey);
00491   event_handler.add_hook("f2", event_fkey);
00492   event_handler.add_hook("f3", event_fkey);
00493   event_handler.add_hook("f4", event_fkey);
00494   event_handler.add_hook("f5", event_fkey);
00495   event_handler.add_hook("f6", event_fkey);
00496   event_handler.add_hook("f7", event_fkey);
00497   event_handler.add_hook("f8", event_fkey);
00498   event_handler.add_hook("f9", event_fkey);
00499   event_handler.add_hook("shift-f1", event_fkey);
00500   event_handler.add_hook("shift-f2", event_fkey);
00501   event_handler.add_hook("shift-f3", event_fkey);
00502   event_handler.add_hook("shift-f4", event_fkey);
00503   event_handler.add_hook("shift-f5", event_fkey);
00504   event_handler.add_hook("shift-f6", event_fkey);
00505   event_handler.add_hook("shift-f7", event_fkey);
00506   event_handler.add_hook("shift-f8", event_fkey);
00507   event_handler.add_hook("shift-f9", event_fkey);
00508   event_handler.add_hook("shift-b", event_B);
00509 
00510   event_handler.add_hook("shift-l", event_L);
00511   event_handler.add_hook("k", event_k);
00512   event_handler.add_hook("a", event_a);
00513   event_handler.add_hook("v", event_v);
00514 }
00515 
00516 int main(int argc, char *argv[]) {
00517   // We call init_libpanda() to be paranoid.  It's not supposed to be
00518   // necessary, but it turns out that static init isn't dependable in
00519   // all cases.
00520   init_libpanda();
00521 
00522   // If we're doing a static link, we should explicitly initialize some
00523   // of our external libraries, or they may not get linked in.
00524 #ifdef LINK_ALL_STATIC
00525   #ifdef HAVE_GL
00526   init_libpandagl();
00527   #endif
00528   #ifdef HAVE_DX
00529   init_libpandadx();
00530   #endif
00531   init_libpandaegg();
00532 #endif
00533 
00534   define_keys = &demo_keys;
00535   return framework_main(argc, argv);
00536 }

Generated on Fri May 2 00:44:07 2003 for Panda by doxygen1.3