#pragma once #include "RenderCommands3D.h" #include "HardwareScene.h" #include #include #include #include #include #include #include namespace ava2 { class Gr2ModelInstance { public: Gr2ModelInstance() = default; static void SetHardwareSkinningEnabled(bool enabled); explicit Gr2ModelInstance(std::shared_ptr impl) : m_impl(std::move(impl)) {} static std::unique_ptr Load(const std::string& filepath); static std::unique_ptr LoadFromMemory(const uint8_t* data, size_t size, const std::string& name = ""); // Load external animation (.gr2) to animate this model bool LoadExternalAnimation(const std::string& anim_filepath); float GetAnimationDuration() const; // Granny loop window: looping motions pass (true, true) so the seam samples across loops. void SetAnimationLoop(bool underflow_loop, bool overflow_loop); // Bone queries & attachments bool GetBoneWorldTransform(const std::string& bone_name, float* out_matrix, float animation_time = -1.0f) const; void AttachModel(const std::string& bone_name, std::shared_ptr attachment, const float* offset_matrix = nullptr); void AttachPart(std::shared_ptr part); void DetachAll(); void DetachModel(const std::string& bone_name); void DetachPart(const std::shared_ptr& part); // CActorInstance::AttachSash: the attached instance plays its own motion 0 on the parent's clock. void SetAttachmentAnimated(const std::string& bone_name, bool animated); // ENABLE_OBJ_SCALLING: the attachment is scaled about the parent origin in Granny (Z-up) space. void SetAttachmentScale(const std::string& bone_name, float sx, float sy, float sz); // CGrannyModelInstance::SetMaterialImagePointer: textures whose StringPath matches src are drawn with dst. void SetSkin(const std::string& src_file_name, const std::string& dst_file_name); void ClearSkins(); const std::string& GetFilePath() const; // Build 3D draw commands for this model and all attached components (weapons, hair, armor parts) std::vector BuildDrawCommands(float animation_time = -1.0f, const float* world_matrix = nullptr, bool forceCPU = false); // Build draw commands for a skinned part using an external bone palette std::vector BuildDrawCommandsWithPalette(const std::vector& skin_matrices, const float* world_matrix = nullptr, const gr2::Skeleton* parent_skeleton = nullptr); bool GetLocalBounds(float& minX, float& minY, float& minZ, float& maxX, float& maxY, float& maxZ) const; const gr2::File& GetFile() const { return m_impl->GetFile(); } const gr2::FileInfo& GetInfo() const { return m_impl->GetInfo(); } std::shared_ptr GetImpl() const { return m_impl; } private: std::shared_ptr m_impl; std::vector> m_attached_models; std::vector> m_attached_parts; }; class Gr2SceneRenderer { public: Gr2SceneRenderer(int width = 1024, int height = 768); ~Gr2SceneRenderer(); void SetCameraLookAt(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ, float upX, float upY, float upZ); // D3DXMatrixLookAtRH as used by the original CCamera::SetViewMatrix, expressed for this // renderer's LH projection: LookAtLH with the x axis negated (RH view x RH proj == this view x LH proj). void SetCameraLookAtRH(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ, float upX, float upY, float upZ); void SetPerspective(float fovYDegrees, float aspect, float zNear, float zFar); void SetViewport(int x, int y, int width, int height); void Clear(uint32_t color = 0xFF101520, float depth = 1.0f); // Rasterize a 3D draw command into the surface using depth buffering and lighting void RenderDraw(SDL_Surface* targetSurface, const Render3DDraw& draw); // Rasterize a list of draw commands (CPU software rasterizer) void RenderScene(SDL_Surface* targetSurface, const std::vector& draws); void RenderScene(SDL_Surface* targetSurface, const std::vector& draws); // Native SDL_GPU depth/perspective pipeline; SDL geometry fallback for software renderers. void RenderSceneGPU(SDL_Renderer* renderer, const std::vector& draws); void RenderSceneGPU(SDL_Renderer* renderer, const std::vector& draws); void SetGPUDevice(SDL_GPUDevice* device); // The SDL_GPU pipeline composites its own (smaller) colour target to the full render target. bool HasHardwarePipeline() const { return m_hardware != nullptr; } void DestroyGPUTextures(); SDL_Texture* GetGPUTexture(SDL_Renderer* renderer, const std::string& texturePath); // Exponential & Linear Depth Fog (modeled after 40250 CMapOutdoor fog) void SetFog(bool enable, uint32_t color = 0xFFCCDDEE, float density = 0.0003f, float fog_near = 300.0f, float fog_far = 3000.0f, bool exp_mode = true); void SetTextureBudgets(size_t cpuBytes,size_t gpuBytes); size_t GetHardwareTextureBytes() const {return m_hardware ? m_hardware->GetTextureBytes() : 0;} size_t GetHardwareTextureUploadCount() const {return m_hardware ? m_hardware->GetTextureUploadCount() : 0;} size_t GetDecodedTextureBytes() const { return m_decodedTextureBytes; } size_t GetTextureDecodeCount() const { return m_textureDecodeCount; } bool IsFogEnabled() const { return m_fog_enabled; } uint32_t GetFogColor() const { return m_fog_color; } void SetLogicalSize(int width, int height) { m_logicalWidth = width; m_logicalHeight = height; } int GetLogicalWidth() const { return m_logicalWidth > 0 ? m_logicalWidth : m_viewport[2]; } int GetLogicalHeight() const { return m_logicalHeight > 0 ? m_logicalHeight : m_viewport[3]; } const float* GetViewMatrix() const { return m_view; } const float* GetProjMatrix() const { return m_proj; } const int* GetViewport() const { return m_viewport; } void GetEyePosition(float& x, float& y, float& z) const { x = m_eye[0]; y = m_eye[1]; z = m_eye[2]; } // Project world coordinates (X, Y, Z) to 2D screen coordinates bool ProjectToScreen(float worldX, float worldY, float worldZ, float& screenX, float& screenY) const; // Unproject 2D screen coordinates (screenX, screenY) to a 3D ray in world space bool ScreenToWorldRay(float screenX, float screenY, float rayOrig[3], float rayDir[3]) const; // Pick 3D point on terrain heightmap via ray marching and binary search refinement bool PickTerrain(float screenX, float screenY, const class TerrainHeightmap& terrain, float& pickX, float& pickY, float& pickZ, float maxRange = 5000.0f) const; const std::vector& GetDepthBuffer() const { return m_depthBuffer; } private: struct TextureData { uint64_t lastUsed = 0; int width = 0; int height = 0; std::vector rgba; }; void TrimTextureCaches(bool protectDecodedWorkingSet); const TextureData* GetTexture(const std::string& texturePath); struct GpuTransformedVertex { float x_clip, y_clip, z_clip, w_clip; float x_screen, y_screen; float u, v; float r, g, b, a; }; struct DrawOrder { int priority = 1; float depth = 0.0f; const Render3DDraw* draw = nullptr; SDL_GPUTexture* texture = nullptr; // resolved once per draw; also the opaque sort key }; float m_eye[3] = {0.0f, 0.0f, 0.0f}; float m_view[16]; float m_proj[16]; int m_viewport[4]; int m_logicalWidth = 0; int m_logicalHeight = 0; std::vector m_depthBuffer; std::unordered_map m_textureCache; struct SDLTextureEntry {SDL_Texture* texture=nullptr;size_t bytes=0;uint64_t lastUsed=0;}; std::unordered_map m_gpuTextureCache; uint64_t m_textureFrame=0; size_t m_decodedTextureBytes=0,m_textureDecodeCount=0,m_decodedTextureBudget=64u*1024u*1024u,m_gpuTextureBudget=256u*1024u*1024u; // Persistent scratch buffers to eliminate per-draw heap allocation churn in RenderSceneGPU std::vector m_scratchSortedDraws; std::vector m_scratchDrawPtrs; std::vector m_scratchGpuXverts; std::vector m_scratchSdlVertices; std::vector m_scratchSdlIndices; std::unique_ptr m_hardware; std::vector m_hardwareVertices; std::vector m_hardwareIndices; std::vector m_hardwareBatches; bool m_fog_enabled = false; uint32_t m_fog_color = 0xFFCCDDEE; float m_fog_density = 0.0003f; float m_fog_near = 300.0f; float m_fog_far = 3000.0f; bool m_fog_exp = true; }; } // namespace ava2