/// <summary> /// This methode stores all data related to this object into the given <see cref="ExportModelContainer" />. /// </summary> /// <param name="modelContainer">The target container.</param> /// <param name="exportOptions">Options for export.</param> protected override void PrepareForExportInternal( ExportModelContainer modelContainer, ExportOptions exportOptions) { modelContainer.EnsureNotNull(nameof(modelContainer)); exportOptions.EnsureNotNull(nameof(exportOptions)); // Get the device and ensure that we've an instance var exportDevice = exportOptions.ExportDevice; exportOptions.EnsureNotNull(nameof(exportDevice)); GeometryResource geometryResource = m_localResources[exportDevice.DeviceIndex]; if (geometryResource != null) { // Ensure that we have geometry infos for the exporter if (!modelContainer.ContainsExportGeometry(geometryResource.Key)) { modelContainer.AddExportGeometry(geometryResource.PrepareForExport()); foreach (MaterialResource actMaterial in geometryResource.GetReferencedMaterials()) { if (!modelContainer.ContainsExportMaterial(actMaterial.Key)) { } } } //base.UpdateAndApplyRenderParameters(renderState); //geometryResource.Render(renderState); } }
/// <summary> /// Creates the default resource for the given type name.. /// </summary> /// <typeparam name="T">The type for which the generic resource should be created.</typeparam> internal static T CreateDefaultResource <T>() where T : Resource { Type resourceType = typeof(T); T result = null; // Try to create default resources if (resourceType == typeof(MaterialResource)) { result = new SimpleColoredMaterialResource() as T; } else if (resourceType == typeof(TextureResource)) { #if DESKTOP result = new LinearGradientTextureResource( Color4.White, Color4.LightGray, GradientDirection.Directional, 32, 32) as T; #else result = new StandardTextureResource( new AssemblyResourceLink( typeof(ResourceDictionary), "SeeingSharp.Multimedia.Resources.Textures.Blank_16x16.png")) as T; #endif } else if (resourceType == typeof(GeometryResource)) { VertexStructure dummyStructure = new VertexStructure(); dummyStructure.FirstSurface.BuildCube24V( Vector3.Zero, new Vector3(1f, 1f, 1f), Color4.White); result = new GeometryResource(dummyStructure) as T; } //Try to create the resource using the standard constructor if (result == null) { #if DESKTOP ConstructorInfo standardConstructor = resourceType.GetConstructor( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); #else ConstructorInfo standardConstructor = resourceType.GetTypeInfo().DeclaredConstructors .FirstOrDefault((actConstructor) => actConstructor.GetParameters().Length <= 0); #endif if (standardConstructor != null) { result = Activator.CreateInstance(resourceType) as T; } } if (result == null) { throw new SeeingSharpGraphicsException("Unable to create default resource for resource type " + resourceType.FullName); } return(result); }
/// <summary> /// Copy the specified geometric data to an index and a vertex buffer. /// </summary> /// <param name="data">Data to load inside the GPU.</param> /// <param name="resource">Resource to return.</param> /// <returns>The updated resource with the new vertex/index values.</returns> private GeometryResource CopyToGPUBuffer(Shape.Descriptor data, GeometryResource resource) { int index = 0; // - Loads all processed vertexs foreach (PointDescriptor point in data.Points) { resource.Vertexs[index++] = new VertexData(new Vector3(point.Position), Color4.Black); } index = 0; // - Loads all processed indexes foreach (FaceDescriptor face in data.Faces) { HalfEdgeDescriptor firstEdge = data.Edges[face.FirstEdge]; HalfEdgeDescriptor edge = firstEdge; do { resource.Indexes[index++] = (int)(edge.Start + resource.Vertexs.Offset / VertexData.SizeInBytes); edge = data.Edges[edge.Next]; } while (edge.Start != face.FirstEdge); } return(resource); }
/// <summary> /// This method must implement the loading business in order to /// provide a rendering cached structure. /// </summary> /// <param name="data">Metadata and data for the loading process.</param> /// <returns>A result's descriptor.</returns> protected override GeometryResource InternalLoad(Shape.Descriptor data) { GeometryResource resource = new GeometryResource { Vertexs = VertexManager.Alloc((uint)data.Points.Length), Indexes = IndexManager.Alloc((uint)data.Edges.Length) }; switch (data.TesselationType) { case Shape.TesselationType.Auto: // - Manage automatic tessellation algorithm selection case Shape.TesselationType.CPU1EarCut: break; case Shape.TesselationType.CPU1Delaunay: break; case Shape.TesselationType.GPU1EarCut: break; case Shape.TesselationType.GPU1Delaunay: break; } return(CopyToGPUBuffer(data, resource)); }
/// <summary> /// Renders the object. /// </summary> /// <param name="renderState">Current render state.</param> private void OnRenderTransparent(RenderState renderState) { GeometryResource geometryResource = m_localResources[renderState.DeviceIndex]; if (geometryResource != null) { base.UpdateAndApplyRenderParameters(renderState); geometryResource.Render(renderState); } }
public void TestConstructCompressedStream() { foreach (var testData in Tests) { var compressedFileBytes = File.ReadAllBytes(testData.CompressedFilePath); using (var ms = new MemoryStream(compressedFileBytes)) { var resource = GeometryResource.Create(testData.RecordInfo.VersionHash); resource.InitFromStream(ms); } } }
/// <summary> /// Tries to get the bounding box for the given render-loop. /// Returns BoundingBox.Empty if it is not available. /// </summary> /// <param name="viewInfo">The ViewInformation for which to get the BoundingBox.</param> public override BoundingBox TryGetBoundingBox(ViewInformation viewInfo) { GeometryResource geometryResource = m_localResources[viewInfo.Device.DeviceIndex]; if ((geometryResource != null) && (geometryResource.IsLoaded)) { BoundingBox result = geometryResource.BoundingBox; result.Transform(this.Transform); return(result); } else { return(BoundingBox.Empty); } }
/// <summary> /// Are resources loaded for the given device? /// </summary> /// <param name="device">The device to check for.</param> public override bool IsLoaded(EngineDevice device) { if (!m_localResources.HasObjectAt(device.DeviceIndex)) { return(false); } GeometryResource geoResource = m_localResources[device.DeviceIndex]; if (geoResource.Key != m_resGeometryKey) { return(false); } return(true); }
/// <summary> /// Creates the default resource for the given type name.. /// </summary> /// <typeparam name="T">The type for which the generic resource should be created.</typeparam> internal static T CreateDefaultResource <T>() where T : Resource { var resourceType = typeof(T); T? result = null; // Try to create default resources if (resourceType == typeof(MaterialResource)) { result = new StandardMaterialResource() as T; } else if (resourceType == typeof(TextureResource)) { result = new StandardTextureResource( new AssemblyResourceLink( typeof(ResourceDictionary), "SeeingSharp.Resources.Textures.Blank_16x16.png")) as T; } else if (resourceType == typeof(GeometryResource)) { var dummyGeometry = new Geometry(); dummyGeometry.FirstSurface.BuildCube( Vector3.Zero, new Vector3(1f, 1f, 1f)) .SetVertexColor(Color4.White); result = new GeometryResource(dummyGeometry) as T; } //Try to create the resource using the standard constructor if (result == null) { var standardConstructor = resourceType.GetTypeInfo().DeclaredConstructors .FirstOrDefault(actConstructor => actConstructor.GetParameters().Length <= 0); if (standardConstructor != null) { result = Activator.CreateInstance(resourceType) as T; } } if (result == null) { throw new SeeingSharpGraphicsException("Unable to create default resource for resource type " + resourceType.FullName); } return(result); }
/// <summary> /// Tries to get the bounding sphere for the given render-loop. /// Returns BoundingSphere.Empty, if it is not available. /// </summary> /// <param name="viewInfo">The ViewInformation for which to get the BoundingSphere.</param> public override BoundingSphere TryGetBoundingSphere(ViewInformation viewInfo) { GeometryResource geometryResource = m_localResources[viewInfo.Device.DeviceIndex]; if ((geometryResource != null) && (geometryResource.IsLoaded)) { // Get BoundingBox object BoundingBox boundingBox = geometryResource.BoundingBox; // Calculate bounding sphare BoundingSphere result; BoundingSphere.FromBox(ref boundingBox, out result); result.Transform(this.Transform); return(result); } else { return(BoundingSphere.Empty); } }
/// <summary> /// Picks an object in 3D-World. /// </summary> /// <param name="rayStart">Start of picking ray.</param> /// <param name="rayDirection"></param> /// <param name="viewInfo">Information about the view that triggered picking.</param> /// <param name="pickingOptions">Some additional options for picking calculations.</param> /// <returns> /// Returns the distance to the object or float.NaN if object is not picked. /// </returns> internal override float Pick(Vector3 rayStart, Vector3 rayDirection, ViewInformation viewInfo, PickingOptions pickingOptions) { GeometryResource geometryResource = m_localResources[viewInfo.Device.DeviceIndex]; if ((geometryResource != null) && (geometryResource.IsLoaded)) { BoundingBox boundingBox = geometryResource.BoundingBox; if (!boundingBox.IsEmpty()) { // Transform picking ray to local space Ray pickingRay = new Ray(rayStart, rayDirection); Matrix4x4 temp; Matrix4x4.Invert(base.Transform, out temp); pickingRay.Transform(temp); // Check for intersection on the bounding box float distance = 0f; if (pickingRay.Intersects(ref boundingBox, out distance)) { if (pickingOptions.OnlyCheckBoundingBoxes) { return(distance); } // Perform picking on polygon level if (geometryResource.Intersects(pickingRay, pickingOptions, out distance)) { return(distance); } } } } return(float.NaN); }