Hello! Working on a software renderer here. Pardon the poorly made application, this is my "quick and dirty" startup for this renderer. Recently I've been working on clipping projected lines to the screen. Unfortunately, it's not working very well . . . for some sides of the screen.
Attached is a video clip of me showing the clipping problem of the left and top of the screen. The bottom and right work correctly . . .
Here's my clipping code:
void Application::clipAndDrawLine(Vector3 &v1, Vector3 &v2) {
//const int INSIDE = 0;
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
int code1 = computeOutcode(v1);
int code2 = computeOutcode(v2);
bool render = true;
while(true) {
if (!(code1 | code2)) {
break;
} else if (code1 & code2) {
render = false;
break;
} else {
float x, y;
int outcodeOut = code1 ? code1 : code2;
if (outcodeOut & TOP) {
x = v1.x + (v2.x - v1.x) * ((HEIGHT - 1) - v1.y) / (v2.y - v1.y);
y = HEIGHT - 1;
} else if (outcodeOut & BOTTOM) {
x = v1.x + (v2.x - v1.x) * (0 - v1.y) / (v2.y - v1.y);
y = 0;
} else if (outcodeOut & RIGHT) {
y = v1.y + (v2.y - v1.y) * ((WIDTH - 1) - v1.x) / (v2.x - v1.x);
x = WIDTH - 1;
} else if (outcodeOut & LEFT) {
y = v1.y + (v2.y - v1.y) * (0 - v1.x) / (v2.x - v1.x);
x = 0;
}
if (outcodeOut == code1) {
v1.x = x;
v1.y = y;
code1 = computeOutcode(v1);
} else {
v2.x = x;
v2.y = y;
code2 = computeOutcode(v2);
}
}
}
if (render) {
renderer.renderLine(v1.x, v1.y, v2.x, v2.y,
SDL_MapRGB(screen->format, 255, 255, 255));
}
}
int Application::computeOutcode(Vector3 &v) {
const int INSIDE = 0;
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
int code = INSIDE;
if (v.x < 0) {
code |= LEFT;
}
if (v.x >= WIDTH) {
code |= RIGHT;
}
if (v.y < 0) {
code |= BOTTOM;
}
if (v.y >= HEIGHT) {
code |= TOP;
}
return code;
}
I can't find the issue here! Does anything look wrong to any of you guys? I believe the problem is with the clipping, seeing as that's where the problem occurs......
-CP