private ScanLine CalculateStepPerRow(Vertex left, Vertex right, int row) { ScanLine ret = new ScanLine(); double width = right.point.x - left.point.x; ret.x = (int)(left.point.x + 0.5); ret.y = row; ret.width = (int)(right.point.x + 0.5) - ret.x; ret.leftvertex = left; if (left.point.x >= right.point.x) { ret.width = 0; } //计算步长 double rate = 1.0 / width; ret.step.point.x = (right.point.x - left.point.x) * rate; ret.step.point.y = (right.point.y - left.point.y) * rate; ret.step.point.z = (right.point.z - left.point.z) * rate; ret.step.point.t = (right.point.t - left.point.t) * rate; ret.step.uv.u = (right.uv.u - left.uv.u) * rate; ret.step.uv.v = (right.uv.v - left.uv.v) * rate; ret.step.color.r = (right.color.r - left.color.r) * rate; ret.step.color.g = (right.color.g - left.color.g) * rate; ret.step.color.b = (right.color.b - left.color.b) * rate; ret.step.normal.x = (right.normal.x - left.normal.x) * rate; ret.step.normal.y = (right.normal.y - left.normal.y) * rate; ret.step.normal.z = (right.normal.z - left.normal.z) * rate; ret.step.WorldPos.x = (right.WorldPos.x - left.WorldPos.x) * rate; ret.step.WorldPos.y = (right.WorldPos.y - left.WorldPos.y) * rate; ret.step.WorldPos.z = (right.WorldPos.z - left.WorldPos.z) * rate; return(ret); }
private void StartRenderLine(ScanLine scanline) { int x = scanline.x; int width = scanline.width; if ((x + width) < 0) { return; } for (; width > 0; x++, width--)//逐像素计算,直到扫描完width长度的点 { if (x >= 0 && x < BitMapWidth) { //深度缓存 if (scanline.leftvertex.point.t < DepthBuffer[scanline.y, x]) { DepthBuffer[scanline.y, x] = scanline.leftvertex.point.t; double u = scanline.leftvertex.uv.u; double v = scanline.leftvertex.uv.v; MyColor mycolor = ReadTexture(u, v); //根据uv读取颜色 非立方体纹理映射 //MyColor mycolor = CubeMap(scanline.leftvertex.WorldPos, this.mesh.CubeCenter); a2v FS = new a2v(scanline.leftvertex.WorldPos, scanline.leftvertex.normal, this.MyCamera.eye); mycolor = Light.LightMode_BlinnPhong(FS, mycolor, IsTexture, IsPointLighting, MyLight); Color color = mycolor.ConvertToColor(); MyBitMap.SetPixel(x, scanline.y, color); //设置对应像素点颜色 } } scanline.leftvertex.NextStep(scanline.step); if (x > BitMapWidth) { break; } } }
private void StartRenderTrapezoid(RenderTriangle r) { int top, bottom; top = (int)(r.topline + 0.5); bottom = (int)(r.downline + 0.5); for (int row = top; row < bottom; row++) { if (row >= 0 && row < this.BitMapHeight) { Vertex left, right; DoubleLinedInterp(r, (double)row + 0.5f, out left, out right); //DoubleLinedInterp(r, (double)row, out left, out right); ScanLine s = CalculateStepPerRow(left, right, row); StartRenderLine(s); } if (row >= this.BitMapHeight) { break; } } }