private void CheckValidity() { if (state.HasFlag(ShaderProgramState.ShadersChanged)) { // we need to recompile shaders RecompileShaders(); ResetUniforms(); RebufferVertices(); } else { if (state.HasFlag(ShaderProgramState.UniformsChanged)) { // reset uniforms ResetUniforms(); } if (state.HasFlag(ShaderProgramState.VerticesChanged)) { // reset vertex buffers RebufferVertices(); } } state = ShaderProgramState.Valid; }
/// <summary> /// Is called when vertex collection changes (uniform added, removed, etc.). /// </summary> private void OnVertexCollectionChange( object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { switch (e.Action) { case System.Collections.Specialized.NotifyCollectionChangedAction.Add: case System.Collections.Specialized.NotifyCollectionChangedAction.Replace: case System.Collections.Specialized.NotifyCollectionChangedAction.Move: // Have new elements in collection notify this class when they get changed. // Also ensure that new vertices will have all the already defined attributes. foreach (var obj in e.NewItems) { Vertex vertex = (Vertex)obj; vertex.PropertyChanged += OnVertexChange; foreach (AttributeInfo attributeInfo in AttributeInfos.Values) { if (vertex.Attributes.ContainsKey(attributeInfo.Id) == false) { vertex.Attributes.Add(attributeInfo.Id, attributeInfo.CreateNewVariable()); } vertex.Attributes[attributeInfo.Id].PropertyChanged += OnVertexChange; } } break; } state |= ShaderProgramState.VerticesChanged; }
// ================================================================================================== // OTHER // ================================================================================================== public Core() { state |= ShaderProgramState.ShadersChanged; state |= ShaderProgramState.UniformsChanged; state |= ShaderProgramState.VerticesChanged; Uniforms = new ObservableCollection <Uniform>(); _Vertices = new ObservableCollection <Vertex>(); _AttributeInfos = new Dictionary <string, AttributeInfo>(); Uniforms.CollectionChanged += OnUniformCollectionChange; Vertices.CollectionChanged += OnVertexCollectionChange; ClearColor = new Vec3f() { X = 0.1f, Y = 0.1f, Z = 0.1f }; }
/// <summary> /// Is called when uniform collection changes (uniform added, removed, etc.). /// </summary> private void OnUniformCollectionChange( object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { switch (e.Action) { case System.Collections.Specialized.NotifyCollectionChangedAction.Add: case System.Collections.Specialized.NotifyCollectionChangedAction.Replace: case System.Collections.Specialized.NotifyCollectionChangedAction.Move: // Have new elements in collection notify this class when they get changed. foreach (var obj in e.NewItems) { Uniform uniform = (Uniform)obj; uniform.PropertyChanged += OnUniformChange; uniform.Variable.PropertyChanged += OnUniformChange; } break; } state |= ShaderProgramState.UniformsChanged; }
/// <summary> /// Is called when single attribute variable changes in some way. /// </summary> private void OnAttributeChange(object sender, System.ComponentModel.PropertyChangedEventArgs e) { state |= ShaderProgramState.VerticesChanged; }
// ================================================================================================== // HANDLING CHANGES // ================================================================================================== /// <summary> /// Is called when single uniform changes in some way. /// </summary> private void OnUniformChange(object sender, System.ComponentModel.PropertyChangedEventArgs e) { state |= ShaderProgramState.UniformsChanged; }