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

# LampMaterial.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * Lampマテリアル実装
    00022  * @author Junpee
    00023  */
    00024
    00025 #include "System/stdafx.h"
    00026 #include "Material/System/LampMaterial.h"
    00027 #include "Graphics/Material/Material.h"
    00028 #include "Texture/MayaTextureManager.h"
    00029
    00030 namespace LampForMaya{
    00031
    00032 // アルファテスト境界
    00033 const float LampMaterial::alphaTestBorder_ = (4.f / 255.f);
    00034
    00035 /// ブレンドテーブル
    00036 const u_int LampMaterial::blendTable_[blendTableSize_] = {
    00037     GL_ZERO,
    00038     GL_ONE,
    00039     GL_SRC_COLOR,
    00040     GL_ONE_MINUS_SRC_COLOR,
    00041     GL_SRC_ALPHA,
    00042     GL_ONE_MINUS_SRC_ALPHA,
    00043     GL_SRC_ALPHA_SATURATE,
    00044     GL_DST_COLOR,
    00045     GL_ONE_MINUS_DST_COLOR,
    00046     GL_DST_ALPHA,
    00047     GL_ONE_MINUS_DST_ALPHA,
    00048 };
    00049
    00050 //------------------------------------------------------------------------------
    00051 // 初期化
    00052 //------------------------------------------------------------------------------
    00053 // コンストラクタ
    00054 LampMaterial::LampMaterial(){
    00055     MayaTextureManager::addReference();
    00056 }
    00057 //------------------------------------------------------------------------------
    00058 // デストラクタ
    00059 LampMaterial::~LampMaterial(){
    00060     MayaTextureManager::removeReference();
    00061 }
    00062 //------------------------------------------------------------------------------
    00063 // マテリアル初期化
    00064 void LampMaterial::materialInitialize(){
    00065     const MObject& outColor = MPxHwShaderNode::outColor;
    00066     MStatus result;
    00067     MFnNumericAttribute attribute;
    00068     // ブレンドモードアトリビュートの作成
    00069     MObject blendMode = attribute.create(
    00070         "blendMode", "bm", MFnNumericData::kInt, 0, &result);
    00071     MayaStatusCheck(result);
    00072     MayaStatusCheck(attribute.setConnectable(false));
    00073     MayaStatusCheck(attribute.setMax(5));
    00074     MayaStatusCheck(attribute.setMin(0));
    00075     MayaStatusCheck(MPxNode::addAttribute(blendMode));
    00076     MayaStatusCheck(MPxNode::attributeAffects(blendMode, outColor));
    00077
    00078     // アルファアトリビュートの作成
    00079     MObject alpha = attribute.create(
    00080         "alpha", "a", MFnNumericData::kFloat, 1.f, &result);
    00081     MayaStatusCheck(result);
    00082     MayaStatusCheck(attribute.setConnectable(false));
    00083     MayaStatusCheck(attribute.setMax(1.f));
    00084     MayaStatusCheck(attribute.setMin(0.f));
    00085     MayaStatusCheck(MPxNode::addAttribute(alpha));
    00086     MayaStatusCheck(MPxNode::attributeAffects(alpha, outColor));
    00087
    00088     // ブレンドソースアトリビュートの作成
    00089     MObject blendSource = attribute.create(
    00090         "blendSource", "bs", MFnNumericData::kInt, 4, &result);
    00091     MayaStatusCheck(result);
    00092     MayaStatusCheck(attribute.setConnectable(false));
    00093     MayaStatusCheck(attribute.setMax(blendTableSize_ - 1));
    00094     MayaStatusCheck(attribute.setMin(0));
    00095     MayaStatusCheck(MPxNode::addAttribute(blendSource));
    00096     MayaStatusCheck(MPxNode::attributeAffects(blendSource, outColor));
    00097
    00098     // ブレンドデスティネーションアトリビュートの作成
    00099     MObject blendDestination = attribute.create(
    00100         "blendDestination", "bd", MFnNumericData::kInt, 5, &result);
    00101     MayaStatusCheck(result);
    00102     MayaStatusCheck(attribute.setConnectable(false));
    00103     MayaStatusCheck(attribute.setMax(blendTableSize_ - 1));
    00104     MayaStatusCheck(attribute.setMin(0));
    00105     MayaStatusCheck(MPxNode::addAttribute(blendDestination));
    00106     MayaStatusCheck(MPxNode::attributeAffects(blendDestination, outColor));
    00107
    00108     // ユーザインターフェースカラーの作成
    00109     MObject uiColor = attribute.createColor("uiColor", "uic", &result);
    00110     MayaStatusCheck(result);
    00111     MayaStatusCheck(attribute.setDefault(1.f, 1.f, 1.f));
    00112     MayaStatusCheck(attribute.setConnectable(false));
    00113     MayaStatusCheck(MPxNode::addAttribute(uiColor));
    00114     MayaStatusCheck(MPxNode::attributeAffects(uiColor, outColor));
    00115
    00116     // Z書き込みアトリビュートの作成
    00117     MObject zWrite = attribute.create(
    00118         "zWrite", "zwt", MFnNumericData::kBoolean, 1, &result);
    00119     MayaStatusCheck(result);
    00120     MayaStatusCheck(attribute.setConnectable(false));
    00121     MayaStatusCheck(MPxNode::addAttribute(zWrite));
    00122     MayaStatusCheck(MPxNode::attributeAffects(zWrite, outColor));
    00123
    00124     // Zテストアトリビュートの作成
    00125     MObject zTest = attribute.create(
    00126         "zTest", "zte", MFnNumericData::kBoolean, 1, &result);
    00127     MayaStatusCheck(result);
    00128     MayaStatusCheck(attribute.setConnectable(false));
    00129     MayaStatusCheck(MPxNode::addAttribute(zTest));
    00130     MayaStatusCheck(MPxNode::attributeAffects(zTest, outColor));
    00131
    00132     // フォグオプションアトリビュートの作成
    00133     MObject fogOption = attribute.create(
    00134         "fogOption", "fgo", MFnNumericData::kInt, 0, &result);
    00135     MayaStatusCheck(result);
    00136     MayaStatusCheck(attribute.setConnectable(false));
    00137     MayaStatusCheck(attribute.setMax(2));
    00138     MayaStatusCheck(attribute.setMin(0));
    00139     MayaStatusCheck(MPxNode::addAttribute(fogOption));
    00140     MayaStatusCheck(MPxNode::attributeAffects(fogOption, outColor));
    00141
    00142     // ライトマスクアトリビュートの作成
    00143     MObject lightMask = attribute.create(
    00144         "lightMask", "lgm", MFnNumericData::kInt, 1, &result);
    00145     MayaStatusCheck(result);
    00146     MayaStatusCheck(attribute.setConnectable(false));
    00147     MayaStatusCheck(attribute.setMax(65535));
    00148     MayaStatusCheck(attribute.setMin(0));
    00149     MayaStatusCheck(MPxNode::addAttribute(lightMask));
    00150     MayaStatusCheck(MPxNode::attributeAffects(lightMask, outColor));
    00151
    00152     // 優先度アトリビュートの作成
    00153     MObject priority = attribute.create(
    00154         "priority", "pri", MFnNumericData::kInt, 0, &result);
    00155     MayaStatusCheck(result);
    00156     MayaStatusCheck(attribute.setConnectable(false));
    00157     MayaStatusCheck(attribute.setMax(Limit::shortMax));
    00158     MayaStatusCheck(attribute.setMin(Limit::shortMin));
    00159     MayaStatusCheck(MPxNode::addAttribute(priority));
    00160     MayaStatusCheck(MPxNode::attributeAffects(priority, outColor));
    00161
    00162     // 透明度アトリビュートの隠蔽
    00163     MFnNumericAttribute outTransparencyAttribute(
    00164         MPxHwShaderNode::outTransparency, &result);
    00165     MayaStatusCheck(result);
    00166     MayaStatusCheck(outTransparencyAttribute.setWritable(false));
    00167     MayaStatusCheck(outTransparencyAttribute.setStorable(false));
    00168
    00169     // マットチャンネルアトリビュートの隠蔽
    00170     MFnNumericAttribute outMatteOpacityAttribute(
    00171         MPxHwShaderNode::outMatteOpacity, &result);
    00172     MayaStatusCheck(result);
    00173     MayaStatusCheck(outMatteOpacityAttribute.setWritable(false));
    00174     MayaStatusCheck(outMatteOpacityAttribute.setStorable(false));
    00175
    00176     // グロウカラーアトリビュートの隠蔽
    00177     MFnNumericAttribute outGlowColorAttribute(
    00178         MPxHwShaderNode::outGlowColor, &result);
    00179     MayaStatusCheck(result);
    00180     MayaStatusCheck(outGlowColorAttribute.setWritable(false));
    00181     MayaStatusCheck(outGlowColorAttribute.setStorable(false));
    00182 }
    00183 //------------------------------------------------------------------------------
    00184 // 描画
    00185 //------------------------------------------------------------------------------
    00186 // マテリアルのセットアップ
    00187 void LampMaterial::materialSetup(){
    00188     // ライティングを有効にする
    00189     glEnable(GL_LIGHTING);
    00190
    00191     // アルファテストを行う
    00192     glEnable(GL_ALPHA_TEST);
    00193     glAlphaFunc(GL_GEQUAL, alphaTestBorder_);
    00194
    00195     // アルファブレンド
    00196     if(getBlendMode() != 0){
    00197         // glBlendEquation()でモード設定できる
    00198         glEnable(GL_BLEND);
    00199         // OpenGLではblendDestinationにSrcAlphaSaturateを指定するとエラーになる
    00200         u_int destination = blendTable_[getBlendDestination()];
    00201         if(destination == GL_SRC_ALPHA_SATURATE){ destination = GL_ZERO; }
    00202         glBlendFunc(blendTable_[getBlendSource()], destination);
    00203     }else{
    00204         glDisable(GL_BLEND);
    00205     }
    00206
    00207     // Z書き込み
    00208     glDepthMask(useZWrite());
    00209
    00210     // Zテスト
    00211     if(useZTest()){
    00212         glEnable(GL_DEPTH_TEST);
    00213     }else{
    00214         glDisable(GL_DEPTH_TEST);
    00215     }
    00216
    00217     // フォグが有効かどうか
    00218     MStatus result;
    00219     int fogEnabled;
    00220     result = MGlobal::executeCommand(MString(
    00221         "modelEditor -q -fogging modelPanel4;"), fogEnabled, false, false);
    00222     MayaStatusCheck(result);
    00223     if(fogEnabled != 0){
    00224         int fogOption = getFogOption();
    00225         if(fogOption == 1){
    00226             // フォグを無効にする
    00227             glDisable(GL_FOG);
    00228         }else if(fogOption == 2){
    00229             // フォグ色を黒にする
    00230             glFogfv(GL_FOG_COLOR, Color4f::black.array);
    00231         }
    00232     }
    00233     MayaOpenGLCheck();
    00234 }
    00235 //------------------------------------------------------------------------------
    00236 // ユーザインターフェースシェーダ
    00237 MStatus LampMaterial::uiShader(const MPlug& plug, MDataBlock& dataBlock){
    00238     MStatus result;
    00239     const MObject& outColor = MPxHwShaderNode::outColor;
    00240     if((plug != outColor) && (plug.parent() != outColor)){
    00241         return MS::kUnknownParameter;
    00242     }
    00243     // 出力アドレスの取得
    00244     MDataHandle outColorHandle = dataBlock.outputValue(outColor, &result);
    00245     MayaStatusCheck(result);
    00246     MFloatVector& outputColor = outColorHandle.asFloatVector();
    00247     // 色を出力
    00248     Color3f uiColor = getUIColor();
    00249     outputColor.x = uiColor.r;
    00250     outputColor.y = uiColor.g;
    00251     outputColor.z = uiColor.b;
    00252     outColorHandle.setClean();
    00253     return MS::kSuccess;
    00254 }
    00255 //------------------------------------------------------------------------------
    00256 // アトリビュートアクセサ
    00257 //------------------------------------------------------------------------------
    00258 // ブレンドモードの取得
    00259 int LampMaterial::getBlendMode() const{
    00260     return MayaAttributeUtility::getInt(thisMObject(), "blendMode");
    00261 }
    00262 //------------------------------------------------------------------------------
    00263 // アルファの取得
    00264 float LampMaterial::getAlpha() const{
    00265     return MayaAttributeUtility::getFloat(thisMObject(), "alpha");
    00266 }
    00267 //------------------------------------------------------------------------------
    00268 // ブレンドソースの取得
    00269 int LampMaterial::getBlendSource() const{
    00270     return MayaAttributeUtility::getInt(thisMObject(), "blendSource");
    00271 }
    00272 //------------------------------------------------------------------------------
    00273 // ブレンドデスティネーションの取得
    00274 int LampMaterial::getBlendDestination() const{
    00275     return MayaAttributeUtility::getInt(thisMObject(), "blendDestination");
    00276 }
    00277 //------------------------------------------------------------------------------
    00278 // ユーザインターフェースカラーの取得
    00279 Color3f LampMaterial::getUIColor() const{
    00280     return MayaAttributeUtility::getColor3f(thisMObject(), "uiColor");
    00281 }
    00282 //------------------------------------------------------------------------------
    00283 // Z書き込みの取得
    00284 bool LampMaterial::useZWrite() const{
    00285     return MayaAttributeUtility::getBool(thisMObject(), "zWrite");
    00286 }
    00287 //------------------------------------------------------------------------------
    00288 // Zテストの取得
    00289 bool LampMaterial::useZTest() const{
    00290     return MayaAttributeUtility::getBool(thisMObject(), "zTest");
    00291 }
    00292 //------------------------------------------------------------------------------
    00293 // フォグオプションの取得
    00294 int LampMaterial::getFogOption() const{
    00295     return MayaAttributeUtility::getInt(thisMObject(), "fogOption");
    00296 }
    00297 //------------------------------------------------------------------------------
    00298 // ライトマスクの取得
    00299 u_int LampMaterial::getLightMask() const{
    00300     return MayaAttributeUtility::getInt(thisMObject(), "lightMask");
    00301 }
    00302 //------------------------------------------------------------------------------
    00303 // 優先度の取得
    00304 int LampMaterial::getPriority() const{
    00305     return MayaAttributeUtility::getInt(thisMObject(), "priority");
    00306 }
    00307 //------------------------------------------------------------------------------
    00308 } // End of namespace LampForMaya
    00309 //------------------------------------------------------------------------------