-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.cpp
More file actions
181 lines (147 loc) · 4.43 KB
/
draw.cpp
File metadata and controls
181 lines (147 loc) · 4.43 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
#include "draw.h"
Vector2d screen(const Vector2d& coords) {
return Vector2d(
(coords.x + 1.0f) * 0.5f * screenWidth,
(1.0f - (coords.y + 1.0f) * 0.5f) * screenHeight);
}
Vector2d project(const Vector3d& coords) {
return Vector2d(
coords.x / coords.z,
coords.y / coords.z);
}
void drawPixel(const Vector2d& coords, const int size)
{
//static const uint32_t colors[] = {
// 0xFFFF0000, 0xFFFF7F00, 0xFFFFFF00, 0xFF00FF00,
// 0xFF00FFFF, 0xFF0000FF, 0xFF4B0082, 0xFF9400D3
//};
//static int colorIndex = 0;
//uint32_t color = colors[colorIndex];
//colorIndex = (colorIndex + 1) % 8; // wrap around
uint32_t color = 0xFFFFFFFF; // white
int cx = static_cast<int>(coords.x + 0.5);
int cy = static_cast<int>(coords.y + 0.5);
int halfW = size / 2;
int halfH = size / 2;
for (int i = -halfW; i < halfW; i++) {
int px = cx + i;
if (px < 0 || px >= screenWidth) continue;
for (int j = -halfH; j < halfH; j++) {
int py = cy + j;
if (py < 0 || py >= screenHeight) continue;
frameBuffer[py * screenWidth + px] = color;
}
}
}
void drawLine(const Vector2d& start, const Vector2d& end)
{
int x0 = static_cast<int>(std::round(start.x));
int y0 = static_cast<int>(std::round(start.y));
int x1 = static_cast<int>(std::round(end.x));
int y1 = static_cast<int>(std::round(end.y));
int dx = std::abs(x1 - x0);
int dy = std::abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;
while (true)
{
drawPixel(Vector2d{ float(x0), float(y0) }, 3);
if (x0 == x1 && y0 == y1) break;
int e2 = 2 * err;
if (e2 > -dy)
{
err -= dy;
x0 += sx;
}
if (e2 < dx)
{
err += dx;
y0 += sy;
}
}
}
Vector3d rotate_xy(Vector3d coords, float angle) {
const float c = std::cos(angle);
const float s = std::sin(angle);
return Vector3d(
coords.x * c - coords.y * s,
coords.x * s + coords.y * c,
coords.z
);
}
Vector3d rotate_yz(Vector3d coords, float angle) {
const float c = std::cos(angle);
const float s = std::sin(angle);
return Vector3d(
coords.x,
coords.y * c - coords.z * s,
coords.y * s + coords.z * c
);
}
Vector3d rotate_xz(Vector3d coords, float angle) {
const float c = std::cos(angle);
const float s = std::sin(angle);
return Vector3d(
coords.x * c - coords.z * s,
coords.y,
coords.x * s + coords.z * c
);
}
Vector3d computeCenter(const std::vector<Vector3d>& verts)
{
Vector3d center{ 0, 0, 0 };
for (const auto& v : verts)
center += v;
return center / verts.size();
}
std::vector<Vector3d> rotateMeshInPlace(const std::vector<Vector3d> verts, double angle, Axis axis)
{
std::vector<Vector3d> res = verts;
Vector3d center = computeCenter(verts);
for (auto& v : res)
{
// move to local space
Vector3d local = v - center;
if(axis == X_AXIS)
local = rotate_xy(local, angle);
else if(axis == Y_AXIS)
local = rotate_xz(local, angle);
else {
local = rotate_yz(local, angle);
}
// rotate
// move back to world space
v = local + center;
}
return res;
}
uint32_t LerpColor(uint32_t c1, uint32_t c2, float t)
{
auto lerp = [](uint8_t a, uint8_t b, float t) {
return (uint8_t)(a + (b - a) * t);
};
uint8_t a1 = (c1 >> 24) & 0xFF;
uint8_t r1 = (c1 >> 16) & 0xFF;
uint8_t g1 = (c1 >> 8) & 0xFF;
uint8_t b1 = c1 & 0xFF;
uint8_t a2 = (c2 >> 24) & 0xFF;
uint8_t r2 = (c2 >> 16) & 0xFF;
uint8_t g2 = (c2 >> 8) & 0xFF;
uint8_t b2 = c2 & 0xFF;
return (lerp(a1, a2, t) << 24) |
(lerp(r1, r2, t) << 16) |
(lerp(g1, g2, t) << 8) |
lerp(b1, b2, t);
}
uint32_t FadeToBlack(uint32_t color)
{
uint8_t a = (color >> 24) & 0xFF;
uint8_t r = (color >> 16) & 0xFF;
uint8_t g = (color >> 8) & 0xFF;
uint8_t b = color & 0xFF;
r = (uint8_t)(r * 0.95f);
g = (uint8_t)(g * 0.95f);
b = (uint8_t)(b * 0.95f);
return (a << 24) | (r << 16) | (g << 8) | b;
}