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

panda/src/testbed/test_texmem.cxx

Go to the documentation of this file.
00001 // Filename: test_texmem.cxx
00002 // Created by:  drose (03Sep02)
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 "pandaFramework.h"
00020 #include "geomQuad.h"
00021 #include "textureAttrib.h"
00022 #include "cmath.h"
00023 #include "mathNumbers.h"
00024 
00025 NodePath bogus_scene;
00026 NodePath old_bogus_scene;
00027 
00028 void
00029 event_T(CPT_Event, void *data) {
00030   PandaFramework *framework = (PandaFramework *)data;
00031   WindowFramework *wf = framework->get_window(0);
00032 
00033   GraphicsStateGuardian *gsg = wf->get_graphics_window()->get_gsg();
00034   Camera *camera = wf->get_camera(0);
00035   NodePath models = framework->get_models();
00036   NodePath render = wf->get_render();
00037 
00038   if (!bogus_scene.is_empty()) {
00039     // We are undoing a previous shift-t.
00040     old_bogus_scene = bogus_scene;
00041     old_bogus_scene.detach_node();
00042     bogus_scene = NodePath();
00043     models.show();
00044     return;
00045   }
00046 
00047   // We are doing a new shift-t.  Hide the normal models, and create a
00048   // new bogus node to show the texture grid object.
00049   models.hide();
00050   bogus_scene = render.attach_new_node("bogus");
00051 
00052   // Try to force a flush of the texture memory by making a scene with
00053   // lots of bogus textures.
00054   static const int num_quads_side = 20;
00055   static const int tex_x_size = 256;
00056   static const int tex_y_size = 256;
00057 
00058   cerr << "Loading " << num_quads_side * num_quads_side << " textures at " 
00059        << tex_x_size << ", " << tex_y_size << "\n";
00060 
00061   GeomNode *gnode = new GeomNode("quads");
00062   bogus_scene.attach_new_node(gnode);
00063 
00064   PNMImage white_center(tex_x_size / 4, tex_y_size / 4);
00065   white_center.fill(1.0f, 1.0f, 1.0f);
00066 
00067   PTA_Colorf colors;
00068   colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
00069   for (int yi = 0; yi < num_quads_side; yi++) {
00070     float y0 = (float)yi / (float)num_quads_side;
00071     float y1 = (float)(yi + 1) / (float)num_quads_side;
00072 
00073     // Map the x, y vertices onto a sphere just for fun.
00074     float px0 = ccos((y0 - 0.5f) * MathNumbers::pi_f);
00075     float px1 = ccos((y1 - 0.5f) * MathNumbers::pi_f);
00076     float py0 = csin((y0 - 0.5f) * MathNumbers::pi_f);
00077     float py1 = csin((y1 - 0.5f) * MathNumbers::pi_f);
00078     for (int xi = 0; xi < num_quads_side; xi++) {
00079       float x0 = (float)xi / (float)num_quads_side;
00080       float x1 = (float)(xi + 1) / (float)num_quads_side;
00081 
00082       float hx0 = ccos(x0 * MathNumbers::pi_f * 2.0f);
00083       float hx1 = ccos(x1 * MathNumbers::pi_f * 2.0f);
00084       float hy0 = csin(x0 * MathNumbers::pi_f * 2.0f);
00085       float hy1 = csin(x1 * MathNumbers::pi_f * 2.0f);
00086 
00087       PNMImage bogus_image(tex_x_size, tex_y_size);
00088       bogus_image.fill(x0, (xi + yi) & 1, y0);
00089       bogus_image.copy_sub_image(white_center,
00090                                  (tex_x_size - white_center.get_x_size()) / 2,
00091                                  (tex_y_size - white_center.get_y_size()) / 2);
00092       
00093       PT(Texture) tex = new Texture;
00094       tex->set_minfilter(Texture::FT_linear_mipmap_linear);
00095       tex->load(bogus_image);
00096 
00097       PTA_Vertexf coords;
00098       PTA_TexCoordf uvs;
00099       coords.push_back(Vertexf(hx0 * px0, hy0 * px0, py0));
00100       coords.push_back(Vertexf(hx1 * px0, hy1 * px0, py0));
00101       coords.push_back(Vertexf(hx1 * px1, hy1 * px1, py1));
00102       coords.push_back(Vertexf(hx0 * px1, hy0 * px1, py1));
00103       uvs.push_back(TexCoordf(0.0f, 0.0f));
00104       uvs.push_back(TexCoordf(1.0f, 0.0f));
00105       uvs.push_back(TexCoordf(1.0f, 1.0f));
00106       uvs.push_back(TexCoordf(0.0f, 1.0f));
00107       
00108       PT(GeomQuad) quad = new GeomQuad;
00109       quad->set_coords(coords);
00110       quad->set_colors(colors, G_OVERALL);
00111       quad->set_num_prims(1);
00112       quad->set_texcoords(uvs, G_PER_VERTEX);
00113       gnode->add_geom(quad, RenderState::make(TextureAttrib::make(tex)));
00114     }
00115   }
00116   cerr << "Done.\n";
00117 }
00118 
00119 int
00120 main(int argc, char *argv[]) {
00121   PandaFramework framework;
00122   framework.open_framework(argc, argv);
00123   framework.set_window_title("Panda Viewer");
00124 
00125   WindowFramework *window = framework.open_window();
00126   if (window != (WindowFramework *)NULL) {
00127     // We've successfully opened a window.
00128 
00129     window->enable_keyboard();
00130     window->setup_trackball();
00131     framework.get_models().instance_to(window->get_render());
00132     if (argc < 2) {
00133       // If we have no arguments, get that trusty old triangle out.
00134       window->load_default_model(framework.get_models());
00135     } else {
00136       window->load_models(framework.get_models(), argc, argv);
00137     }
00138     window->loop_animations();
00139 
00140     framework.enable_default_keys();
00141     framework.get_event_handler().add_hook("shift-t", event_T, &framework);
00142     framework.main_loop();
00143   }
00144 
00145   framework.report_frame_rate(nout);
00146   return (0);
00147 }

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