///<summary> /// Internal implementation of <see cref="HardwareBuffer.Lock"/>. ///</summary> protected override PixelBox LockImpl(BasicBox lockBox, BufferLocking options) { _lockedBox = lockBox; // Set extents and format var rval = new PixelBox(lockBox, Format); var sizeInBytes = PixelUtil.GetMemorySize(lockBox.Width, lockBox.Height, lockBox.Depth, XnaHelper.Convert(surface.Format)); if (_bufferBytes == null || _bufferBytes.Length != sizeInBytes) { _bufferBytes = new byte[sizeInBytes]; #if !SILVERLIGHT if (surface != null) { surface.GetData(mipLevel, XnaHelper.ToRectangle(lockBox), _bufferBytes, 0, _bufferBytes.Length); } else if (cube != null) { cube.GetData(face, mipLevel, XnaHelper.ToRectangle(lockBox), _bufferBytes, 0, _bufferBytes.Length); } else { volume.GetData(mipLevel, lockBox.Left, lockBox.Top, lockBox.Right, lockBox.Bottom, lockBox.Front, lockBox.Back, _bufferBytes, 0, _bufferBytes.Length); } #endif } rval.Data = BufferBase.Wrap(_bufferBytes); return(rval); }
private static Texture2D GetTexture2D(GraphicsDevice graphicsDevice, TextureCube textureCube, CubeMapFace cubeMapFace) { // Unfortunately, we cannot treat the TextureCube as Texture2D[] in XNA. // We could try to copy all faces into a 3x2 Texture2D, but this is problematic: // - Extracting DXT compressed faces is difficult. // - Additional texel border required for correct texture filtering at edges. // + The skybox could be rendered with a single draw call. // // --> Manually convert TextureCube to Texture2D[6] and store array in Tag. var faces = textureCube.Tag as Texture2D[]; if (faces == null || faces.Length != 6) { if (textureCube.Tag != null) { throw new GraphicsException("The SkyboxRenderer (Reach profile) needs to store information in Tag property of the skybox texture, but the Tag property is already in use."); } faces = new Texture2D[6]; var size = textureCube.Size; int numberOfBytes; switch (textureCube.Format) { case SurfaceFormat.Color: numberOfBytes = size * size * 4; break; case SurfaceFormat.Dxt1: numberOfBytes = size * size / 2; break; default: throw new GraphicsException("The SkyboxRenderer (Reach profile) only supports the following surface formats: Color, Dxt1."); } var face = new byte[numberOfBytes]; for (int i = 0; i < 6; i++) { var texture2D = new Texture2D(graphicsDevice, size, size, false, textureCube.Format); textureCube.GetData((CubeMapFace)i, face); texture2D.SetData(face); faces[i] = texture2D; } textureCube.Tag = faces; textureCube.Disposing += OnTextureCubeDisposing; } return(faces[(int)cubeMapFace]); }
} // Lerp #endregion #region Generate Spherical Harmonic from CubeMap /// <summary> /// Generate a spherical harmonic from the faces of a cubemap, treating each pixel as a light source and averaging the result. /// </summary> public static SphericalHarmonicL2 GenerateSphericalHarmonicFromCubeMap(TextureCube cubeMap) { SphericalHarmonicL2 sh = new SphericalHarmonicL2(); // Extract the 6 faces of the cubemap. for (int face = 0; face < 6; face++) { CubeMapFace faceId = (CubeMapFace)face; // Get the transformation for this face, Matrix cubeFaceMatrix; switch (faceId) { case CubeMapFace.PositiveX: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(1, 0, 0), new Vector3(0, 1, 0)); break; case CubeMapFace.NegativeX: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(-1, 0, 0), new Vector3(0, 1, 0)); break; case CubeMapFace.PositiveY: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(0, 1, 0), new Vector3(0, 0, 1)); break; case CubeMapFace.NegativeY: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(0, -1, 0), new Vector3(0, 0, -1)); break; case CubeMapFace.PositiveZ: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(0, 0, -1), new Vector3(0, 1, 0)); break; case CubeMapFace.NegativeZ: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(0, 0, 1), new Vector3(0, 1, 0)); break; default: throw new ArgumentOutOfRangeException(); } Color[] colorArray = new Color[cubeMap.Size * cubeMap.Size]; cubeMap.GetData(faceId, colorArray); // Extract the spherical harmonic for this face and accumulate it. sh += ExtractSphericalHarmonicForCubeFace(cubeFaceMatrix, colorArray, cubeMap.Size); } //average out over the sphere return(sh.GetWeightedAverageLightInputFromSphere()); } // GenerateSphericalHarmonicFromCubeMap
public void ShouldSetAndGetData(int size) { var dataSize = size * size; var textureCube = new TextureCube(gd, size, false, SurfaceFormat.Color); for (var i = 0; i < 6; i++) { var savedData = new Color[dataSize]; for (var index = 0; index < dataSize; index++) { savedData[index] = new Color(index + i, index + i, index + i); } textureCube.SetData((CubeMapFace)i, savedData); var readData = new Color[dataSize]; textureCube.GetData((CubeMapFace)i, readData); Assert.AreEqual(savedData, readData); } textureCube.Dispose(); }
public SkySphere(ContentManager content, GraphicsDevice graphcisDevice, TextureCube Texture, float scale) { //model = new Object(content.Load<Model>("skysphere_mesh"), Vector3.Zero, Vector3.Zero, new Vector3(100000),GraphicsDevice); /*model = new Object(new Vector3(1000), "Models\\SkySphereMesh"); model.Scale = 0.5f;*/ model = new Object(new Vector3(0, 0, 0), "Models\\SphereHighPoly");// "Models\\SkySphereMesh");//Model Model, Vector3 Position, Vector3 Rotation,Vector3 Scale, //model.Scale = 1000; //model.Scale = 10000; model.Scale = scale; TextureCube = Texture; Color[] c = new Color[TextureCube.Size * TextureCube.Size]; TextureCube.GetData<Color>(CubeMapFace.PositiveZ, c); effect = content.Load<Effect>("SkySphereEffect"); effect.Parameters["CubeMap"].SetValue(Texture); //effect = content.Load<Effect>("SkySphere"); //effect.Parameters["SkyboxTexture"].SetValue(Texture); model.SetModelEffect(effect, false); this.graphics = graphcisDevice; }
public void ShouldSetAndGetData(int size) { Game.DrawWith += (sender, e) => { var dataSize = size * size; var textureCube = new TextureCube(Game.GraphicsDevice, size, false, SurfaceFormat.Color); for (var i = 0; i < 6; i++) { var savedData = new Color[dataSize]; for (var index = 0; index < dataSize; index++) savedData[index] = new Color(index + i, index + i, index + i); textureCube.SetData((CubeMapFace) i, savedData); var readData = new Color[dataSize]; textureCube.GetData((CubeMapFace) i, readData); Assert.AreEqual(savedData, readData); } }; Game.Run(); }
public void ShouldSetAndGetData(int size) { Game.DrawWith += (sender, e) => { var dataSize = size * size; var textureCube = new TextureCube(Game.GraphicsDevice, size, false, SurfaceFormat.Color); for (var i = 0; i < 6; i++) { var savedData = new Color[dataSize]; for (var index = 0; index < dataSize; index++) { savedData[index] = new Color(index + i, index + i, index + i); } textureCube.SetData((CubeMapFace)i, savedData); var readData = new Color[dataSize]; textureCube.GetData((CubeMapFace)i, readData); Assert.AreEqual(savedData, readData); } }; Game.Run(); }
private static Texture2D GetTexture2D(GraphicsDevice graphicsDevice, TextureCube textureCube, CubeMapFace cubeMapFace) { // Unfortunately, we cannot treat the TextureCube as Texture2D[] in XNA. // We could try to copy all faces into a 3x2 Texture2D, but this is problematic: // - Extracting DXT compressed faces is difficult. // - Additional texel border required for correct texture filtering at edges. // + The skybox could be rendered with a single draw call. // // --> Manually convert TextureCube to Texture2D[6] and store array in Tag. var faces = textureCube.Tag as Texture2D[]; if (faces == null || faces.Length != 6) { if (textureCube.Tag != null) throw new GraphicsException("The SkyboxRenderer (Reach profile) needs to store information in Tag property of the skybox texture, but the Tag property is already in use."); faces = new Texture2D[6]; var size = textureCube.Size; int numberOfBytes; switch (textureCube.Format) { case SurfaceFormat.Color: numberOfBytes = size * size * 4; break; case SurfaceFormat.Dxt1: numberOfBytes = size * size / 2; break; default: throw new GraphicsException("The SkyboxRenderer (Reach profile) only supports the following surface formats: Color, Dxt1."); } var face = new byte[numberOfBytes]; for (int i = 0; i < 6; i++) { var texture2D = new Texture2D(graphicsDevice, size, size, false, textureCube.Format); textureCube.GetData((CubeMapFace)i, face); texture2D.SetData(face); faces[i] = texture2D; } textureCube.Tag = faces; textureCube.Disposing += OnTextureCubeDisposing; } return faces[(int)cubeMapFace]; }
} // Lerp #endregion #region Generate Spherical Harmonic from CubeMap /// <summary> /// Generate a spherical harmonic from the faces of a cubemap, treating each pixel as a light source and averaging the result. /// </summary> public static SphericalHarmonicL2 GenerateSphericalHarmonicFromCubeMap(TextureCube cubeMap) { SphericalHarmonicL2 sh = new SphericalHarmonicL2(); // Extract the 6 faces of the cubemap. for (int face = 0; face < 6; face++) { CubeMapFace faceId = (CubeMapFace)face; // Get the transformation for this face, Matrix cubeFaceMatrix; switch (faceId) { case CubeMapFace.PositiveX: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(1, 0, 0), new Vector3(0, 1, 0)); break; case CubeMapFace.NegativeX: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(-1, 0, 0), new Vector3(0, 1, 0)); break; case CubeMapFace.PositiveY: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(0, 1, 0), new Vector3(0, 0, 1)); break; case CubeMapFace.NegativeY: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(0, -1, 0), new Vector3(0, 0, -1)); break; case CubeMapFace.PositiveZ: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(0, 0, -1), new Vector3(0, 1, 0)); break; case CubeMapFace.NegativeZ: cubeFaceMatrix = Matrix.CreateLookAt(Vector3.Zero, new Vector3(0, 0, 1), new Vector3(0, 1, 0)); break; default: throw new ArgumentOutOfRangeException(); } Color[] colorArray = new Color[cubeMap.Size * cubeMap.Size]; cubeMap.GetData(faceId, colorArray); // Extract the spherical harmonic for this face and accumulate it. sh += ExtractSphericalHarmonicForCubeFace(cubeFaceMatrix, colorArray, cubeMap.Size); } //average out over the sphere return sh.GetWeightedAverageLightInputFromSphere(); } // GenerateSphericalHarmonicFromCubeMap