Skip to content

Commit 148f432

Browse files
feat: add texture support to Shape, Removed redundant ray-casting/polygon rendering logic
1 parent 728409c commit 148f432

9 files changed

Lines changed: 44 additions & 140 deletions

File tree

include/Shapes/Circle.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@ class Circle : public Shape {
3434
void drawAntiAliasedPoint(Pixels &points, int cx, int cy, int x, int y,
3535
float intensity);
3636
void fillCircle(Pixels &points, int cx, int cy, int r);
37-
void fillCircleAntiAliased(Pixels &points, int cx, int cy, int r);
3837
};

include/Shapes/Polygon.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,4 @@ class Polygon : public Shape {
2929
std::vector<LineSegment *> getSegments();
3030
void getInsidePoints(Pixels &points,
3131
const std::vector<std::pair<int, int>> &vertices);
32-
void getInsidePointsWithTexture(
33-
Pixels &points, const std::vector<std::pair<int, int>> &vertices);
3432
};

include/Shapes/RegularPolygon.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,4 @@ class RegularPolygon : public Shape {
5050
std::vector<LineSegment *> getSegments();
5151
void getInsidePoints(Pixels &points,
5252
const std::vector<std::pair<int, int>> &vertices);
53-
54-
void getInsidePointsWithTexture(
55-
Pixels &points, const std::vector<std::pair<int, int>> &vertices);
5653
};

src/Shapes/Circle.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,45 +59,6 @@ void Circle::fillCircle(Pixels &pixels, int cx, int cy, int r) {
5959
}
6060
}
6161

62-
void Circle::fillCircleAntiAliased(Pixels &points, int cx, int cy, int r) {
63-
int x = 0;
64-
int y = r;
65-
int d = 3 - 2 * r;
66-
67-
auto drawline = [&](int x1, int y1, int x2, int y2) {
68-
for (int i = x1; i <= x2; i++) {
69-
Color sampledColor = sampleTexture(i, y1);
70-
float alpha = sampledColor.a;
71-
if (alpha > 0.01f) {
72-
points.push_back(Pixel(i, y1,
73-
Color(sampledColor.r, sampledColor.g,
74-
sampledColor.b, alpha)));
75-
}
76-
}
77-
};
78-
79-
while (y >= x) {
80-
drawline(cx - x, cy + y, cx + x, cy + y);
81-
if (y != 0) {
82-
drawline(cx - x, cy - y, cx + x, cy - y);
83-
}
84-
if (x != y) {
85-
drawline(cx - y, cy + x, cx + y, cy + x);
86-
if (x != 0) {
87-
drawline(cx - y, cy - x, cx + y, cy - x);
88-
}
89-
}
90-
91-
x++;
92-
if (d > 0) {
93-
y--;
94-
d = d + 4 * (x - y) + 10;
95-
} else {
96-
d = d + 4 * x + 6;
97-
}
98-
}
99-
}
100-
10162
void Circle::drawAliased(Pixels &pixels) {
10263
auto center = getTransformedPosition(0, 0);
10364
int r = radius;
@@ -160,6 +121,6 @@ void Circle::drawAntiAliased(Pixels &pixels) {
160121
}
161122

162123
if (fill) {
163-
fillCircleAntiAliased(pixels, center.first, center.second, r);
124+
fillCircle(pixels, center.first, center.second, r);
164125
}
165126
}

src/Shapes/Polygon.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -89,41 +89,3 @@ void Polygon::drawAntiAliased(Pixels &pixels) {
8989
getInsidePoints(pixels, transformedVertices);
9090
}
9191
}
92-
void Polygon::getInsidePointsWithTexture(
93-
Pixels &points, const std::vector<std::pair<int, int>> &vertices) {
94-
if (vertices.empty())
95-
return;
96-
97-
int minX = vertices[0].first;
98-
int maxX = vertices[0].first;
99-
int minY = vertices[0].second;
100-
int maxY = vertices[0].second;
101-
102-
for (const auto &v : vertices) {
103-
minX = std::min(minX, v.first);
104-
maxX = std::max(maxX, v.first);
105-
minY = std::min(minY, v.second);
106-
maxY = std::max(maxY, v.second);
107-
}
108-
109-
for (int x = static_cast<int>(minX); x <= static_cast<int>(maxX); x++) {
110-
for (int y = static_cast<int>(minY); y <= static_cast<int>(maxY); y++) {
111-
bool inside = false;
112-
size_t n = vertices.size();
113-
for (size_t i = 0, j = n - 1; i < n; j = i++) {
114-
int xi = vertices[i].first, yi = vertices[i].second;
115-
int xj = vertices[j].first, yj = vertices[j].second;
116-
117-
bool intersect = ((yi > y) != (yj > y)) &&
118-
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
119-
if (intersect)
120-
inside = !inside;
121-
}
122-
123-
if (inside) {
124-
Color sampledColor = sampleTexture(x, y);
125-
points.push_back(Pixel(x, y, sampledColor));
126-
}
127-
}
128-
}
129-
}

