public void Build(DataStructure dataStructure, string rendererFilename = "") { if (string.IsNullOrEmpty(rendererFilename)) { rendererFilename = this.GetFilename(dataStructure); } var rendererType = new CodeTypeDeclaration(dataStructure.RendererName); rendererType.IsClass = true; rendererType.IsPartial = true; rendererType.BaseTypes.Add(typeof(Renderer)); rendererType.Comments.Add(new CodeCommentStatement("<summary>", true)); rendererType.Comments.Add(new CodeCommentStatement(string.Format("Renderer of {0}", dataStructure.TargetName), true)); rendererType.Comments.Add(new CodeCommentStatement("</summary>", true)); BuildCreate(rendererType, dataStructure); BuildConstructor(rendererType, dataStructure); BuildDoInitialize(rendererType, dataStructure); BuildDoRender(rendererType, dataStructure); var parserNamespace = new CodeNamespace("CSharpGL"); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Object).Namespace)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Collections.Generic.List<int>).Namespace)); parserNamespace.Types.Add(rendererType); //生成代码 using (var stream = new StreamWriter(rendererFilename, false)) { CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CodeGeneratorOptions opentions = new CodeGeneratorOptions();//代码生成选项 opentions.BlankLinesBetweenMembers = true; opentions.BracingStyle = "C"; opentions.ElseOnClosing = false; opentions.IndentString = " "; opentions.VerbatimOrder = true; codeProvider.GenerateCodeFromNamespace(parserNamespace, stream, opentions); } }
public void Build(DataStructure dataStructure, string modelFilename = "") { if (string.IsNullOrEmpty(modelFilename)) { modelFilename = this.GetFilename(dataStructure); } // public class DemoModel : IBufferable { } var modelType = new CodeTypeDeclaration(dataStructure.ModelName); modelType.IsClass = true; modelType.IsPartial = true; modelType.BaseTypes.Add(typeof(IBufferable)); modelType.Comments.Add(new CodeCommentStatement("<summary>", true)); modelType.Comments.Add(new CodeCommentStatement(string.Format("Model of {0}", dataStructure.TargetName), true)); modelType.Comments.Add(new CodeCommentStatement("</summary>", true)); BuildFields(modelType, dataStructure); BuildGetVertexAttributeBuffer(modelType, dataStructure); BuildGetIndex(modelType, dataStructure); var parserNamespace = new CodeNamespace("CSharpGL"); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Object).Namespace)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Collections.Generic.List<int>).Namespace)); parserNamespace.Types.Add(modelType); //生成代码 using (var stream = new StreamWriter(modelFilename, false)) { CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CodeGeneratorOptions opentions = new CodeGeneratorOptions();//代码生成选项 opentions.BlankLinesBetweenMembers = true; opentions.BracingStyle = "C"; opentions.ElseOnClosing = false; opentions.IndentString = " "; opentions.VerbatimOrder = true; codeProvider.GenerateCodeFromNamespace(parserNamespace, stream, opentions); } }
public override string Build(DataStructure data) { var builder = new StringBuilder(); builder.AppendLine(this.Version); builder.AppendLine(); builder.AppendLine("void main(void)"); builder.AppendLine("{"); builder.AppendLine(" // TODO: setup output color ..."); builder.AppendLine("}"); builder.AppendLine(); return builder.ToString(); }
public static DataStructure Parse(XElement xElement) { string targetName = xElement.Attribute(strTargetName).Value; bool zeroIndexBuffer = bool.Parse(xElement.Attribute(strZeroIndexBuffer).Value); DrawMode mode = (DrawMode)Enum.Parse(typeof(DrawMode), xElement.Attribute(strDrawMode).Value); var result = new DataStructure(); result.TargetName = targetName; result.ZeroIndexBuffer = zeroIndexBuffer; result.DrawMode = mode; foreach (var item in xElement.Elements(VertexAttribute.strVertexAttribute)) { VertexAttribute property = VertexAttribute.Parse(item); result.propertyList.Add(property); } return result; }
public override string Build(DataStructure data) { var builder = new StringBuilder(); builder.AppendLine(this.Version); builder.AppendLine(); foreach (var item in data.PropertyList) { builder.AppendLine(item.ToGLSL()); } builder.AppendLine(); builder.AppendLine("void main(void)"); builder.AppendLine("{"); builder.AppendLine(" // TODO: setup gl_Position = ..."); builder.AppendLine("}"); builder.AppendLine(); return builder.ToString(); }
private void BuildConstructor(CodeTypeDeclaration rendererType, DataStructure dataStructure) { //throw new NotImplementedException(); var method = new CodeConstructor(); method.Attributes = MemberAttributes.Private; method.Name = dataStructure.RendererName; var model = new CodeParameterDeclarationExpression(typeof(IBufferable), "model"); method.Parameters.Add(model); var shaderCode = new CodeParameterDeclarationExpression(typeof(ShaderCode[]), shaderCodes); method.Parameters.Add(shaderCode); var map = new CodeParameterDeclarationExpression(typeof(AttributeMap), "attributeMap"); method.Parameters.Add(map); var last = new CodeParameterDeclarationExpression(new CodeTypeReference(string.Format("params {0}[]", typeof(GLState).Name)), "switches"); method.Parameters.Add(last); method.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("model")); method.BaseConstructorArgs.Add(new CodeVariableReferenceExpression(shaderCodes)); method.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("attributeMap")); method.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("switches")); rendererType.Members.Add(method); }
/// <summary> /// fields. /// </summary> /// <param name="modelType"></param> /// <param name="dataStructure"></param> private void BuildFields(CodeTypeDeclaration modelType, DataStructure dataStructure) { foreach (var item in dataStructure.PropertyList) { { // public const string position = "position"; var constField = new CodeMemberField(typeof(string), string.Format("{0}", item.NameInModel)); constField.Attributes = MemberAttributes.Public | MemberAttributes.Const; constField.InitExpression = new CodePrimitiveExpression(item.NameInModel); modelType.Members.Add(constField); } { // private PropertyBuffer positionBuffer; var bufferField = new CodeMemberField(typeof(VertexBuffer), item.BufferName); modelType.Members.Add(bufferField); } } { // private IndexBuffer indexBuffer; var bufferField = new CodeMemberField(typeof(IndexBuffer), indexBuffer); modelType.Members.Add(bufferField); } }
private void BuildDoInitialize(CodeTypeDeclaration rendererType, DataStructure dataStructure) { //throw new NotImplementedException(); }
private void BuildDoRender(CodeTypeDeclaration rendererType, DataStructure dataStructure) { //throw new NotImplementedException(); var method = new CodeMemberMethod(); method.Attributes = MemberAttributes.Family | MemberAttributes.Override; method.Name = "DoRender"; const string arg = "arg"; method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(RenderEventArgs), arg)); method.Statements.Add(new CodeCommentStatement("mat4 projection = arg.Camera.GetProjectionMatrix();")); method.Statements.Add(new CodeCommentStatement("mat4 view = arg.Camera.GetViewMatrix();")); method.Statements.Add(new CodeCommentStatement("mat4 model = this.GetModelMatrix();")); method.Statements.Add(new CodeCommentStatement("this.SetUniform(\"projectionMatrix\", projection);")); method.Statements.Add(new CodeCommentStatement("this.SetUniform(\"viewMatrix\", view);")); method.Statements.Add(new CodeCommentStatement("this.SetUniform(\"modelMatrix\", model);")); method.Statements.Add(new CodeSnippetStatement(string.Format(" base.DoRender(arg);"))); rendererType.Members.Add(method); }
public string GetFilename(DataStructure dataStructure) { return(string.Format("{0}.cs", dataStructure.RendererName)); }
private void BuildCreate(CodeTypeDeclaration rendererType, DataStructure dataStructure) { CodeMemberMethod method = CreateDeclaration(dataStructure); CreateBody(method, dataStructure); rendererType.Members.Add(method); }
public string GetFilename(DataStructure dataStructure) { return string.Format("{0}.cs", dataStructure.RendererName); }
private void CreateBody(CodeMemberMethod method, DataStructure dataStructure) { { // var shaderCodes = new ShaderCode[2]; method.Statements.Add(new CodeSnippetStatement(string.Format(" var {0} = new {1}[2];", shaderCodes, typeof(ShaderCode).Name))); method.Statements.Add(new CodeSnippetStatement(string.Format(" {0}[0] = new {1}(File.ReadAllText(@\"shaders\\{2}.vert\"), {3}.{4});", shaderCodes, typeof(ShaderCode).Name, dataStructure.TargetName, ShaderType.VertexShader.GetType().Name, ShaderType.VertexShader))); method.Statements.Add(new CodeSnippetStatement(string.Format(" {0}[1] = new {1}(File.ReadAllText(@\"shaders\\{2}.frag\"), {3}.{4});", shaderCodes, typeof(ShaderCode).Name, dataStructure.TargetName, ShaderType.VertexShader.GetType().Name, ShaderType.FragmentShader))); } { // var map = new AttributeMap(); method.Statements.Add(new CodeVariableDeclarationStatement("var", "map", new CodeObjectCreateExpression(typeof(AttributeMap)))); // map.Add("in_Position", GroundModel.strPosition); foreach (var item in dataStructure.PropertyList) { method.Statements.Add(new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("map"), "Add", new CodePrimitiveExpression(item.NameInShader), new CodeSnippetExpression(string.Format("{0}.{1}", dataStructure.ModelName, item.NameInModel)))); } } const string renderer = "renderer"; { // var renderer = new GroundRenderer(model, shaderCodes, map); method.Statements.Add(new CodeSnippetStatement(string.Format(" var {0} = new {1}({2}, {3}, map);", renderer, dataStructure.RendererName, model, shaderCodes))); } { // setup renderer's Lengths, WorldPosition etc. method.Comments.Add(new CodeCommentStatement(string.Format("renderer.Lengths = ..."))); method.Comments.Add(new CodeCommentStatement(string.Format("renderer.WorldPosition = ..."))); } { // return renderer; method.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(renderer))); } }
private CodeMemberMethod CreateDeclaration(DataStructure dataStructure) { var method = new CodeMemberMethod(); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.ReturnType = new CodeTypeReference(dataStructure.RendererName); method.Name = "Create"; var parameter0 = new CodeParameterDeclarationExpression(dataStructure.ModelName, model); method.Parameters.Add(parameter0); method.Comments.Add(new CodeCommentStatement(string.Format("you can replace {0} with {1} in the method's parameter.", dataStructure.ModelName, typeof(IBufferable).Name))); return method; }
/// <summary> /// public PropertyBuffer GetProperty(string bufferName, string varNameInShader) /// </summary> /// <param name="modelType"></param> /// <param name="dataStructure"></param> private void BuildGetVertexAttributeBuffer(CodeTypeDeclaration modelType, DataStructure dataStructure) { //public PropertyBuffer GetProperty(string bufferName, string varNameInShader) var method = GetVertexAttributeBufferDeclaration(); GetVertexAttributeBufferBody(method, dataStructure); modelType.Members.Add(method); }
public override string GetFilename(DataStructure dataStructure) { return string.Format("{0}.vert", dataStructure.TargetName); }
private void GetIndexBody(CodeMemberMethod method, DataStructure dataStructure) { // if (indexBuffer == null) var ifStatement = new CodeConditionStatement( new CodeBinaryOperatorExpression( new CodeVariableReferenceExpression(indexBuffer), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null))); method.Statements.Add(ifStatement); if (dataStructure.ZeroIndexBuffer) { // using (var buffer = new ZeroIndexBuffer(DrawMode.LineStrip, 0, BigDipperModel.positions.Length)) var usingBegin = new CodeSnippetStatement(string.Format(" using (var buffer = new ZeroIndexBuffer({0}.{1}, 0, ))", dataStructure.DrawMode.GetType().Name, dataStructure.DrawMode)); ifStatement.TrueStatements.Add(usingBegin); ifStatement.TrueStatements.Add(new CodeSnippetStatement(" {// begin of using")); ifStatement.TrueStatements.Add(new CodeSnippetStatement(string.Format(" indexBuffer = buffer.GetBuffer();"))); ifStatement.TrueStatements.Add(new CodeSnippetStatement(" }// end of using")); } else { // using (var buffer = new OneIndexBuffer<uint>(this.model.mode, BufferUsage.StaticDraw)) var usingBegin = new CodeSnippetStatement(string.Format(" using (var buffer = new OneIndexBuffer(IndexElementType.UInt, {0}.{1}, BufferUsage.StaticDraw))", dataStructure.DrawMode.GetType().Name, dataStructure.DrawMode)); ifStatement.TrueStatements.Add(usingBegin); ifStatement.TrueStatements.Add(new CodeSnippetStatement(" {// begin of using")); ifStatement.TrueStatements.Add(new CodeSnippetStatement(" buffer.Create();")); // unsafe { ifStatement.TrueStatements.Add(new CodeSnippetStatement(" unsafe")); ifStatement.TrueStatements.Add(new CodeSnippetStatement(" {// begin of unsafe")); // var array = (uint*)buffer.Header.ToPointer(); ifStatement.TrueStatements.Add(new CodeSnippetStatement(string.Format(" var array = (uint*)buffer.Header.ToPointer();"))); // array[0] = ...;; ifStatement.TrueStatements.Add(new CodeSnippetStatement(string.Format(" // TODO: set array's values: array[0] = ...;"))); // } ifStatement.TrueStatements.Add(new CodeSnippetStatement(" }// end of unsafe")); ifStatement.TrueStatements.Add(new CodeSnippetStatement(string.Format(" indexBuffer = buffer.GetBuffer();"))); ifStatement.TrueStatements.Add(new CodeSnippetStatement(" }// end of using")); } method.Statements.Add(new CodeMethodReturnStatement( new CodeVariableReferenceExpression(indexBuffer))); }
/// <summary> /// body of public PropertyBuffer GetProperty(string bufferName, string varNameInShader) /// </summary> /// <param name="method"></param> private void GetVertexAttributeBufferBody(CodeMemberMethod method, DataStructure dataStructure) { foreach (var item in dataStructure.PropertyList) { // if (bufferName == position) var ifStatement = new CodeConditionStatement( new CodeBinaryOperatorExpression( new CodeVariableReferenceExpression(bufferName), CodeBinaryOperatorType.IdentityEquality, new CodeVariableReferenceExpression(item.NameInModel))); method.Statements.Add(ifStatement); // if (positionBuffer != null) var ifStatement2 = new CodeConditionStatement( new CodeBinaryOperatorExpression( new CodeVariableReferenceExpression(item.BufferName), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null))); ifStatement.TrueStatements.Add(ifStatement2); // using (var buffer = new PropertyBuffer<vec3>(varNameInShader)) var usingBegin = new CodeSnippetStatement(string.Format(" using(var buffer = new VertexAttributeBuffer<{0}>({1}))", item.AttributeType, varNameInShader)); ifStatement2.TrueStatements.Add(usingBegin); ifStatement2.TrueStatements.Add(new CodeSnippetStatement(" {// begin of using")); var create = new CodeSnippetStatement(" buffer.Create();"); ifStatement2.TrueStatements.Add(create); // unsafe { ifStatement2.TrueStatements.Add(new CodeSnippetStatement(" unsafe")); ifStatement2.TrueStatements.Add(new CodeSnippetStatement(" {// begin of unsafe")); // var array = (vec3*)buffer.Header.ToPointer(); ifStatement2.TrueStatements.Add(new CodeSnippetStatement(string.Format(" var array = ({0}*)buffer.Header.ToPointer();", item.AttributeType))); // array[0] = ...;; ifStatement2.TrueStatements.Add(new CodeSnippetStatement(string.Format(" // TODO: set array's values: array[0] = ...;"))); // } ifStatement2.TrueStatements.Add(new CodeSnippetStatement(" }// end of unsafe")); ifStatement2.TrueStatements.Add(new CodeSnippetStatement(string.Format(" {0} = buffer.GetBuffer();", item.BufferName))); ifStatement2.TrueStatements.Add(new CodeSnippetStatement(" }// end of using")); ifStatement.TrueStatements.Add(new CodeMethodReturnStatement( new CodeVariableReferenceExpression(item.BufferName))); } // throw new NotImplementedException(); { // This CodeThrowExceptionStatement throws a new System.Exception. var throwException = new CodeThrowExceptionStatement( // codeExpression parameter indicates the exception to throw. // You must use an object create expression to new an exception here. new CodeObjectCreateExpression( // createType parameter inidicates the type of object to create. new CodeTypeReference(typeof(System.ArgumentException)), // parameters parameter indicates the constructor parameters. new CodeExpression[] { new CodePrimitiveExpression(bufferName) })); method.Statements.Add(throwException); } }
private CodeMemberMethod GetIndexDeclaration(DataStructure dataStructure) { var method = new CodeMemberMethod(); method.Attributes = MemberAttributes.Public | MemberAttributes.Final; method.ReturnType = new CodeTypeReference(typeof(IndexBuffer)); method.Name = "GetIndexBuffer"; return method; }
/// <summary> /// public IndexBuffer GetIndex() /// </summary> /// <param name="modelType"></param> /// <param name="dataStructure"></param> private void BuildGetIndex(CodeTypeDeclaration modelType, DataStructure dataStructure) { CodeMemberMethod method = GetIndexDeclaration(dataStructure); GetIndexBody(method, dataStructure); modelType.Members.Add(method); }
public override string GetFilename(DataStructure dataStructure) { return(string.Format("{0}.vert", dataStructure.TargetName)); }