1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* Copyright (C) 2006-2010 - Frictional Games
*
* This file is part of HPL1 Engine.
*/
#include "hpl1/engine/graphics/RenderState.h"
#include "hpl1/engine/graphics/Renderer3D.h"
#include "hpl1/debug.h"
#include "hpl1/engine/graphics/GPUProgram.h"
#include "hpl1/engine/graphics/LowLevelGraphics.h"
#include "hpl1/engine/graphics/Material.h"
#include "hpl1/engine/graphics/VertexBuffer.h"
#include "hpl1/engine/math/Math.h"
#include "hpl1/engine/scene/Camera3D.h"
#include "hpl1/engine/scene/Light3D.h"
#include "hpl1/engine/scene/Light3DSpot.h"
#include "hpl1/engine/scene/PortalContainer.h"
namespace hpl {
template<class T>
static int GetCompareVal(T a, T b) {
if (a == b)
return 0;
else
return a < b ? -1 : 1;
}
//////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
int iRenderState::Compare(const iRenderState *apState) const {
switch (mType) {
case eRenderStateType_Sector:
return CompareSector(apState);
case eRenderStateType_Pass:
return ComparePass(apState);
case eRenderStateType_DepthTest:
return CompareDepthTest(apState);
case eRenderStateType_Depth:
return CompareDepth(apState);
case eRenderStateType_AlphaMode:
return CompareAlpha(apState);
case eRenderStateType_BlendMode:
return CompareBlend(apState);
case eRenderStateType_VertexProgram:
return CompareVtxProg(apState);
case eRenderStateType_FragmentProgram:
return CompareFragProg(apState);
case eRenderStateType_Texture:
return CompareTexture(apState);
case eRenderStateType_VertexBuffer:
return CompareVtxBuff(apState);
case eRenderStateType_Matrix:
return CompareMatrix(apState);
case eRenderStateType_Render:
return CompareRender(apState);
case eRenderStateType_GpuProgram:
return compareGpuProgram(apState);
default:
break;
}
return 0;
}
//-----------------------------------------------------------------------
void iRenderState::SetMode(cRenderSettings *apSettings) {
switch (mType) {
case eRenderStateType_Sector:
SetSectorMode(apSettings);
break;
case eRenderStateType_Pass:
SetPassMode(apSettings);
break;
case eRenderStateType_DepthTest:
SetDepthTestMode(apSettings);
break;
case eRenderStateType_Depth:
SetDepthMode(apSettings);
break;
case eRenderStateType_AlphaMode:
SetAlphaMode(apSettings);
break;
case eRenderStateType_BlendMode:
SetBlendMode(apSettings);
break;
case eRenderStateType_Texture:
SetTextureMode(apSettings);
break;
case eRenderStateType_VertexBuffer:
SetVtxBuffMode(apSettings);
break;
case eRenderStateType_Matrix:
SetMatrixMode(apSettings);
break;
case eRenderStateType_Render:
SetRenderMode(apSettings);
break;
case eRenderStateType_GpuProgram:
setGpuProgMode(apSettings);
default:
break;
}
}
//-----------------------------------------------------------------------
void iRenderState::Set(const iRenderState *apState) {
mType = apState->mType;
switch (mType) {
case eRenderStateType_Sector:
mpSector = apState->mpSector;
break;
case eRenderStateType_Pass:
mlPass = apState->mlPass;
break;
case eRenderStateType_DepthTest:
mbDepthTest = apState->mbDepthTest;
break;
case eRenderStateType_Depth:
mfZ = apState->mfZ;
break;
case eRenderStateType_AlphaMode:
mAlphaMode = apState->mAlphaMode;
break;
case eRenderStateType_BlendMode:
mBlendMode = apState->mBlendMode;
mChannelMode = apState->mChannelMode;
break;
case eRenderStateType_VertexProgram:
mpVtxProgram = apState->mpVtxProgram;
mpVtxProgramSetup = apState->mpVtxProgramSetup;
mbUsesEye = apState->mbUsesEye;
mbUsesLight = apState->mbUsesLight;
mpLight = apState->mpLight;
break;
case eRenderStateType_FragmentProgram:
mpFragProgram = apState->mpFragProgram;
mpFragProgramSetup = apState->mpFragProgramSetup;
break;
case eRenderStateType_Texture:
for (int i = 0; i < MAX_TEXTUREUNITS; i++)
mpTexture[i] = apState->mpTexture[i];
break;
case eRenderStateType_VertexBuffer:
mpVtxBuffer = apState->mpVtxBuffer;
break;
case eRenderStateType_Matrix:
mpModelMatrix = apState->mpModelMatrix;
mpInvModelMatrix = apState->mpInvModelMatrix;
mvScale = apState->mvScale;
break;
case eRenderStateType_Render:
mpObject = apState->mpObject;
break;
case eRenderStateType_GpuProgram:
gpuProgram = apState->gpuProgram;
gpuProgramSetup = apState->gpuProgramSetup;
mbUsesEye = apState->mbUsesEye;
mbUsesLight = apState->mbUsesLight;
mpLight = apState->mpLight;
default:
break;
}
}
//-----------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
//////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
void iRenderState::SetSectorMode(cRenderSettings *apSettings) {
if (apSettings->mbLog)
Log("Sector: %d\n", mpSector);
apSettings->mpSector = mpSector;
if (mpSector) {
apSettings->mpLowLevel->SetTextureConstantColor(apSettings->mAmbientColor * mpSector->GetAmbientColor());
} else {
apSettings->mpLowLevel->SetTextureConstantColor(apSettings->mAmbientColor);
}
// To make sure that new ambient is set:
apSettings->mpFragmentProgram = NULL;
}
//-----------------------------------------------------------------------
void iRenderState::SetPassMode(cRenderSettings *apSettings) {
if (apSettings->mbLog)
Log("Pass: %d\n", mlPass);
}
//-----------------------------------------------------------------------
void iRenderState::SetDepthTestMode(cRenderSettings *apSettings) {
if (apSettings->mbDepthTest != mbDepthTest) {
apSettings->mpLowLevel->SetDepthTestActive(mbDepthTest);
apSettings->mbDepthTest = mbDepthTest;
if (apSettings->mbLog)
Log("Setting depth test: %d\n", mbDepthTest ? 1 : 0);
}
}
//-----------------------------------------------------------------------
void iRenderState::SetDepthMode(cRenderSettings *apSettings) {
}
//-----------------------------------------------------------------------
void iRenderState::SetAlphaMode(cRenderSettings *apSettings) {
if (mAlphaMode != apSettings->mAlphaMode) {
if (apSettings->mbLog)
Log("Setting alpha: ");
apSettings->mAlphaMode = mAlphaMode;
if (mAlphaMode == eMaterialAlphaMode_Solid) {
apSettings->mpLowLevel->SetAlphaTestActive(false);
if (apSettings->mbLog)
Log("Solid");
} else {
// apSettings->mpLowLevel->SetTextureConstantColor(cColor(0,0));
// apSettings->mpLowLevel->SetTextureEnv(eTextureParam_ColorSource1,eTextureSource_Constant);
// apSettings->mpLowLevel->SetAlphaTestActive(false);
apSettings->mpLowLevel->SetAlphaTestActive(true);
apSettings->mpLowLevel->SetAlphaTestFunc(eAlphaTestFunc_GreaterOrEqual, 0.6f);
if (apSettings->mbLog)
Log("Trans");
}
if (apSettings->mbLog)
Log("\n");
}
}
//-----------------------------------------------------------------------
void iRenderState::SetBlendMode(cRenderSettings *apSettings) {
if (mBlendMode != apSettings->mBlendMode) {
if (apSettings->mbLog)
Log("Setting blend mode: ");
apSettings->mBlendMode = mBlendMode;
if (mBlendMode == eMaterialBlendMode_None) {
apSettings->mpLowLevel->SetBlendActive(false);
if (apSettings->mbLog)
Log("None");
} else {
apSettings->mpLowLevel->SetBlendActive(true);
switch (mBlendMode) {
case eMaterialBlendMode_Add:
apSettings->mpLowLevel->SetBlendFunc(eBlendFunc_One, eBlendFunc_One);
if (apSettings->mbLog)
Log("Add");
break;
case eMaterialBlendMode_Replace:
apSettings->mpLowLevel->SetBlendFunc(eBlendFunc_One, eBlendFunc_Zero);
if (apSettings->mbLog)
Log("Replace");
break;
case eMaterialBlendMode_Mul:
apSettings->mpLowLevel->SetBlendFunc(eBlendFunc_Zero, eBlendFunc_SrcColor);
if (apSettings->mbLog)
Log("Mul");
break;
case eMaterialBlendMode_MulX2:
apSettings->mpLowLevel->SetBlendFunc(eBlendFunc_DestColor, eBlendFunc_SrcColor);
if (apSettings->mbLog)
Log("MulX2");
break;
case eMaterialBlendMode_Alpha:
apSettings->mpLowLevel->SetBlendFunc(eBlendFunc_SrcAlpha, eBlendFunc_OneMinusSrcAlpha);
if (apSettings->mbLog)
Log("Alpha");
break;
case eMaterialBlendMode_DestAlphaAdd:
apSettings->mpLowLevel->SetBlendFunc(eBlendFunc_DestAlpha, eBlendFunc_One);
if (apSettings->mbLog)
Log("DestAlphaAdd");
break;
default:
break;
}
}
if (apSettings->mbLog)
Log("\n");
}
if (mChannelMode != apSettings->mChannelMode) {
if (apSettings->mbLog)
Log("Setting channel: ");
apSettings->mChannelMode = mChannelMode;
switch (mChannelMode) {
case eMaterialChannelMode_RGBA:
apSettings->mpLowLevel->SetColorWriteActive(true, true, true, true);
if (apSettings->mbLog)
Log("RGBA");
break;
case eMaterialChannelMode_RGB:
apSettings->mpLowLevel->SetColorWriteActive(true, true, true, false);
if (apSettings->mbLog)
Log("RGB");
break;
case eMaterialChannelMode_A:
apSettings->mpLowLevel->SetColorWriteActive(false, false, false, true);
if (apSettings->mbLog)
Log("A");
break;
case eMaterialChannelMode_Z:
apSettings->mpLowLevel->SetColorWriteActive(false, false, false, false);
if (apSettings->mbLog)
Log("Z");
break;
default:
break;
}
if (apSettings->mbLog)
Log("\n");
}
}
//-----------------------------------------------------------------------
void iRenderState::setGpuProgMode(cRenderSettings *settings) {
if (gpuProgram != settings->gpuProgram) {
if (gpuProgram != settings->gpuProgram) {
if (gpuProgram) {
gpuProgram->Bind();
if (gpuProgramSetup) {
gpuProgramSetup->Setup(gpuProgram, settings);
}
settings->gpuProgramSetup = gpuProgramSetup;
// reset this so all matrix setting are set to vertex program.
settings->mbMatrixWasNULL = false;
if (mbUsesLight) {
gpuProgram->SetColor4f("LightColor", mpLight->GetDiffuseColor());
settings->mpLight = mpLight;
} else {
settings->mpLight = nullptr;
}
settings->mbUsesLight = mbUsesLight;
settings->mbUsesEye = mbUsesEye;
} else {
settings->gpuProgram->UnBind();
}
settings->gpuProgram = gpuProgram;
} else if (settings->gpuProgram && mbUsesLight && mpLight != settings->mpLight) {
gpuProgram->SetColor4f("LightColor", mpLight->GetDiffuseColor());
settings->mpLight = mpLight;
}
}
}
//-----------------------------------------------------------------------
void iRenderState::SetTextureMode(cRenderSettings *apSettings) {
for (int i = 0; i < MAX_TEXTUREUNITS; i++) {
if (apSettings->mpTexture[i] != mpTexture[i]) {
if (apSettings->mbLog) {
if (mpTexture[i] == NULL)
Log("Setting texture: %d / %d : NULL\n", i, (size_t)mpTexture[i]);
else
Log("Setting texture: %d / %d : '%s'\n", i, (size_t)mpTexture[i],
mpTexture[i]->GetName().c_str());
}
apSettings->mpLowLevel->SetTexture(i, mpTexture[i]);
apSettings->mpTexture[i] = mpTexture[i];
}
}
}
//-----------------------------------------------------------------------
void iRenderState::SetVtxBuffMode(cRenderSettings *apSettings) {
if (mpVtxBuffer != apSettings->mpVtxBuffer) {
if (apSettings->mbLog)
Log("Setting vertex buffer: %d\n", (size_t)mpVtxBuffer);
if (apSettings->mpVtxBuffer)
apSettings->mpVtxBuffer->UnBind();
apSettings->mpVtxBuffer = mpVtxBuffer;
if (mpVtxBuffer) {
mpVtxBuffer->Bind();
}
}
}
//-----------------------------------------------------------------------
void iRenderState::SetMatrixMode(cRenderSettings *apSettings) {
// It is a normal matrix
if (mpModelMatrix) {
cMatrixf mtxModel = cMath::MatrixMul(apSettings->mpCamera->GetViewMatrix(),
*mpModelMatrix);
if (apSettings->mbLog)
Log("Setting model matrix: %s ", cMath::MatrixToChar(*mpModelMatrix));
apSettings->mpLowLevel->SetMatrix(eMatrix_ModelView, mtxModel);
apSettings->mbMatrixWasNULL = false;
}
// NULL matrix
else {
// If NULL already is set, no need for changes.
if (apSettings->mbMatrixWasNULL)
return;
if (apSettings->mbLog)
Log("Setting model matrix: Identity (NULL) ");
apSettings->mpLowLevel->SetMatrix(eMatrix_ModelView, apSettings->mpCamera->GetViewMatrix());
apSettings->mbMatrixWasNULL = true;
}
if (apSettings->gpuProgram) {
// Might be quicker if this is set directly
apSettings->gpuProgram->SetMatrixf("worldViewProj",
eGpuProgramMatrix_ViewProjection,
eGpuProgramMatrixOp_Identity);
if (apSettings->gpuProgramSetup) {
apSettings->gpuProgramSetup->SetupMatrix(mpModelMatrix, apSettings);
}
if (apSettings->mbUsesLight) {
if (apSettings->mbLog)
Log("Light ");
if (mpModelMatrix) {
// Light position
cVector3f vLocalLight = cMath::MatrixMul(*mpInvModelMatrix,
apSettings->mpLight->GetLightPosition());
apSettings->gpuProgram->SetVec3f("LightPos", vLocalLight);
// LightDir Div, use scale to make attenuation correct!
cVector3f vLightDirDiv = mvScale / apSettings->mpLight->GetFarAttenuation();
apSettings->gpuProgram->SetVec3f("LightDirMul", vLightDirDiv);
if (apSettings->mbLog)
Log("(%s) LightDirMul (%s) ", vLocalLight.ToString().c_str(), vLightDirDiv.ToString().c_str());
// Light view projection
if (apSettings->mpLight->GetLightType() == eLight3DType_Spot) {
if (apSettings->mbLog)
Log("SpotLightViewProj ");
cLight3DSpot *pSpotLight = static_cast<cLight3DSpot *>(apSettings->mpLight);
apSettings->gpuProgram->SetMatrixf("spotViewProj",
cMath::MatrixMul(pSpotLight->GetViewProjMatrix(), *mpModelMatrix));
}
} else {
// Light position
apSettings->gpuProgram->SetVec3f("LightPos", apSettings->mpLight->GetLightPosition());
// LightDir Div
apSettings->gpuProgram->SetVec3f("LightDirMul", 1.0f / apSettings->mpLight->GetFarAttenuation());
// Light view projection
if (apSettings->mpLight->GetLightType() == eLight3DType_Spot) {
if (apSettings->mbLog)
Log("SpotLightViewProj ");
cLight3DSpot *pSpotLight = static_cast<cLight3DSpot *>(apSettings->mpLight);
apSettings->gpuProgram->SetMatrixf("spotViewProj", pSpotLight->GetViewProjMatrix());
}
}
}
if (apSettings->mbUsesEye) {
if (apSettings->mbLog)
Log("Eye ");
if (mpModelMatrix) {
cVector3f vLocalEye = cMath::MatrixMul(*mpInvModelMatrix,
apSettings->mpCamera->GetEyePosition());
apSettings->gpuProgram->SetVec3f("EyePos", vLocalEye);
} else {
apSettings->gpuProgram->SetVec3f("EyePos", apSettings->mpCamera->GetEyePosition());
}
}
}
if (apSettings->mbLog)
Log("\n");
}
//-----------------------------------------------------------------------
void iRenderState::SetRenderMode(cRenderSettings *apSettings) {
if (apSettings->mbLog)
Log("Drawing\n-----------------\n");
if (apSettings->mDebugFlags & eRendererDebugFlag_RenderLines) {
apSettings->mpVtxBuffer->Draw(eVertexBufferDrawType_Lines);
} else {
apSettings->mpVtxBuffer->Draw();
}
}
//-----------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
// COMPARE METHODS
//////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
int iRenderState::CompareSector(const iRenderState *apState) const {
return (size_t)mpSector < (size_t)apState->mpSector;
}
//-----------------------------------------------------------------------
int iRenderState::ComparePass(const iRenderState *apState) const {
return (int)mlPass < (int)apState->mlPass;
}
//-----------------------------------------------------------------------
int iRenderState::CompareDepthTest(const iRenderState *apState) const {
return (int)mbDepthTest < (int)apState->mbDepthTest;
}
//-----------------------------------------------------------------------
int iRenderState::CompareDepth(const iRenderState *apState) const {
if (ABS(mfZ - apState->mfZ) < 0.00001f)
return 0;
else
return mfZ < apState->mfZ ? 1 : -1;
}
//-----------------------------------------------------------------------
int iRenderState::CompareAlpha(const iRenderState *apState) const {
return GetCompareVal((int)mAlphaMode, (int)apState->mAlphaMode);
}
//-----------------------------------------------------------------------
int iRenderState::CompareBlend(const iRenderState *apState) const {
int lRet = GetCompareVal((int)mChannelMode, (int)apState->mChannelMode);
if (lRet == 0) {
return GetCompareVal((int)mBlendMode, (int)apState->mBlendMode);
}
return lRet;
}
//-----------------------------------------------------------------------
int iRenderState::CompareVtxProg(const iRenderState *apState) const {
return GetCompareVal(mpVtxProgram, apState->mpVtxProgram);
}
//-----------------------------------------------------------------------
int iRenderState::CompareFragProg(const iRenderState *apState) const {
return GetCompareVal(mpFragProgram, apState->mpFragProgram);
}
//-----------------------------------------------------------------------
int iRenderState::CompareTexture(const iRenderState *apState) const {
for (int i = 0; i < MAX_TEXTUREUNITS - 1; ++i) {
if (mpTexture[i] != apState->mpTexture[i])
return GetCompareVal(mpTexture[i], apState->mpTexture[i]);
}
return GetCompareVal(mpTexture[MAX_TEXTUREUNITS - 1], apState->mpTexture[MAX_TEXTUREUNITS - 1]);
}
//-----------------------------------------------------------------------
int iRenderState::CompareVtxBuff(const iRenderState *apState) const {
return GetCompareVal(mpVtxBuffer, apState->mpVtxBuffer);
}
//-----------------------------------------------------------------------
int iRenderState::CompareMatrix(const iRenderState *apState) const {
return GetCompareVal(mpModelMatrix, apState->mpModelMatrix);
}
//-----------------------------------------------------------------------
int iRenderState::CompareRender(const iRenderState *apState) const {
return GetCompareVal(mpObject, apState->mpObject);
}
int iRenderState::compareGpuProgram(const iRenderState *state) const {
return GetCompareVal(gpuProgram, state->gpuProgram);
}
//-----------------------------------------------------------------------
} // namespace hpl
|