Example #1
0
 //-----------------------
 public void EndPaint()
 {
     _target = null;
 }
Example #2
0
        //-----------------------
        public void Paint(MeshInst meshInst, int rangeIdx, uint[] triangleMask, PaintMode paintMode)
        {
            if (meshInst == null)
            {
                return;
            }

            Mesh mesh = meshInst.Mesh;

            if (mesh == null)
            {
                return;
            }

            // get the material associated with the meshInst's range.
            Material material = meshInst.GetMaterial(rangeIdx);

            if (material == null)
            {
                return;
            }

            // if the material isn't ubertextured, skip it.
            if (!material.Ubertextured)
            {
                return;
            }

            // determine if the current range and the paint frustum
            // intersect.
            if (!_maskCamera.Frustum.IsOBoxInside(meshInst.GetRangeOBox(rangeIdx)))
            {
                return;
            }

            // configure the ubertexture renderer for the current material's
            // targets.
            UberTexture uberTexture = new UberTexture(material.GetPass(0).UberTexture);

            if (uberTexture == null)
            {
                return;
            }

            _target = uberTexture;

            // get the inverse of the mesh instance's transform.  This is
            // so we can move the cameras into the mesh instance's local
            // space for culling and rasterization purposes.
            Matrix invInstXForm = new Matrix();

            if (!meshInst.Transform.Inverse(out invInstXForm))
            {
                return;
            }

            // get the source camera's transform.
            Matrix sourceCamXForm = new Matrix(_sourceCamera.Rotation, _sourceCamera.Position);
            Matrix maskCamXForm   = new Matrix(_maskCamera.Rotation, _maskCamera.Position);

            sourceCamXForm = invInstXForm * sourceCamXForm;
            maskCamXForm   = invInstXForm * maskCamXForm;

            // build cameras that are local to the mesh instance.
            Camera localSourceCamera = new Camera(sourceCamXForm.Translate, sourceCamXForm.Rotate,
                                                  _sourceCamera.Projection);
            Camera localMaskCamera = new Camera(maskCamXForm.Translate, maskCamXForm.Rotate,
                                                _maskCamera.Projection);

            // set the paint targets.
            _rasterizer.RenderTarget = _target;

            // set the camera info.
            _rasterizer.SetVertexConstants(0, localSourceCamera.ViewProjMatrix);
            _rasterizer.SetVertexConstants(4, localMaskCamera.ViewProjMatrix);

            // set the blending mode to accumulate.
            float blendingMode = 1.0f;

            if (_maskPaintMode == MaskMode.Subtract)
            {
                blendingMode = 0.0f;
            }

            // if the paint mode is 'fill' then disable the per-pixel clip.
            float perPixelClip = 1.0f;

            if (paintMode == PaintMode.Fill)
            {
                perPixelClip = 0.0f;
            }

            // set the mask-only value.
            float maskValue = 0.0f;

            if (paintMode == PaintMode.Mask)
            {
                maskValue = 1.0f;
            }

            // setup the shader modes.
            _rasterizer.SetFragmentConstant(0, new Vector4(blendingMode, perPixelClip, maskValue, 0.0f));

            // set the camera's rotation for normals.
            Matrix cameraRot = new Matrix(localSourceCamera.Rotation, new Vector3(0.0f, 0.0f, 0.0f));

            _rasterizer.SetFragmentConstants(1, cameraRot);

            // now grab the mesh's vertices and throw them at the
            // uber-texture renderer.
            uint triCount = CalculateVisibleTriangles(localMaskCamera, meshInst.Mesh, rangeIdx);

            // now render the visible triangles.
            _rasterizer.RenderTriangleList(meshInst.Mesh.VertexBuffer, _visibleTriangles,
                                           0, 3 * triCount);
        }