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 : /* 19 : * Home to the ModelRenderer class, an abstract base class that manages 20 : * a per-frame list of submitted models, as well as simple helper 21 : * classes. 22 : */ 23 : 24 : #ifndef INCLUDED_MODELRENDERER 25 : #define INCLUDED_MODELRENDERER 26 : 27 : #include "graphics/MeshManager.h" 28 : #include "graphics/RenderableObject.h" 29 : #include "renderer/SceneRenderer.h" 30 : 31 : #include <memory> 32 : #include <span> 33 : #include <vector> 34 : 35 : class CModel; 36 : class CShaderDefines; 37 : class CVector3D; 38 : namespace Renderer::Backend { class IDeviceCommandContext; } 39 : struct SColor4ub; 40 : template <typename T> class VertexArrayIterator; 41 : 42 : class ModelVertexRenderer; 43 : class RenderModifier; 44 : 45 : /** 46 : * Class CModelRData: Render data that is maintained per CModel. 47 : * ModelRenderer implementations may derive from this class to store 48 : * per-CModel data. 49 : * 50 : * The main purpose of this class over CRenderData is to track which 51 : * ModelRenderer the render data belongs to (via the key that is passed 52 : * to the constructor). When a model changes the renderer it uses 53 : * (e.g. via run-time modification of the gpu skinning configuration), 54 : * the old ModelRenderer's render data is supposed to be replaced by 55 : * the new data. 56 : */ 57 : class CModelRData : public CRenderData 58 : { 59 : public: 60 0 : CModelRData(const void* key) : m_Key(key) { } 61 : 62 : /** 63 : * GetKey: Retrieve the key that can be used to identify the 64 : * ModelRenderer that created this data. 65 : * 66 : * @return The opaque key that was passed to the constructor. 67 : */ 68 0 : const void* GetKey() const { return m_Key; } 69 : 70 : private: 71 : /// The key for model renderer identification 72 : const void* m_Key; 73 : }; 74 : 75 : 76 : /** 77 : * ModelRenderer renders a per-frame list of models. It loads the appropriate 78 : * shaders for rendering each model, and that batches by shader technique (and 79 : * by mesh and texture). 80 : * 81 : * ModelRenderer delegates vertex transformation/setup to a 82 : * ModelVertexRenderer. It delegates fragment stage setup to a RenderModifier. 83 : * 84 : * ModelRenderer also contains a number of static helper functions 85 : * for building vertex arrays. 86 : */ 87 : class ModelRenderer 88 : { 89 : public: 90 : /** 91 : * Render: Render submitted models, using the given RenderModifier to setup 92 : * the fragment stage. 93 : * 94 : * preconditions : PrepareModels must be called after all models have been 95 : * submitted and before calling Render. 96 : * 97 : * @param modifier The RenderModifier that specifies the fragment stage. 98 : * @param flags If flags is 0, all submitted models are rendered. 99 : * If flags is non-zero, only models that contain flags in their 100 : * CModel::GetFlags() are rendered. 101 : */ 102 : void Render( 103 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 104 : ModelVertexRenderer& modelVertexRenderer, RenderModifier& modifier, const CShaderDefines& context, 105 : int cullGroup, int flags, const ERenderMode renderMode, std::span<CModel*> submissions); 106 : 107 : /** 108 : * CopyPositionAndNormals: Copy unanimated object-space vertices and 109 : * normals into the given vertex array. 110 : * 111 : * @param mdef The underlying CModelDef that contains mesh data. 112 : * @param Position Points to the array that will receive 113 : * position vectors. The array behind the iterator 114 : * must be large enough to hold model->GetModelDef()->GetNumVertices() 115 : * vertices. 116 : * @param Normal Points to the array that will receive normal vectors. 117 : * The array behind the iterator must be as large as the Position array. 118 : */ 119 : static void CopyPositionAndNormals( 120 : const CModelDefPtr& mdef, 121 : const VertexArrayIterator<CVector3D>& Position, 122 : const VertexArrayIterator<CVector3D>& Normal); 123 : 124 : /** 125 : * BuildPositionAndNormals: Build animated vertices and normals, 126 : * transformed into world space. 127 : * 128 : * @param model The model that is to be transformed. 129 : * @param Position Points to the array that will receive 130 : * transformed position vectors. The array behind the iterator 131 : * must be large enough to hold model->GetModelDef()->GetNumVertices() 132 : * vertices. It must allow 16 bytes to be written to each element 133 : * (i.e. provide 4 bytes of padding after each CVector3D). 134 : * @param Normal Points to the array that will receive transformed 135 : * normal vectors. The array behind the iterator must be as large as 136 : * the Position array. 137 : */ 138 : static void BuildPositionAndNormals( 139 : CModel* model, 140 : const VertexArrayIterator<CVector3D>& Position, 141 : const VertexArrayIterator<CVector3D>& Normal); 142 : 143 : /** 144 : * BuildUV: Copy UV coordinates into the given vertex array. 145 : * 146 : * @param mdef The model def. 147 : * @param UV Points to the array that will receive UV coordinates. 148 : * The array behind the iterator must large enough to hold 149 : * mdef->GetNumVertices() vertices. 150 : */ 151 : static void BuildUV( 152 : const CModelDefPtr& mdef, 153 : const VertexArrayIterator<float[2]>& UV, 154 : int UVset); 155 : 156 : /** 157 : * BuildIndices: Create the indices array for the given CModelDef. 158 : * 159 : * @param mdef The model definition object. 160 : * @param Indices The index array, must be able to hold 161 : * mdef->GetNumFaces()*3 elements. 162 : */ 163 : static void BuildIndices( 164 : const CModelDefPtr& mdef, const VertexArrayIterator<std::uint16_t>& Indices); 165 : 166 : /** 167 : * GenTangents: Generate tangents for the given CModelDef. 168 : * 169 : * @param mdef The model definition object. 170 : * @param newVertices An out vector of the unindexed vertices with tangents added. 171 : * The new vertices cannot be used with existing face index and must be welded/reindexed. 172 : */ 173 : static void GenTangents(const CModelDefPtr& mdef, std::vector<float>& newVertices, bool gpuSkinning); 174 : }; 175 : 176 : #endif // INCLUDED_MODELRENDERER