LCOV - code coverage report
Current view: top level - source/graphics - TerrainTextureEntry.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 113 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "TerrainTextureEntry.h"
      21             : 
      22             : #include "graphics/MaterialManager.h"
      23             : #include "graphics/TerrainProperties.h"
      24             : #include "graphics/TerrainTextureManager.h"
      25             : #include "graphics/TextureManager.h"
      26             : #include "lib/debug.h"
      27             : #include "lib/path.h"
      28             : #include "lib/utf8.h"
      29             : #include "ps/CLogger.h"
      30             : #include "ps/CStrIntern.h"
      31             : #include "ps/CStrInternStatic.h"
      32             : #include "ps/Errors.h"
      33             : #include "ps/Filesystem.h"
      34             : #include "ps/XMB/XMBData.h"
      35             : #include "ps/XMB/XMBStorage.h"
      36             : #include "ps/XML/Xeromyces.h"
      37             : #include "renderer/Renderer.h"
      38             : #include "renderer/SceneRenderer.h"
      39             : #include "renderer/backend/Sampler.h"
      40             : 
      41             : #include <cmath>
      42             : #include <cstddef>
      43             : #include <string>
      44             : #include <utility>
      45             : 
      46           0 : CTerrainTextureEntry::CTerrainTextureEntry(CTerrainPropertiesPtr properties, const VfsPath& path):
      47           0 :     m_pProperties(properties),
      48           0 :     m_BaseColor(0),
      49           0 :     m_BaseColorValid(false)
      50             : {
      51           0 :     ENSURE(properties);
      52             : 
      53           0 :     CXeromyces XeroFile;
      54           0 :     if (XeroFile.Load(g_VFS, path, "terrain_texture") != PSRETURN_OK)
      55             :     {
      56           0 :         LOGERROR("Terrain xml not found ({})", path.string8());
      57           0 :         return;
      58             :     }
      59             : 
      60             :     #define EL(x) int el_##x = XeroFile.GetElementID(#x)
      61             :     #define AT(x) int at_##x = XeroFile.GetAttributeID(#x)
      62           0 :     EL(tag);
      63           0 :     EL(terrain);
      64           0 :     EL(texture);
      65           0 :     EL(textures);
      66           0 :     EL(material);
      67           0 :     EL(props);
      68           0 :     EL(alphamap);
      69           0 :     AT(file);
      70           0 :     AT(name);
      71             :     #undef AT
      72             :     #undef EL
      73             : 
      74           0 :     XMBElement root = XeroFile.GetRoot();
      75             : 
      76           0 :     if (root.GetNodeName() != el_terrain)
      77             :     {
      78           0 :         LOGERROR("Invalid terrain format (unrecognized root element '{}')",
      79             :             XeroFile.GetElementString(root.GetNodeName()));
      80           0 :         return;
      81             :     }
      82             : 
      83           0 :     std::vector<std::pair<CStr, VfsPath> > samplers;
      84           0 :     VfsPath alphamap("standard");
      85           0 :     m_Tag = utf8_from_wstring(path.Basename().string());
      86             : 
      87           0 :     for (XMBElement child : root.GetChildNodes())
      88             :     {
      89           0 :         int child_name = child.GetNodeName();
      90             : 
      91           0 :         if (child_name == el_textures)
      92             :         {
      93           0 :             for (XMBElement textures_element : child.GetChildNodes())
      94             :             {
      95           0 :                 ENSURE(textures_element.GetNodeName() == el_texture);
      96             : 
      97           0 :                 CStr name;
      98           0 :                 VfsPath terrainTexturePath;
      99           0 :                 for (XMBAttribute relativePath : textures_element.GetAttributes())
     100             :                 {
     101           0 :                     if (relativePath.Name == at_file)
     102           0 :                         terrainTexturePath = VfsPath("art/textures/terrain") / relativePath.Value.FromUTF8();
     103           0 :                     else if (relativePath.Name == at_name)
     104           0 :                         name = relativePath.Value;
     105           0 :                 }
     106           0 :                 samplers.emplace_back(name, terrainTexturePath);
     107           0 :                 if (name == str_baseTex.string())
     108           0 :                     m_DiffuseTexturePath = terrainTexturePath;
     109             :             }
     110             : 
     111             :         }
     112           0 :         else if (child_name == el_material)
     113             :         {
     114           0 :             VfsPath mat = VfsPath("art/materials") / child.GetText().FromUTF8();
     115           0 :             if (CRenderer::IsInitialised())
     116           0 :                 m_Material = g_Renderer.GetSceneRenderer().GetMaterialManager().LoadMaterial(mat);
     117           0 :         }
     118           0 :         else if (child_name == el_alphamap)
     119             :         {
     120           0 :             alphamap = child.GetText().FromUTF8();
     121             :         }
     122           0 :         else if (child_name == el_props)
     123             :         {
     124           0 :             CTerrainPropertiesPtr ret (new CTerrainProperties(properties));
     125           0 :             ret->LoadXml(child, &XeroFile, path);
     126           0 :             m_pProperties = ret;
     127           0 :         }
     128           0 :         else if (child_name == el_tag)
     129             :         {
     130           0 :             m_Tag = child.GetText();
     131             :         }
     132             :     }
     133             : 
     134           0 :     for (size_t i = 0; i < samplers.size(); ++i)
     135             :     {
     136           0 :         CTextureProperties texture(samplers[i].second);
     137           0 :         texture.SetAddressMode(Renderer::Backend::Sampler::AddressMode::REPEAT);
     138           0 :         texture.SetAnisotropicFilter(true);
     139             : 
     140           0 :         if (CRenderer::IsInitialised())
     141             :         {
     142           0 :             CTexturePtr texptr = g_Renderer.GetTextureManager().CreateTexture(texture);
     143           0 :             m_Material.AddSampler(CMaterial::TextureSampler(samplers[i].first, texptr));
     144           0 :         }
     145           0 :     }
     146             : 
     147           0 :     if (CRenderer::IsInitialised())
     148           0 :         m_TerrainAlpha = g_TexMan.LoadAlphaMap(alphamap);
     149             : 
     150           0 :     float texAngle{0.f};
     151           0 :     float texSize{1.f};
     152             : 
     153           0 :     if (m_pProperties)
     154             :     {
     155           0 :         m_Groups = m_pProperties->GetGroups();
     156           0 :         texAngle = m_pProperties->GetTextureAngle();
     157           0 :         texSize = m_pProperties->GetTextureSize();
     158             :     }
     159           0 :     GenerateTextureMatrix(texAngle, texSize);
     160             : 
     161           0 :     GroupVector::iterator it=m_Groups.begin();
     162           0 :     for (;it!=m_Groups.end();++it)
     163           0 :         (*it)->AddTerrain(this);
     164           0 : }
     165             : 
     166           0 : CTerrainTextureEntry::CTerrainTextureEntry(const CStr tag):
     167           0 :     m_pProperties(nullptr),
     168           0 :     m_BaseColor(0),
     169           0 :     m_BaseColorValid(false),
     170           0 :     m_DiffuseTexturePath(""),
     171           0 :     m_Tag(tag)
     172             : {
     173           0 :     if (!CRenderer::IsInitialised())
     174           0 :         return;
     175             : 
     176           0 :     const VfsPath alphamap{"standard"};
     177           0 :     const VfsPath mat{VfsPath{"art/materials"} / "terrain_norm_spec.xml"};
     178           0 :     m_Material = g_Renderer.GetSceneRenderer().GetMaterialManager().LoadMaterial(mat);
     179           0 :     const CTexturePtr texptr{g_Renderer.GetTextureManager().GetErrorTexture()};
     180           0 :     m_Material.AddSampler(CMaterial::TextureSampler{str_baseTex, texptr});
     181           0 :     m_TerrainAlpha = g_TexMan.LoadAlphaMap(alphamap);
     182             : 
     183           0 :     GenerateTextureMatrix(0.0f, 1.f);
     184           0 : }
     185             : 
     186           0 : CTerrainTextureEntry::~CTerrainTextureEntry()
     187             : {
     188           0 :     for (GroupVector::iterator it=m_Groups.begin();it!=m_Groups.end();++it)
     189           0 :         (*it)->RemoveTerrain(this);
     190           0 : }
     191             : 
     192             : // BuildBaseColor: calculate the root color of the texture, used for coloring minimap, and store
     193             : // in m_BaseColor member
     194           0 : void CTerrainTextureEntry::BuildBaseColor()
     195             : {
     196             :     // Use the explicit properties value if possible
     197           0 :     if (m_pProperties && m_pProperties->HasBaseColor())
     198             :     {
     199           0 :         m_BaseColor=m_pProperties->GetBaseColor();
     200           0 :         m_BaseColorValid = true;
     201           0 :         return;
     202             :     }
     203             : 
     204             :     // Use the texture color if available
     205           0 :     if (GetTexture()->TryLoad())
     206             :     {
     207           0 :         m_BaseColor = GetTexture()->GetBaseColor();
     208           0 :         m_BaseColorValid = true;
     209             :     }
     210             : }
     211             : 
     212           0 : void CTerrainTextureEntry::GenerateTextureMatrix(const float texAngle, const float texSize)
     213             : {
     214           0 :     m_TextureMatrix.SetZero();
     215           0 :     m_TextureMatrix._11 = cosf(texAngle) / texSize;
     216           0 :     m_TextureMatrix._13 = -sinf(texAngle) / texSize;
     217           0 :     m_TextureMatrix._21 = -sinf(texAngle) / texSize;
     218           0 :     m_TextureMatrix._23 = -cosf(texAngle) / texSize;
     219           0 :     m_TextureMatrix._44 = 1.f;
     220           0 : }

Generated by: LCOV version 1.16