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

LampBasicMaterial.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 "System/MayaTypeID.h"
00027 #include "Material/Basic/LampBasicMaterial.h"
00028 #include "Material/Utility/LampMaterialUtility.h"
00029 #include "Texture/MayaTextureManager.h"
00030 
00031 namespace LampForMaya{
00032 
00033 /// 型識別ID
00034 const MTypeId LampBasicMaterial::id(MayaTypeID::basicMaterial);
00035 
00036 //------------------------------------------------------------------------------
00037 // コンストラクタ
00038 LampBasicMaterial::LampBasicMaterial(){
00039 }
00040 //------------------------------------------------------------------------------
00041 // デストラクタ
00042 LampBasicMaterial::~LampBasicMaterial(){
00043 }
00044 //------------------------------------------------------------------------------
00045 // コンストラクタ前処理
00046 void LampBasicMaterial::postConstructor(){
00047     // マルチプロセッサ対応なし
00048     setMPSafe(false);
00049 }
00050 //------------------------------------------------------------------------------
00051 // 初期化
00052 MStatus LampBasicMaterial::initialize(){
00053     // マテリアルの初期化
00054     LampMaterial::materialInitialize();
00055     // ベーステクスチャアトリビュートの作成
00056     LampMaterialUtility::addBaseTextureAttribute();
00057     // 光沢テクスチャアトリビュートの作成
00058     LampMaterialUtility::addGlossTextureAttribute();
00059     // ライトテクスチャアトリビュートの作成
00060     LampMaterialUtility::addLightTextureAttribute();
00061     // 汚れテクスチャアトリビュートの作成
00062     LampMaterialUtility::addStainTextureAttribute();
00063     // ディフューズアトリビュートの作成
00064     LampMaterialUtility::addDiffuseAttribute();
00065     // スペキュラアトリビュートの作成
00066     LampMaterialUtility::addSpecularAttribute();
00067     // アンビエントアトリビュートの作成
00068     LampMaterialUtility::addAmbientAttribute();
00069     // エミッシブアトリビュートの作成
00070     LampMaterialUtility::addEmissiveAttribute();
00071     return MS::kSuccess;
00072 }
00073 //------------------------------------------------------------------------------
00074 // 描画
00075 //------------------------------------------------------------------------------
00076 // ソフトウェアレンダリング
00077 MStatus LampBasicMaterial::compute(const MPlug& plug, MDataBlock& dataBlock){
00078     return uiShader(plug, dataBlock);
00079 }
00080 //------------------------------------------------------------------------------
00081 // 透明度を持つかどうか
00082 bool LampBasicMaterial::hasTransparency(){ return isBlendEnabled(); }
00083 //------------------------------------------------------------------------------
00084 // 頂点あたりいくつの法泉を必要とするか
00085 int LampBasicMaterial::normalsPerVertex(){ return 1; }
00086 //------------------------------------------------------------------------------
00087 // 頂点あたりいくつのカラーを必要とするか
00088 int LampBasicMaterial::colorsPerVertex(){ return 0; }
00089 //------------------------------------------------------------------------------
00090 // 頂点あたりいくつのUVを必要とするか
00091 int LampBasicMaterial::texCoordsPerVertex(){
00092     int maxTexCoord = 0;
00093     if(getBaseUVIndex() > maxTexCoord){ maxTexCoord = getBaseUVIndex(); }
00094 //  if(getGlossUVIndex() > maxTexCoord){ maxTexCoord = getGlossUVIndex(); }
00095 //  if(getLightUVIndex() > maxTexCoord){ maxTexCoord = getLightUVIndex(); }
00096 //  if(getStainUVIndex() > maxTexCoord){ maxTexCoord = getStainUVIndex(); }
00097     return maxTexCoord + 1;
00098 }
00099 //------------------------------------------------------------------------------
00100 // 描画
00101 MStatus LampBasicMaterial::glGeometry(
00102     const MDagPath& shapePath, int prim, u_int writable,
00103     int indexCount, const u_int* indexArray,
00104     int vertexCount, const int* vertexIDs, const float* vertexArray,
00105     int normalCount, const float** normalArrays,
00106     int colorCount, const float** colorArrays,
00107     int texCoordCount, const float** texCoordArrays){
00108     MStatus result;
00109 
00110     // スタックにアトリビュートを退避
00111     glPushAttrib(GL_ALL_ATTRIB_BITS);
00112     glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
00113 
00114     // マテリアルのセットアップ
00115     materialSetup();
00116 
00117     // 位置の設定
00118     glEnableClientState(GL_VERTEX_ARRAY);
00119     glVertexPointer(3, GL_FLOAT, 0, vertexArray);
00120 
00121     // 法線の設定
00122     if(normalCount > 0){
00123         glEnableClientState(GL_NORMAL_ARRAY);
00124         glNormalPointer(GL_FLOAT, 0, normalArrays[0]);
00125     }else{
00126         glDisableClientState(GL_NORMAL_ARRAY);
00127         glNormal3f(0.f, 0.f, 1.f);
00128     }
00129 
00130     // テクスチャ
00131     if(MayaTextureManager::bind(getBaseTexture())){
00132         // UVの設定
00133         // 複数UVに対応する場合はgetTexCoordSetNamesとか使う? 
00134         int baseUVIndex = getBaseUVIndex();
00135         if(texCoordCount > baseUVIndex){
00136             glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00137             glTexCoordPointer(2, GL_FLOAT, 0, texCoordArrays[baseUVIndex]);
00138         }else{
00139             glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00140         }
00141     }
00142 
00143     // ディフューズ
00144     Color4f diffuse(getDiffuseColor());
00145     diffuse.a = getAlpha();
00146     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse.array);
00147 
00148     // スペキュラ
00149     Color4f specular(getSpecularColor());
00150     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular.array);
00151     glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, getSpecularPower());
00152 
00153     // アンビエント
00154     Color4f ambient(getAmbientColor());
00155     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient.array);
00156 
00157     // エミッシブ
00158     Color4f emissive(getEmissiveColor());
00159     if(colorCount > 0){
00160         // 頂点カラーをエミッションとして扱う
00161         glEnableClientState(GL_COLOR_ARRAY);
00162         glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION);
00163         glEnable(GL_COLOR_MATERIAL);
00164         if(writable & kWriteColorArrays){
00165             float* colors = (float*)colorArrays[0];
00166             for(int i = 0; i < vertexCount; i++){
00167                 colors[0] = colors[0] + emissive.r;
00168                 colors[1] = colors[1] + emissive.g;
00169                 colors[2] = colors[2] + emissive.b;
00170                 colors[3] = colors[3];
00171                 colors += 4;
00172             }
00173         }
00174         glColorPointer(4, GL_FLOAT, 0, colorArrays[0]);
00175     }else{
00176         // colorsPerVertex()が1を返す場合、こちらにはこない
00177         glDisableClientState(GL_COLOR_ARRAY);
00178         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emissive.array);
00179     }
00180 
00181     // 描画
00182     glDrawElements(prim, indexCount, GL_UNSIGNED_INT, indexArray);
00183 
00184     glPopClientAttrib();
00185     glPopAttrib();
00186 
00187     MayaStatusCheck(result);
00188     MayaOpenGLCheck();
00189     return result;
00190 }
00191 //------------------------------------------------------------------------------
00192 // アトリビュートアクセサ
00193 //------------------------------------------------------------------------------
00194 // ベーステクスチャの取得
00195 MObject LampBasicMaterial::getBaseTexture() const{
00196     return LampMaterialUtility::getTexture(thisMObject(), "color");
00197 }
00198 //------------------------------------------------------------------------------
00199 // ベーステクスチャ名の取得
00200 String LampBasicMaterial::getBaseTextureName() const{
00201     return LampMaterialUtility::getTextureName(thisMObject(), "color");
00202 }
00203 //------------------------------------------------------------------------------
00204 // ベースUVインデックスの取得
00205 int LampBasicMaterial::getBaseUVIndex() const{
00206     return MayaAttributeUtility::getInt(thisMObject(), "baseUVIndex");
00207 }
00208 //------------------------------------------------------------------------------
00209 // 光沢テクスチャの取得
00210 MObject LampBasicMaterial::getGlossTexture() const{
00211     return LampMaterialUtility::getTexture(thisMObject(), "glossTexture");
00212 }
00213 //------------------------------------------------------------------------------
00214 // 光沢テクスチャ名の取得
00215 String LampBasicMaterial::getGlossTextureName() const{
00216     return LampMaterialUtility::getTextureName(thisMObject(), "glossTexture");
00217 }
00218 //------------------------------------------------------------------------------
00219 // 光沢UVインデックスの取得
00220 int LampBasicMaterial::getGlossUVIndex() const{
00221     return MayaAttributeUtility::getInt(thisMObject(), "glossUVIndex");
00222 }
00223 //------------------------------------------------------------------------------
00224 // ライトテクスチャの取得
00225 MObject LampBasicMaterial::getLightTexture() const{
00226     return LampMaterialUtility::getTexture(thisMObject(), "lightTexture");
00227 }
00228 //------------------------------------------------------------------------------
00229 // ライトテクスチャ名の取得
00230 String LampBasicMaterial::getLightTextureName() const{
00231     return LampMaterialUtility::getTextureName(thisMObject(), "lightTexture");
00232 }
00233 //------------------------------------------------------------------------------
00234 // ライトUVインデックスの取得
00235 int LampBasicMaterial::getLightUVIndex() const{
00236     return MayaAttributeUtility::getInt(thisMObject(), "lightUVIndex");
00237 }
00238 //------------------------------------------------------------------------------
00239 // 汚れテクスチャの取得
00240 MObject LampBasicMaterial::getStainTexture() const{
00241     return LampMaterialUtility::getTexture(thisMObject(), "stainTexture");
00242 }
00243 //------------------------------------------------------------------------------
00244 // 汚れテクスチャ名の取得
00245 String LampBasicMaterial::getStainTextureName() const{
00246     return LampMaterialUtility::getTextureName(thisMObject(), "stainTexture");
00247 }
00248 //------------------------------------------------------------------------------
00249 // 汚れUVインデックスの取得
00250 int LampBasicMaterial::getStainUVIndex() const{
00251     return MayaAttributeUtility::getInt(thisMObject(), "stainUVIndex");
00252 }
00253 //------------------------------------------------------------------------------
00254 // ディフューズカラーの取得
00255 Color3f LampBasicMaterial::getDiffuseColor() const{
00256     return MayaAttributeUtility::getColor3f(thisMObject(), "diffuseColor");
00257 }
00258 //------------------------------------------------------------------------------
00259 // スペキュラカラーの取得
00260 Color3f LampBasicMaterial::getSpecularColor() const{
00261     return MayaAttributeUtility::getColor3f(thisMObject(), "specularColor");
00262 }
00263 //------------------------------------------------------------------------------
00264 // スペキュラパワーーの取得
00265 float LampBasicMaterial::getSpecularPower() const{
00266     return MayaAttributeUtility::getFloat(thisMObject(), "specularPower");
00267 }
00268 //------------------------------------------------------------------------------
00269 // アンビエントカラーの取得
00270 Color3f LampBasicMaterial::getAmbientColor() const{
00271     return MayaAttributeUtility::getColor3f(thisMObject(), "ambientColor");
00272 }
00273 //------------------------------------------------------------------------------
00274 // エミッシブカラーの取得
00275 Color3f LampBasicMaterial::getEmissiveColor() const{
00276     return MayaAttributeUtility::getColor3f(thisMObject(), "emissiveColor");
00277 }
00278 //------------------------------------------------------------------------------
00279 } // End of namespace LampForMaya
00280 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:55 2005 for LampForMaya by doxygen 1.3.2