예제 #1
0
    void TestRenderer()
    {
        Helper.display = false;

        List <Triangle3D> ts = bsp.GetSquence(cam.transform.position);

        Helper.DisplayTriangleList(ts, "bsp");

        for (int i = 0; i < ts.Count; i++)
        {
            if (!FaceCulling(ts[i].normal, cam.transform.forward))
            {
                ts.RemoveAt(i);
                i--;
            }
        }

        Helper.DisplayTriangleList(ts, "face culling");

        //观察坐标系,左手坐标系
        m_Transform cam_t = new m_Transform();

        cam_t.CopyFromTransform(cam.transform);
        for (int i = 0; i < ts.Count; i++)
        {
            Triangle3D t = ts[i];
            t.a      = cam_t.InverseTransformPoint(t.a);
            t.b      = cam_t.InverseTransformPoint(t.b);
            t.c      = cam_t.InverseTransformPoint(t.c);
            t.normal = cam_t.InverseTransformDirection(t.normal);
            ts[i]    = t;
        }

        int count = ts.Count;

        for (int i = 0; i < count; i++)
        {
            ts.AddRange(ViewSpaceFrustrumClipping(ts[0], cam));
            ts.RemoveAt(0);
        }

        Helper.DisplayTriangleList(ts, "Frustrum Clipping");

        List <Triangle3D> ts_ScreenSpace = new List <Triangle3D>(ts.Count);
        Matrix4x4         p = GetProjectionMatrix(cam);
        //转换到右手坐标
        Matrix4x4 negetive = Matrix4x4.identity;

        negetive.m22 = -1;
        Matrix4x4 total = p * negetive;

        for (int i = 0; i < ts.Count; i++)
        {
            Triangle3D t = ts[i];
            t.a = ClipSpaceToScreenSpace(total * V4Point(t.a), pixelWidth, pixelHeight);
            t.b = ClipSpaceToScreenSpace(total * V4Point(t.b), pixelWidth, pixelHeight);
            t.c = ClipSpaceToScreenSpace(total * V4Point(t.c), pixelWidth, pixelHeight);
            ts_ScreenSpace.Add(t);
        }

        Helper.DisplayTriangleList(ts_ScreenSpace, "Sceen Space");

        for (int i = 0; i < ts_ScreenSpace.Count; i++)
        {
            Triangle3D t  = ts_ScreenSpace[i];
            Vector2[]  t2 = new Vector2[3];
            t2[0] = t.a;
            t2[1] = t.b;
            t2[2] = t.c;
            DrawTriangleScanLine(colorBuffer, t2);
        }
    }