Example #1
0
        public override void Present(Buffer<Color4> buf)
        {
            Graphics g = Graphics.FromImage(bitmap);

            for (int x = 0; x < width; x++)
            {
                for (int y=0; y<height; y++)
                {
                    Color4 data = buf.readOneData(x, y);
                    Color c = Color.FromArgb((int)(data.Alpha*255), (int)(data.Red*255), (int)(data.Green*255), (int)(data.Blue*255));
                    g.DrawRectangle(new Pen(c), x, height-y, 1, 1);
                }
            }

            g.Save();
            g.Dispose();
            bitmap.Save("dd.png", System.Drawing.Imaging.ImageFormat.Png);
        }
Example #2
0
        public void drawTriange(Vertex[] vertexList, int[] index, Buffer<Color4> outPutBuffer, Buffer<float> zBuffer, Texture t)
        {
            Vertex v1 = vertexList[index[0]];
            Vertex v2 = vertexList[index[1]];
            Vertex v3 = vertexList[index[2]];
            float tMinX =v1.pos.X;
            float tMinY = v1.pos.Y;
            float tMaxX = v1.pos.X;
            float tMaxY = v1.pos.Y;

            for (int i = 0; i < 3; i++)
            {
                Vertex v = vertexList[index[i]];
                if (v.pos.X < tMinX)
                    tMinX = v.pos.X;

                if (v.pos.Y < tMinY)
                    tMinY = v.pos.Y;

                if (v.pos.X > tMaxX)
                    tMaxX = v.pos.X;

                if (v.pos.Y > tMaxY)
                    tMaxY = v.pos.Y;
            }

            int minX = (int)Math.Floor(tMinX);
            int maxX = (int)Math.Ceiling(tMaxX);
            int minY = (int)Math.Floor(tMinY);
            int maxY = (int)Math.Ceiling(tMaxY);

            for (int posX = minX; posX <= maxX; posX++)
            {
                for (int posY = minY; posY <= maxY; posY++)
                {
                    float posX0 = v1.pos.X;
                    float posX1 = v2.pos.X;
                    float posX2 = v3.pos.X;
                    float posY0 = v1.pos.Y;
                    float posY1 = v2.pos.Y;
                    float posY2 = v3.pos.Y;

                    float index0 = ((posY1 - posY2) * posX + (posX2 - posX1) * posY + posX1 * posY2 - posX2 * posY1) /
                        ((posY1 - posY2) * posX0 + (posX2 - posX1) * posY0 + posX1 * posY2 - posX2 * posY1);

                    float index1 = ((posY2 - posY0) * posX + (posX0 - posX2) * posY + posX2 * posY0 - posX0 * posY2) /
                        ((posY2 - posY0) * posX1 + (posX0 - posX2) * posY1 + posX2 * posY0 - posX0 * posY2);

                    float index2 = ((posY0 - posY1) * posX + (posX1 - posX0) * posY + posX0 * posY1 - posX1 * posY0) /
                        ((posY0 - posY1) * posX2 + (posX1 - posX0) * posY2 + posX0 * posY1 - posX1 * posY0);

                    if (index0 > 0 && index1 > 0 && index2 > 0)
                    {
                        Color4 c = new Color4();
                        c.Alpha = v1.color.Alpha * index0 + v2.color.Alpha * index1 + v3.color.Alpha * index2;
                        c.Red = v1.color.Red * index0 + v2.color.Red * index1 + v3.color.Red * index2;
                        c.Green = v1.color.Green * index0 + v2.color.Green * index1 + v3.color.Green * index2;
                        c.Blue = v1.color.Blue * index0 + v2.color.Blue * index1 + v3.color.Blue * index2;
                        float z = v1.pos.Z * index0 + v2.pos.Z * index1 + v3.pos.Z * index2;

                        bool offScreen = posX < 0 || posX >= SRDevice.Device.GetWidth() || posY < 0 || posY >= SRDevice.Device.GetHeight();
                        if (!offScreen)
                        {
                            float curZ = zBuffer.readOneData(posX, posY);
                            if (z < curZ)
                            {
                                zBuffer.writeOneData(posX, posY, z);
                                if (t != null)
                                {
                                    float u = v1.uv.X * index0 + v2.uv.X * index1 + v3.uv.X * index2;
                                    float v = v1.uv.Y * index0 + v2.uv.Y * index1 + v3.uv.Y * index2;
                                    Math.Min(Math.Max(u, 0.0f), 1.0f);
                                    Math.Min(Math.Max(v, 0.0f), 1.0f);
                                    outPutBuffer.writeOneData(posX, posY, t.getPixel(u, v));
                                }
                                else
                                {
                                    outPutBuffer.writeOneData(posX, posY, c);
                                }
                            }
                        }
                    }
                }
            }
        }