forked from tshino/softcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.cpp
166 lines (149 loc) · 5.07 KB
/
sender.cpp
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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <softcam/softcam.h>
const int WIDTH = 320;
const int HEIGHT = 240;
/// A Rendering Example of Bouncing Balls
class BouncingBalls
{
public:
static constexpr int NUM_BALLS = 2;
static constexpr float RADIUS = 30.0f;
static constexpr float GRAVITY = 400.0f;
static constexpr float STIFFNESS = 600.0f;
struct Ball
{
float x = RADIUS, y = RADIUS * 2.0f;
float vx = 100.0f, vy = 0.0f;
float ax = 0.0f, ay = 0.0f;
float rx = RADIUS, ry = RADIUS;
void move(float delta)
{
vx += delta * ax;
vy += delta * ay;
x += delta * vx;
y += delta * vy;
float cx = std::min(std::max(x, RADIUS), (float)WIDTH - RADIUS);
float cy = std::min(y, (float)HEIGHT - RADIUS);
ax = (cx - x) * STIFFNESS;
ay = (cy - y) * STIFFNESS + GRAVITY;
rx = std::max(RADIUS - std::abs(cx - x), RADIUS * 0.1f);
ry = std::max(RADIUS - std::abs(cy - y), RADIUS * 0.1f);
}
void collide(Ball& other)
{
float dx = x - other.x;
float dy = y - other.y;
float d = std::sqrt(dx * dx + dy * dy);
if (d <= RADIUS * 2.0f)
{
float p = RADIUS * 2.0f - d;
float nx = dx / std::max(RADIUS, d);
float ny = dy / std::max(RADIUS, d);
ax += p * STIFFNESS * nx;
ay += p * STIFFNESS * ny;
other.ax -= p * STIFFNESS * nx;
other.ay -= p * STIFFNESS * ny;
}
}
bool hit(int sx, int sy) const
{
float dx = (float(sx) + 0.5f - x) / rx;
float dy = (float(sy) + 0.5f - y) / ry;
return 1.0f >= dx * dx + dy * dy;
}
};
Ball balls[NUM_BALLS];
BouncingBalls()
{
for (int i = 0; i < NUM_BALLS; i++)
{
balls[i].x = (float)(WIDTH * (i + 1) / (2 * NUM_BALLS));
balls[i].y = (float)(HEIGHT * i / (2 * NUM_BALLS)) + RADIUS * 2.0f;
balls[i].vx = RADIUS * 2.0f * (float)(i + 1);
}
}
void move(float delta)
{
for (int i = 0; i < NUM_BALLS; i++)
{
balls[i].move(delta);
}
for (int i = 0; i < NUM_BALLS; i++)
{
for (int j = i + 1; j < NUM_BALLS; j++)
{
balls[i].collide(balls[j]);
}
}
}
void draw(unsigned char *image)
{
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
if (std::any_of(
balls, balls + NUM_BALLS,
[x,y](Ball& ball) { return ball.hit(x, y); }))
{
int red = x * 256 / WIDTH;
int green = 255;
int blue = y * 256 / HEIGHT;
image[0] = (unsigned char)blue;
image[1] = (unsigned char)green;
image[2] = (unsigned char)red;
}
else
{
image[0] = 0;
image[1] = 0;
image[2] = 0;
}
image += 3;
}
}
}
};
int main()
{
// First, create a virtual camera instance with scCreateCamera().
// A virtual camera is a source of a video image stream.
// The dimension width and height can be any positive number
// that is a multiple of 4.
// The third argument framerate is used to make sending frames at regular intervals.
// This framerate argument can be omitted, and the default framerate is 60.
// If you want to send every frame immediately without the frame rate regulator,
// specify 0 to the framerate argument, then it will be a variable frame rate.
scCamera cam = scCreateCamera(WIDTH, HEIGHT, 60);
if (!cam)
{
std::printf("failed to create camera\n");
return 1;
}
std::printf("Ok, Softcam is now active.\n");
// Here, we wait for an application to connect to this camera.
// You can comment out this line to start sending frames immediately
// no matter there is a receiver or not.
scWaitForConnection(cam);
// Our canvas is a simple array of RGB pixels.
// Note that the color component order is BGR, not RGB.
// This is due to the convention of DirectShow.
std::vector<unsigned char> image(WIDTH * HEIGHT * 3);
// This is an example class for drawing something to the canvas.
BouncingBalls balls;
for (int i = 0; i < 100000; i++)
{
// Draw bouncing balls.
balls.move(1.0f / 60.0f);
balls.draw(image.data());
// Send the image as a newly captured frame of the camera.
scSendFrame(cam, image.data());
}
// Delete the camera instance.
// The receiver application will no longer receive new frames from this camera.
scDeleteCamera(cam);
std::printf("Softcam has been shut down.\n");
}