Beispiel #1
0
        public Canvas(int width, int height, Color color)
        {
            _width  = width;
            _height = height;
            Bitmap bitmap = new Bitmap(_width, _height);

            _bitmapLock = new BitmapLock(bitmap, ImageLockMode.ReadWrite);

            FillBitmap(color);

            _zBuffer = new float[_width, _height];

            for (int x = 0; x < _width; x++)
            {
                for (int y = 0; y < _height; y++)
                {
                    _zBuffer[x, y] = float.MinValue;
                }
            }
        }
Beispiel #2
0
        public void DrawMesh(Mesh mesh, Line.LineType lineType)
        {
            _depth = mesh.Polygons.Max(p => p.Vertices.Max(v => v.Z)) -
                     mesh.Polygons.Min(p => p.Vertices.Min(v => v.Z));
            _texture = new BitmapLock(mesh.Texture, ImageLockMode.ReadOnly);

            if (_cameraType == CameraType.Perspective)
            {
                _viewport = GetViewport();
                _view     = LookAt(new Vector3(-0.2f, 0.3f, 0), new Vector3(0.5f, 1, 0));
            }

            _shader = new GouraudVertexShader(_lightDirection)
            {
                View     = _view,
                Viewport = _viewport
            };

            foreach (Polygon polygon in mesh.Polygons)
            {
                for (int i = 0; i < polygon.Vertices.Length; i++)
                {
                    polygon.Vertices[i] = TransformVector(polygon.Vertices[i]);
                }


                float intensity = 0f;
                if (IsPolygonShouldBeDrawn(polygon, out intensity))
                {
                    if (polygon.UVs.Length == 0)
                    {
                        DrawPolygon(polygon, intensity, lineType);
                    }
                    else
                    {
                        DrawPolygonWithTex(polygon, lineType);
                    }
                }
            }
        }