-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfps.cpp
More file actions
120 lines (103 loc) · 3.96 KB
/
fps.cpp
File metadata and controls
120 lines (103 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* An example mplot::Visual scene, containing a HexGrid.
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <chrono>
#include <sstream>
#include <sm/vec>
#include <sm/hexgrid>
#include <mplot/Visual.h>
#include <mplot/VisualDataModel.h>
#include <mplot/VisualTextModel.h>
#include <mplot/HexGridVisual.h>
int main()
{
mplot::Visual v(1600, 1000, "mplot::Visual");
v.fov = 15.0f;
v.zFar = 200.0f;
v.lightingEffects();
mplot::VisualTextModel<>* fps_tm;
v.addLabel ("0 FPS", {0.13f, -0.23f, 0.0f}, fps_tm); // With fps_tm can update the VisualTextModel with fps_tm->setupText("new text")
// Create a hexgrid to show in the scene
constexpr float hex_to_hex = 0.02f;
sm::hexgrid hg(hex_to_hex, 15.0f, 0.0f);
hg.setEllipticalBoundary (4.0f, 4.0f);
std::cout << "Number of hexes in grid:" << hg.num() << std::endl;
std::stringstream sss;
sss << "Surface evaluated at " << hg.num() << " coordinates";
v.addLabel (sss.str(), {0.0f, 0.0f, 0.0f});
// Make some dummy data (a radially symmetric Bessel fn) to make an interesting surface
std::vector<float> data(hg.num(), 0.0f);
std::vector<float> r(hg.num(), 0.0f);
float k = 1.0f;
for (unsigned int hi = 0; hi < hg.num(); ++hi) {
// x/y: hg.d_x[hi] hg.d_y[hi]
r[hi] = std::sqrt (hg.d_x[hi] * hg.d_x[hi] + hg.d_y[hi] * hg.d_y[hi]);
data[hi] = std::sin (k * r[hi]) / k * r[hi];
}
// Add a HexGridVisual to display the HexGrid within the mplot::Visual scene
sm::vec<float, 3> offset = { 0.0f, -0.05f, 0.0f };
auto hgv = std::make_unique<mplot::HexGridVisual<float>>(&hg, offset);
v.bindmodel (hgv);
hgv->setScalarData (&data);
hgv->hexVisMode = mplot::HexVisMode::Triangles;
hgv->finalize();
auto hgvp = v.addVisualModel (hgv);
using namespace std::chrono;
using sc = std::chrono::steady_clock;
auto t00 = sc::now();
auto t0 = sc::now();
auto t1 = sc::now();
auto t2 = sc::now();
auto data_dur = sc::duration{0};
auto update_dur = sc::duration{0};
auto all_dur = sc::duration{0};
double data_fps = 0.0;
double update_fps = 0.0;
double rest_fps = 0.0;
double all_fps = 0.0;
unsigned int fcount = 0u;
while (v.readyToFinish() == false) {
all_dur += sc::now() - t00;
t00 = sc::now();
v.poll();
if (k > 8.0f) { k = 1.0f; }
t0 = sc::now();
#pragma omp parallel for shared(r,k,data)
for (unsigned int hi = 0; hi < hg.num(); ++hi) {
data[hi] = std::sin (k * r[hi]) / k * r[hi];
}
t1 = sc::now();
data_dur += (t1 - t0);
if (v.validVisualModel (hgvp) != nullptr) { // Test hgvp is still valid
hgvp->updateData (&data);
}
t2 = sc::now();
update_dur += (t2 - t1);
k += 0.02f;
if (fcount == 500) {
// Update FPS text
double data_tau = duration_cast<milliseconds>(data_dur).count();
double update_tau = duration_cast<milliseconds>(update_dur).count();
double all_tau = duration_cast<milliseconds>(all_dur).count();
double rest_tau = all_tau - data_tau - update_tau;
data_fps = std::max (data_fps, std::round((((double)fcount/data_tau))*1000.0));
update_fps = std::max (update_fps, std::round((((double)fcount/update_tau))*1000.0));
all_fps = std::max (all_fps, std::round((((double)fcount/all_tau))*1000.0));
rest_fps = std::max (rest_fps, std::round((((double)fcount/rest_tau))*1000.0));
std::stringstream ss;
ss << "FPS: " << data_fps << " [dat] " << update_fps << " [upd] " << rest_fps << " [rest] " << all_fps << " [all]\n";
fps_tm->setupText (ss.str());
data_dur = sc::duration{0};
update_dur = sc::duration{0};
all_dur = sc::duration{0};
fcount = 0;
v.waitevents (0.00001);
}
v.render();
fcount++;
}
return 0;
}