//颜色转换 public static System.Drawing.Color SystemColor(Color a) { a.r = NewMath.Round(a.r, 0, 1); a.g = NewMath.Round(a.g, 0, 1); a.b = NewMath.Round(a.b, 0, 1); float _r = a.r * 255f; float _g = a.g * 255f; float _b = a.b * 255f; return(System.Drawing.Color.FromArgb((int)_r, (int)_g, (int)_b)); }
//光照相关 public void SetLightingColor(Vectex v1) { //将顶点转换到世界空间中 Vectex v = Vectex.Colone(v1); //求世界空间中的法向量 Vector3 n = v.nomal * device.ts.world.Inverse().Transpose(); //光线向量 Vector3 l = device.light.lightPosition - v.pos; //公式 c=cr(ca+cl*n*l) float t = NewMath.Round(Vector3.Dot(n, l), 0, 1); Color c = device.light.diffuse * (device.light.ambient + device.light.sourse * t); c = NewMath.Round(c, 0, 1); v1.lightcolor = c; }
public Color(System.Drawing.Color a) { this.r = NewMath.Round((float)a.R / 255f, 0, 1); this.g = NewMath.Round((float)a.G / 255f, 0, 1); this.b = NewMath.Round((float)a.B / 255f, 0, 1); }
//扫描填色 public void DrawScanline(Vectex v1, Vectex v2, int yIndex) { float x1 = v1.pos.x; float x2 = v2.pos.x; float dx = v2.pos.x - v1.pos.x; float x = x1; //扫描密度,可以减小 float stepx = 0.5f; int xIndex = (int)(x + 0.5f); for (; x <= x2 && xIndex <= x2; x += stepx) { //插值当前点 Vectex p; float t = (x - x1) / dx; p = NewMath.Interp(v1, v2, t); //画点坐标 xIndex = (int)(x + 0.5f); if (xIndex >= device.width || xIndex <= 0 || yIndex <= 0 || yIndex >= device.height) { return; } if (p.rhw >= device.zBuffer[xIndex, yIndex]) { device.zBuffer[xIndex, yIndex] = p.rhw; float w = 1 / p.rhw; if (device.lightMode == LightMode.ON) { Color lighlingColor = new Color(1, 1, 1); lighlingColor = w * p.lightcolor; if (device.renderState == RenderState.COLOR) { Color vertColor = new Color(1, 1, 1); vertColor = w * p.color; Color newColor = NewMath.Round(0.7f * vertColor + 0.3f * lighlingColor, 0, 1); device.frameBuffer.SetPixel(xIndex, yIndex, Color.SystemColor(newColor)); } if (device.renderState == RenderState.TEXTURE) { int uIndex; int vIndex; //目前只有点插值 int u = (int)(p.uvs.u * w * device.texWidth + 0.5f); int v = (int)(p.uvs.v * w * device.texHeight + 0.5f); uIndex = (int)NewMath.Round(u, 0, device.texWidth - 1); vIndex = (int)NewMath.Round(v, 0, device.texHeight - 1); //获取颜色 Color texColor = new Color(1, 1, 1); texColor = new Color(device.textureBuffer.GetPixel(uIndex, vIndex)); Color newColor = NewMath.Round(0.7f * texColor + 0.3f * lighlingColor, 0, 1); //画像素点 device.frameBuffer.SetPixel(xIndex, yIndex, Color.SystemColor(newColor)); } } else { if (device.renderState == RenderState.COLOR) { //用顶点颜色插值 Color vertColor = new Color(1, 1, 1); vertColor = w * p.color; device.frameBuffer.SetPixel(xIndex, yIndex, Color.SystemColor(vertColor)); } if (device.renderState == RenderState.TEXTURE) { int uIndex; int vIndex; //目前只有点插值 int u = (int)(p.uvs.u * w * device.texWidth + 0.5f); int v = (int)(p.uvs.v * w * device.texHeight + 0.5f); uIndex = (int)NewMath.Round(u, 0, device.texWidth - 1); vIndex = (int)NewMath.Round(v, 0, device.texHeight - 1); //获取颜色 Color texColor = new Color(1, 1, 1); texColor = new Color(device.textureBuffer.GetPixel(uIndex, vIndex)); //画像素点 device.frameBuffer.SetPixel(xIndex, yIndex, Color.SystemColor(texColor)); } } } } }