FactoryCraftModel.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "FactoryCraftModel.h"
  2. #include <Shader.h>
  3. #include "CustomDX11API.h"
  4. #include "Globals.h"
  5. #include "World.h"
  6. FactoryCraftModel::FactoryCraftModel()
  7. : Model3D()
  8. {
  9. vertexLightBuffer = 0;
  10. lightBuffer = 0;
  11. vertexLightBufferCount = 0;
  12. visible = 1;
  13. destroyedState = 0;
  14. breakTextur
  15. = uiFactory.initParam.bildschirm->zGraphicsApi()->createOrGetTextur(
  16. "blocks.ltdb/crack.png", 0);
  17. effectAlpha = 0;
  18. }
  19. FactoryCraftModel::~FactoryCraftModel()
  20. {
  21. breakTextur->release();
  22. if (vertexLightBuffer) vertexLightBuffer->release();
  23. delete[] lightBuffer;
  24. }
  25. struct TexturEffect
  26. {
  27. bool enabled;
  28. float percentage;
  29. };
  30. void FactoryCraftModel::beforeRender(Framework::GraphicsApi* api,
  31. Framework::Shader* zVertexShader,
  32. Framework::Shader* zPixelShader)
  33. {
  34. if (!World::INSTANCE) return;
  35. CustomDX11API* cApi = dynamic_cast<CustomDX11API*>(api);
  36. if (cApi)
  37. {
  38. char buffer[8];
  39. *(int*)buffer
  40. = vertexLightBufferCount == 0 ? 1 : vertexLightBufferCount;
  41. *(float*)(buffer + 4) = World::INSTANCE->getDayLightFactor();
  42. zVertexShader->füllConstBuffer(
  43. buffer, cApi->getVertexShaderLightBufferIndex(), 8);
  44. if (vertexLightBuffer)
  45. {
  46. cApi->setVertexLightBuffer(vertexLightBuffer);
  47. }
  48. else
  49. {
  50. cApi->setVertexLightBuffer(
  51. World::INSTANCE->zFallbackVertexLightBuffer());
  52. }
  53. if (destroyedState > 0 && effectAlpha)
  54. {
  55. TexturEffect e = {1, 1.f + destroyedState};
  56. zPixelShader->füllConstBuffer(
  57. (char*)&e, 3, sizeof(TexturEffect));
  58. }
  59. }
  60. }
  61. void FactoryCraftModel::afterRender(Framework::GraphicsApi* api,
  62. Framework::Shader* zVertexShader,
  63. Framework::Shader* zPixelShader)
  64. {
  65. if (!World::INSTANCE) return;
  66. CustomDX11API* cApi = dynamic_cast<CustomDX11API*>(api);
  67. if (cApi)
  68. {
  69. char buffer[8];
  70. *(int*)buffer = 0;
  71. *(float*)(buffer + 4) = World::INSTANCE->getDayLightFactor();
  72. zVertexShader->füllConstBuffer(
  73. buffer, cApi->getVertexShaderLightBufferIndex(), 8);
  74. }
  75. }
  76. void FactoryCraftModel::setVertexLightBuffer(__int64* data, int vertexCount)
  77. {
  78. if (!vertexLightBuffer)
  79. {
  80. vertexLightBuffer = uiFactory.initParam.bildschirm->zGraphicsApi()
  81. ->createStructuredBuffer(16);
  82. }
  83. if (lightBuffer)
  84. {
  85. delete[] lightBuffer;
  86. }
  87. lightBuffer = data;
  88. vertexLightBufferCount = vertexCount;
  89. vertexLightBuffer->setData(data);
  90. vertexLightBuffer->setLength(vertexCount * 8);
  91. vertexLightBuffer->copieren();
  92. }
  93. __int64* FactoryCraftModel::zLightBuffer()
  94. {
  95. if (vertexLightBuffer)
  96. {
  97. vertexLightBuffer->setChanged();
  98. }
  99. return lightBuffer;
  100. }
  101. void FactoryCraftModel::copyLightToGPU()
  102. {
  103. if (vertexLightBuffer)
  104. {
  105. vertexLightBuffer->copieren();
  106. }
  107. }
  108. void FactoryCraftModel::setVisible(bool visible)
  109. {
  110. this->visible = visible;
  111. }
  112. void FactoryCraftModel::setDestroyedState(float percentage)
  113. {
  114. destroyedState = percentage;
  115. }
  116. void FactoryCraftModel::setUseEffectAlpha(bool useAlpha)
  117. {
  118. effectAlpha = useAlpha;
  119. }
  120. bool FactoryCraftModel::needRenderPolygon(int index)
  121. {
  122. return visible;
  123. }
  124. Textur* FactoryCraftModel::zEffectTextur()
  125. {
  126. return destroyedState > 0 ? breakTextur : 0;
  127. }
  128. float FactoryCraftModel::getEffectPercentage()
  129. {
  130. return destroyedState;
  131. }