public DXScene(SharpDX.Direct3D11.Device device) { RenderGroups = new Dictionary<DXShader, RenderGroup>(); _transformBuffer = new Buffer(device, new BufferDescription() { Usage = ResourceUsage.Dynamic, SizeInBytes = System.Runtime.InteropServices.Marshal.SizeOf<Matrix>(), BindFlags = BindFlags.ConstantBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, StructureByteStride = 0 }); _globalLightsBuffer = new Buffer(device, new BufferDescription() { Usage = ResourceUsage.Dynamic, SizeInBytes = System.Runtime.InteropServices.Marshal.SizeOf<GlobalLightsData>(), BindFlags = BindFlags.ConstantBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, StructureByteStride = 0 }); WorldMatrix = Matrix.Identity; _rootNode = new DXSceneNode() { Position = new Vector3(), Orientation = Quaternion.Identity, Scale = new Vector3(1.0f) }; _rootNode.UpdateTransfromMatrix(); _globalLights = new GlobalLightsData() { Ambient = new Color4(0.5f, 0.5f, 0.5f, 1.0f), Direction = new Vector3(0.0f, -1.0f, 0.0f), Directional = new Color4(0.5f, 0.5f, 0.5f, 1.0f) }; }
public DXSceneNode CreateChildNode() { DXSceneNode node = new DXSceneNode(); node.Parent = this; node.Scale = new Vector3(1.0f); node.Orientation = Quaternion.Identity; node.Position = new Vector3(0.0f); node.TransformationMatrix = Matrix.Identity; Children.Add(node); return node; }
void RenderNode(DeviceContext device, DXSceneNode node, Matrix derivedTrnasform) { Matrix transform = node.TransformationMatrix * derivedTrnasform; if(node.AttachedObjects.Count > 0) { transform.Transpose(); DataStream stream; var dataBox = device.MapSubresource(_transformBuffer, 0, MapMode.WriteDiscard, MapFlags.None); stream = new DataStream(dataBox.DataPointer, _transformBuffer.Description.SizeInBytes, true, true); stream.Write(transform); device.UnmapSubresource(_transformBuffer, 0); //to update the data on GPU stream.Dispose(); transform.Transpose(); device.VertexShader.SetConstantBuffer((int)ConstantBufferSlots.WorldViewProjMatrix, _transformBuffer); foreach(var model in node.AttachedObjects) { model.Shader.RenderFirstPass(device); model.Render(device); for(int i = 1; i < model.Shader.Passes.Count; ++i) { model.Shader.RenderPass(device, i); model.Render(device); } } } foreach(var childNode in node.Children) { RenderNode(device, childNode, transform); } }