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

panda/src/shader/projtexShader.cxx

Go to the documentation of this file.
00001 // Filename: projtexShader.cxx
00002 // Created by:  mike (09Jan97)
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 #include "projtexShader.h"
00019 #include "config_shader.h"
00020 
00021 #include <lightTransition.h>
00022 #include <textureTransition.h>
00023 #include <get_rel_pos.h>
00024 #include <dftraverser.h>
00025 #include <colorBlendTransition.h>
00026 #include <depthTestTransition.h>
00027 #include <graphicsStateGuardian.h>
00028 #include <texGenTransition.h>
00029 #include <texMatrixTransition.h>
00030 #include <perspectiveProjection.h>
00031 #include <attribTraverser.h>
00032 #include <textureApplyTransition.h>
00033 #include <directRenderTraverser.h>
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 // Static variables
00037 ////////////////////////////////////////////////////////////////////
00038 TypeHandle ProjtexShader::_type_handle;
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: ProjtexShader::constructor
00042 //       Access:
00043 //  Description:
00044 ////////////////////////////////////////////////////////////////////
00045 ProjtexShader::ProjtexShader(Texture* texture,
00046                              ColorBlendProperty::Mode mode)
00047   : FrustumShader(), _blend(mode)
00048 {
00049   set_texture(texture);
00050 }
00051 
00052 ////////////////////////////////////////////////////////////////////
00053 //     Function: ProjtexShader::config
00054 //       Access:
00055 //  Description:
00056 ////////////////////////////////////////////////////////////////////
00057 void ProjtexShader::config(void)
00058 {
00059   Configurable::config();
00060 
00061   nassertv(_texture != (Texture *)NULL);
00062 }
00063 
00064 ////////////////////////////////////////////////////////////////////
00065 //     Function: ProjtexShader::apply
00066 //       Access:
00067 //  Description:
00068 ////////////////////////////////////////////////////////////////////
00069 void ProjtexShader::
00070 apply(Node *node, const AllAttributesWrapper &init_state,
00071       const AllTransitionsWrapper &net_trans, GraphicsStateGuardian *gsg) {
00072 
00073   Shader::apply(node, init_state, net_trans, gsg);
00074 
00075   // Make sure the shader has been configured properly
00076   if (get_num_frusta() == 0) {
00077     shader_cat.error()
00078       << "ProjtexShader::apply() - frusta list is empty" << endl;
00079     return;
00080   } else if (get_num_frusta() > 1) {
00081     shader_cat.warning()
00082       << "ProjtexShader::apply() - frusta list has more than one "
00083       << "- ignoring all but first one for now..." << endl;
00084   }
00085 
00086   // Copy the transition wrapper so we can modify it freely.
00087   AllTransitionsWrapper trans(net_trans);
00088 
00089   // Create the texture projector transition
00090   // NOTE: If the node being projected upon has its own textures,
00091   // we need to render it first with its own texturing before
00092   // this projected texture pass and then blend the results
00093   LMatrix4f model_mat;
00094   const Projection* projection = _frusta[0]->get_projection();
00095   get_rel_mat(node, _frusta[0], model_mat);
00096 
00097   // Create the texture generation transition
00098   TexGenTransition *tg = new TexGenTransition;
00099   tg->set_texture_projector();
00100   trans.set_transition(tg);
00101   // Create the texture transition
00102   nassertv(_texture != (Texture *)NULL);
00103   TextureTransition *t = new TextureTransition;
00104   t->set_on(_texture);
00105   t->set_priority(_priority);
00106   trans.set_transition(t);
00107 
00108   if (_viz != (Shader::Visualize*)0L)
00109     _viz->DisplayTexture(_texture, this);
00110   // Create the texture matrix transition
00111   // Define texture matrix so that object space coords used as
00112   // tex coords will be projected onto the frustum's viewing plane
00113   // An additional scale and translate is required to go from x,y
00114   // NDC coords in [-1,1] to tex coords [0,1]
00115   LMatrix4f tex_mat, proj_mat;
00116   const PerspectiveProjection *pp = DCAST(PerspectiveProjection, projection);
00117   proj_mat = pp->get_projection_mat();
00118   tex_mat = model_mat * proj_mat *
00119     LMatrix4f::scale_mat(LVector3f(0.5,0.5,1)) *
00120     LMatrix4f::translate_mat(LVector3f(0.5,0.5,0));
00121 
00122   TexMatrixTransition *tm = new TexMatrixTransition;
00123   tm->set_matrix(tex_mat);
00124   trans.set_transition(tm);
00125 
00126   // Turn lighting off
00127   trans.set_transition(new LightTransition(LightTransition::all_off()));
00128   // Do some extra work if we're doing the 2-pass version (e.g. the
00129   // object we are shading is textured)
00130   if (_multipass_on) {
00131     //Probably should move the brains for determing blending out to
00132     //shader transition
00133 
00134     // Set a color blend mode that assumes this is a second pass over
00135     // textured geometry
00136     ColorBlendTransition *cb = new ColorBlendTransition(_blend);
00137     trans.set_transition(cb);
00138 
00139     TextureApplyTransition *ta =
00140       new TextureApplyTransition(TextureApplyProperty::M_decal);
00141     trans.set_transition(ta);
00142 
00143     // Set the depth test to M_equal (? Or should this be M_none?)
00144     DepthTestTransition *dta =
00145       new DepthTestTransition(DepthTestProperty::M_equal);
00146     trans.set_transition(dta);
00147   }
00148 
00149   DirectRenderTraverser drt(gsg, RenderRelation::get_class_type());
00150   gsg->render_subgraph(&drt, node, init_state, trans);
00151 }
00152 
00153 
00154 

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