void drawPolygon( uint8_t _x0, uint8_t _y0, uint8_t _x1, uint8_t _y1, uint8_t _x2, uint8_t _y2, uint8_t _x3, uint8_t _y3, uint32_t _c) { int rl[MAX_X_3D*2]; int j,i; for (i=0;i<MAX_X_3D;++i) { rl[2*i+0] = MAX_Y_3D+1; rl[2*i+1] = -1; } rasterLine( _x0, _y0, _x1, _y1, rl ); rasterLine( _x1, _y1, _x2, _y2, rl ); rasterLine( _x2, _y2, _x3, _y3, rl ); rasterLine( _x3, _y3, _x0, _y0, rl ); for (i=0;i<MAX_X_3D;++i) { if (rl[2*i+1]>rl[2*i+0]) { for( j=rl[2*i+0]; j<=rl[2*i+1]; ++j ) { frame[j*MAX_Y_3D+i]=_c; } } } }
void drawLine(FrameBuffer * colorBuff) { for (int i=0; i<(int)OBJ->numtriangles; ++i) { //cout << "tri:" << i << endl; //if (tri[i].N.z < 0) continue; // backface Vertex a(OBJ, OBJ->triangles[i].vindices[0]); Vertex b(OBJ, OBJ->triangles[i].vindices[1]); Vertex c(OBJ, OBJ->triangles[i].vindices[2]); rasterLine(colorBuff, a, b); rasterLine(colorBuff, a, c); rasterLine(colorBuff, b, c); } }
int main(int argc, char **argv) { //printf("Num args is %d\n", argc); if (argc < 7) { printf("Usage: ras width height x0 y0 x1 y1\n"); exit(1); } int a[6]; int i; for (i=0; i<6; i++) { a[i] = atoi(argv[i+1]); } imageData idata = getNewImageData(a[0], a[1]); rasterLine(idata, a[2], a[3], a[4], a[5]); writeImage("out.png", idata); cleanupImageData(idata); return 0; }
void rasterTriangle(FrameBuffer* buf, GLMtriangle t) { Vertex a(OBJ, t.vindices[0]); Vertex b(OBJ, t.vindices[1]); Vertex c(OBJ, t.vindices[2]); if (b.pos.y > a.pos.y) swap(a, b); if (c.pos.y > a.pos.y) swap(a, c); if (c.pos.y > b.pos.y) swap(b, c); Vector ab = b.pos-a.pos; Vector bc = c.pos-b.pos; Vector ca = a.pos-c.pos; Vertex left, right; GLfloat y, x1, x2; GLfloat m1_inv, m2_inv; left = a; right = a; x1 = a.pos.x; x2 = a.pos.x; y = a.pos.y; if (ab.y == 0) // top { right = b; x2 = b.pos.x; m1_inv = ca.x/ca.y; m2_inv = bc.x/bc.y; rasterLine(buf, left, right); y--; while (y > c.pos.y) { x1=x1-m1_inv; x2=x2-m2_inv; left = interpolate(a, c, x1, y); right = interpolate(b, c, x2, y); rasterLine(buf, left, right); y--; } } else if (bc.y == 0) // bottom { m1_inv = ab.x/ab.y; m2_inv = ca.x/ca.y; rasterLine(buf, left, right); y--; while (y > c.pos.y) { x1=x1-m1_inv; x2=x2-m2_inv; left = interpolate(a, b, x1, y); right = interpolate(c, a, x2, y); rasterLine(buf, left, right); y--; } } else { m1_inv = ab.x/ab.y; m2_inv = ca.x/ca.y; rasterLine(buf, left, right); y--; while (y > b.pos.y) { x1=x1-m1_inv; x2=x2-m2_inv; left = interpolate(a, b, x1, y); right = interpolate(c, a, x2, y); rasterLine(buf, left, right); y--; } m1_inv = bc.x/bc.y; y = b.pos.y; x1 = b.pos.x; x2 = a.pos.x + m2_inv*(y-a.pos.y); left = b; right = interpolate(c, a, x2, y); rasterLine(buf, left, right); y--; while (y > c.pos.y) { x1=x1-m1_inv; x2=x2-m2_inv; left = interpolate(b, c, x1, y); right = interpolate(c, a, x2, y); rasterLine(buf, left, right); y--; } } }