|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * |
| 4 | + * @author Jonathan Wilson |
| 5 | + * |
| 6 | + * @brief Prop Drawing Code |
| 7 | + * |
| 8 | + * @copyright Thyme is free software: you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU General Public License |
| 10 | + * as published by the Free Software Foundation, either version |
| 11 | + * 2 of the License, or (at your option) any later version. |
| 12 | + * A full copy of the GNU General Public License can be found in |
| 13 | + * LICENSE |
| 14 | + */ |
| 15 | +#include "w3dpropbuffer.h" |
| 16 | +#include "assetmgr.h" |
| 17 | +#include "baseheightmap.h" |
| 18 | +#include "camera.h" |
| 19 | +#include "geometry.h" |
| 20 | +#include "globaldata.h" |
| 21 | +#include "light.h" |
| 22 | +#include "lightenv.h" |
| 23 | +#include "partitionmanager.h" |
| 24 | +#include "playerlist.h" |
| 25 | +#include "rinfo.h" |
| 26 | +#include "w3dshroud.h" |
| 27 | + |
| 28 | +W3DPropBuffer::W3DPropBuffer() : m_propsUpdated(false), m_redoCull(false) |
| 29 | +{ |
| 30 | + m_isInited = false; |
| 31 | + Clear_All_Props(); |
| 32 | + m_light = new LightClass(LightClass::DIRECTIONAL); |
| 33 | + m_shroudMaterial = new W3DShroudMaterialPassClass(); |
| 34 | + m_isInited = true; |
| 35 | +} |
| 36 | + |
| 37 | +W3DPropBuffer::~W3DPropBuffer() |
| 38 | +{ |
| 39 | + for (int i = 0; i < MAX_PROP_TYPES; i++) { |
| 40 | + Ref_Ptr_Release(m_propTypes[i].render_obj); |
| 41 | + } |
| 42 | + |
| 43 | + Ref_Ptr_Release(m_light); |
| 44 | + Ref_Ptr_Release(m_shroudMaterial); |
| 45 | +} |
| 46 | + |
| 47 | +void W3DPropBuffer::Cull(CameraClass *camera) |
| 48 | +{ |
| 49 | + for (int i = 0; i < m_numProps; i++) { |
| 50 | + m_props[i].is_visible = !camera->Cull_Sphere(m_props[i].bounding_sphere); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void W3DPropBuffer::Clear_All_Props() |
| 55 | +{ |
| 56 | + m_numProps = 0; |
| 57 | + |
| 58 | + for (int i = 0; i < MAX_PROP_TYPES; i++) { |
| 59 | + Ref_Ptr_Release(m_propTypes[i].render_obj); |
| 60 | + m_propTypes[i].name.Clear(); |
| 61 | + } |
| 62 | + |
| 63 | + m_numPropTypes = 0; |
| 64 | +} |
| 65 | + |
| 66 | +int W3DPropBuffer::Add_Prop_Type(const Utf8String &name) |
| 67 | +{ |
| 68 | + if (m_numPropTypes >= MAX_PROP_TYPES) { |
| 69 | + captainslog_dbgassert(false, "Too many kinds of props in map. Reduce kinds of props, or raise prop limit."); |
| 70 | + return 0; |
| 71 | + } else { |
| 72 | + m_propTypes[m_numPropTypes].render_obj = W3DAssetManager::Get_Instance()->Create_Render_Obj(name.Str()); |
| 73 | + |
| 74 | + if (m_propTypes[m_numPropTypes].render_obj == nullptr) { |
| 75 | + captainslog_dbgassert(false, "Unable to find model for prop %s", name.Str()); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + |
| 79 | + m_propTypes[m_numPropTypes].name = name; |
| 80 | + m_propTypes[m_numPropTypes].bounding_sphere = m_propTypes[m_numPropTypes].render_obj->Get_Bounding_Sphere(); |
| 81 | + return m_numPropTypes++; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void W3DPropBuffer::Add_Prop(int id, Coord3D position, float orientation, float scale, const Utf8String &name) |
| 86 | +{ |
| 87 | + if (m_numProps < MAX_PROPS && m_isInited) { |
| 88 | + int index = -1; |
| 89 | + |
| 90 | + for (int i = 0; i < m_numPropTypes; i++) { |
| 91 | + if (m_propTypes[i].name.Compare_No_Case(name) == 0) { |
| 92 | + index = i; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (index >= 0 || (index = Add_Prop_Type(name), index >= 0)) { |
| 98 | + Matrix3D tm(true); |
| 99 | + tm.Rotate_Z(orientation); |
| 100 | + tm.Scale(scale); |
| 101 | + tm.Set_Translation(Vector3(position.x, position.y, position.z)); |
| 102 | + m_props[m_numProps].position = position; |
| 103 | + m_props[m_numProps].id = id; |
| 104 | + m_props[m_numProps].shroud_status = SHROUDED_INVALID; |
| 105 | + m_props[m_numProps].render_obj = m_propTypes[index].render_obj->Clone(); |
| 106 | + m_props[m_numProps].render_obj->Set_Transform(tm); |
| 107 | + m_props[m_numProps].render_obj->Set_ObjectScale(scale); |
| 108 | + m_props[m_numProps].prop_type = index; |
| 109 | + m_props[m_numProps].bounding_sphere = m_propTypes[index].bounding_sphere; |
| 110 | + m_props[m_numProps].bounding_sphere.Center += Vector3(position.x, position.y, position.z); |
| 111 | + m_props[m_numProps].is_visible = false; |
| 112 | + m_numProps++; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +bool W3DPropBuffer::Update_Prop_Position(int id, const Coord3D &position, float orientation, float scale) |
| 118 | +{ |
| 119 | + for (int i = 0; i < m_numProps; i++) { |
| 120 | + if (m_props[i].id == id) { |
| 121 | + Matrix3D tm(true); |
| 122 | + tm.Rotate_Z(orientation); |
| 123 | + tm.Scale(scale); |
| 124 | + tm.Set_Translation(Vector3(position.x, position.y, position.z)); |
| 125 | + m_props[i].position = position; |
| 126 | + m_props[i].render_obj->Set_Transform(tm); |
| 127 | + m_props[i].render_obj->Set_ObjectScale(scale); |
| 128 | + m_props[i].bounding_sphere = m_propTypes[m_props[i].prop_type].bounding_sphere; |
| 129 | + m_props[i].bounding_sphere.Center += Vector3(position.x, position.y, position.z); |
| 130 | + m_propsUpdated = true; |
| 131 | + return true; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return false; |
| 136 | +} |
| 137 | + |
| 138 | +void W3DPropBuffer::Remove_Prop(int id) |
| 139 | +{ |
| 140 | + for (int i = 0; i < m_numProps; i++) { |
| 141 | + if (m_props[i].id == id) { |
| 142 | + m_props[i].position.Set(0.0f, 0.0f, 0.0f); |
| 143 | + m_props[i].prop_type = -1; |
| 144 | + Ref_Ptr_Release(m_props[i].render_obj); |
| 145 | + m_props[i].bounding_sphere.Center = Vector3(0.0f, 0.0f, 0.0f); |
| 146 | + m_props[i].bounding_sphere.Radius = 1.0f; |
| 147 | + m_propsUpdated = true; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void W3DPropBuffer::Remove_Props_For_Construction(const Coord3D *position, const GeometryInfo &geometry, float angle) |
| 153 | +{ |
| 154 | + for (int i = 0; i < m_numProps; i++) { |
| 155 | + if (m_props[i].render_obj != nullptr) { |
| 156 | + float radius = m_props[i].bounding_sphere.Radius; |
| 157 | + GeometryInfo geometry2(GEOMETRY_CYLINDER, false, 5.0f * radius, 2.0f * radius, 2.0f * radius); |
| 158 | + |
| 159 | + if (g_thePartitionManager->Geom_Collides_With_Geom( |
| 160 | + position, geometry, angle, &m_props[i].position, geometry2, 0.0f)) { |
| 161 | + m_props[i].position.Set(0.0f, 0.0f, 0.0f); |
| 162 | + m_props[i].prop_type = -1; |
| 163 | + Ref_Ptr_Release(m_props[i].render_obj); |
| 164 | + m_props[i].bounding_sphere.Center = Vector3(0.0f, 0.0f, 0.0f); |
| 165 | + m_props[i].bounding_sphere.Radius = 1.0f; |
| 166 | + m_propsUpdated = true; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +void W3DPropBuffer::Notify_Shroud_Changed() |
| 173 | +{ |
| 174 | + for (int i = 0; i < m_numProps; i++) { |
| 175 | + m_props[i].shroud_status = g_thePartitionManager == nullptr ? SHROUDED_NONE : SHROUDED_INVALID; |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +void W3DPropBuffer::Draw_Props(RenderInfoClass &rinfo) |
| 180 | +{ |
| 181 | + if (m_redoCull) { |
| 182 | + Cull(&rinfo.m_camera); |
| 183 | + } |
| 184 | + |
| 185 | + GlobalData::TerrainLighting *lighting = |
| 186 | + g_theWriteableGlobalData->m_terrainObjectLighting[g_theWriteableGlobalData->m_timeOfDay]; |
| 187 | + LightEnvironmentClass lightenv; |
| 188 | + Vector3 object_center(0.0f, 0.0f, 0.0f); |
| 189 | + Vector3 ambient(lighting[0].ambient.red, lighting[0].ambient.green, lighting[0].ambient.blue); |
| 190 | + lightenv.Reset(object_center, ambient); |
| 191 | + Matrix3D tm; |
| 192 | + Vector3 v(0.0f, 0.0f, 0.0f); |
| 193 | + Vector3 x(1.0f, 0.0f, 0.0f); |
| 194 | + Vector3 y(0.0f, 1.0f, 0.0f); |
| 195 | + |
| 196 | + for (int i = 0; i < LIGHT_COUNT; i++) { |
| 197 | + m_light->Set_Ambient(v); |
| 198 | + m_light->Set_Diffuse(Vector3(lighting[i].diffuse.red, lighting[i].diffuse.green, lighting[i].diffuse.blue)); |
| 199 | + m_light->Set_Specular(v); |
| 200 | + tm.Set(x, y, Vector3(lighting[i].lightPos.x, lighting[i].lightPos.y, lighting[i].lightPos.z), v); |
| 201 | + m_light->Set_Transform(tm); |
| 202 | + lightenv.Add_Light(*m_light); |
| 203 | + } |
| 204 | + |
| 205 | + rinfo.m_lightEnvironment = &lightenv; |
| 206 | + |
| 207 | + for (int i = 0; i < m_numProps; i++) { |
| 208 | + if (m_props[i].is_visible && m_props[i].render_obj != nullptr) { |
| 209 | + if (g_thePlayerList == nullptr || g_thePartitionManager == nullptr) { |
| 210 | + m_props[i].shroud_status = SHROUDED_NONE; |
| 211 | + } |
| 212 | + |
| 213 | + if (m_props[i].shroud_status == SHROUDED_INVALID) { |
| 214 | + int index; |
| 215 | + |
| 216 | + if (g_thePlayerList != nullptr) { |
| 217 | + index = g_thePlayerList->Get_Local_Player()->Get_Player_Index(); |
| 218 | + } else { |
| 219 | + index = 0; |
| 220 | + } |
| 221 | + |
| 222 | + m_props[i].shroud_status = |
| 223 | + g_thePartitionManager->Get_Prop_Shroud_Status_For_Player(index, &m_props[i].position); |
| 224 | + |
| 225 | + if (m_props[i].shroud_status < SHROUDED_UNK4 && m_props[i].shroud_status > SHROUDED_INVALID) { |
| 226 | + if (g_theTerrainRenderObject->Get_Shroud() != nullptr && m_props[i].shroud_status != SHROUDED_INVALID) { |
| 227 | + rinfo.Push_Material_Pass(m_shroudMaterial); |
| 228 | + m_props[i].render_obj->Render(rinfo); |
| 229 | + rinfo.Pop_Material_Pass(); |
| 230 | + } else { |
| 231 | + m_props[i].render_obj->Render(rinfo); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + rinfo.m_lightEnvironment = nullptr; |
| 239 | +} |
0 commit comments