/// <summary> /// Resets the RenderState to the default settings. /// (Check each property documentation for its default value.) /// </summary> public void Reset() { Alpha = 1.0f; BlendMode = Display.BlendMode.NORMAL; RenderTarget = null; ClipRect = null; if (_modelviewMatrix != null) { _modelviewMatrix.Identity(); } else { _modelviewMatrix = Matrix2D.Create(); } if (_projectionMatrix3D != null) { _projectionMatrix3D.Identity(); } else { _projectionMatrix3D = Matrix3D.Create(); } if (_mvpMatrix3D == null) { _mvpMatrix3D = Matrix3D.Create(); } }
/// <summary> /// Creates a new effect. /// </summary> public Effect() { _mvpMatrix3D = Matrix3D.Create(); ProgramBaseName = GetType().Name; SparrowSharp.ContextCreated += OnContextCreated; TextureSmoothing = TextureSmoothing.Bilinear; }
/** Creates a new, empty instance. */ public FilterHelper(TextureFormat textureFormat) { _preferredScale = SparrowSharp.ContentScaleFactor; TextureFormat = textureFormat; _sizeStep = 64; // must be POT! _pool = new Stack <Texture>(); _projectionMatrix = Matrix3D.Create(); _targetBounds = Rectangle.Create(); SetSize(_sizeStep, _sizeStep); }
/** Creates a perspective projection matrix suitable for 2D and 3D rendering. * * <p>The first 4 parameters define which area of the stage you want to view (the camera * will 'zoom' to exactly this region). The final 3 parameters determine the perspective * in which you're looking at the stage.</p> * * <p>The stage is always on the rectangle that is spawned up between x- and y-axis (with * the given size). All objects that are exactly on that rectangle (z equals zero) will be * rendered in their true size, without any distortion.</p> * * <p>If you pass only the first 4 parameters, the camera will be set up above the center * of the stage, with a field of view of 1.0 rad.</p> */ public static Matrix3D CreatePerspectiveProjectionMatrix( float x, float y, float width, float height, float stageWidth = 0f, float stageHeight = 0f, float[] cameraPos = null) { Matrix3D outMatrix = Matrix3D.Create(); if (stageWidth <= 0) { stageWidth = width; } if (stageHeight <= 0) { stageHeight = height; } if (cameraPos == null) { cameraPos = new[] { stageWidth / 2f, stageHeight / 2f, // -> center of stage stageWidth / (float)Math.Tan(0.5f) * 0.5f }; // -> fieldOfView = 1.0 rad } float focalLength = Math.Abs(cameraPos[2]); float offsetX = cameraPos[0] - stageWidth / 2f; float offsetY = cameraPos[1] - stageHeight / 2f; float far = focalLength * 20f; const float near = 1f; float scaleX = stageWidth / width; float scaleY = stageHeight / height; // set up general perspective float[] sMatrixData = new float[16]; sMatrixData[0] = 2 * focalLength / stageWidth; // 0,0 sMatrixData[5] = -2 * focalLength / stageHeight; // 1,1 [negative to invert y-axis] sMatrixData[10] = far / (far - near); // 2,2 sMatrixData[14] = -far * near / (far - near); // 2,3 sMatrixData[11] = 1; // 3,2 // now zoom in to visible area sMatrixData[0] *= scaleX; sMatrixData[5] *= scaleY; sMatrixData[8] = scaleX - 1 - 2 * scaleX * (x - offsetX) / stageWidth; sMatrixData[9] = -scaleY + 1 + 2 * scaleY * (y - offsetY) / stageHeight; outMatrix.CopyRawDataFrom(sMatrixData); outMatrix.PrependTranslation( -stageWidth / 2.0f - offsetX, -stageHeight / 2.0f - offsetY, focalLength); return(outMatrix); }
public void Translate(double Tx, double Ty, double Tz) { /* * Матрица перемещения: * 1 0 0 Tx * 0 1 0 Ty * 0 0 1 Tz * 0 0 0 1 */ Matrix3D tm = new Matrix3D(); tm.Create(m41: Tx, m42: Ty, m43: Tz); Copy(tm, this); }
public void Scale(double Sx, double Sy, double Sz) { /* * Матрица масштабирования: * Sx 0 0 0 * 0 Sy 0 0 * 0 0 Sz 0 * 0 0 0 1 */ Matrix3D sm = new Matrix3D(); sm.Create(m11: Sx, m22: Sy, m33: Sz); Copy(sm, this); }
/// <summary> /// Updates the actual shader matrix. /// Always call this after you are finished manipulating the color matrix /// </summary> public void UpdateShaderMatrix() { // the shader needs the matrix components in a different order, // and it needs the offsets in the range 0-1. float[] matrix = Matrix.Values; _shaderMatrix = Matrix3D.Create(new float[] { matrix[0], matrix[1], matrix[2], matrix[3], matrix[5], matrix[6], matrix[7], matrix[8], matrix[10], matrix[11], matrix[12], matrix[13], matrix[15], matrix[16], matrix[17], matrix[18] }); _shaderOffset = new[] { matrix[4] / 255.0f, matrix[9] / 255.0f, matrix[14] / 255.0f, matrix[19] / 255.0f }; }
// 3D transformation /// <summary> /// Creates a matrix that represents the transformation from the local coordinate system /// to another. This method supports three dimensional objects created via 'Sprite3D'. /// </summary> public Matrix3D GetTransformationMatrix3D(DisplayObject targetSpace) { DisplayObject currentObject; Matrix3D outM = Matrix3D.Create(); if (targetSpace == this) { return(outM); } if (targetSpace == _parent || (targetSpace == null && _parent == null)) { outM.CopyFrom(TransformationMatrix3D); return(outM); } if (targetSpace == null || targetSpace == Base) { // targetCoordinateSpace 'null' represents the target space of the base object. // -> move up from this to base currentObject = this; while (currentObject != targetSpace) { outM.Append(currentObject.TransformationMatrix3D); currentObject = currentObject._parent; } return(outM); } if (targetSpace._parent == this) // optimization { outM = targetSpace.GetTransformationMatrix3D(this); outM.Invert(); return(outM); } // 1. find a common parent of this and the target space var commonParent = FindCommonParent(this, targetSpace); // 2. move up from this to common parent currentObject = this; while (currentObject != commonParent) { outM.Append(currentObject.TransformationMatrix3D); currentObject = currentObject._parent; } if (commonParent == targetSpace) { return(outM); } // 3. now move up from target until we reach the common parent var sHelperMatrix3D = Matrix3D.Create(); currentObject = targetSpace; while (currentObject != commonParent) { sHelperMatrix3D.Append(currentObject.TransformationMatrix3D); currentObject = currentObject._parent; } // 4. now combine the two matrices sHelperMatrix3D.Invert(); outM.Append(sHelperMatrix3D); return(outM); }
public ColorMatrixEffect() { Matrix = new ColorMatrix(); _shaderMatrix = Matrix3D.Create(); }