-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
314 lines (267 loc) · 9.74 KB
/
Copy pathmain.c
File metadata and controls
314 lines (267 loc) · 9.74 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "raylib.h"
#include "raymath.h"
#define TRAIL_SIZE 300
#define NUM_ORBITERS 300
#define READ_END 0
#define WRITE_END 1
// ---- Output video quality settings ----
// Bump these for even higher quality (at the cost of render/encode time).
#ifndef OUT_WIDTH
#define OUT_WIDTH 1920
#endif
#ifndef OUT_HEIGHT
#define OUT_HEIGHT 1080
#endif
#ifndef SS
// Supersampling factor: the scene is rendered at SS times the output
// resolution and then downscaled before it's written to ffmpeg. This gives
// free anti-aliasing (smooth sphere/line edges) without relying on MSAA
// support on an offscreen render texture.
#define SS 2
#endif
#ifndef FPS
#define FPS 60
#endif
#ifndef DURATION_SECONDS
#define DURATION_SECONDS 30
#endif
#define RENDER_WIDTH (OUT_WIDTH * SS)
#define RENDER_HEIGHT (OUT_HEIGHT * SS)
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
// Matches raylib's internal CAMERA_ORBITAL_SPEED (rcamera.h), so the orbit
// looks identical to before.
#define ORBIT_SPEED 0.5f
typedef struct {
float azimuth;
float elevation;
float speedAz;
float speedEl;
float accelAz;
float accelEl;
float radius;
Vector3 trail[TRAIL_SIZE];
int trailIndex;
} Orbiter;
typedef struct {
Vector3 position;
Orbiter orbiters[NUM_ORBITERS];
Color startColor;
Color endColor;
float coreRadius;
float orbitInnerRadius;
float orbitMiddleRadius;
float orbitOuterRadius;
} RasenganEffect;
static float r2(void) { return (float)drand48(); }
static Color ColorLerp(Color c1, Color c2, float t) {
return (Color){(unsigned char)(c1.r + (c2.r - c1.r) * t),
(unsigned char)(c1.g + (c2.g - c1.g) * t),
(unsigned char)(c1.b + (c2.b - c1.b) * t),
(unsigned char)(c1.a + (c2.a - c1.a) * t)};
}
RasenganEffect InitRasengan(Vector3 position, Color startColor, Color endColor,
float coreRadius, float innerRadius,
float middleRadius, float outerRadius) {
RasenganEffect effect = {0};
effect.position = position;
effect.startColor = startColor;
effect.endColor = endColor;
effect.coreRadius = coreRadius;
effect.orbitInnerRadius = innerRadius;
effect.orbitMiddleRadius = middleRadius;
effect.orbitOuterRadius = outerRadius;
for (int i = 0; i < NUM_ORBITERS; i++) {
effect.orbiters[i].azimuth = r2() * 2.0f * PI;
effect.orbiters[i].elevation = r2() * PI;
effect.orbiters[i].speedAz = 0.01f + r2() * 0.02f;
effect.orbiters[i].speedEl = -0.015f + r2() * 0.03f;
effect.orbiters[i].accelAz = 0.0f;
effect.orbiters[i].accelEl = 0.0f;
if (i < NUM_ORBITERS * 0.4f) {
effect.orbiters[i].radius = innerRadius;
} else if (i < NUM_ORBITERS * 0.6f) {
effect.orbiters[i].radius = middleRadius;
} else {
effect.orbiters[i].radius = outerRadius;
}
effect.orbiters[i].trailIndex = 0;
for (int j = 0; j < TRAIL_SIZE; j++) {
effect.orbiters[i].trail[j] = position;
}
}
return effect;
}
void UpdateRasenganOrbiters(RasenganEffect *effect) {
for (int i = 0; i < NUM_ORBITERS; i++) {
Orbiter *orb = &effect->orbiters[i];
orb->accelAz = -0.0005f + r2() * 0.001f;
orb->accelEl = -0.0005f + r2() * 0.001f;
orb->speedAz += orb->accelAz;
orb->speedEl += orb->accelEl;
orb->speedAz = fmaxf(-0.05f, fminf(0.05f, orb->speedAz));
orb->speedEl = fmaxf(-0.05f, fminf(0.05f, orb->speedEl));
orb->azimuth += orb->speedAz;
orb->elevation += orb->speedEl;
orb->azimuth = fmodf(orb->azimuth + 2.0f * PI, 2.0f * PI);
if (orb->elevation < 0.0f) {
orb->elevation = -orb->elevation;
orb->speedEl = -orb->speedEl;
} else if (orb->elevation > PI) {
orb->elevation = 2.0f * PI - orb->elevation;
orb->speedEl = -orb->speedEl;
}
float r = orb->radius;
float theta = orb->azimuth;
float phi = orb->elevation;
Vector3 relativePos = {r * sinf(phi) * cosf(theta), r * cosf(phi),
r * sinf(phi) * sinf(theta)};
Vector3 pos = {relativePos.x + effect->position.x,
relativePos.y + effect->position.y,
relativePos.z + effect->position.z};
orb->trail[orb->trailIndex] = pos;
orb->trailIndex = (orb->trailIndex + 1) % TRAIL_SIZE;
}
}
void DrawRasengan(const RasenganEffect *effect) {
// Higher ring/slice count than the raylib default (16x16) so the core
// sphere stays smooth at 1080p+ instead of looking faceted.
DrawSphereEx(effect->position, effect->coreRadius, 32, 32,
(Color){255, 255, 255, 180});
for (int i = 0; i < NUM_ORBITERS; i++) {
const Orbiter *orb = &effect->orbiters[i];
int count =
(orb->trail[TRAIL_SIZE - 1].x == 0 &&
orb->trail[TRAIL_SIZE - 1].y == 0 && orb->trail[TRAIL_SIZE - 1].z == 0)
? orb->trailIndex
: TRAIL_SIZE;
if (count > 1) {
int startIndex = orb->trailIndex;
Vector3 prevPoint = orb->trail[startIndex];
for (int j = 1; j < count; j++) {
int currIndex = (startIndex + j) % TRAIL_SIZE;
float t = (float)j / (count - 1);
Color segmentColor = ColorLerp(effect->startColor, effect->endColor, t);
if (prevPoint.x == effect->position.x &&
prevPoint.y == effect->position.y &&
prevPoint.z == effect->position.z) {
prevPoint = orb->trail[currIndex];
}
DrawLine3D(prevPoint, orb->trail[currIndex], segmentColor);
prevPoint = orb->trail[currIndex];
}
}
}
}
// Deterministic orbital camera step, one video frame at a time. This mirrors
// raylib's built-in UpdateCamera(camera, CAMERA_ORBITAL), which rotates by
// ORBIT_SPEED * GetFrameTime() each call. That's fine for real-time playback,
// but for offline export we render frames as fast as the machine allows
// (no FPS cap), so wall-clock time no longer matches video time. Stepping by
// a fixed 1/FPS "frame time" instead keeps the recorded motion identical to
// a real-time 60fps run, regardless of how fast rendering actually goes.
static void StepOrbitCamera(Camera3D *camera, float dt) {
Vector3 offset = Vector3Subtract(camera->position, camera->target);
offset = Vector3RotateByAxisAngle(offset, camera->up, -ORBIT_SPEED * dt);
camera->position = Vector3Add(camera->target, offset);
}
int main(void) {
int pipefd[2];
if (pipe(pipefd) < 0) {
printf("pipe failed\n");
return 1;
}
pid_t child = fork();
if (child < 0) {
printf("fork failed\n");
return 1;
}
if (child == 0) {
if (dup2(pipefd[READ_END], STDIN_FILENO) < 0) {
printf("dup2 failed\n");
return 1;
}
close(pipefd[WRITE_END]);
close(pipefd[READ_END]);
int ret = execlp(
"ffmpeg", "ffmpeg", "-loglevel", "error", "-stats", "-y", "-f",
"rawvideo", "-pix_fmt", "rgba", "-s",
STR(OUT_WIDTH) "x" STR(OUT_HEIGHT), "-r", STR(FPS), "-an", "-i", "-",
"-c:v", "libx264", "-preset", "slow", "-crf", "14", "-pix_fmt",
"yuv420p", "-movflags", "+faststart", "output.mp4", NULL);
if (ret < 0) {
printf("exec failed\n");
return 1;
}
}
close(pipefd[READ_END]);
srand48(time(NULL));
InitWindow(OUT_WIDTH, OUT_HEIGHT, "Rasengan - rendering output.mp4");
Camera3D camera = {0};
camera.position = (Vector3){4.0f, 3.0f, 4.0f};
camera.target = (Vector3){0.0f, 1.0f, 0.0f};
camera.up = (Vector3){0.0f, 1.0f, 0.0f};
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
RasenganEffect rasengan1 =
InitRasengan((Vector3){0.0f, 1.0f, 0.0f}, // Position
(Color){0, 180, 255, 255}, // Start color (blue)
(Color){200, 255, 255, 255}, // End color (white)
0.3f, // Core radius
0.3f, 0.9f, 1.5f // Layer radii
);
// No FPS cap: we're not showing this live, we're exporting frame-by-frame
// as fast as the GPU/CPU can render them. Camera motion stays correct
// because StepOrbitCamera advances by a fixed 1/FPS step, not real time.
RenderTexture2D screen = LoadRenderTexture(RENDER_WIDTH, RENDER_HEIGHT);
SetTextureFilter(screen.texture, TEXTURE_FILTER_BILINEAR);
const int totalFrames = FPS * DURATION_SECONDS;
for (int i = 0; i < totalFrames && !WindowShouldClose(); i++) {
UpdateRasenganOrbiters(&rasengan1);
StepOrbitCamera(&camera, 1.0f / FPS);
BeginTextureMode(screen);
ClearBackground(BLACK);
BeginMode3D(camera);
DrawRasengan(&rasengan1);
DrawGrid(10, 1.0f);
EndMode3D();
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK);
// Render texture origin is flipped vertically relative to the screen,
// and it's SS times larger than the window, so scale it down on the way
// to the window too (matches what gets written to the video).
DrawTexturePro(
screen.texture,
(Rectangle){0, 0, (float)screen.texture.width,
-(float)screen.texture.height},
(Rectangle){0, 0, (float)OUT_WIDTH, (float)OUT_HEIGHT}, (Vector2){0, 0},
0.0f, WHITE);
DrawText(TextFormat("Rendering frame %d / %d", i + 1, totalFrames), 10, 10,
20, RAYWHITE);
EndDrawing();
Image image = LoadImageFromTexture(screen.texture);
if (SS != 1) {
ImageResize(&image, OUT_WIDTH, OUT_HEIGHT); // supersampled downscale = AA
}
ImageFlipVertical(&image);
write(pipefd[WRITE_END], image.data,
(size_t)OUT_WIDTH * OUT_HEIGHT * sizeof(uint32_t));
UnloadImage(image);
}
UnloadRenderTexture(screen);
CloseWindow();
close(pipefd[WRITE_END]);
wait(NULL);
printf("Done rendering output.mp4 (%dx%d @ %d fps, %ds)\n", OUT_WIDTH,
OUT_HEIGHT, FPS, DURATION_SECONDS);
return 0;
}