Skip to content

Commit

Permalink
rasterizer: handle 0 length lines correctly (fixes #95)
Browse files Browse the repository at this point in the history
debug: added Line command parsing
  • Loading branch information
JaCzekanski committed Jan 2, 2021
1 parent 76e311a commit 04437eb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/device/gpu/render/render_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ void Render::drawLine(gpu::GPU* gpu, const primitive::Line& line) {
int dy = y1 - y0;
int derror = std::abs(dy) * 2;
int error = !steep;
int _y = y0;
int y = y0;

float length = sqrtf(powf(x1 - x0, 2) + powf(y1 - y0, 2));
if (length == 0) {
length = 1.f; // Prevent NaN in getColor
}

// TODO: Precalculate color stepping
auto getColor = [&](int x, int y) -> RGB {
Expand Down Expand Up @@ -81,16 +84,16 @@ void Render::drawLine(gpu::GPU* gpu, const primitive::Line& line) {
VRAM[y][x] = c.raw;
};

for (int _x = x0; _x <= x1; _x++) {
for (int x = x0; x <= x1; x++) {
if (steep) {
// TODO: Remove insideDrawingArea calls
if (gpu->insideDrawingArea(_y, _x)) putPixel(_y, _x, getColor(_x, _y));
if (gpu->insideDrawingArea(y, x)) putPixel(y, x, getColor(x, y));
} else {
if (gpu->insideDrawingArea(_x, _y)) putPixel(_x, _y, getColor(_x, _y));
if (gpu->insideDrawingArea(x, y)) putPixel(x, y, getColor(x, y));
}
error += derror;
if (error > dx) {
_y += (y1 > y0 ? 1 : -1);
y += (y1 > y0 ? 1 : -1);
error -= dx * 2;
}
}
Expand Down
28 changes: 27 additions & 1 deletion src/platform/windows/gui/debug/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,33 @@ void GPU::handlePolygonCommand(const gpu::PolygonArgs arg, const std::vector<uin
}

void GPU::handleLineCommand(const gpu::LineArgs arg, const std::vector<uint32_t> &arguments) {
(void)arg; // TODO: Parse Line commands
int vertexCount;
if (!arg.gouraudShading) {
vertexCount = arguments.size() - 1; // ignore arg[0] aka base color
} else {
vertexCount = arguments.size() / 2;
}

std::string flags;
if (arg.semiTransparency) flags += "semi-transparent, ";
if (arg.gouraudShading) flags += "Gouraud-shaded, ";
if (arg.polyLine) flags += "poly-line, ";
ImGui::Text("Flags: %s", flags.c_str());

int ptr = 0;
for (int i = 0; i < vertexCount; i++) {
uint32_t color = arg.gouraudShading ? arguments[ptr++] : arguments[0];
int16_t x = extend_sign<11>(arguments[ptr] & 0xffff);
int16_t y = extend_sign<11>((arguments[ptr] & 0xffff0000) >> 16);
ptr++;

x += last_offset_x;
y += last_offset_y;

colorBox(RGB{color});
ImGui::SameLine();
ImGui::Text("Pos: %dx%d", x, y);
}
}

void GPU::handleRectangleCommand(const gpu::RectangleArgs arg, const std::vector<uint32_t> &arguments) {
Expand Down

0 comments on commit 04437eb

Please sign in to comment.