00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00091 int LampBasicMaterial::texCoordsPerVertex(){
00092 int maxTexCoord = 0;
00093 if(getBaseUVIndex() > maxTexCoord){ maxTexCoord = getBaseUVIndex(); }
00094
00095
00096
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
00133
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
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
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
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
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
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 }
00280