public static UniformNameMap LoadUniformNameMap(string shaderName) { string content = ManifestResourceLoader.LoadTextFile(string.Format( "{0}.{0}.UniformNameMap.xml", shaderName)); UniformNameMap result = UniformNameMap.Parse(XElement.Parse(content)); return(result); }
//Camera modelRotationCamera; //SatelliteRotator modelRotator; //private float rotateAngle; public FormSimpleRenderer() { InitializeComponent(); this.camera = new Camera(CameraType.Perspecitive, this.glCanvas1.Width, this.glCanvas1.Height); this.camera.Position = new GLM.vec3(5, 5, 5); this.cameraRotator = new SatelliteRotator(this.camera); //this.modelRotationCamera = new Camera(CameraType.Perspecitive, this.glCanvas1.Width, this.glCanvas1.Height); //this.modelRotator = new SatelliteRotator(this.modelRotationCamera); this.modelRotator = new ArcBallRotator(this.camera); { const string strGround = "Ground"; IConvert2BufferPointer model = new Ground(); CodeShader[] shaders = new CodeShader[2]; shaders[0] = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadShaderSourceCode(strGround, CodeShader.GLSLShaderType.VertexShader); shaders[1] = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadShaderSourceCode(strGround, CodeShader.GLSLShaderType.FragmentShader); PropertyNameMap propertyNameMap = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadPropertyNameMap(strGround); UniformNameMap uniformNameMap = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadUniformNameMap(strGround); this.groundRenderer = new ModernRenderer(model, shaders, propertyNameMap, uniformNameMap); this.groundRenderer.Initialize(); } Padding uiPadding = new System.Windows.Forms.Padding(10, 10, 10, 10); Size uiSize = new System.Drawing.Size(50, 50); IUILayoutParam uiParam = new IUILayoutParam(AnchorStyles.Left | AnchorStyles.Bottom, uiPadding, uiSize); uiAxis = new SimpleUIAxis(uiParam); uiAxis.Initialize(); { IModel model = (new TeapotFactory()).Create(1.0f); this.renderer = new SimpleRenderer(model); this.renderer.Initialize();//不在此显式初始化也可以。 } this.glCanvas1.MouseWheel += glCanvas1_MouseWheel; this.glCanvas1.KeyPress += glCanvas1_KeyPress; this.glCanvas1.MouseDown += glCanvas1_MouseDown; this.glCanvas1.MouseMove += glCanvas1_MouseMove; this.glCanvas1.MouseUp += glCanvas1_MouseUp; this.glCanvas1.OpenGLDraw += glCanvas1_OpenGLDraw; this.glCanvas1.Resize += glCanvas1_Resize; }
public void GenerateUinformNameMap(string uniformNameMapFullname) { List <ShaderField> shaderFieldList = new List <ShaderField>(); foreach (var item in this.VertexShaderFieldList) { if (item.Qualider == FieldQualifier.Uniform) { bool exists = false; foreach (var shaderField in shaderFieldList) { if (shaderField.FieldName == item.FieldName) { exists = true; } } if (!exists) { shaderFieldList.Add(item); } } } foreach (var item in this.GeometryShaderFieldList) { if (item.Qualider == FieldQualifier.Uniform) { bool exists = false; foreach (var shaderField in shaderFieldList) { if (shaderField.FieldName == item.FieldName) { exists = true; } } if (!exists) { shaderFieldList.Add(item); } } } foreach (var item in this.FragmentShaderFieldList) { if (item.Qualider == FieldQualifier.Uniform) { bool exists = false; foreach (var shaderField in shaderFieldList) { if (shaderField.FieldName == item.FieldName) { exists = true; } } if (!exists) { shaderFieldList.Add(item); } } } UniformNameMap map = new UniformNameMap(); foreach (var item in shaderFieldList) { map.Add(item.FieldName, item.FieldName); } map.ToXElement().Save(uniformNameMapFullname); }
//Camera modelRotationCamera; //SatelliteRotator modelRotator; //private float rotateAngle; public FormNewNormalLine() { InitializeComponent(); this.camera = new Camera(CameraType.Perspecitive, this.glCanvas1.Width, this.glCanvas1.Height); this.camera.Position = new GLM.vec3(50, 5, 5); this.cameraRotator = new SatelliteRotator(this.camera); //this.modelRotationCamera = new Camera(CameraType.Perspecitive, this.glCanvas1.Width, this.glCanvas1.Height); //this.modelRotator = new SatelliteRotator(this.modelRotationCamera); this.modelRotator = new ArcBallRotator(this.camera); { const string strGround = "Ground"; IConvert2BufferPointer model = new Ground(); CodeShader[] shaders = new CodeShader[2]; shaders[0] = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadShaderSourceCode(strGround, CodeShader.GLSLShaderType.VertexShader); shaders[1] = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadShaderSourceCode(strGround, CodeShader.GLSLShaderType.FragmentShader); PropertyNameMap propertyNameMap = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadPropertyNameMap(strGround); UniformNameMap uniformNameMap = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadUniformNameMap(strGround); this.groundRenderer = new ModernRenderer(model, shaders, propertyNameMap, uniformNameMap); this.groundRenderer.Initialize(); } Padding uiPadding = new System.Windows.Forms.Padding(10, 10, 10, 10); Size uiSize = new System.Drawing.Size(50, 50); IUILayoutParam uiParam = new IUILayoutParam(AnchorStyles.Left | AnchorStyles.Bottom, uiPadding, uiSize); uiAxis = new SimpleUIAxis(uiParam); uiAxis.Initialize(); //IModel model = (new TeapotFactory()).Create(1.0f); { IConvert2BufferPointer model = new NewNormalLineDemoModel(); CodeShader[] codeShaders = new CodeShader[3]; codeShaders[0] = new CodeShader(ShaderHelper.Load("NewNormalLine.vert"), CodeShader.GLSLShaderType.VertexShader); codeShaders[1] = new CodeShader(ShaderHelper.Load("NewNormalLine.geom"), CodeShader.GLSLShaderType.GeometryShader); codeShaders[2] = new CodeShader(ShaderHelper.Load("NewNormalLine.frag"), CodeShader.GLSLShaderType.FragmentShader); var propertyNameMap = new PropertyNameMap(); propertyNameMap.Add("in_Position", NewNormalLineDemoModel.strPosition); propertyNameMap.Add("in_Normal", NewNormalLineDemoModel.strNormal); var uniformNameMap = new UniformNameMap(); uniformNameMap.Add("model matrix", "modelMatrix"); uniformNameMap.Add("view matrix", "viewMatrix"); uniformNameMap.Add("projection matrix", "projectionMatrix"); uniformNameMap.Add("show model", "showModel"); uniformNameMap.Add("show normal", "showNormal"); uniformNameMap.Add("normal length", "normalLength"); this.renderer = new ModernRenderer(model, codeShaders, propertyNameMap, uniformNameMap); this.renderer.Initialize();//不在此显式初始化也可以。 this.renderer.SetUniformValue("show model", 1.0f); this.renderer.SetUniformValue("show normal", 1.0f); this.renderer.SetUniformValue("normal length", 1.0f); } //this.demoLifebar = new DemoLifeBar(0.2f, 0.02f, 4); //this.demolifebarRenderer = new NormalLineRenderer(this.demoLifebar); //this.demolifebarRenderer.Initialize(); this.lifebarRenderer = new LifeBarRenderer(new LifeBar(2f, 0.2f, 4f)); this.lifebarRenderer.Initialize(); //StringModel stringModel = DummyStringModelFactory.GetModel("teapot"); this.stringRenderer = new StringRenderer("teapot".GetDummyModel()); this.stringRenderer.Initialize(); uiParam = new IUILayoutParam(AnchorStyles.Right | AnchorStyles.Bottom, new Padding(10, 10, 120, 10), new Size(50, 50)); this.label = new DummyLabel(uiParam, "Hello Label!"); this.label.Initialize(); this.glCanvas1.MouseWheel += glCanvas1_MouseWheel; this.glCanvas1.KeyPress += glCanvas1_KeyPress; this.glCanvas1.MouseDown += glCanvas1_MouseDown; this.glCanvas1.MouseMove += glCanvas1_MouseMove; this.glCanvas1.MouseUp += glCanvas1_MouseUp; this.glCanvas1.OpenGLDraw += glCanvas1_OpenGLDraw; this.glCanvas1.Resize += glCanvas1_Resize; Application.Idle += Application_Idle; }
private void LoadModel() { string datafilePrefix = @"data\floordata"; int noneZeroItemCount = 0; bool minSet = false, maxSet = false; float min = 0, max = 0; char[] separator = new char[] { ' ', '\t', '\r', '\n' }; for (int i = 0; i < 20; i++) { string filename = datafilePrefix + (i + 1).ToString() + ".txt"; //using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) using (var sr = new StreamReader(filename)) { while (!sr.EndOfStream) { var line = sr.ReadLine(); var parts = line.Split(separator, StringSplitOptions.RemoveEmptyEntries); foreach (var part in parts) { float value; if (float.TryParse(part, out value)) { if (value != 0.0f) { noneZeroItemCount++; } if (!minSet) { min = value; minSet = true; } if (!maxSet) { max = value; maxSet = true; } if (value < min) { min = value; } if (max < value) { max = value; } } else { throw new Exception(); } } } } } ColorBar colorBar = ColorBar.GetDefault(); int xSize = 921, ySize = 921, zSize = 20; int vertexIndex = 0; List <vec3> positionList = new List <vec3>(); List <vec3> colorList = new List <vec3>(); for (int i = 0; i < 20; i++) { string filename = datafilePrefix + (i + 1).ToString() + ".txt"; using (var sr = new StreamReader(filename)) { while (!sr.EndOfStream) { var line = sr.ReadLine(); var parts = line.Split(separator, StringSplitOptions.RemoveEmptyEntries); foreach (var part in parts) { float value; if (float.TryParse(part, out value)) { if (value != 0.0f) { vec3 color = colorBar.GetColor(min, max, value); //vec3 color = new vec3(1, 1, 1); colorList.Add(color); float x = vertexIndex % xSize; float y = (vertexIndex % (xSize * ySize)) / ySize; float z = vertexIndex / ySize / xSize; if (vertexIndex != x + y * xSize + z * ySize * xSize) { Console.WriteLine("asdf"); } vec3 position = new vec3(x - xSize / 2, y - ySize / 2, z - zSize / 2); position = position / 10; positionList.Add(position); } vertexIndex++; } else { throw new Exception(); } } } } } RadarModel model = new RadarModel(positionList, colorList); CodeShader[] codeShaders = new CodeShader[2]; codeShaders[0] = new CodeShader(File.ReadAllText("Radar.vert"), CodeShader.GLSLShaderType.VertexShader); codeShaders[1] = new CodeShader(File.ReadAllText("Radar.frag"), CodeShader.GLSLShaderType.FragmentShader); PropertyNameMap propertyNameMap = PropertyNameMap.Parse(XElement.Load("Radar.PropertyNameMap.xml")); UniformNameMap uniformNameMap = UniformNameMap.Parse(XElement.Load("Radar.UniformNameMap.xml")); this.modelRenderer = new ModernRenderer(model, codeShaders, propertyNameMap, uniformNameMap); this.modelRenderer.SwitchList.Add(new PointSpriteSwitch()); this.modelRenderer.Initialize();//不在此显式初始化也可以。 this.modelRenderer.SetUniformValue("canvasWidth", (float)this.glCanvas1.Width); this.modelRenderer.SetUniformValue("canvasHeight", (float)this.glCanvas1.Height); this.modelRenderer.SetUniformValue("brightness", 1.0f); Texture2D texture = new Texture2D(); texture.Initialize(new Bitmap("cloud30.png")); this.modelRenderer.SetUniformValue("cloudTexture", new samplerValue(texture.Name, ActiveTextureIndex.Texture0)); }