I'm developing a simple polygon class (using allegro) to use in games in a way similar to level 10 graphics on the snes. Like with one ground polygon and sprites rendered over that all interacting in a 2d way.
I made this class and it works ok, but it renders these weird artifact things at the edge of the polygon. I thought it was just an off by one error in the rendering code, but I tried that and it didn't work. The class uses two functions to render, a main render function and a conversion function that handles rotation, translation, and screen coords.
Code:
void poly_t::render(BITMAP* buffer) {
float x, y;
int bmpx, bmpy, screenx, screeny;
point_t point;
//step through poly and save converted screen coords to buffers
for (x = -w/2; x < w/2; x += w / float(bmp->w)) {
for (y = -h/2; y < h/2; y += h / float(bmp->h)) {
//gen interpolated 3d point (object origin)
point.set(x,y,0);
//convert 3d point to world origin to screen
conv(point,screenx,screeny);
//find location in bitmap
bmpx = int((x + w/2) / w * float(bmp->w));
bmpy = int((y + h/2) / h * float(bmp->h));
if (bmpx < 0 || bmpx >= max || bmpy < 0 || bmpy >= max)
error("Invalid bmp coords (" + tostring(bmpx) + "," + tostring(bmpy) + ")");
//save data to buffers
screenbufferx[bmpx][bmpy] = screenx;
screenbuffery[bmpx][bmpy] = screeny;
screenbufferc[bmpx][bmpy] = getpixel(bmp,bmpx,bmpy);
}
}
//use buffers to render poly with triangles
for (bmpx = 1; bmpx < bmp->w - 2; ++bmpx) {
for (bmpy = 1; bmpy < bmp->h - 2; ++bmpy) {
triangle(buffer,screenbufferx[bmpx][bmpy],screenbuffery[bmpx][bmpy],
screenbufferx[bmpx+1][bmpy],screenbuffery[bmpx+1][bmpy],
screenbufferx[bmpx][bmpy+1],screenbuffery[bmpx][bmpy+1],
screenbufferc[bmpx][bmpy]);
triangle(buffer,screenbufferx[bmpx+1][bmpy+1],screenbuffery[bmpx+1][bmpy+1],
screenbufferx[bmpx+1][bmpy],screenbuffery[bmpx+1][bmpy],
screenbufferx[bmpx][bmpy+1],screenbuffery[bmpx][bmpy+1],
screenbufferc[bmpx][bmpy]);
}
}
}
//object origin points in
void poly_t::conv(point_t& point, int& screenx, int& screeny) {
point_t x, y, z;
//rotate about x-axis
x.x = point.x;
x.y = cos(th.x) * point.y - sin(th.x) * point.z;
x.z = sin(th.x) * point.y + cos(th.x) * point.z;
//rotate about y-axis
y.x = cos(th.y) * x.x + sin(th.y) * x.z;
y.y = x.y;
y.z = sin(th.y) * x.x + cos(th.y) * x.z;
//rotate about z-axis
z.x = cos(th.z) * y.x - sin(th.z) * y.y;
z.y = sin(th.z) * y.x + cos(th.z) * y.y;
z.z = y.z;
//translate
z.x += pos.x;
z.y += pos.y;
z.z += pos.z;
//to screen
screenx = SCREEN_W/2 + int((z.x / z.z) * float(SCREEN_W/2));
screeny = SCREEN_H/2 + int((z.y / z.z) * float(SCREEN_W/2));
}
I'm totally stumped. All that code looks good to me, but something's not working just a little tiny bit. I'll attach a pic of the problem in case that might identify this as some standard problem.