-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmetrics.c
More file actions
271 lines (233 loc) · 8.02 KB
/
Copy pathmetrics.c
File metadata and controls
271 lines (233 loc) · 8.02 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* CyanBun96 - a few basic performance metric displays.
* Designed to be easily portable - just copy-paste the ones you need and adjust
* the variables at the top!
* The camera must be a global for the ones that print text.
* Framebuffer must be global for the frame graph.
*
* drawFPS (line 34) - simple FPS counter, updates once per second.
* drawFrameGraph (line 55) - frame time graph
* drawFrameMinMax5pLow (line 88) - frame time stats, including shortest time to
* render, longest time to render, and 5% lows
* drawTriStatsAvg (line 119) - the average number of triangles at different
* points in the rendering process
* drawJitter (line 156) - frame jitter, less means smoother rendering with less
* stutters
* Insert calls right before the frame is copied to the screen (here line 248)
*/
#include <float.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <plush/plush.h>
#include "ex.h"
pl_Light *light;
pl_Obj *teapot;
pl_Mat *material;
pl_Cam *camera;
uint8_t framebuffer[W * H];
float zbuffer[W * H];
uint8_t palette[768];
void drawFPS() { /* CyanBun96 */
int text_xpos = camera->ClipLeft + 5;
int text_ypos = camera->ClipTop + 5;
int text_color = 50;
static int framecount = 0; /* replace with a global if you have one */
static int old_framecount = 0;
static int old_tick = 0;
static uint8_t string[16] = "... FPS";
if (exClock() - old_tick > exClockPerSecond()) {
int frames_drawn = framecount - old_framecount;
old_tick = exClock();
old_framecount = framecount;
snprintf(string, sizeof(string), "%d FPS", frames_drawn);
}
framecount++; /* remove if incremented globally */
plTextPrintf(camera, text_xpos, text_ypos, 0, text_color, string);
}
void drawFrameGraph() { /* CyanBun96 */
int i, j;
int xpos = camera->ClipLeft + 5;
int ypos = camera->ClipTop + 25;
int color = 50; /* top color, fades down */
float fade_speed = 1.5; /* color fade speed, set to 0 for solid */
float scale = 2.5; /* applied before clipping */
static int frametimes[100]; /* less = shorter graph */
static int framecount = 0; /* replace with a global if you have one */
static int time_n = sizeof(frametimes) / sizeof(int);
static int old_tick = 0;
int frametime = exClock() - old_tick;
old_tick = exClock();
frametimes[framecount % time_n] = (int)(frametime * scale) % 200;
framecount++; /* remove if incremented globally */
for (i = 0; i < time_n; i++) { /* right-to-left, bright-to-dark */
int len;
float cur_color = color;
if (fade_speed > 0 && (color * fade_speed) < frametimes[i])
len = color * fade_speed;
else
len = frametimes[i];
for (j = 0; j < len; j++) {
int xcoord = xpos + len - j;
int ycoord = W * (ypos + i);
framebuffer[xcoord + ycoord] = (int)cur_color;
cur_color -= fade_speed;
}
}
}
static int int_comp(const void *a, const void *b) { /* required for qsort */
return (*(int *)a - *(int *)b);
}
void drawFrameMinMax5pLow() { /* CyanBun96 */
int i;
int text_xpos = camera->ClipLeft + 5;
int text_ypos = camera->ClipTop + 135;
int text_color = 50;
static int framecount = 0; /* replace with a global if you have one */
static int frametimes[100]; /* less = more frequent updates */
static int time_n = sizeof(frametimes) / sizeof(int);
static int old_tick = 0;
static uint8_t string[64] = "Min: ...\nMax: ...\n5pLow: ...";
frametimes[framecount % time_n] = exClock() - old_tick;
old_tick = exClock();
if (framecount % time_n == 0) {
int fivePcLow = 0;
qsort(frametimes, time_n, sizeof(int), int_comp);
for (i = 0; i < time_n * 0.05; i++)
fivePcLow += frametimes[time_n - 1 - i];
fivePcLow /= time_n * 0.05;
snprintf(string, sizeof(string), "Min: %d\nMax: %d\n5pLow: %d",
frametimes[0], frametimes[time_n - 1], fivePcLow);
}
framecount++; /* remove if incremented globally */
plTextPrintf(camera, text_xpos, text_ypos, 0, text_color, string);
}
void drawTriStatsAvg() { /* CyanBun96 */
int i;
int text_xpos = camera->ClipLeft + 5;
int text_ypos = camera->ClipTop + 195;
int text_color = 50;
static int framecount = 0; /* replace with a global if you have one */
static int tristats[100 * 4]; /* less = more frequent updates */
/* tristats size must be a multiple of 4 */
static int stat_n = sizeof(tristats) / sizeof(int) / 4;
static uint8_t string[128]="Tris: ...\nCull: ...\nClip: ...\nTssl: ...";
tristats[framecount % stat_n] = plRender_TriStats[0];
tristats[framecount % stat_n + stat_n] = plRender_TriStats[1];
tristats[framecount % stat_n + stat_n * 2] = plRender_TriStats[2];
tristats[framecount % stat_n + stat_n * 3] = plRender_TriStats[3];
if (framecount % stat_n == 0) {
int tris = 0, cull = 0, clip = 0, tssl = 0;
for (i = 0; i < stat_n; i++) {
tris += tristats[i];
cull += tristats[i + stat_n];
clip += tristats[i + stat_n * 2];
tssl += tristats[i + stat_n * 3];
}
tris /= stat_n;
cull /= stat_n;
clip /= stat_n;
tssl /= stat_n;
snprintf(string, sizeof(string),
"Tris: %d\nCull: %d\nClip: %d\nTssl: %d",
tris, cull, clip, tssl);
}
framecount++; /* remove if incremented globally */
plTextPrintf(camera, text_xpos, text_ypos, 0, text_color, string);
}
void drawJitter() { /* CyanBun96 */
int i;
int text_xpos = camera->ClipLeft + 5;
int text_ypos = camera->ClipTop + 275;
int text_color = 50;
static int framecount = 0; /* replace with a global if you have one */
static int frametimes[100]; /* less = more frequent updates */
static int time_n = sizeof(frametimes) / sizeof(int);
static int old_tick = 0;
static uint8_t string[64] = "Jitter: ...";
frametimes[framecount % time_n] = exClock() - old_tick;
old_tick = exClock();
if (framecount % time_n == 0) {
float stdev, avg, var;
int sum = 0;
for (i = 0; i < time_n; i++)
sum += frametimes[i];
avg = (float)sum / time_n;
var = 0.0f;
for (i = 0; i < time_n; i++) {
float d = frametimes[i] - avg;
var += d * d;
}
var /= time_n;
stdev = plSqrt(var);
snprintf((char*)string, sizeof(string), "Jitter: %.2f", stdev);
}
framecount++; /* remove if incremented globally */
plTextPrintf(camera, text_xpos, text_ypos, 0, text_color, string);
}
int main(int argc, char **argv)
{
/* setup graphics mode */
exSetGraphics();
/* create material */
/* these ambient/diffuse/specular values are taken from Haiku's GLTeapot */
material = plMatCreate();
material->NumGradients = 50;
material->ShadeType = PL_SHADE_GOURAUD;
material->Ambient[0] = 0.1745 * 255;
material->Ambient[1] = 0.03175 * 255;
material->Ambient[2] = 0.03175 * 255;
material->Diffuse[0] = 0.61424 * 255;
material->Diffuse[1] = 0.10136 * 255;
material->Diffuse[2] = 0.10136 * 255;
material->Specular[0] = 0.727811 * 255;
material->Specular[1] = 0.626959 * 255;
material->Specular[2] = 0.626959 * 255;
plMatInit(material);
/* create palette */
plMatMakeOptPal(palette, 1, 255, &material, 1);
palette[0] = palette[1] = palette[2] = 0;
plMatMapToPal(material, palette, 0, 255);
exSetPalette(palette);
/* load fork model */
teapot = plObjCreate(NULL);
teapot->Model = plReadWavefrontMdl("teapot.obj", material);
plMdlScale(teapot->Model, 32);
plMdlTranslate(teapot->Model, 0, -32, 0);
/* create camera */
camera = plCamCreate(W, H, W * 3.0 / (H * 4.0), 90.0, framebuffer, zbuffer);
camera->Z = -300;
/* create light */
light = plLightSet(plLightCreate(), PL_LIGHT_VECTOR, 0.0, 0.0, 0.0, 1.0, 1.0);
/* main loop */
while (!exGetKey())
{
/* rotate model */
teapot->Xa += 1.0;
teapot->Ya += 1.0;
teapot->Za += 1.0;
/* clear back buffer */
memset(zbuffer, 0, sizeof(zbuffer));
memset(framebuffer, 0, sizeof(framebuffer));
/* render frame */
plRenderBegin(camera);
plRenderLight(light);
plRenderObj(teapot);
plRenderEnd();
drawFPS();
drawFrameGraph();
drawFrameMinMax5pLow();
drawTriStatsAvg();
drawJitter();
/* wait for vsync, then copy to screen */
exWaitVSync();
memcpy(exGraphMem, framebuffer, sizeof(framebuffer));
}
/* clean up */
plCamDelete(camera);
plLightDelete(light);
plObjDelete(teapot);
plMatDelete(material);
/* shut down video */
exSetText();
return 0;
}