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 "LampBasic.h"
00026 #include "Graphics/Shader/FixedShader.h"
00027 #include "Graphics/Renderer/RenderingDevice.h"
00028 #include "Graphics/Renderer/DrawRequest.h"
00029 #include "Graphics/Fog/Fog.h"
00030 #include "Graphics/SceneNode/SceneNode.h"
00031 #include "Graphics/Light/DirectionalLight.h"
00032 #include "Graphics/Light/PointLight.h"
00033 #include "Graphics/MeshData/MeshData.h"
00034 #include "Graphics/Material/Material.h"
00035 #include "Graphics/Texture/SurfaceTexture.h"
00036
00037
00038 #include "Graphics/Model/Model.h"
00039
00040 namespace Lamp{
00041
00042
00043
00044 FixedShader::FixedShader(){
00045 }
00046
00047
00048 FixedShader::~FixedShader(){
00049 }
00050
00051
00052
00053
00054 void FixedShader::buildMaterialStart(Material* material){
00055 Shader::buildMaterialStart(material);
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 void FixedShader::setFixedTexture(int textureStage, Texture* texture){
00067
00068 device_->setTexture(textureStage, texture);
00069
00070 SurfaceTexture* surfaceTexture = texture->castSurfaceTexture();
00071 if(surfaceTexture != NULL){
00072
00073 device_->setTextureTransform2(textureStage,
00074 surfaceTexture->getRepeatUV(), surfaceTexture->getOffsetUV());
00075
00076 device_->setTextureAddressMode2(textureStage,
00077 surfaceTexture->getAddressModeU(),
00078 surfaceTexture->getAddressModeV());
00079 }
00080 }
00081
00082
00083 int FixedShader::setFixedBaseTexture(
00084 int colorStage, Texture* baseTexture, int baseUVIndex){
00085 if(baseTexture == NULL){ return colorStage; }
00086
00087 setFixedTexture(colorStage, baseTexture);
00088
00089 device_->setColorTextureStage(colorStage,
00090 D3DTOP_MODULATE, D3DTA_CURRENT, D3DTA_TEXTURE, baseUVIndex);
00091 colorStage++;
00092 return colorStage;
00093 }
00094
00095
00096 int FixedShader::setFixedLightTexture(
00097 int colorStage, Texture* lightTexture, int lightUVIndex){
00098 if(lightTexture == NULL){ return colorStage; }
00099
00100 setFixedTexture(colorStage, lightTexture);
00101
00102 device_->setColorTextureStage(colorStage,
00103 D3DTOP_ADD, D3DTA_CURRENT, D3DTA_TEXTURE, lightUVIndex);
00104 colorStage++;
00105 return colorStage;
00106 }
00107
00108
00109 int FixedShader::setFixedStainTexture(
00110 int colorStage, Texture* stainTexture, int stainUVIndex){
00111 if(stainTexture == NULL){ return colorStage; }
00112
00113 setFixedTexture(colorStage, stainTexture);
00114
00115 device_->setColorTextureStage(colorStage,
00116 D3DTOP_MODULATE, D3DTA_CURRENT, D3DTA_TEXTURE, stainUVIndex);
00117 colorStage++;
00118 return colorStage;
00119 }
00120
00121
00122
00123
00124 void FixedShader::setupFixedDraw(DrawRequest* request){
00125
00126 device_->setRenderState(D3DRS_NORMALIZENORMALS,
00127 request->requireNormalize());
00128
00129 setMatrixFixed(request);
00130
00131 setFogOptionFixed(request);
00132
00133 setupFixedLight(request);
00134 }
00135
00136
00137 void FixedShader::setMatrixFixed(DrawRequest* request){
00138 if(request->isSceneNodeChanged()){
00139 device_->setWorldMatrix(request->getSceneNode()->getWorldMatrix());
00140 }
00141 }
00142
00143
00144 void FixedShader::setFogOptionFixed(DrawRequest* request){
00145 Material::FogOption fogOption = request->getMaterial()->getFogOption();
00146 if(fogOption == Material::fogOptionDisable){
00147 device_->setRenderState(D3DRS_FOGENABLE, false);
00148 }else if(fogOption == Material::fogOptionBlack){
00149 device_->setRenderState(D3DRS_FOGCOLOR, 0);
00150 }
00151 }
00152
00153
00154 void FixedShader::setupFixedLight(DrawRequest* request){
00155 Material* material = request->getMaterial();
00156
00157 if(!material->useLight()){ return; }
00158
00159
00160
00161 device_->setAmbientColor(request->getAmbientColor());
00162
00163
00164 u_int lightMask = material->getLightMask();
00165 int lightCount = 0;
00166 int directionalLightCount = request->getDirectionalLightCount();
00167 for(int i = 0; i < directionalLightCount; i++){
00168 DirectionalLight* directionalLight = request->getDirectionalLight(i);
00169
00170 if((directionalLight->getLightMask() & lightMask) == 0){ continue; }
00171 device_->enableDirectionalLight(lightCount, directionalLight);
00172 lightCount++;
00173
00174 if(lightCount == maxActiveLightCount_){ return; }
00175 }
00176
00177
00178
00179 int localLightCount = request->getLocalLightCount();
00180
00181 if(localLightCount + lightCount > maxActiveLightCount_){
00182 request->sortLocalLights();
00183 }
00184 for(int i = 0; i < localLightCount; i++){
00185 LocalLight* localLight = request->getLocalLight(i);
00186 if(localLight->isPointLight()){
00187 device_->enablePointLight(lightCount, localLight->castPointLight());
00188 }else{
00189 ErrorOut("FixedShader::setupFixedLight() 非サポートのライトです");
00190 }
00191 lightCount++;
00192
00193 if(lightCount == maxActiveLightCount_){ return; }
00194 }
00195
00196
00197 device_->closeLight(lightCount);
00198 }
00199
00200
00201
00202
00203 void FixedShader::drawFixed(DrawRequest* request){
00204 Mesh* mesh = request->getMesh();
00205
00206 bool deformed = false;
00207 deformed |= mesh->characterDeform();
00208 if(deformed){
00209
00210
00211 device_->setVertexDeclaration(mesh->getDeformedVertexDeclaration());
00212
00213 device_->setVertexBuffer(mesh->getDeformedVertexBuffer(),
00214 mesh->getDeformedVertexSize());
00215
00216 }else if(request->isMeshDataChanged() || request->isPipelineModeChanged()){
00217
00218 device_->setVertexDeclaration(mesh->getVertexDeclaration());
00219
00220 device_->setVertexBuffer(mesh->getVertexBuffer(),
00221 mesh->getVertexSize());
00222 }
00223
00224 drawCall(request);
00225 }
00226
00227
00228
00229
00230 void FixedShader::resetFixedDraw(DrawRequest* request){
00231
00232 resetFogOptionFixed(request);
00233 }
00234
00235
00236 void FixedShader::resetFogOptionFixed(DrawRequest* request){
00237 Material::FogOption fogOption = request->getMaterial()->getFogOption();
00238 if(fogOption == Material::fogOptionDisable){
00239 device_->setRenderState(D3DRS_FOGENABLE, true);
00240 }else if(fogOption == Material::fogOptionBlack){
00241 device_->setRenderState(D3DRS_FOGCOLOR,
00242 request->getFog()->getColor().getARGB());
00243 }
00244 }
00245
00246 }
00247