#pragma once #include "RenderEnums.h" #include "ConstantBuffers/StaticBuffer.h" #include "ConstantBuffers/LightBuffer.h" #include "ConstantBuffers/MiscBuffer.h" #include "ConstantBuffers/HUDBarBuffer.h" #include "ConstantBuffers/HUDBuffer.h" #include "ConstantBuffers/ShadowLightBuffer.h" #include "ConstantBuffers/RoomBuffer.h" #include "ConstantBuffers/ItemBuffer.h" #include "ConstantBuffers/AnimatedBuffer.h" #include "Frustum.h" #include "RendererBucket.h" #include "items.h" #include "effect.h" #include "IndexBuffer/IndexBuffer.h" #include "VertexBuffer/VertexBuffer.h" #include "RenderTarget2D/RenderTarget2D.h" #include "ConstantBuffers/CameraMatrixBuffer.h" #include "Texture2D/Texture2D.h" #include "ConstantBuffers\SpriteBuffer.h" #include "RenderTargetCube\RenderTargetCube.h" #include "RenderView/RenderView.h" #include #include "ConstantBuffer/ConstantBuffer.h" #include "RenderTargetCubeArray/RenderTargetCubeArray.h" #include #include #include #include #include struct CAMERA_INFO; namespace T5M::Renderer { constexpr size_t MAX_DYNAMIC_SHADOWS = 1; using TexturePair = std::tuple; constexpr auto NUM_ANIMATED_SETS = 1024; //constexpr auto MAX_LIGHTS_DRAW = 16384; constexpr auto MAX_DYNAMIC_LIGHTS = 16384; constexpr auto MAX_DRAW_STATICS = 16384; constexpr auto MAX_Bones = 32; constexpr auto MAX_SPRITES = 16384; constexpr auto REFERENCE_RES_WIDTH = 800; constexpr auto REFERENCE_RES_HEIGHT = 450; constexpr auto HUD_UNIT_X = 1.0f / REFERENCE_RES_WIDTH; constexpr auto HUD_UNIT_Y = 1.0f / REFERENCE_RES_HEIGHT; constexpr auto HUD_ZERO_Y = -REFERENCE_RES_HEIGHT; struct RendererDisplayMode { int Width; int Height; int RefreshRate; }; struct RendererVideoAdapter { std::string Name; int Index; std::vector DisplayModes; }; struct RendererHUDBar { VertexBuffer vertexBufferBorder; IndexBuffer indexBufferBorder; VertexBuffer vertexBuffer; IndexBuffer indexBuffer; /* Initialises a new Bar for rendering. the Coordinates are set in the Reference Resolution (default 800x600). The colors are setup like this (4 triangles) 0-------1 | \ / | | >2< | | / \ | 3-------4 */ RendererHUDBar(ID3D11Device* m_device, int x, int y, int w, int h, int borderSize, std::array colors); }; struct RendererStringToDraw { float X; float Y; int Flags; std::wstring String; DirectX::SimpleMath::Vector3 Color; }; struct RendererBone { DirectX::SimpleMath::Vector3 Translation; DirectX::SimpleMath::Matrix GlobalTransform; DirectX::SimpleMath::Matrix Transform; DirectX::SimpleMath::Vector3 GlobalTranslation; std::vector Children; RendererBone* Parent; int Index; DirectX::SimpleMath::Vector3 ExtraRotation; byte ExtraRotationFlags; RendererBone(int index) { Index = index; ExtraRotationFlags = 0; Translation = DirectX::SimpleMath::Vector3(0, 0, 0); ExtraRotation = DirectX::SimpleMath::Vector3(0, 0, 0); } }; struct RendererLight { DirectX::SimpleMath::Vector3 Position; float Type; DirectX::SimpleMath::Vector3 Color; bool Dynamic; DirectX::SimpleMath::Vector4 Direction; float Intensity; float In; float Out; float Range; }; struct RendererAnimatedTexture { DirectX::SimpleMath::Vector2 UV[4]; }; struct RendererAnimatedTextureSet { int NumTextures; std::vector Textures; }; struct RendererStatic { int Id; short RoomIndex; MESH_INFO* Mesh; DirectX::SimpleMath::Matrix World; }; struct RendererRoom { ROOM_INFO* Room; DirectX::SimpleMath::Vector4 AmbientLight; std::vector buckets; std::vector Lights; std::vector Statics; bool Visited; float Distance; int RoomNumber; }; struct RendererRoomNode { short From; short To; DirectX::SimpleMath::Vector4 ClipPort; }; struct RendererItem { int Id; ITEM_INFO* Item; DirectX::SimpleMath::Matrix World; DirectX::SimpleMath::Matrix Translation; DirectX::SimpleMath::Matrix Rotation; DirectX::SimpleMath::Matrix Scale; DirectX::SimpleMath::Matrix AnimationTransforms[32]; int NumMeshes; std::vector Lights; bool DoneAnimations; }; struct RendererMesh { BoundingSphere Sphere; std::vector buckets; std::vector Positions; }; struct RendererEffect { int Id; FX_INFO* Effect; DirectX::SimpleMath::Matrix World; RendererMesh* Mesh; std::vector Lights; }; struct RendererObject { int Id; std::vector ObjectMeshes; RendererBone* Skeleton; std::vector AnimationTransforms; std::vector BindPoseTransforms; std::vector LinearizedBones; bool DoNotDraw; bool HasDataInBucket[NUM_BUCKETS]; bool HasDataInAnimatedBucket[NUM_BUCKETS]; ~RendererObject() { /*for (int i = 0; i < ObjectMeshes.size(); i++) delete ObjectMeshes[i];*/ for (int i = 0; i < LinearizedBones.size(); i++) delete LinearizedBones[i]; } }; struct RendererSprite { int Width; int Height; Vector2 UV[4]; T5M::Renderer::Texture2D* Texture; }; struct RendererSpriteSequence { int Id; std::vector SpritesList; int NumSprites; RendererSpriteSequence() { Id = 0; NumSprites = 0; } RendererSpriteSequence(int id, int num) { Id = id; NumSprites = num; SpritesList = std::vector(NumSprites); } RendererSpriteSequence(const RendererSpriteSequence& rhs) { Id = rhs.Id; NumSprites = rhs.NumSprites; SpritesList = rhs.SpritesList; } RendererSpriteSequence& operator=(const RendererSpriteSequence& other) { if (this != &other) { Id = other.Id; NumSprites = other.NumSprites; SpritesList = std::vector(NumSprites); std::copy(other.SpritesList.begin(), other.SpritesList.end(),back_inserter(SpritesList)); } return *this; } }; struct RendererLine3D { DirectX::SimpleMath::Vector3 start; DirectX::SimpleMath::Vector3 end; DirectX::SimpleMath::Vector4 color; }; struct RendererWeatherParticle { float X, Y, Z; float AngleH; float AngleV; float Size; short Room; bool Reset; bool Draw; }; struct RendererUnderwaterDustParticle { float X, Y, Z; short Life; short Room; bool Reset; }; struct RendererLine2D { DirectX::SimpleMath::Vector2 Vertices[2]; DirectX::SimpleMath::Vector4 Color; }; class Renderer11 { private: // Core DX11 objects Microsoft::WRL::ComPtr m_device = nullptr; Microsoft::WRL::ComPtr m_context = nullptr; Microsoft::WRL::ComPtr m_swapChain = nullptr; std::unique_ptr m_states = nullptr; Microsoft::WRL::ComPtr m_subtractiveBlendState = nullptr; Microsoft::WRL::ComPtr m_inputLayout = nullptr; D3D11_VIEWPORT m_viewport; D3D11_VIEWPORT m_shadowMapViewport; Viewport m_viewportToolkit; std::vector m_adapters; // Main back buffer ID3D11RenderTargetView* m_backBufferRTV; ID3D11Texture2D* m_backBufferTexture; ID3D11DepthStencilState* m_depthStencilState; ID3D11DepthStencilView* m_depthStencilView; ID3D11Texture2D* m_depthStencilTexture; T5M::Renderer::RenderTarget2D m_dumpScreenRenderTarget; T5M::Renderer::RenderTarget2D m_renderTarget; T5M::Renderer::RenderTarget2D m_currentRenderTarget; T5M::Renderer::RenderTarget2D m_shadowMap; T5M::Renderer::RenderTargetCube m_reflectionCubemap; // Shaders Microsoft::WRL::ComPtr m_vsRooms; Microsoft::WRL::ComPtr m_vsRooms_Anim; Microsoft::WRL::ComPtr m_psRooms; Microsoft::WRL::ComPtr m_vsItems; Microsoft::WRL::ComPtr m_psItems; Microsoft::WRL::ComPtr m_vsHairs; Microsoft::WRL::ComPtr m_psHairs; Microsoft::WRL::ComPtr m_vsStatics; Microsoft::WRL::ComPtr m_psStatics; Microsoft::WRL::ComPtr m_vsSky; Microsoft::WRL::ComPtr m_psSky; Microsoft::WRL::ComPtr m_vsSprites; Microsoft::WRL::ComPtr m_psSprites; Microsoft::WRL::ComPtr m_vsSolid; Microsoft::WRL::ComPtr m_psSolid; Microsoft::WRL::ComPtr m_vsInventory; Microsoft::WRL::ComPtr m_psInventory; Microsoft::WRL::ComPtr m_vsFullScreenQuad; Microsoft::WRL::ComPtr m_psFullScreenQuad; Microsoft::WRL::ComPtr m_vsShadowMap; Microsoft::WRL::ComPtr m_psShadowMap; Microsoft::WRL::ComPtr m_vsHUD; Microsoft::WRL::ComPtr m_psHUDColor; Microsoft::WRL::ComPtr m_psHUDTexture; Microsoft::WRL::ComPtr m_psHUDBarColor; Microsoft::WRL::ComPtr m_shadowSampler; // Constant buffers RenderView gameCamera; ConstantBuffer m_cbCameraMatrices; CItemBuffer m_stItem; ConstantBuffer m_cbItem; CStaticBuffer m_stStatic; ConstantBuffer m_cbStatic; CLightBuffer m_stLights; ConstantBuffer m_cbLights; CMiscBuffer m_stMisc; ConstantBuffer m_cbMisc; CRoomBuffer m_stRoom; ConstantBuffer m_cbRoom; CAnimatedBuffer m_stAnimated; ConstantBuffer m_cbAnimated; CShadowLightBuffer m_stShadowMap; ConstantBuffer m_cbShadowMap; CHUDBuffer m_stHUD; ConstantBuffer m_cbHUD; CHUDBarBuffer m_stHUDBar; ConstantBuffer m_cbHUDBar; CSpriteBuffer m_stSprite; ConstantBuffer m_cbSprite; // Text and sprites std::unique_ptr m_gameFont; std::unique_ptr m_spriteBatch; std::vector m_strings; int m_blinkColorValue; int m_blinkColorDirection; std::unique_ptr> m_primitiveBatch; // System resources T5M::Renderer::Texture2D m_HUDBarBorderTexture; T5M::Renderer::Texture2D m_caustics[NUM_CAUSTICS_TEXTURES]; T5M::Renderer::Texture2D m_binocularsTexture; T5M::Renderer::Texture2D m_whiteTexture; T5M::Renderer::RenderTargetCubeArray m_shadowMaps; // Level data T5M::Renderer::Texture2D m_titleScreen; T5M::Renderer::Texture2D m_loadScreen; T5M::Renderer::Texture2D m_textureAtlas; T5M::Renderer::Texture2D m_skyTexture; T5M::Renderer::Texture2D m_logo; VertexBuffer m_roomsVertexBuffer; IndexBuffer m_roomsIndexBuffer; VertexBuffer m_moveablesVertexBuffer; IndexBuffer m_moveablesIndexBuffer; VertexBuffer m_staticsVertexBuffer; IndexBuffer m_staticsIndexBuffer; std::vector m_rooms; DirectX::SimpleMath::Matrix m_hairsMatrices[12]; short m_numHairVertices; short m_numHairIndices; std::vector m_hairVertices; std::vector m_hairIndices; std::vector m_dynamicLights; std::vector m_lines3DToDraw; std::vector m_lines2DToDraw; std::vector m_bucketsToDraw; int m_nextSprite; RendererLine3D* m_lines3DBuffer; int m_nextLine3D; RendererLine2D* m_lines2DBuffer; int m_nextLine2D; RendererLight* m_shadowLight; std::vector> m_moveableObjects; std::vector> m_staticObjects; std::vector m_sprites; int m_numMoveables; int m_numStatics; int m_numSprites; int m_numSpritesSequences; std::vector m_spriteSequences; std::unordered_map m_meshPointersToMesh; DirectX::SimpleMath::Matrix m_LaraWorldMatrix; std::vector m_animatedTextureSets; int m_numAnimatedTextureSets; int m_currentCausticsFrame; RendererUnderwaterDustParticle m_underwaterDustParticles[NUM_UNDERWATER_DUST_PARTICLES]; bool m_firstUnderwaterDustParticles = true; std::vector m_meshes; std::vector m_roomTextures; std::vector m_animatedTextures; std::vector m_moveablesTextures; std::vector m_staticsTextures; std::vector m_spritesTextures; // Debug variables int m_numDrawCalls = 0; // Preallocated pools of objects for avoiding new/delete // Items and effects are safe (can't be more than 1024 items in TR), // lights should be oversized (eventually ignore lights more than MAX_LIGHTS) RendererItem m_items[NUM_ITEMS]; RendererEffect m_effects[NUM_ITEMS]; RendererLight m_lights[MAX_LIGHTS]; int m_nextLight; int m_currentY; // Times for debug int m_timeUpdate; int m_timeDraw; int m_timeFrame; // Others bool m_firstWeather; RendererWeatherParticle m_rain[NUM_RAIN_DROPS]; RendererWeatherParticle m_snow[NUM_SNOW_PARTICLES]; RENDERER_FADE_STATUS m_fadeStatus = RENDERER_FADE_STATUS::NO_FADE; float m_fadeFactor; int m_progress; bool m_enableCinematicBars = false; int m_pickupRotation; // Private functions int getAnimatedTextureInfo(short textureId); void drawAllStrings(); void fromTrAngle(DirectX::SimpleMath::Matrix* matrix, short* frameptr, int index); void buildHierarchy(RendererObject* obj); void buildHierarchyRecursive(RendererObject* obj, RendererBone* node, RendererBone* parentNode); void updateAnimation(RendererItem* item, RendererObject& obj, ANIM_FRAME** frmptr, short frac, short rate, int mask,bool useObjectWorldRotation = false); bool printDebugMessage(int x, int y, int alpha, byte r, byte g, byte b, LPCSTR Message); void getVisibleObjects(int from, int to, RenderView& renderView); bool checkPortal(short roomIndex, ROOM_DOOR* portal,const Matrix& viewProjection); void collectRooms(RenderView& renderView); void collectItems(short roomNumber, RenderView& renderView); void collectStatics(short roomNumber, RenderView& renderView); void collectLightsForEffect(short roomNumber, RendererEffect* effect, RenderView& renderView); void collectLightsForItem(short roomNumber, RendererItem* item, RenderView& renderView); void collectLightsForRoom(short roomNumber, RenderView& renderView); void prepareLights(RenderView& view); void collectEffects(short roomNumber, RenderView& renderView); void clearSceneItems(); void updateItemsAnimations(RenderView& view); void updateEffects(RenderView& view); int getFrame(short animation, short frame, ANIM_FRAME** framePtr, int* rate); bool drawAmbientCubeMap(short roomNumber); bool sphereBoxIntersection(DirectX::SimpleMath::Vector3 boxMin, DirectX::SimpleMath::Vector3 boxMax, DirectX::SimpleMath::Vector3 sphereCentre, float sphereRadius); void drawHorizonAndSky(ID3D11DepthStencilView* depthTarget); void drawRooms(bool transparent, bool animated, RenderView& view); void drawItems(bool transparent, bool animated,RenderView& view); void drawAnimatingItem(RenderView& view,RendererItem* item, bool transparent, bool animated); void drawBaddieGunflashes(RenderView& view); void drawScaledSpikes(RenderView& view,RendererItem* item, bool transparent, bool animated); void drawExpandingPlatform(RenderView& view, RendererItem* item, bool transparent, bool animated); void drawStatics(bool transparent, RenderView& view); void renderShadowMap(RenderView& view); void drawWraithExtra(RenderView& view,RendererItem* item, bool transparent, bool animated); void drawLara(RenderView& view,bool transparent, bool shadowMap); void printDebugMessage(LPCSTR message, ...); void drawFires(RenderView& view); void drawSparks(RenderView& view); void drawSmokes(RenderView& view); void drawEnergyArcs(RenderView& view); void drawBlood(RenderView& view); void drawDrips(RenderView& view); void drawBubbles(RenderView& view); void drawEffects(RenderView& view,bool transparent); void drawEffect(RenderView& view,RendererEffect* effect, bool transparent); void drawSplahes(RenderView& view); void drawSprites(RenderView& view); void drawLines3D(RenderView& view); void drawLines2D(RenderView& view); void drawOverlays(RenderView& view); void drawRopes(RenderView& view); void drawBats(RenderView& view); void drawRats(RenderView& view); void drawLittleBeetles(RenderView& view); void drawSpiders(RenderView& view); bool drawGunFlashes(RenderView& view); void drawGunShells(RenderView& view); void renderInventoryScene(ID3D11RenderTargetView* target, ID3D11DepthStencilView* depthTarget, ID3D11ShaderResourceView* background); void renderTitleMenu(); void renderPauseMenu(); void renderNewInventory(); void drawStatistics(); void drawExamines(); void drawDiary(); void drawDebris(RenderView& view,bool transparent); void drawFullScreenImage(ID3D11ShaderResourceView* texture, float fade, ID3D11RenderTargetView* target, ID3D11DepthStencilView* depthTarget); void createBillboardMatrix(DirectX::SimpleMath::Matrix* out, DirectX::SimpleMath::Vector3* particlePos, DirectX::SimpleMath::Vector3* cameraPos, float rotation); void drawShockwaves(RenderView& view); void drawRipples(RenderView& view); void drawUnderwaterDust(RenderView& view); void drawFullScreenQuad(ID3D11ShaderResourceView* texture, DirectX::SimpleMath::Vector3 color, bool cinematicBars); bool isRoomUnderwater(short roomNumber); bool isInRoom(int x, int y, int z, short roomNumber); void drawColoredQuad(int x, int y, int w, int h, DirectX::SimpleMath::Vector4 color); void initialiseScreen(int w, int h, int refreshRate, bool windowed, HWND handle, bool reset); void initialiseBars(); void drawSmokeParticles(RenderView& view); void drawSparkParticles(RenderView& view); void drawDripParticles(RenderView& view); void drawExplosionParticles(RenderView& view); void renderToCubemap(const RenderTargetCube& dest,const Vector3& pos,int roomNumber); void drawLaraHolsters(bool transparent); void drawLaraMesh(RendererMesh* mesh, bool transparent); void drawSimpleParticles(RenderView& view); void setBlendMode(BLEND_MODES blendMode); public: RendererMesh* getRendererMeshFromTrMesh(RendererObject* obj, MESH* meshPtr, short boneIndex, int isJoints, int isHairs); DirectX::SimpleMath::Matrix View; DirectX::SimpleMath::Matrix Projection; DirectX::SimpleMath::Matrix ViewProjection; float FieldOfView; int ScreenWidth; int ScreenHeight; bool Windowed; int NumTexturePages; Renderer11(); ~Renderer11(); void Create(); void EnumerateVideoModes(); void Initialise(int w, int h, int refreshRate, bool windowed, HWND handle); void Draw(); bool PrepareDataForTheRenderer(); void UpdateCameraMatrices(CAMERA_INFO* cam, float roll, float fov); void renderSimpleScene(ID3D11RenderTargetView* target, ID3D11DepthStencilView* depthTarget, RenderView& view); void DumpGameScene(); void renderInventory(); void renderTitle(); void renderScene(ID3D11RenderTargetView* target, ID3D11DepthStencilView* depthTarget, RenderView& view); void drawPickup(short objectNum); int SyncRenderer(); void drawString(int x, int y, const char* string, D3DCOLOR color, int flags); void clearDynamicLights(); void addDynamicLight(int x, int y, int z, short falloff, byte r, byte g, byte b); void freeRendererData(); void enableCinematicBars(bool value); void fadeIn(); void fadeOut(); void renderLoadingScreen(std::wstring& fileName); void updateProgress(float value); bool isFading(); void getLaraBonePosition(DirectX::SimpleMath::Vector3* pos, int bone); void toggleFullScreen(); bool isFullsScreen(); std::vector* getAdapters(); void renderTitleImage(); void addLine2D(int x1, int y1, int x2, int y2, byte r, byte g, byte b, byte a); void addLine3D(DirectX::SimpleMath::Vector3 start, DirectX::SimpleMath::Vector3 end, DirectX::SimpleMath::Vector4 color); void changeScreenResolution(int width, int height, int frequency, bool windowed); void flipRooms(short roomNumber1, short roomNumber2); void resetAnimations(); void updateLaraAnimations(bool force); void updateItemAnimations(int itemNumber, bool force); void getLaraAbsBonePosition(DirectX::SimpleMath::Vector3* pos, int joint); void getItemAbsBonePosition(int itemNumber, DirectX::SimpleMath::Vector3* pos, int joint); int getSpheres(short itemNumber, BoundingSphere* ptr, char worldSpace, DirectX::SimpleMath::Matrix local); void getBoneMatrix(short itemNumber, int joint, DirectX::SimpleMath::Matrix* outMatrix); void drawObjectOn2DPosition(short x, short y, short objectNum, short rotX, short rotY, short rotZ, float scale1); void drawBar(float percent, const RendererHUDBar * const bar, int frame, bool poison); RendererMesh* getMesh(int meshIndex); private: Texture2D createDefaultNormalTexture(); void drawFootprints(RenderView& view); template ConstantBuffer createConstantBuffer() { return ConstantBuffer(m_device.Get()); } }; extern Renderer11 g_Renderer; }