// TODO: Abstract Resource Crreation for Uniforms, Vertex Layouts, Disposing override protected void CreateResources() { RgbaFloat lightColor = RgbaFloat.LightGrey; var lightLookAt = new Vector4(0, 0, 0, 1); var lightCam = new OrthographicCamera(35, 35, Light.DEFAULT_POSITION, lightLookAt); _sceneRuntimeState.Light = new Light(lightCam, lightColor, 0.1f); // string filePath = Path.Combine(AppContext.BaseDirectory, "Models/sphere.obj"); //string filePath = Path.Combine(AppContext.BaseDirectory, "Models/300_polygon_sphere_100mm.STL"); // string filePath = "Models/sphere_centered.obj"; string filePath = "Models/chinesedragon.dae"; // string filePath = "Models/Box.dae"; _model = AssimpLoader.LoadFromFileWithRealtimeMaterial <VertexPositionNormal>(AppContext.BaseDirectory, filePath, VertexPositionNormal.HenzaiType); //GeometryUtils.GenerateSphericalTextureCoordinatesFor(_model.meshes[0]); /// Uniform 1 - Camera _cameraProjViewBuffer = _factory.CreateBuffer(new BufferDescription(Camera.SizeInBytes, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var resourceLayoutElementDescription = new ResourceLayoutElementDescription("projViewWorld", ResourceKind.UniformBuffer, ShaderStages.Vertex); ResourceLayoutElementDescription[] resourceLayoutElementDescriptions = { resourceLayoutElementDescription }; var resourceLayoutDescription = new ResourceLayoutDescription(resourceLayoutElementDescriptions); BindableResource[] bindableResources = new BindableResource[] { _cameraProjViewBuffer }; _cameraResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescription); var resourceSetDescription = new ResourceSetDescription(_cameraResourceLayout, bindableResources); _cameraResourceSet = _factory.CreateResourceSet(resourceSetDescription); // Uniform 2 - Material _materialBuffer = _factory.CreateBuffer(new BufferDescription(RealtimeMaterial.SizeInBytes, BufferUsage.UniformBuffer)); var resourceLayoutElementDescriptionMaterial = new ResourceLayoutElementDescription("material", ResourceKind.UniformBuffer, ShaderStages.Fragment); ResourceLayoutElementDescription[] resourceLayoutElementDescriptionsMaterial = { resourceLayoutElementDescriptionMaterial }; var resourceLayoutDescriptionMaterial = new ResourceLayoutDescription(resourceLayoutElementDescriptionsMaterial); BindableResource[] bindableResourcesMaterial = new BindableResource[] { _materialBuffer }; _materialResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescriptionMaterial); var resourceSetDescriptionMaterial = new ResourceSetDescription(_materialResourceLayout, bindableResourcesMaterial); _materialResourceSet = _factory.CreateResourceSet(resourceSetDescriptionMaterial); // Uniform 3 - Light _lightBuffer = _factory.CreateBuffer(new BufferDescription(Light.SizeInBytes, BufferUsage.UniformBuffer)); var resourceLayoutElementDescriptionLight = new ResourceLayoutElementDescription("light", ResourceKind.UniformBuffer, ShaderStages.Fragment); ResourceLayoutElementDescription[] resourceLayoutElementDescriptionsLight = { resourceLayoutElementDescriptionLight }; var resourceLayoutDescriptionLight = new ResourceLayoutDescription(resourceLayoutElementDescriptionsLight); BindableResource[] bindableResourcesLight = new BindableResource[] { _lightBuffer }; _lightResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescriptionLight); var resourceSetDescriptionLight = new ResourceSetDescription(_lightResourceLayout, bindableResourcesLight); _lightResourceSet = _factory.CreateResourceSet(resourceSetDescriptionLight); for (int i = 0; i < _model.MeshCount; i++) { var mesh = _model.GetMesh(i); DeviceBuffer vertexBuffer = _factory.CreateBuffer(new BufferDescription(mesh.Vertices.LengthUnsigned() * VertexPositionNormal.SizeInBytes, BufferUsage.VertexBuffer)); DeviceBuffer indexBuffer = _factory.CreateBuffer(new BufferDescription(mesh.Indices.LengthUnsigned() * sizeof(ushort), BufferUsage.IndexBuffer)); _vertexBuffers.Add(vertexBuffer); _indexBuffers.Add(indexBuffer); GraphicsDevice.UpdateBuffer(vertexBuffer, 0, mesh.Vertices); GraphicsDevice.UpdateBuffer(indexBuffer, 0, mesh.Indices); } VertexLayoutDescription vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3) //new VertexElementDescription("UV",VertexElementSemantic.TextureCoordinate,VertexElementFormat.Float2) ); _vertexShader = IO.LoadShader("Phong", ShaderStages.Vertex, GraphicsDevice); _fragmentShader = IO.LoadShader("Phong", ShaderStages.Fragment, GraphicsDevice); GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription() { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, // Wireframe doesnt seem to work with metal frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false ), PrimitiveTopology = PrimitiveTopology.TriangleList, //ResourceLayouts = new ResourceLayout[] {_cameraResourceLayout,_materialResourceLayout,_lightResourceLayout}, ResourceLayouts = new ResourceLayout[] { _cameraResourceLayout, _lightResourceLayout, _materialResourceLayout }, ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, shaders: new Shader[] { _vertexShader, _fragmentShader } ), Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription }; _pipeline = _factory.CreateGraphicsPipeline(pipelineDescription); }
override protected void CreateResources() { RgbaFloat lightColor = RgbaFloat.White; // RgbaFloat lightColor = RgbaFloat.LightGrey; //var lightPos = new Vector4(-0.5f,0.1f,10,1); var lightPos = new Vector4(0.0f, 0.0f, 0, 1); var lookAt = new Vector4(0, 0, 0, 1) - new Vector4(0, 1, 0, 1); //var lightCam = new OrthographicCamera(50.0f, 50.0f, lightPos, lookAt); //_sceneRuntimeState.Light = new Light(lightCam,lightColor,0.1f); _sceneRuntimeState.Camera = Camera; _sceneRuntimeState.SpotLight = Light.NO_POINTLIGHT; var omniCameras = CubeMap.GenerateOmniCameras(lightPos, 1024, 1024); _sceneRuntimeState.OmniLights = Light.GenerateOmniLights(omniCameras, lightColor, 0.1f); // string filePath = Path.Combine(AppContext.BaseDirectory, "armor/armor.dae"); // string filePath = Path.Combine(AppContext.BaseDirectory, "nanosuit/nanosuit.obj"); var scale = Matrix4x4.CreateScale(0.05f, 0.05f, 0.05f); //var scale = Matrix4x4.CreateScale(1.00f,1.00f,1.00f); string filePath = "models/chinesedragon.dae"; // string filePath = "Models/Box.dae"; var model = AssimpLoader.LoadFromFileWithRealtimeMaterial <VertexPositionNormal>(AppContext.BaseDirectory, filePath, VertexPositionNormal.HenzaiType); var newModelTranslation = Matrix4x4.CreateTranslation(new Vector3(0, 20, 0)); var modelRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormal>(model, "PhongOmni", "PhongOmni", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.OMNI_SHADOW_MAPS), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); modelRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; model.SetNewWorldTransformation(ref newModelTranslation, true); //TODO: Write method to remove ambient terms var sponzaModels = AssimpLoader.LoadRealtimeModelsFromFile(AppContext.BaseDirectory, "sponza/sponza.obj"); var sponzaPNTTB = sponzaModels.modelPNTTB; var sponzaPC = sponzaModels.modelPC; for (int i = 0; i < sponzaPNTTB.MaterialCount; i++) { sponzaPNTTB.GetMaterial(i).ambient = new Vector4(0.3f, 0.3f, 0.3f, 1.0f); } for (int i = 0; i < sponzaPC.MaterialCount; i++) { sponzaPC.GetMaterial(i).ambient = new Vector4(0.3f, 0.3f, 0.3f, 1.0f); } sponzaPNTTB.SetNewWorldTransformation(ref scale, true); sponzaPC.SetNewWorldTransformation(ref scale, true); var sponzaRuntimeState //TOOD: Omni Fragment shader produces a runtime error = new ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent>(sponzaPNTTB, "PhongBitangentTextureOmni", "PhongBitangentTextureOmni", VertexRuntimeTypes.VertexPositionNormalTextureTangentBitangent, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.OMNI_SHADOW_MAPS), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sponzaRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPNTTB; sponzaRuntimeState.CallSamplerGeneration += ResourceGenerator.GenerateTriLinearSampler; sponzaRuntimeState.CallTextureResourceLayoutGeneration += ResourceGenerator.GenerateTextureResourceLayoutForNormalMapping; sponzaRuntimeState.CallTextureResourceSetGeneration += ResourceGenerator.GenerateTextureResourceSetForNormalMapping; var sponzaRuntimeStateColorOnly = new ModelRuntimeDescriptor <VertexPositionColor>(sponzaPC, "Color", "Color", VertexRuntimeTypes.VertexPositionColor, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.OMNI_SHADOW_MAPS), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sponzaRuntimeStateColorOnly.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPC; /// var floor = new Model <VertexPositionNormalTextureTangentBitangent, RealtimeMaterial>("paving/", GeometryFactory.GenerateQuadPNTTB_XY(), new RealtimeMaterial()); var floorMeshZero = floor.GetMesh(0); var flootMaterialZero = floor.GetMaterial(0); flootMaterialZero.textureDiffuse = "pavingColor.jpg"; flootMaterialZero.textureNormal = "pavingNorm.jpg"; flootMaterialZero.ambient = new Vector4(0.3f, 0.3f, 0.3f, 1.0f); var floorTranslation = Matrix4x4.CreateTranslation(-6, 0, -7); var floorScale = Matrix4x4.CreateScale(100.0f, 100.0f, 1); var newTrans = Matrix4x4.Multiply(floorScale, floorTranslation); floor.SetNewWorldTransformation(ref floorTranslation, true); var floorRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent>(floor, "PhongBitangentTextureOmni", "PhongBitangentTextureOmni", VertexRuntimeTypes.VertexPositionNormalTextureTangentBitangent, PrimitiveTopology.TriangleStrip, new RenderDescription(RenderFlags.NORMAL | RenderFlags.OMNI_SHADOW_MAPS), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); floorRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPNTTB; floorRuntimeState.CallTextureResourceLayoutGeneration += ResourceGenerator.GenerateTextureResourceLayoutForNormalMapping; floorRuntimeState.CallTextureResourceSetGeneration += ResourceGenerator.GenerateTextureResourceSetForNormalMapping; floorRuntimeState.CallSamplerGeneration += ResourceGenerator.GenerateTriLinearSampler; ///// var floor2 = new Model <VertexPositionNormalTextureTangentBitangent, RealtimeMaterial>("paving/", GeometryFactory.GenerateQuadPNTTB_XZ(), new RealtimeMaterial()); var floorMeshZero2 = floor2.GetMesh(0); var flootMaterialZero2 = floor2.GetMaterial(0); flootMaterialZero2.textureDiffuse = "pavingColor.jpg"; flootMaterialZero2.textureNormal = "pavingNorm.jpg"; flootMaterialZero2.ambient = new Vector4(0.3f, 0.3f, 0.3f, 1.0f); var floorTranslation2 = Matrix4x4.CreateTranslation(0, -5, 0); var floorScale2 = Matrix4x4.CreateScale(100.0f, 1.0f, 100.0f); var newTrans2 = Matrix4x4.Multiply(floorScale2, floorTranslation2); floor2.SetNewWorldTransformation(ref newTrans2, true); var floorRuntimeState2 = new ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent>(floor2, "PhongBitangentTextureOmni", "PhongBitangentTextureOmni", VertexRuntimeTypes.VertexPositionNormalTextureTangentBitangent, PrimitiveTopology.TriangleStrip, new RenderDescription(RenderFlags.NORMAL | RenderFlags.OMNI_SHADOW_MAPS), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); floorRuntimeState2.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPNTTB; floorRuntimeState2.CallTextureResourceLayoutGeneration += ResourceGenerator.GenerateTextureResourceLayoutForNormalMapping; floorRuntimeState2.CallTextureResourceSetGeneration += ResourceGenerator.GenerateTextureResourceSetForNormalMapping; floorRuntimeState2.CallSamplerGeneration += ResourceGenerator.GenerateTriLinearSampler; // var skyBox = new Model <VertexPosition, RealtimeMaterial>("cloudtop", GeometryFactory.GenerateCube(true), new RealtimeMaterial()); var skyBoxMaterial = skyBox.GetMaterial(0); skyBoxMaterial.AssignCubemapPaths("cloudtop_ft.png", "cloudtop_bk.png", "cloudtop_lf.png", "cloudtop_rt.png", "cloudtop_up.png", "cloudtop_dn.png"); _skyBoxRuntimeState = new ModelRuntimeDescriptor <VertexPosition>(skyBox, "Skybox", "Skybox", VertexRuntimeTypes.VertexPosition, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); _skyBoxRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForP; _skyBoxRuntimeState.CallSamplerGeneration += ResourceGenerator.GenerateBiLinearSampler; _skyBoxRuntimeState.CallTextureResourceLayoutGeneration += ResourceGenerator.GenerateTextureResourceLayoutForCubeMapping; _skyBoxRuntimeState.CallTextureResourceSetGeneration += ResourceGenerator.GenerateTextureResourceSetForCubeMapping; _sun = new Model <VertexPositionNormal, RealtimeMaterial>(string.Empty, GeometryFactory.GenerateSphereNormal(100, 100, 0.3f), new RealtimeMaterial()); _sun.GetMaterial(0).ambient = lightColor.ToVector4(); Vector3 newTranslation = new Vector3(lightPos.X, lightPos.Y, lightPos.Z); _sun.SetNewWorldTranslation(ref newTranslation, true); var sunRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormal>(_sun, "PhongNoShadow", "PhongNoShadow", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sunRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; var _sun2 = new Model <VertexPositionNormal, RealtimeMaterial>(string.Empty, GeometryFactory.GenerateSphereNormal(100, 100, 0.5f), new RealtimeMaterial()); _sun2.GetMaterial(0).ambient = lightColor.ToVector4(); Vector3 newTranslation2 = new Vector3(5, -2, 1); _sun2.SetNewWorldTranslation(ref newTranslation2, true); var sunRuntimeState2 = new ModelRuntimeDescriptor <VertexPositionNormal>(_sun2, "PhongOmni", "PhongOmni", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.OMNI_SHADOW_MAPS), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sunRuntimeState2.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; var _sun3 = new Model <VertexPositionNormal, RealtimeMaterial>(string.Empty, GeometryFactory.GenerateSphereNormal(100, 100, 0.5f), new RealtimeMaterial()); _sun3.GetMaterial(0).ambient = lightColor.ToVector4(); Vector3 newTranslation3 = new Vector3(-5, 0, -3.0f); _sun3.SetNewWorldTranslation(ref newTranslation3, true); var sunRuntimeState3 = new ModelRuntimeDescriptor <VertexPositionNormal>(_sun3, "PhongOmni", "PhongOmni", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.OMNI_SHADOW_MAPS), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sunRuntimeState3.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; var _sun4 = new Model <VertexPositionNormal, RealtimeMaterial>(string.Empty, GeometryFactory.GenerateSphereNormal(100, 100, 0.2f), new RealtimeMaterial()); _sun4.GetMaterial(0).ambient = lightColor.ToVector4(); Vector3 newTranslation4 = new Vector3(0, -2, 0.0f); _sun4.SetNewWorldTranslation(ref newTranslation4, true); var sunRuntimeState4 = new ModelRuntimeDescriptor <VertexPositionNormal>(_sun4, "PhongOmni", "PhongOmni", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.OMNI_SHADOW_MAPS), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sunRuntimeState4.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; //TODO: Automate this //_modelPNTTBDescriptorList.Add(sponzaRuntimeState); _modelPNTTBDescriptorList.Add(floorRuntimeState); //Rendering this seems to crate aretfacts on the other floor _modelPNTTBDescriptorList.Add(floorRuntimeState2); //_modelPCDescriptorList.Add(sponzaRuntimeStateColorOnly); //_modelPDescriptorList.Add(_skyBoxRuntimeState); _modelPNDescriptorList.Add(sunRuntimeState3); _modelPNDescriptorList.Add(sunRuntimeState); _modelPNDescriptorList.Add(sunRuntimeState2); _modelPNDescriptorList.Add(sunRuntimeState4); //_modelPNDescriptorList.Add(modelRuntimeState); InstanceData[] instancingData = { InstanceData.NO_DATA }; //TODO: Abstrct this foreach (var modelDescriptor in _modelPNTTBDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PNTTBRuntimeGeometry.AddModel(modelDescriptor); } foreach (var modelDescriptor in _modelPNDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PNRuntimeGeometry.AddModel(modelDescriptor); } foreach (var modelDescriptor in _modelPTDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PTRuntimeGeometry.AddModel(modelDescriptor); } foreach (var modelDescriptor in _modelPCDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PCRuntimeGeometry.AddModel(modelDescriptor); } foreach (var modelDescriptor in _modelPDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PRuntimeGeometry.AddModel(modelDescriptor); } }
override protected void CreateResources() { // RgbaFloat lightColor = RgbaFloat.Orange; RgbaFloat lightColor = RgbaFloat.LightGrey; var lightPos = new Vector4(10, 20, 0, 1); var meshPos = new Vector4(0, 0, 0, 1); var lookAt = meshPos - lightPos; //TODO: Position seems to be buggy var lightCam = new OrthographicCamera(50, 50, lightPos, lookAt); _sceneRuntimeState.Light = new Light(lightCam, lightColor, 0.1f); _sceneRuntimeState.Camera = Camera; _sceneRuntimeState.SpotLight = Light.NO_POINTLIGHT; // string filePath = Path.Combine(AppContext.BaseDirectory, "armor/armor.dae"); // string filePath = Path.Combine(AppContext.BaseDirectory, "nanosuit/nanosuit.obj"); _nanosuit = AssimpLoader.LoadFromFileWithRealtimeMaterial <VertexPositionNormalTextureTangentBitangent>(AppContext.BaseDirectory, "nanosuit/nanosuit.obj", VertexPositionNormalTextureTangentBitangent.HenzaiType); // _nanosuit.SetAmbientForAllMeshes(new Vector4(0.1f,0.1f,0.1f,1.0f)); // _model = AssimpLoader.LoadFromFile<VertexPositionNormalTextureTangentBitangent>(AppContext.BaseDirectory,"sponza/sponza.obj",VertexPositionNormalTextureTangentBitangent.HenzaiType); GeometryUtils.GenerateTangentAndBitagentSpaceFor(_nanosuit); // GeometryUtils.CheckTBN(_model); // var sun = new Model<VertexPositionNormalTextureTangentBitangent>("water",GeometryFactory.generateSphereTangentBitangent(100,100,1)); _sun = new Model <VertexPositionNormal, RealtimeMaterial>(string.Empty, GeometryFactory.GenerateSphereNormal(100, 100, 1), new RealtimeMaterial()); var meshZero = _sun.GetMesh(0); var materialZero = _sun.GetMaterial(0); materialZero.textureDiffuse = "Water.jpg"; materialZero.textureNormal = "WaterNorm.jpg"; materialZero.ambient = lightColor.ToVector4(); // _sun.meshes[0].TryGetMaterial().ambient = lightColor.ToVector4(); Vector3 newTranslation = new Vector3(lightPos.X, lightPos.Y, lightPos.Z); _sun.SetNewWorldTranslation(ref newTranslation, true); var nanoSuitRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent>(_nanosuit, "PhongBitangentTexture", "PhongBitangentTexture", VertexRuntimeTypes.VertexPositionNormalTextureTangentBitangent, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.SHADOW_MAP), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); nanoSuitRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPNTTB; nanoSuitRuntimeState.CallSamplerGeneration += ResourceGenerator.GenerateTriLinearSampler; nanoSuitRuntimeState.CallTextureResourceLayoutGeneration += ResourceGenerator.GenerateTextureResourceLayoutForNormalMapping; nanoSuitRuntimeState.CallTextureResourceSetGeneration += ResourceGenerator.GenerateTextureResourceSetForNormalMapping; // var sunRuntimeState = new ModelRuntimeState<VertexPositionNormalTextureTangentBitangent>(sun,"PhongBitangentTexture","PhongBitangentTexture"); // sunRuntimeState.CallVertexLayoutGeneration+=ResourceGenerator.GenerateVertexLayoutForPNTTB; // sunRuntimeState.CallSamplerGeneration+=ResourceGenerator.GenerateLinearSampler; // sunRuntimeState.CallTextureResourceLayoutGeneration+=ResourceGenerator.GenerateTextureResourceLayoutForNormalMapping; // sunRuntimeState.CallTextureResourceSetGeneration+=ResourceGenerator.GenerateTextureResourceSetForNormalMapping; // _modelStatesList.Add(sunRuntimeState); var sunRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormal>(_sun, "Phong", "PhongNoShadow", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sunRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; var plane = new Model <VertexPositionNormal, RealtimeMaterial>(String.Empty, GeometryFactory.GenerateQuadPN_XZ(), new RealtimeMaterial()); var newPlaneTranslation = Matrix4x4.CreateTranslation(new Vector3(0, 0, 0)); var newPlaneScale = Matrix4x4.CreateScale(new Vector3(100, 1, 100)); var trafo = newPlaneScale * newPlaneTranslation; plane.SetNewWorldTransformation(ref trafo, true); var planeRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormal>(plane, "Phong", "Phong", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleStrip, new RenderDescription(RenderFlags.NORMAL | RenderFlags.SHADOW_MAP), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); planeRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; _modelPNTTBDescriptorList.Add(nanoSuitRuntimeState); _modelPNDescriptorList.Add(planeRuntimeState); _modelPNDescriptorList.Add(sunRuntimeState); InstanceData[] instancingData = { InstanceData.NO_DATA }; foreach (var modelDescriptor in _modelPNTTBDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PNTTBRuntimeGeometry.AddModel(modelDescriptor); } foreach (var modelDescriptor in _modelPNDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PNRuntimeGeometry.AddModel(modelDescriptor); } }
// TODO: Abstract Resource Crreation for Uniforms, Vertex Layouts, Disposing override protected void CreateResources() { //string filePath = Path.Combine(AppContext.BaseDirectory, "Models/sphere.obj"); // huge //string filePath = Path.Combine(AppContext.BaseDirectory, "Models/sphere_centered.obj"); // no texture coordiantes string filePath = "armor/armor.dae"; _model = AssimpLoader.LoadFromFileWithRealtimeMaterial <VertexPositionNormalTextureTangent>(AppContext.BaseDirectory, filePath, VertexPositionNormalTextureTangent.HenzaiType); //GeometryUtils.GenerateSphericalTextureCoordinatesFor(_model.meshes[0], UVMappingTypes.Spherical_Coordinates,true); GeometryUtils.GenerateTangentSpaceFor(_model); // _model = new Model<VertexPositionNormalTextureTangent>(GeometryFactory.generateSphere(100,100,1.0f)); /// Uniform 1 - Camera _cameraProjViewBuffer = _factory.CreateBuffer(new BufferDescription(192, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var resourceLayoutElementDescription = new ResourceLayoutElementDescription("projViewWorld", ResourceKind.UniformBuffer, ShaderStages.Vertex); ResourceLayoutElementDescription[] resourceLayoutElementDescriptions = { resourceLayoutElementDescription }; var resourceLayoutDescription = new ResourceLayoutDescription(resourceLayoutElementDescriptions); BindableResource[] bindableResources = new BindableResource[] { _cameraProjViewBuffer }; _cameraResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescription); var resourceSetDescription = new ResourceSetDescription(_cameraResourceLayout, bindableResources); _cameraResourceSet = _factory.CreateResourceSet(resourceSetDescription); // Uniform 2 - Material _materialBuffer = _factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); var resourceLayoutElementDescriptionMaterial = new ResourceLayoutElementDescription("material", ResourceKind.UniformBuffer, ShaderStages.Fragment); ResourceLayoutElementDescription[] resourceLayoutElementDescriptionsMaterial = { resourceLayoutElementDescriptionMaterial }; var resourceLayoutDescriptionMaterial = new ResourceLayoutDescription(resourceLayoutElementDescriptionsMaterial); BindableResource[] bindableResourcesMaterial = new BindableResource[] { _materialBuffer }; _materialResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescriptionMaterial); var resourceSetDescriptionMaterial = new ResourceSetDescription(_materialResourceLayout, bindableResourcesMaterial); _materialResourceSet = _factory.CreateResourceSet(resourceSetDescriptionMaterial); // Uniform 3 - Light _lightBuffer = _factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); var resourceLayoutElementDescriptionLight = new ResourceLayoutElementDescription("light", ResourceKind.UniformBuffer, ShaderStages.Vertex); ResourceLayoutElementDescription[] resourceLayoutElementDescriptionsLight = { resourceLayoutElementDescriptionLight }; var resourceLayoutDescriptionLight = new ResourceLayoutDescription(resourceLayoutElementDescriptionsLight); BindableResource[] bindableResourcesLight = new BindableResource[] { _lightBuffer }; _lightResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescriptionLight); var resourceSetDescriptionLight = new ResourceSetDescription(_lightResourceLayout, bindableResourcesLight); _lightResourceSet = _factory.CreateResourceSet(resourceSetDescriptionLight); for (int i = 0; i < _model.MeshCount; i++) { var mesh = _model.GetMesh(i); DeviceBuffer vertexBuffer = _factory.CreateBuffer(new BufferDescription(mesh.Vertices.LengthUnsigned() * VertexPositionNormalTextureTangent.SizeInBytes, BufferUsage.VertexBuffer)); DeviceBuffer indexBuffer = _factory.CreateBuffer(new BufferDescription(mesh.Indices.LengthUnsigned() * sizeof(ushort), BufferUsage.IndexBuffer)); _vertexBuffers.Add(vertexBuffer); _indexBuffers.Add(indexBuffer); GraphicsDevice.UpdateBuffer(vertexBuffer, 0, mesh.Vertices); GraphicsDevice.UpdateBuffer(indexBuffer, 0, mesh.Indices); } //Texture Samper // ImageSharpTexture diffuseTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "Textures", "earth.jpg")); // ImageSharpTexture diffuseTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "Textures", "Water.jpg")); ImageSharpTexture diffuseTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "armor", "diffuse.png")); Texture sphereDiffuseTexture = diffuseTexture.CreateDeviceTexture(GraphicsDevice, _factory); TextureView sphereDiffuseTextureView = _factory.CreateTextureView(sphereDiffuseTexture); // ImageSharpTexture normalTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "Textures", "WaterNorm.jpg")); ImageSharpTexture normalTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "armor", "normal.png")); Texture sphereNormalTexture = normalTexture.CreateDeviceTexture(GraphicsDevice, _factory); TextureView sphereNormalTextureView = _factory.CreateTextureView(sphereNormalTexture); ResourceLayout textureLayout = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("DiffuseTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("DiffuseSampler", ResourceKind.Sampler, ShaderStages.Fragment), new ResourceLayoutElementDescription("NormTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("NormSampler", ResourceKind.Sampler, ShaderStages.Fragment) )); var sampler = _factory.CreateSampler(new SamplerDescription { AddressModeU = SamplerAddressMode.Wrap, AddressModeV = SamplerAddressMode.Wrap, AddressModeW = SamplerAddressMode.Wrap, Filter = SamplerFilter.MinLinear_MagLinear_MipLinear, LodBias = 0, MinimumLod = 0, MaximumLod = uint.MaxValue, MaximumAnisotropy = 0, }); _textureResourceSet = _factory.CreateResourceSet(new ResourceSetDescription( textureLayout, sphereDiffuseTextureView, GraphicsDevice.LinearSampler, sphereNormalTextureView, sampler )); VertexLayoutDescription vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3), new VertexElementDescription("UV", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("Tangent", VertexElementSemantic.Normal, VertexElementFormat.Float3) ); _vertexShader = IO.LoadShader("PhongTexture", ShaderStages.Vertex, GraphicsDevice); _fragmentShader = IO.LoadShader("PhongTexture", ShaderStages.Fragment, GraphicsDevice); GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription() { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false ), PrimitiveTopology = PrimitiveTopology.TriangleList, ResourceLayouts = new ResourceLayout[] { _cameraResourceLayout, _lightResourceLayout, _materialResourceLayout, textureLayout }, ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, shaders: new Shader[] { _vertexShader, _fragmentShader } ), Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription }; _pipeline = _factory.CreateGraphicsPipeline(pipelineDescription); }
override protected void CreateResources() { RgbaFloat lightColor = RgbaFloat.White; // RgbaFloat lightColor = RgbaFloat.LightGrey; var lightPos = new Vector4(0, 100, 0, 1); var lookAt = new Vector4(0, 0, 0, 1) - new Vector4(0, 50, 0, 1); var lightCam = new OrthographicCamera(50.0f, 50.0f, lightPos, lookAt); _sceneRuntimeState.Light = new Light(lightCam, lightColor, 0.1f); _sceneRuntimeState.Camera = Camera; _sceneRuntimeState.SpotLight = Light.NO_POINTLIGHT; // string filePath = Path.Combine(AppContext.BaseDirectory, "armor/armor.dae"); // string filePath = Path.Combine(AppContext.BaseDirectory, "nanosuit/nanosuit.obj"); var scale = Matrix4x4.CreateScale(0.05f, 0.05f, 0.05f); //var scale = Matrix4x4.CreateScale(1.00f,1.00f,1.00f); string filePath = "models/chinesedragon.dae"; // string filePath = "Models/Box.dae"; var model = AssimpLoader.LoadFromFileWithRealtimeMaterial <VertexPositionNormal>(AppContext.BaseDirectory, filePath, VertexPositionNormal.HenzaiType); var newModelTranslation = Matrix4x4.CreateTranslation(new Vector3(0, 20, 0)); var modelRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormal>(model, "Phong", "Phong", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.SHADOW_MAP), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); modelRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; model.SetNewWorldTransformation(ref newModelTranslation, true); var plane = new Model <VertexPositionNormal, RealtimeMaterial>(String.Empty, GeometryFactory.GenerateQuadPN_XZ(), new RealtimeMaterial()); var newPlaneTranslation = Matrix4x4.CreateTranslation(new Vector3(0, 10, 0)); var newPlaneScale = Matrix4x4.CreateScale(new Vector3(30, 1, 30)); var trafo = newPlaneScale * newPlaneTranslation; plane.SetNewWorldTransformation(ref trafo, true); var planeRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormal>(plane, "Phong", "Phong", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleStrip, new RenderDescription(RenderFlags.NORMAL | RenderFlags.SHADOW_MAP), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); planeRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; //TODO: Write method to remove ambient terms var sponzaModels = AssimpLoader.LoadRealtimeModelsFromFile(AppContext.BaseDirectory, "sponza/sponza.obj"); var sponzaPNTTB = sponzaModels.modelPNTTB; var sponzaPC = sponzaModels.modelPC; for (int i = 0; i < sponzaPNTTB.MaterialCount; i++) { sponzaPNTTB.GetMaterial(i).ambient = new Vector4(0.3f, 0.3f, 0.3f, 1.0f); } for (int i = 0; i < sponzaPC.MaterialCount; i++) { sponzaPC.GetMaterial(i).ambient = new Vector4(0.3f, 0.3f, 0.3f, 1.0f); } sponzaPNTTB.SetNewWorldTransformation(ref scale, true); sponzaPC.SetNewWorldTransformation(ref scale, true); var sponzaRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent>(sponzaPNTTB, "PhongBitangentTexture", "PhongBitangentTexture", VertexRuntimeTypes.VertexPositionNormalTextureTangentBitangent, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.SHADOW_MAP), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sponzaRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPNTTB; sponzaRuntimeState.CallSamplerGeneration += ResourceGenerator.GenerateTriLinearSampler; sponzaRuntimeState.CallTextureResourceLayoutGeneration += ResourceGenerator.GenerateTextureResourceLayoutForNormalMapping; sponzaRuntimeState.CallTextureResourceSetGeneration += ResourceGenerator.GenerateTextureResourceSetForNormalMapping; // var sponzaRuntimeStateTexOnly = new ModelRuntimeDescriptor<VertexPositionTexture>(sponzaPT,"Texture","Texture", VertexTypes.VertexPositionTexture,PrimitiveTopology.TriangleList); // sponzaRuntimeStateTexOnly.CallVertexLayoutGeneration+=ResourceGenerator.GenerateVertexLayoutForPT; // sponzaRuntimeStateTexOnly.CallSamplerGeneration+=ResourceGenerator.GenerateTriLinearSampler; // sponzaRuntimeStateTexOnly.CallTextureResourceLayoutGeneration+=ResourceGenerator.GenerateTextureResourceLayoutForDiffuseMapping; // sponzaRuntimeStateTexOnly.CallTextureResourceSetGeneration+=ResourceGenerator.GenerateTextureResourceSetForDiffuseMapping; var sponzaRuntimeStateColorOnly = new ModelRuntimeDescriptor <VertexPositionColor>(sponzaPC, "Color", "Color", VertexRuntimeTypes.VertexPositionColor, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL | RenderFlags.SHADOW_MAP), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sponzaRuntimeStateColorOnly.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPC; // var sunRuntimeState = new ModelRuntimeState<VertexPositionNormalTextureTangentBitangent>(sun,"PhongBitangentTexture","PhongBitangentTexture"); // sunRuntimeState.CallVertexLayoutGeneration+=ResourceGenerator.GenerateVertexLayoutForPNTTB; // sunRuntimeState.CallSamplerGeneration+=ResourceGenerator.GenerateLinearSampler; // sunRuntimeState.CallTextureResourceLayoutGeneration+=ResourceGenerator.GenerateTextureResourceLayoutForNormalMapping; // sunRuntimeState.CallTextureResourceSetGeneration+=ResourceGenerator.GenerateTextureResourceSetForNormalMapping; var skyBox = new Model <VertexPosition, RealtimeMaterial>("cloudtop", GeometryFactory.GenerateCube(true), new RealtimeMaterial()); var skyBoxMaterial = skyBox.GetMaterial(0); skyBoxMaterial.AssignCubemapPaths("cloudtop_ft.png", "cloudtop_bk.png", "cloudtop_lf.png", "cloudtop_rt.png", "cloudtop_up.png", "cloudtop_dn.png"); _skyBoxRuntimeState = new ModelRuntimeDescriptor <VertexPosition>(skyBox, "Skybox", "Skybox", VertexRuntimeTypes.VertexPosition, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); _skyBoxRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForP; _skyBoxRuntimeState.CallSamplerGeneration += ResourceGenerator.GenerateBiLinearSampler; _skyBoxRuntimeState.CallTextureResourceLayoutGeneration += ResourceGenerator.GenerateTextureResourceLayoutForCubeMapping; _skyBoxRuntimeState.CallTextureResourceSetGeneration += ResourceGenerator.GenerateTextureResourceSetForCubeMapping; _sun = new Model <VertexPositionNormal, RealtimeMaterial>(string.Empty, GeometryFactory.GenerateSphereNormal(100, 100, 1), new RealtimeMaterial()); _sun.GetMaterial(0).ambient = lightColor.ToVector4(); // _sun.meshes[0].TryGetMaterial().ambient = lightColor.ToVector4(); Vector3 newTranslation = new Vector3(lightPos.X, lightPos.Y, lightPos.Z); _sun.SetNewWorldTranslation(ref newTranslation, true); var sunRuntimeState = new ModelRuntimeDescriptor <VertexPositionNormal>(_sun, "Phong", "PhongNoShadow", VertexRuntimeTypes.VertexPositionNormal, PrimitiveTopology.TriangleList, new RenderDescription(RenderFlags.NORMAL), new InstancingRenderDescription(RenderFlags.NONE, InstancingDataFlags.EMPTY)); sunRuntimeState.CallVertexLayoutGeneration += ResourceGenerator.GenerateVertexLayoutForPN; //TODO: Automate this _modelPNTTBDescriptorList.Add(sponzaRuntimeState); _modelPCDescriptorList.Add(sponzaRuntimeStateColorOnly); _modelPDescriptorList.Add(_skyBoxRuntimeState); // _modelPTDescriptorList.Add(sponzaRuntimeStateTexOnly); // _modelStatesList.Add(sunRuntimeState); _modelPNDescriptorList.Add(sunRuntimeState); _modelPNDescriptorList.Add(modelRuntimeState); //_modelPNDescriptorList.Add(planeRuntimeState); InstanceData[] instancingData = { InstanceData.NO_DATA }; //TODO: Abstrct this foreach (var modelDescriptor in _modelPNTTBDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PNTTBRuntimeGeometry.AddModel(modelDescriptor); } foreach (var modelDescriptor in _modelPNDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PNRuntimeGeometry.AddModel(modelDescriptor); } foreach (var modelDescriptor in _modelPTDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PTRuntimeGeometry.AddModel(modelDescriptor); } foreach (var modelDescriptor in _modelPCDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PCRuntimeGeometry.AddModel(modelDescriptor); } foreach (var modelDescriptor in _modelPDescriptorList) { FillRuntimeDescriptor(modelDescriptor, _sceneRuntimeState, instancingData); PRuntimeGeometry.AddModel(modelDescriptor); } }