PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Core/Blocks/Input/SystemParameter.cs

http://visual-shader-editor.googlecode.com/
C# | 141 lines | 97 code | 16 blank | 28 comment | 2 complexity | f2291550b7ffc6fa22db7776618ab4f3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. Copyright (c) 2011, Pawel Szczurek
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  6. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or
  7. other materials provided with the distribution.
  8. Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without
  9. specific prior written permission.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  11. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  12. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  13. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  14. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  15. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. */
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. using Core.Basic;
  21. using Core.Main;
  22. using Core.Var;
  23. using System.Diagnostics;
  24. using Core.CodeGeneration.Code;
  25. using Core.Environment;
  26. namespace Core.Blocks.Input
  27. {
  28. [Block(Name = "SystemParameter", Path = "Input")]
  29. public class SystemParameter : BaseBlock
  30. {
  31. public class Parameter
  32. {
  33. public string Name;
  34. public Format Format;
  35. public Action<ICompiledShader> Upload;
  36. }
  37. public static readonly List<Parameter> Parameters = new List<Parameter>(new Parameter[]{
  38. new Parameter(){Name = "Model", Format = Format.FLOAT4X4,
  39. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("Model", x.Owner.SystemParameters.GetParameter(Matrix44Parameter.Model)))},
  40. new Parameter(){Name = "ModelView", Format = Format.FLOAT4X4,
  41. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("ModelView", x.Owner.SystemParameters.GetParameter(Matrix44Parameter.Model) * x.Owner.SystemParameters.GetParameter(Matrix44Parameter.View)))},
  42. new Parameter(){Name = "ModelViewProjection", Format = Format.FLOAT4X4,
  43. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("ModelViewProjection", x.Owner.SystemParameters.GetParameter(Matrix44Parameter.Model) * x.Owner.SystemParameters.GetParameter(Matrix44Parameter.View) * x.Owner.SystemParameters.GetParameter(Matrix44Parameter.Projection)))},
  44. new Parameter(){Name = "View", Format = Format.FLOAT4X4,
  45. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("View", x.Owner.SystemParameters.GetParameter(Matrix44Parameter.View)))},
  46. new Parameter(){Name = "ViewProjection", Format = Format.FLOAT4X4,
  47. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("ViewProjection", x.Owner.SystemParameters.GetParameter(Matrix44Parameter.View) * x.Owner.SystemParameters.GetParameter(Matrix44Parameter.Projection)))},
  48. new Parameter(){Name = "Projection", Format = Format.FLOAT4X4,
  49. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("Projection", x.Owner.SystemParameters.GetParameter(Matrix44Parameter.Projection)))},
  50. new Parameter(){Name = "Time", Format = Format.FLOAT,
  51. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("Time", x.Owner.SystemParameters.GetParameter(Vector1Parameter.Time)))},
  52. new Parameter(){Name = "SinTime", Format = Format.FLOAT,
  53. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("SinTime", new Vector1f((float)System.Math.Sin(x.Owner.SystemParameters.GetParameter(Vector1Parameter.Time).X))))},
  54. new Parameter(){Name = "CameraPositon", Format = Format.FLOAT3,
  55. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("CameraPositon", x.Owner.SystemParameters.GetParameter(Vector3Parameter.CameraPosition)))},
  56. new Parameter(){Name = "CameraUp", Format = Format.FLOAT3,
  57. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("CameraUp", x.Owner.SystemParameters.GetParameter(Vector3Parameter.CameraUp)))},
  58. new Parameter(){Name = "CameraForward", Format = Format.FLOAT3,
  59. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("CameraForward", x.Owner.SystemParameters.GetParameter(Vector3Parameter.CameraForward)))},
  60. new Parameter(){Name = "CameraRight", Format = Format.FLOAT3,
  61. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("CameraRight", x.Owner.SystemParameters.GetParameter(Vector3Parameter.CameraRight)))},
  62. new Parameter(){Name = "LightColor", Format = Format.FLOAT3,
  63. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("LightColor", x.Owner.SystemParameters.GetParameter(Vector3Parameter.LightColor)))},
  64. new Parameter(){Name = "LightAttenuation", Format = Format.FLOAT3,
  65. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("LightAttenuation", x.Owner.SystemParameters.GetParameter(Vector3Parameter.LightAttenuation)))},
  66. new Parameter(){Name = "LightPosition", Format = Format.FLOAT3,
  67. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("LightPosition", x.Owner.SystemParameters.GetParameter(Vector3Parameter.LightPosition)))},
  68. new Parameter(){Name = "LightForward", Format = Format.FLOAT3,
  69. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("LightForward", x.Owner.SystemParameters.GetParameter(Vector3Parameter.LightForward)))},
  70. new Parameter(){Name = "AmbientColor", Format = Format.FLOAT3,
  71. Upload = new Action<ICompiledShader>((x)=>x.SetParameter("AmbientColor", x.Owner.SystemParameters.GetParameter(Vector3Parameter.AmbientColor)))},
  72. //new Parameter(){Name = "LightShadowMap", Format = Format.TEXTURE},
  73. //new Parameter(){Name = "LightColorMap", Format = Format.TEXTURE},
  74. //new Parameter(){Name = "EnvironmentCubeMap", Format = Format.TEXTURE},
  75. });
  76. public class SystemParameterOutput : BlockOutput
  77. {
  78. public SystemParameterOutput(BaseBlock owner, Format f, string name)
  79. : base(owner, f, name)
  80. {
  81. }
  82. protected override Variable CreateVariable()
  83. {
  84. //ignore output format, always use correct one
  85. return Owner.BlockManager.VariableManager.CreateVariable(Name, Parameters.Find((x)=>x.Name == Name).Format, VerticesStreamSemantic.NONE, 0, Owner, true);
  86. }
  87. }
  88. public SystemParameter(BlockManager owner)
  89. : base(owner, new SystemParameterOptionsWindow())
  90. {
  91. AddOutput(new SystemParameterOutput(this, Format.FLOAT, "Time"));
  92. }
  93. public override void SetShaderParameters(Core.Environment.ICompiledShader s)
  94. {
  95. base.SetShaderParameters(s);
  96. foreach (var p in Parameters)
  97. p.Upload(s);
  98. }
  99. protected override BlockOutput CreateOutput()
  100. {
  101. return new SystemParameterOutput(this, Format.FLOAT4, "Time");
  102. }
  103. protected internal override void GenerateCode(Core.CodeGeneration.ShaderCodeGenerator sc)
  104. {
  105. sc.AddParameter(Outputs[0].Variable);
  106. }
  107. public string CurrentParameterName
  108. {
  109. get { return Outputs[0].Name; }
  110. set
  111. {
  112. Outputs[0].Name = value;
  113. Outputs[0].Format = Parameters.Find((x) => x.Name == value).Format;
  114. }
  115. }
  116. }
  117. }