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

pandatool/src/maya/mayaShader.cxx

Go to the documentation of this file.
00001 // Filename: mayaShader.cxx
00002 // Created by:  drose (01Feb00)
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 "mayaShader.h"
00020 #include "maya_funcs.h"
00021 #include "config_maya.h"
00022 #include "string_utils.h"
00023 #include "pnmImageHeader.h"  // for lumin_red, etc.
00024 #include "pset.h"
00025 
00026 #include "pre_maya_include.h"
00027 #include <maya/MFnDependencyNode.h>
00028 #include <maya/MFnLambertShader.h>
00029 #include <maya/MPlug.h>
00030 #include <maya/MPlugArray.h>
00031 #include <maya/MColor.h>
00032 #include <maya/MObject.h>
00033 #include <maya/MStatus.h>
00034 #include "post_maya_include.h"
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //     Function: MayaShader::Constructor
00038 //       Access: Public
00039 //  Description: Reads the Maya "shading engine" to determine the
00040 //               relevant shader properties.
00041 ////////////////////////////////////////////////////////////////////
00042 MayaShader::
00043 MayaShader(MObject engine) {
00044   MFnDependencyNode engine_fn(engine);
00045 
00046   set_name(engine_fn.name().asChar());
00047 
00048   if (maya_cat.is_debug()) {
00049     maya_cat.debug()
00050       << "Reading shading engine " << get_name() << "\n";
00051   }
00052 
00053   bool found_shader = false;
00054   MPlug shader_plug = engine_fn.findPlug("surfaceShader");
00055   if (!shader_plug.isNull()) {
00056     MPlugArray shader_pa;
00057     shader_plug.connectedTo(shader_pa, true, false);
00058 
00059     for (size_t i = 0; i < shader_pa.length() && !found_shader; i++) {
00060       MObject shader = shader_pa[0].node();
00061       found_shader = read_surface_shader(shader);
00062     }
00063   }
00064 }
00065 
00066 ////////////////////////////////////////////////////////////////////
00067 //     Function: MayaShader::Destructor
00068 //       Access: Public
00069 //  Description: 
00070 ////////////////////////////////////////////////////////////////////
00071 MayaShader::
00072 ~MayaShader() {
00073 }
00074 
00075 ////////////////////////////////////////////////////////////////////
00076 //     Function: MayaShader::output
00077 //       Access: Public
00078 //  Description: 
00079 ////////////////////////////////////////////////////////////////////
00080 void MayaShader::
00081 output(ostream &out) const {
00082   out << "Shader " << get_name();
00083 }
00084 
00085 ////////////////////////////////////////////////////////////////////
00086 //     Function: MayaShader::write
00087 //       Access: Public
00088 //  Description: 
00089 ////////////////////////////////////////////////////////////////////
00090 void MayaShader::
00091 write(ostream &out) const {
00092   out << "Shader " << get_name() << "\n"
00093       << "  color:\n";
00094   _color.write(out);
00095   out << "  transparency:\n";
00096   _transparency.write(out);
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function: MayaShader::get_rgba
00101 //       Access: Public
00102 //  Description: Returns the overall color of the shader as a
00103 //               single-precision rgba value, where the alpha
00104 //               component represents transparency according to the
00105 //               Panda convention.  If no overall color is specified
00106 //               (_has_flat_color is not true), this returns white.
00107 //
00108 //               Normally, Maya makes texture color override the flat
00109 //               color, so if a texture is also applied (_has_texture
00110 //               is true), this value is not used by Maya.
00111 ////////////////////////////////////////////////////////////////////
00112 Colorf MayaShader::
00113 get_rgba() const {
00114   Colorf rgba(1.0f, 1.0f, 1.0f, 1.0f);
00115 
00116   if (_color._has_flat_color) {
00117     rgba[0] = (float)_color._flat_color[0];
00118     rgba[1] = (float)_color._flat_color[1];
00119     rgba[2] = (float)_color._flat_color[2];
00120   }
00121 
00122   if (_transparency._has_flat_color) {
00123     // Maya supports colored transparency, but we only support
00124     // grayscale transparency.  Use the pnmimage constants to
00125     // convert color to grayscale.
00126     double trans =
00127       _transparency._flat_color[0] * lumin_red + 
00128       _transparency._flat_color[1] * lumin_grn + 
00129       _transparency._flat_color[2] * lumin_blu;
00130     rgba[3] = 1.0f - (float)trans;
00131   }
00132 
00133   return rgba;
00134 }
00135 
00136 ////////////////////////////////////////////////////////////////////
00137 //     Function: MayaShader::read_surface_shader
00138 //       Access: Private
00139 //  Description: Extracts out the shading information from the Maya
00140 //               surface shader.
00141 ////////////////////////////////////////////////////////////////////
00142 bool MayaShader::
00143 read_surface_shader(MObject shader) {
00144   MStatus status;
00145   MFnDependencyNode shader_fn(shader);
00146 
00147   if (maya_cat.is_spam()) {
00148     maya_cat.spam()
00149       << "  Reading surface shader " << shader_fn.name() << "\n";
00150   }
00151 
00152   // First, check for a connection to the color attribute.  This could
00153   // be a texture map or something, and will override whatever the
00154   // shader says for color.
00155 
00156   MPlug color_plug = shader_fn.findPlug("color");
00157   if (color_plug.isNull()) {
00158     // Or maybe a connection to outColor.  Not sure how this differs
00159     // from just color, but empirically it seems that either might be
00160     // used.
00161     color_plug = shader_fn.findPlug("outColor");
00162   }
00163     
00164   if (!color_plug.isNull()) {
00165     MPlugArray color_pa;
00166     color_plug.connectedTo(color_pa, true, false);
00167 
00168     for (size_t i = 0; i < color_pa.length(); i++) {
00169       _color.read_surface_color(color_pa[0].node());
00170     }
00171   }
00172 
00173   // Transparency is stored separately.
00174   MPlug trans_plug = shader_fn.findPlug("transparency");
00175   if (trans_plug.isNull()) {
00176     trans_plug = shader_fn.findPlug("outTransparency");
00177   }
00178     
00179   if (!trans_plug.isNull()) {
00180     MPlugArray trans_pa;
00181     trans_plug.connectedTo(trans_pa, true, false);
00182 
00183     for (size_t i = 0; i < trans_pa.length(); i++) {
00184       _transparency.read_surface_color(trans_pa[0].node());
00185     }
00186   }
00187 
00188   // Also try to get the ordinary color directly from the surface
00189   // shader.
00190   if (shader.hasFn(MFn::kLambert)) {
00191     MFnLambertShader lambert_fn(shader);
00192     MColor color = lambert_fn.color(&status);
00193     if (status) {
00194       // Warning! The alpha component of color doesn't mean
00195       // transparency in Maya.
00196       _color._has_flat_color = true;
00197       _color._flat_color.set(color.r, color.g, color.b, color.a);
00198       _transparency._flat_color.set(0.0, 0.0, 0.0, 0.0);
00199 
00200       // Get the transparency separately.
00201       color = lambert_fn.transparency(&status);
00202       if (status) {
00203         _transparency._has_flat_color = true;
00204         _transparency._flat_color.set(color.r, color.g, color.b, color.a);
00205       }
00206     }
00207   }
00208 
00209   if (!_color._has_flat_color && !_color._has_texture) {
00210     if (maya_cat.is_spam()) {
00211       maya_cat.spam()
00212         << "  Color definition not found.\n";
00213     }
00214   }
00215   return true;
00216 }

Generated on Fri May 2 03:21:19 2003 for Panda-Tool by doxygen1.3