Skyscraper 2.0
polygon.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Polygon Object
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#include "globals.h"
25#include "sbs.h"
26#include "dynamicmesh.h"
27#include "triangle.h"
28#include "mesh.h"
29#include "polymesh.h"
30#include "texture.h"
31#include "utility.h"
32#include "polygon.h"
33
34namespace SBS {
35
36Polygon::Polygon(Object *parent, const std::string &name, MeshObject *meshwrapper, GeometrySet &geometry, std::vector<Triangle> &triangles, Matrix3 &tex_matrix, Vector3 &tex_vector, const std::string &material, Plane &plane) : ObjectBase(parent)
37{
38 mesh = meshwrapper;
39 t_matrix = tex_matrix;
40 t_vector = tex_vector;
41 this->material = material;
42 this->plane = plane;
43 this->geometry = geometry;
44 this->triangles = triangles;
45 SetName(name);
46 size = triangles.size() * (sizeof(unsigned int) * 3);
47
48 vertex_count = 0;
49 for (size_t i = 0; i < geometry.size(); i++)
50 {
51 vertex_count += geometry[i].size();
52 size += geometry[i].size() * sizeof(Geometry);
53 }
54
56
58
59 //register texture usage
60 if (material != "")
62}
63
73
75{
76 //return texture mapping matrix and vector
77 tm = t_matrix;
78 tv = t_vector;
79}
80
81void Polygon::Move(const Vector3 &vector, Real speed)
82{
83 bool dynamic = mesh->UsingDynamicBuffers();
84
85 for (size_t i = 0; i < geometry.size(); i++)
86 {
87 for (size_t j = 0; j < geometry[i].size(); j++)
88 {
89 Polygon::Geometry &data = geometry[i][j];
90 data.vertex += sbs->ToRemote(vector * speed);
91 }
92 }
93
94 //update vertices in render buffer, if using dynamic buffers
95 if (dynamic == true)
97}
98
100{
101 //convert to an absolute plane
102 Plane plane2(plane.normal, sbs->ToRemote(mesh->GetPosition()));
103 return Plane(plane.normal, -(plane.d + plane2.d));
104}
105
107{
108 //returns the extents of a polygon
109 //coord must be either 1 (for x), 2 (for y) or 3 (for z)
110
111 //return 0,0 if coord value is out of range
112 if (coord < 1 || coord > 3)
113 return Vector2(0, 0);
114
115 //get polygon extents
116 PolyArray poly;
117 for (size_t i = 0; i < geometry.size(); i++)
118 {
119 for (size_t j = 0; j < geometry[i].size(); j++)
120 {
121 poly.emplace_back(geometry[i][j].vertex);
122 }
123 }
124
125 Vector2 extents = sbs->GetUtility()->GetExtents(poly, coord);
126
127 return extents;
128}
129
131{
132 bool dynamic = mesh->UsingDynamicBuffers();
133
134 Vector2 extents = GetExtents(2);
135
136 for (size_t i = 0; i < geometry.size(); i++)
137 {
138 for (size_t j = 0; j < geometry[i].size(); j++)
139 {
140 Polygon::Geometry &data = geometry[i][j];
141 if (data.vertex.y == extents.y)
142 data.vertex.y = sbs->ToRemote(newheight);
143 }
144 }
145
146 //update vertices in render buffer, if using dynamic buffers
147 if (dynamic == true)
149}
150
151bool Polygon::ReplaceTexture(const std::string &oldtexture, const std::string &newtexture)
152{
153 //replace material named oldtexture with newtexture
154
155 if (oldtexture == newtexture)
156 return false;
157
158 if (material == oldtexture)
159 {
160 std::string old = material;
162 material = newtexture;
164
165 bool result = mesh->GetDynamicMesh()->ChangeTexture(oldtexture, newtexture, mesh);
166 if (result == false)
167 {
168 material = old; //revert name
169 return false;
170 }
171
172 //mesh->ResetPrepare();
173 return true;
174 }
175 return false;
176}
177
178bool Polygon::ChangeTexture(const std::string &texture, bool matcheck)
179{
180 //changes a texture
181 //if matcheck is true, exit if old and new textures are the same
182
183 if (matcheck == true)
184 {
185 if (material == texture)
186 return false;
187 }
188
189 std::string old = material;
191 material = texture;
193
194 bool result = mesh->GetDynamicMesh()->ChangeTexture(old, texture, mesh);
195 if (result == false)
196 {
197 material = old; //revert name
198 return false;
199 }
200
201 //mesh->ResetPrepare();
202 return true;
203}
204
206{
207 int offset = 0;
208 for (size_t i = 0; i < geometry.size(); i++)
209 {
210 for (size_t j = 0; j < geometry[i].size(); j++)
211 {
212 if (index == offset + j)
213 return geometry[i][j].vertex;
214 }
215 offset += geometry[i].size();
216 }
217 return Vector3::ZERO;
218}
219
220}
void UpdateVertices(MeshObject *client, const std::string &material="", Polygon *polygon=0, bool single=false)
bool ChangeTexture(const std::string &old_texture, const std::string &new_texture, MeshObject *client=0)
void ResetPrepare()
Definition mesh.cpp:686
bool UsingDynamicBuffers()
Definition mesh.cpp:649
DynamicMesh * MeshWrapper
Definition mesh.h:98
DynamicMesh * GetDynamicMesh()
Definition mesh.cpp:1043
void SetName(const std::string &name)
Definition object.cpp:72
virtual Vector3 GetPosition(bool relative=false)
Definition object.cpp:321
std::vector< Triangle > triangles
Definition polygon.h:49
Polygon(Object *parent, const std::string &name, MeshObject *meshwrapper, GeometrySet &geometry, std::vector< Triangle > &triangles, Matrix3 &tex_matrix, Vector3 &tex_vector, const std::string &material, Plane &plane)
Definition polygon.cpp:36
size_t size
Definition polygon.h:58
int vertex_count
Definition polygon.h:48
Vector3 t_vector
Definition polygon.h:54
std::vector< std::vector< Geometry > > GeometrySet
Definition polygon.h:44
void Move(const Vector3 &vector, Real speed=1.0)
Definition polygon.cpp:81
bool ChangeTexture(const std::string &texture, bool matcheck=true)
Definition polygon.cpp:178
std::string material
Definition polygon.h:56
Vector3 GetVertex(int index)
Definition polygon.cpp:205
GeometrySet geometry
Definition polygon.h:47
Matrix3 t_matrix
Definition polygon.h:53
MeshObject * mesh
Definition polygon.h:46
Vector2 GetExtents(int coord)
Definition polygon.cpp:106
bool ReplaceTexture(const std::string &oldtexture, const std::string &newtexture)
Definition polygon.cpp:151
Plane GetAbsolutePlane()
Definition polygon.cpp:99
Plane plane
Definition polygon.h:50
void GetTextureMapping(Matrix3 &t_matrix, Vector3 &t_vector)
Definition polygon.cpp:74
void ChangeHeight(Real newheight)
Definition polygon.cpp:130
Utility * GetUtility()
Definition sbs.cpp:4611
bool FastDelete
Definition sbs.h:188
TextureManager * GetTextureManager()
Definition sbs.cpp:4558
int PolygonCount
Definition sbs.h:192
Real ToRemote(Real local_value)
Definition sbs.cpp:2407
void DecrementTextureUsage(const std::string &name)
Definition texture.cpp:1660
void IncrementTextureUsage(const std::string &name)
Definition texture.cpp:1648
Vector2 GetExtents(PolyArray &varray, int coord, bool flip_z=false)
Definition utility.cpp:49
Ogre::Matrix3 Matrix3
Definition globals.h:64
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
Ogre::Plane Plane
Definition globals.h:65
Ogre::Vector2 Vector2
Definition globals.h:59
std::vector< Vector3 > PolyArray
Definition sbs.h:118