Exemplo n.º 1
0
 public IVertex(Vertex v)
 {
     this.position = (Int3)v.position;
     this.normal = v.normal;
     this.color = v.color;
     this.uv = v.uv;
 }
Exemplo n.º 2
0
        private void RenderTriangle2(Vertex _v1, Vertex _v2, Vertex _v3, Material material, Float3 lightDirection)
        {
            IVertex v1 = new IVertex(_v1);
            IVertex v2 = new IVertex(_v2);
            IVertex v3 = new IVertex(_v3);

            if (v1.y > v2.y) Utils.swap(ref v1, ref v2);
            if (v1.y > v3.y) Utils.swap(ref v1, ref v3);
            if (v2.y > v3.y) Utils.swap(ref v2, ref v3);

            int triangleYHeight = v3.y - v1.y + 1;
            int firstSegmentHeight = v2.y - v1.y + 1;
            int secondSegmentHeight = v3.y - v2.y + 1;

            for (int y = v1.y; y <= v3.y; y++)
            {
                bool isFirstSegment = y < v2.y;
                int segmentStartY = isFirstSegment ? v1.y : v2.y;

                float alpha = (float)(y - v1.y) / (float)triangleYHeight;
                IVertex A = IVertex.lerp(v1, v3, alpha);

                float beta = (float)(y - segmentStartY) / (float)(isFirstSegment ? firstSegmentHeight : secondSegmentHeight);
                IVertex B = IVertex.lerp(isFirstSegment ? v1 : v2, isFirstSegment ? v2 : v3, beta);

                if (A.x > B.x)
                    Utils.swap(ref A, ref B);
                for (int x = A.x; x <= B.x; x++)
                {
                    // check extremes
                    float delta = (A.x == B.x) ? 1.0f : (float)(x - A.x) / (float)(B.x - A.x);

                    IVertex C = IVertex.lerp(A, B, delta);

                    float lc = getLamberComponent(C.normal, lightDirection);
                    int intLambert = (int)(lc * 255);
                    Color c = Color.FromArgb(intLambert, intLambert, intLambert);

                    c = tex2D(material.diffuseTexture, C.u, C.v);
                    DrawPointToFrameBuffer(x, y, C.z, c);
                }
            }
        }