-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointsRenderer.cpp
More file actions
53 lines (47 loc) · 1.53 KB
/
Copy pathPointsRenderer.cpp
File metadata and controls
53 lines (47 loc) · 1.53 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
#include "PointsRenderer.hpp"
namespace PointsRenderer
{
Color getCurrentColor(int x, int y, Drawable* surface)
{
return Color{ surface->getPixel(x, y) };
}
bool pointInRect(const Point4D& point, const Rect& rect)
{
auto x = std::round(point.x);
auto y = std::round(point.y);
return x >= rect.x && x < rect.right() && y >= rect.y && y < rect.bottom();
}
// Draw to surface
void drawToSurface(const Point4D& screenPoint, Drawable* drawSurface, const Color& colorToPaint)
{
drawSurface->setPixel(static_cast<int>(std::round(screenPoint.x)), static_cast<int>(std::round(screenPoint.y)), colorToPaint.asUnsigned());
}
void drawPixel(const Point4D& screenPoint, Drawable* drawSurface, const Color& colorToPaint, Matrix2D<double>& zBuffer, const Rect& viewPort, const Camera& camera)
{
auto x = static_cast<int>(std::round(screenPoint.x - viewPort.x));
auto y = static_cast<int>(std::round(screenPoint.y - viewPort.y));
auto currentZ = zBuffer[x][y];
auto newZ = std::round(screenPoint.z);
//auto newZ = screenPoint.z;
if (newZ < currentZ && newZ >= camera.near)
{
zBuffer[x][y] = newZ;
drawToSurface(screenPoint, drawSurface, colorToPaint);
}
else
{
int a = 0;
a += 1;
}
}
void PointsRenderer::renderPoints(const std::vector<Point4D>& points, Drawable * drawSurface, Matrix2D<double>& zBuffer, const Rect & viewPort, const Camera& camera)
{
for (const auto& point : points)
{
if (pointInRect(point, viewPort))
{
drawPixel(point, drawSurface, point.color, zBuffer, viewPort, camera);
}
}
}
}