public void DrawTriangle(TextureBuffer target, Matrix4 transformationMatrix, ref int[,] zbuffer) { VertexParam[] transformedCoords = new VertexParam[vertices.Length]; for (int vertexNum = 0; vertexNum < vertices.Length; vertexNum++) { transformedCoords[vertexNum] = vertices[vertexNum].position.Transform(transformationMatrix, 1); } target.DrawTriangle(transformedCoords[0], transformedCoords[1], transformedCoords[2], vertices[0].texture, vertices[1].texture, vertices[2].texture, ref zbuffer); }
private Point3F ConvertToRange(VertexParam coordinate) { Point3F point; point.x = width / 2 + coordinate.values[0] * width; point.y = height / 2 + coordinate.values[1] * height; point.z = height / 2 + coordinate.values[2] * height; return(point); }
public void DrawTriangle(VertexParam coordinate1, VertexParam coordinate2, VertexParam coordinate3, VertexParam point1texture, VertexParam point2texture, VertexParam point3texture, ref int[,] zbuffer) { Point3F point1 = ConvertToRange(coordinate1); Point3F point2 = ConvertToRange(coordinate2); Point3F point3 = ConvertToRange(coordinate3); int x0 = (int)Math.Floor(Math.Min(point1.x, Math.Min(point2.x, point3.x))); int x1 = (int)Math.Ceiling(Math.Max(point1.x, Math.Max(point2.x, point3.x))); int y0 = (int)Math.Floor(Math.Min(point1.y, Math.Min(point2.y, point3.y))); int y1 = (int)Math.Ceiling(Math.Max(point1.y, Math.Max(point2.y, point3.y))); float[,] convertedTextureCoordinates = new float[3, 2]; VertexParam[] verticesTextures = new VertexParam[3]; verticesTextures[0] = point1texture; verticesTextures[1] = point2texture; verticesTextures[2] = point3texture; convertedTextureCoordinates = ConvertToTexture(verticesTextures); if ((x1 > width && y1 > height && x0 > height && y0 > height) || (x0 < 0 && x1 < 0 && y0 < 0 && y1 < 0)) { return; // Проверка выхода прямоугольника за пределы рисуемого объекта } else { x0 = x0 < 0 ? 0 : x0; // Проверка частичного выхода прямоугольника за пределы рисуемого объекта (и исправление) y0 = y0 < 0 ? 0 : y0; x1 = x1 > width ? width : x1; y1 = y1 > height ? height : y1; for (int x = x0; x < x1; x++) { for (int y = y0; y < y1; y++) { DrawPixel(x, y, point1, point2, point3, convertedTextureCoordinates, ref zbuffer[x, y]); } } } }
public VertexParam normal; // нормально public Vertex(VertexParam position, VertexParam texture, VertexParam normal) { this.position = position; this.texture = texture; this.normal = normal; }