void line(int x0, int y0, int x1, int y1, uint8_t color) { // Check if line is connected with a dot behind the // camera if (x0 == VERTEX_BEHIND_CAMERA || x1 == VERTEX_BEHIND_CAMERA) return; if (!isInViewport(x0, y0)) { if (!isInViewport(x1, y1)) return; int dummy = x0; x0 = x1; x1 = dummy; dummy = y0; y0 = y1; y1 = dummy; } // Bresenham int dx = abs(x1 - x0), sx = x0<x1 ? 1 : -1; int dy = abs(y1 - y0), sy = y0<y1 ? 1 : -1; int err = (dx>dy? dx : -dy) / 2, e2; while(true) { // Optimize me: For now, we just ignore pixels outside the screen. We might // want to clip those, though. if (!isInViewport(x0, y0)) break; lcdSetPixel(x0, y0, color); if (x0==x1 && y0==y1) break; e2 = err; if (e2 >-dx) { err -= dy; x0 += sx; } if (e2 < dy) { err += dx; y0 += sy; } } }
/** * Set pixle color r,g,b in raster for x,y */ void bkgl::setPixel(Point p) { if (isInScreen(p) && isInViewport(p) && checkzbuffer(p)) { Color light = calculateIntensity(p.world, p.normal.normalize()); Color spec = calculateSpecularColor(p.world, p.normal.normalize()); Color newColor = applyLightToColor(light, p.color, spec); raster[((p.y*SCREENWIDTH) + p.x)*3 + 0] = newColor[0]; raster[((p.y*SCREENWIDTH) + p.x)*3 + 1] = newColor[1]; raster[((p.y*SCREENWIDTH) + p.x)*3 + 2] = newColor[2]; if (gldepthTest) zbuffer[(p.y*SCREENWIDTH) + p.x] = p.z; } }