Skip to content

Commit f21de43

Browse files
committed
Optimize RadarRenderer::renderBoxPyrMesh()
1 parent 090f82d commit f21de43

File tree

4 files changed

+145
-137
lines changed

4 files changed

+145
-137
lines changed

include/MeshObstacle.h

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <iostream>
2525
#include <string>
2626
#include <vector>
27+
#include <deque>
2728

2829
// Common headers
2930
#include "vectors.h"
@@ -106,8 +107,18 @@ class MeshObstacle final : public Obstacle
106107
int getVertexCount() const;
107108
int getNormalCount() const;
108109
int getTexcoordCount() const;
109-
int getFaceCount() const;
110-
MeshFace* getFace(int face) const;
110+
int getFaceCount() const
111+
{
112+
return faces.size();
113+
}
114+
const MeshFace* getFace(int face) const
115+
{
116+
return &faces[face];
117+
}
118+
MeshFace* getFace(int face)
119+
{
120+
return &faces[face];
121+
}
111122
const float* getPosition() const;
112123
bool useSmoothBounce() const;
113124
bool noClusters() const;
@@ -122,13 +133,24 @@ class MeshObstacle final : public Obstacle
122133
void print(std::ostream& out, const std::string& indent) const override;
123134
void printOBJ(std::ostream& out, const std::string& indent) const override;
124135

136+
struct RadarFaceCache
137+
{
138+
bool noRadar;
139+
float posZ;
140+
float sizeZ;
141+
int phydrv;
142+
int vertexCount;
143+
float vx[4];
144+
float vy[4];
145+
};
146+
std::vector<RadarFaceCache> radarCache;
147+
125148
private:
126149
void makeFacePointers(const std::vector<int>& _vertices,
127150
const std::vector<int>& _normals,
128151
const std::vector<int>& _texcoords,
129152
float**& v, float**& n, float**& t);
130153

131-
private:
132154
static const char* typeName;
133155

134156
std::string name;
@@ -142,8 +164,7 @@ class MeshObstacle final : public Obstacle
142164
afvec3* normals;
143165
int texcoordCount;
144166
afvec2* texcoords;
145-
int faceCount, faceSize;
146-
MeshFace** faces;
167+
std::deque<MeshFace> faces;
147168
bool smoothBounce;
148169
bool noclusters;
149170
bool inverted; // used during building. can be ditched if
@@ -198,16 +219,6 @@ inline int MeshObstacle::getTexcoordCount() const
198219
return texcoordCount;
199220
}
200221

201-
inline int MeshObstacle::getFaceCount() const
202-
{
203-
return faceCount;
204-
}
205-
206-
inline MeshFace* MeshObstacle::getFace(int face) const
207-
{
208-
return faces[face];
209-
}
210-
211222
inline const float* MeshObstacle::getPosition() const
212223
{
213224
return pos;

src/bzflag/RadarRenderer.cxx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,21 +1065,19 @@ void RadarRenderer::renderBoxPyrMesh()
10651065
for (i = 0; i < count; i++)
10661066
{
10671067
const MeshObstacle* mesh = (const MeshObstacle*) meshes[i];
1068+
const auto* cachePtr = mesh->radarCache.data();
10681069
int faces = mesh->getFaceCount();
10691070

10701071
for (int f = 0; f < faces; f++)
10711072
{
1072-
const MeshFace* face = mesh->getFace(f);
1073+
const auto& cache = cachePtr[f];
10731074
if (enhanced)
10741075
{
1075-
if (face->getPlane()[2] <= 0.0f)
1076-
continue;
1077-
const BzMaterial* bzmat = face->getMaterial();
1078-
if ((bzmat != NULL) && bzmat->getNoRadar())
1076+
if (cache.noRadar)
10791077
continue;
10801078
}
1081-
float z = face->getPosition()[2];
1082-
float bh = face->getSize()[2];
1079+
float z = cache.posZ;
1080+
float bh = cache.sizeZ;
10831081

10841082
if (BZDBCache::useMeshForRadar)
10851083
{
@@ -1089,18 +1087,24 @@ void RadarRenderer::renderBoxPyrMesh()
10891087

10901088
const float cs = colorScale(z, bh);
10911089
// draw death faces with a soupcon of red
1092-
const PhysicsDriver* phydrv = PHYDRVMGR.getDriver(face->getPhysicsDriver());
1090+
const PhysicsDriver* phydrv = PHYDRVMGR.getDriver(cache.phydrv);
10931091
if ((phydrv != NULL) && phydrv->getIsDeath())
10941092
glColor4f(0.75f * cs, 0.25f * cs, 0.25f * cs, transScale(z, bh));
10951093
else
10961094
glColor4f(0.25f * cs, 0.5f * cs, 0.5f * cs, transScale(z, bh));
10971095
// draw the face as a triangle fan
1098-
int vertexCount = face->getVertexCount();
1096+
int vertexCount = cache.vertexCount;
10991097
glBegin(GL_TRIANGLE_FAN);
1100-
for (int v = 0; v < vertexCount; v++)
1098+
for (int v = 0; v < std::min(vertexCount, 4); v++)
1099+
glVertex2f(cache.vx[v], cache.vy[v]);
1100+
if (vertexCount > 4)
11011101
{
1102-
const float* pos = face->getVertex(v);
1103-
glVertex2f(pos[0], pos[1]);
1102+
const MeshFace* face = mesh->getFace(f);
1103+
for (int v = 4; v < vertexCount; v++)
1104+
{
1105+
const float* pos = face->getVertex(v);
1106+
glVertex2f(pos[0], pos[1]);
1107+
}
11041108
}
11051109
glEnd();
11061110
}

0 commit comments

Comments
 (0)