src/Shapes/Rectangle.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@ void Rectangle::getInsidePoints(
3232

3333
int minX = vertices[0].first, maxX = vertices[0].first;
3434
int minY = vertices[0].second, maxY = vertices[0].second;
35-
for (int i = 1; i < 4; i++) {
36-
minX = std::min(minX, vertices[i].first);
37-
maxX = std::max(maxX, vertices[i].first);
38-
minY = std::min(minY, vertices[i].second);
39-
maxY = std::max(maxY, vertices[i].second);
35+
36+
// 1. Setup vertices and pre-compute edge vectors for the cross-product
37+
int vx[4], vy[4], ex[4], ey[4];
38+
for (int i = 0; i < 4; i++) {
39+
vx[i] = vertices[i].first;
40+
vy[i] = vertices[i].second;
41+
minX = std::min(minX, vx[i]);
42+
maxX = std::max(maxX, vx[i]);
43+
minY = std::min(minY, vy[i]);
44+
maxY = std::max(maxY, vy[i]);
45+
}
46+
47+
for (int i = 0; i < 4; i++) {
48+
ex[i] = vertices[(i + 1) % 4].first - vx[i];
49+
ey[i] = vertices[(i + 1) % 4].second - vy[i];
4050
}
4151

4252
float u0, v0, uX, vX, uY, vY;
@@ -49,22 +59,31 @@ void Rectangle::getInsidePoints(
4959
float duy = uY - u0;
5060
float dvy = vY - v0;
5161

52-
int v0x = vertices[0].first, v0y = vertices[0].second;
53-
int dx1 = vertices[1].first - v0x, dy1 = vertices[1].second - v0y;
54-
int dx2 = vertices[3].first - v0x, dy2 = vertices[3].second - v0y;
55-
int lenSq1 = dx1 * dx1 + dy1 * dy1;
56-
int lenSq2 = dx2 * dx2 + dy2 * dy2;
57-
5862
for (int y = minY; y <= maxY; ++y) {
5963
float rowU = u0 + (y - minY) * duy;
6064
float rowV = v0 + (y - minY) * dvy;
6165

6266
for (int x = minX; x <= maxX; ++x) {
63-
int relX = x - v0x, relY = y - v0y;
64-
int dot1 = relX * dx1 + relY * dy1;
65-
int dot2 = relX * dx2 + relY * dy2;
6667

67-
if (dot1 >= 0 && dot1 <= lenSq1 && dot2 >= 0 && dot2 <= lenSq2) {
68+
bool inside = true;
69+
bool hasPos = false;
70+
bool hasNeg = false;
71+
72+
for (int i = 0; i < 4; ++i) {
73+
int cross = (x - vx[i]) * ey[i] - (y - vy[i]) * ex[i];
74+
75+
if (cross > 0)
76+
hasPos = true;
77+
else if (cross < 0)
78+
hasNeg = true;
79+
80+
if (hasPos && hasNeg) {
81+
inside = false;
82+
break;
83+
}
84+
}
85+
86+
if (inside) {
6887
Color c = texture ? texture->sample((int)(rowU + 0.5f),
6988
(int)(rowV + 0.5f))
7089
: color;

src/Shapes/RegularPolygon.cpp

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void RegularPolygon::getInsidePoints(
7272
}
7373
}
7474
}
75+
7576
Collider *RegularPolygon::defaultCollider() {
7677
int effectiveRadius =
7778
useSideLength ? calculateRadiusFromSideLength(sideLength) : radius;
@@ -133,42 +134,3 @@ void RegularPolygon::drawAntiAliased(Pixels &pixels) {
133134
getInsidePoints(pixels, vertices);
134135
}
135136
}
136-
137-
void RegularPolygon::getInsidePointsWithTexture(
138-
Pixels &points, const std::vector<std::pair<int, int>> &vertices) {
139-
if (vertices.empty())
140-
return;
141-
142-
int minX = vertices[0].first;
143-
int maxX = vertices[0].first;
144-
int minY = vertices[0].second;
145-
int maxY = vertices[0].second;
146-
147-
for (const auto &v : vertices) {
148-
minX = std::min(minX, v.first);
149-
maxX = std::max(maxX, v.first);
150-
minY = std::min(minY, v.second);
151-
maxY = std::max(maxY, v.second);
152-
}
153-
154-
for (int x = static_cast<int>(minX); x <= static_cast<int>(maxX); x++) {
155-
for (int y = static_cast<int>(minY); y <= static_cast<int>(maxY); y++) {
156-
bool inside = false;
157-
size_t n = vertices.size();
158-
for (size_t i = 0, j = n - 1; i < n; j = i++) {
159-
int xi = vertices[i].first, yi = vertices[i].second;
160-
int xj = vertices[j].first, yj = vertices[j].second;
161-
162-
bool intersect = ((yi > y) != (yj > y)) &&
163-
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
164-
if (intersect)
165-
inside = !inside;
166-
}
167-
168-
if (inside) {
169-
Color sampledColor = sampleTexture(x, y);
170-
points.push_back(Pixel(x, y, sampledColor));
171-
}
172-
}
173-
}
174-
}

src/Shapes/Shape.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ void Shape::getInsidePoints(Pixels &points,
462462
}
463463

464464
if (inside) {
465-
points.push_back(Pixel(x, y, color));
465+
Color c = texture ? sampleTexture(x, y) : color;
466+
points.push_back(Pixel(x, y, c));
466467
}
467468
}
468469
}

standalone/sdkconfig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22
# Automatically generated file. DO NOT EDIT.
3-
# Espressif IoT Development Framework (ESP-IDF) 5.5.2 Project Configuration
3+
# Espressif IoT Development Framework (ESP-IDF) 5.5.3 Project Configuration
44
#
55
CONFIG_SOC_ADC_SUPPORTED=y
66
CONFIG_SOC_UART_SUPPORTED=y
@@ -370,6 +370,7 @@ CONFIG_SOC_WIFI_HW_TSF=y
370370
CONFIG_SOC_WIFI_FTM_SUPPORT=y
371371
CONFIG_SOC_WIFI_GCMP_SUPPORT=y
372372
CONFIG_SOC_WIFI_WAPI_SUPPORT=y
373+
CONFIG_SOC_WIFI_TXOP_SUPPORT=y
373374
CONFIG_SOC_WIFI_CSI_SUPPORT=y
374375
CONFIG_SOC_WIFI_MESH_SUPPORT=y
375376
CONFIG_SOC_WIFI_SUPPORT_VARIABLE_BEACON_WINDOW=y
@@ -773,6 +774,7 @@ CONFIG_EFUSE_MAX_BLK_LEN=256
773774
#
774775
CONFIG_ESP_TLS_USING_MBEDTLS=y
775776
# CONFIG_ESP_TLS_USE_SECURE_ELEMENT is not set
777+
CONFIG_ESP_TLS_USE_DS_PERIPHERAL=y
776778
# CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS is not set
777779
# CONFIG_ESP_TLS_SERVER_SESSION_TICKETS is not set
778780
# CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK is not set
@@ -1019,6 +1021,7 @@ CONFIG_ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT=2000
10191021
#
10201022
# Hardware Settings
10211023
#
1024+
CONFIG_ESP_HW_SUPPORT_FUNC_IN_IRAM=y
10221025

10231026
#
10241027
# Chip revision
@@ -1079,6 +1082,8 @@ CONFIG_RTC_CLK_SRC_INT_RC=y
10791082
# CONFIG_RTC_CLK_SRC_EXT_OSC is not set
10801083
# CONFIG_RTC_CLK_SRC_INT_8MD256 is not set
10811084
CONFIG_RTC_CLK_CAL_CYCLES=1024
1085+
CONFIG_RTC_CLK_FUNC_IN_IRAM=y
1086+
CONFIG_RTC_TIME_FUNC_IN_IRAM=y
10821087
# end of RTC Clock Config
10831088

10841089
#

0 commit comments

Comments
 (0)