-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.h
177 lines (167 loc) · 3.93 KB
/
renderer.h
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef RENDERER_H_INCLUDED
#define RENDERER_H_INCLUDED
#include "mesh.h"
#include <chrono>
using namespace std;
inline double realTimer()
{
auto timePointInNanoseconds = chrono::time_point_cast<chrono::nanoseconds>(chrono::steady_clock::now());
return 1e-9 * timePointInNanoseconds.time_since_epoch().count();
}
struct Renderer
{
Renderer(const Renderer & rt) = delete;
const Renderer & operator =(const Renderer & rt) = delete;
virtual ~Renderer()
{
}
virtual void render(const Mesh & m) = 0;
void render(shared_ptr<Mesh> m)
{
assert(m != nullptr);
render(*m);
}
void render(TransformedMesh m)
{
render((Mesh)m);
}
void render(ColorizedTransformedMesh m)
{
render((Mesh)m);
}
void render(ColorizedMesh m)
{
render((Mesh)m);
}
void render(TransformedMeshRef m)
{
render((Mesh)m);
}
void render(ColorizedTransformedMeshRef m)
{
render((Mesh)m);
}
void render(ColorizedMeshRef m)
{
render((Mesh)m);
}
void render(TransformedMeshRRef &&m)
{
render(Mesh(std::move(m)));
}
void render(ColorizedTransformedMeshRRef &&m)
{
render(Mesh(std::move(m)));
}
void render(ColorizedMeshRRef &&m)
{
render(Mesh(std::move(m)));
}
virtual void calcScales() = 0;
protected:
virtual void clearInternal(ColorF bg) = 0;
float scaleXValue = 1;
float scaleYValue = 1;
double lastFlipTime = -1;
float fps = 60.0;
size_t frameCount = 0;
virtual void onSetFPS()
{
}
void copyScales(const Renderer &otherRenderer)
{
scaleXValue = otherRenderer.scaleXValue;
scaleYValue = otherRenderer.scaleYValue;
}
void calcFPS()
{
if(lastFlipTime == -1)
{
lastFlipTime = realTimer();
frameCount = 0;
return;
}
frameCount++;
double time = realTimer();
if(time - lastFlipTime > 0.01)
{
fps = frameCount / (time - lastFlipTime);
lastFlipTime = time;
frameCount = 0;
onSetFPS();
}
}
void calcScales(size_t w, size_t h, float aspectRatio = -1)
{
if(aspectRatio > 0)
{
if(aspectRatio > 1)
{
scaleXValue = aspectRatio;
scaleYValue = 1;
}
else
{
scaleXValue = 1;
scaleYValue = 1 / aspectRatio;
}
}
else if(w > h)
{
scaleXValue = (float)w / h;
scaleYValue = 1;
}
else
{
scaleXValue = 1;
scaleYValue = (float)h / w;
}
}
public:
Renderer()
: scaleXValue(1), scaleYValue(1)
{
}
void clear(ColorF backgroundColor = RGBF(0, 0, 0))
{
calcScales();
clearInternal(backgroundColor);
}
float scaleX() const
{
return scaleXValue;
}
float scaleY() const
{
return scaleYValue;
}
virtual void enableWriteDepth(bool v) = 0;
virtual shared_ptr<Texture> preloadTexture(shared_ptr<Texture> texture)
{
return texture;
}
};
struct WindowRenderer : public Renderer
{
virtual void flip() = 0;
virtual double timer()
{
return realTimer();
}
};
struct ImageRenderer : public Renderer
{
virtual shared_ptr<Texture> finish() = 0;
virtual void resize(size_t newW, size_t newH, float newAspectRatio = -1) = 0;
};
void setDefaultRendererSize(int w, int h, float aspectRatio = -1);
int getDefaultRendererWidth();
int getDefaultRendererHeight();
float getDefaultRendererAspectRatio();
shared_ptr<WindowRenderer> getWindowRenderer();
shared_ptr<ImageRenderer> makeImageRenderer(size_t w, size_t h, float aspectRatio = -1);
inline double timer()
{
return getWindowRenderer()->timer();
}
#endif // RENDERER_H_INCLUDED