Advertisement
maxkhl

Draw Logik

Aug 11th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.73 KB | None | 0 0
  1. /// <summary>
  2.         /// Preparation for mesh drawing. (Sets all the GL Stuff)
  3.         /// </summary>
  4.         private static void Prepare(RenderOptions renderOptions)
  5.         {
  6.             //Clean up textures
  7.             GL.ActiveTexture(TextureUnit.Texture3);
  8.             GL.BindTexture(TextureTarget.Texture2D, 0);
  9.  
  10.             GL.ActiveTexture(TextureUnit.Texture2);
  11.             GL.BindTexture(TextureTarget.Texture2D, 0);
  12.  
  13.             GL.ActiveTexture(TextureUnit.Texture1);
  14.             GL.BindTexture(TextureTarget.Texture2D, 0);
  15.  
  16.             GL.ActiveTexture(TextureUnit.Texture0);
  17.             GL.BindTexture(TextureTarget.Texture2D, 0);
  18.  
  19.             GL.ActiveTexture(TextureUnit.Texture0);
  20.  
  21.             //Set Wireframe
  22.             if (renderOptions.Wireframe)
  23.                 GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
  24.  
  25.             GL.EnableClientState(ArrayCap.VertexArray);
  26.             GL.EnableClientState(ArrayCap.NormalArray);
  27.  
  28.             GL.EnableClientState(ArrayCap.TextureCoordArray);
  29.             GL.Enable(EnableCap.Texture2D);
  30.             //GL.EnableClientState(ArrayCap.ColorArray);
  31.             /*if (renderOptions.Color)
  32.             {
  33.                 //Color
  34.                 GL.Disable(EnableCap.Texture2D);
  35.             }
  36.             else
  37.             {
  38.                 //Texture
  39.             }*/
  40.  
  41.             if (renderOptions.Shader != null)
  42.                 renderOptions.Shader.Bind();
  43.  
  44.             if (renderOptions.SetUniform != null)
  45.                 renderOptions.SetUniform(renderOptions.Shader);
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Finalizes the draw cycle. (Sets all GL things to default again)
  50.         /// </summary>
  51.         /// <param name="renderOptions"></param>
  52.         private static void Finalize(RenderOptions renderOptions)
  53.         {
  54.             //Unbind Shader
  55.             if (renderOptions.Shader != null)
  56.                 renderOptions.Shader.UnBind();
  57.  
  58.             //Disabel Vertex attributes
  59.             GL.DisableVertexAttribArray(0); // Tangents
  60.             GL.DisableVertexAttribArray(1); // BiTangents
  61.  
  62.             //Reset Client states
  63.             GL.DisableClientState(ArrayCap.VertexArray);
  64.             GL.DisableClientState(ArrayCap.NormalArray);
  65.  
  66.             GL.DisableClientState(ArrayCap.ColorArray);
  67.  
  68.             GL.ClientActiveTexture(TextureUnit.Texture3);
  69.             GL.DisableClientState(ArrayCap.TextureCoordArray);
  70.  
  71.             GL.ClientActiveTexture(TextureUnit.Texture2);
  72.             GL.DisableClientState(ArrayCap.TextureCoordArray);
  73.  
  74.             GL.ClientActiveTexture(TextureUnit.Texture1);
  75.             GL.DisableClientState(ArrayCap.TextureCoordArray);
  76.  
  77.             GL.ClientActiveTexture(TextureUnit.Texture0);
  78.             GL.DisableClientState(ArrayCap.TextureCoordArray);
  79.  
  80.             //Reset Transform Matrix
  81.             var ident = Matrix4.Identity;
  82.             GL.LoadMatrix(ref ident);
  83.  
  84.             //Reset Wireframe
  85.             if (renderOptions.Wireframe)
  86.                 GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
  87.  
  88.             //Reset Active Textures
  89.             GL.ClientActiveTexture(TextureUnit.Texture0);
  90.             GL.ActiveTexture(TextureUnit.Texture0);
  91.  
  92.             //Check for errors
  93.             Tools.OpenGL.CheckError();
  94.         }
  95.  
  96.         /// <summary>
  97.         /// Starts drawing the mesh (comes before DrawCore)
  98.         /// </summary>
  99.         public virtual void BeginDraw(ref RenderOptions renderOptions)
  100.         {
  101.             // Set shader when not specified
  102.             if (renderOptions.Shader == null)
  103.                 if (this.Shader == null)
  104.                 {
  105.                     renderOptions.Shader = DefaultShader;
  106.                 }
  107.                 else
  108.                 {
  109.                     renderOptions.Shader = this.Shader;
  110.                 }
  111.  
  112.  
  113.             // Set uniform-action when not specified
  114.             if (renderOptions.SetUniform == null)
  115.                 renderOptions.SetUniform = SetShaderParameters;
  116.  
  117.  
  118.  
  119.             //var texIndexLocation = renderOptions.Shader.GetAttribLocation("TextureIndex");
  120.             //if (texIndexLocation >= 0)
  121.             //    GL.EnableVertexAttribArray(0);
  122.  
  123.             // Begin
  124.             Prepare(renderOptions);
  125.  
  126.  
  127.             GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);
  128.  
  129.             GL.VertexPointer(3, VertexPointerType.Float, Vertex.Stride, 0);
  130.  
  131.             GL.NormalPointer(NormalPointerType.Float, Vertex.Stride, Vector3.SizeInBytes);
  132.  
  133.  
  134.  
  135.             GL.ClientActiveTexture(TextureUnit.Texture0);
  136.             GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, 2 * Vector3.SizeInBytes);
  137.             GL.EnableClientState(ArrayCap.TextureCoordArray);
  138.  
  139.             GL.ClientActiveTexture(TextureUnit.Texture1);
  140.             GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, (2 * Vector3.SizeInBytes) + (Vector2.SizeInBytes));
  141.             GL.EnableClientState(ArrayCap.TextureCoordArray);
  142.  
  143.             GL.ClientActiveTexture(TextureUnit.Texture2);
  144.             GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, (2 * Vector3.SizeInBytes) + (Vector2.SizeInBytes * 2));
  145.             GL.EnableClientState(ArrayCap.TextureCoordArray);
  146.  
  147.             GL.ClientActiveTexture(TextureUnit.Texture3);
  148.             GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, (2 * Vector3.SizeInBytes) + (Vector2.SizeInBytes * 3));
  149.             GL.EnableClientState(ArrayCap.TextureCoordArray);
  150.  
  151.             //GL.VertexAttribPointer()
  152.             //GL.EnableClientState(ArrayCap.TextureCoordArray);
  153.            // GL.EnableClientState(ArrayCap.TextureCoordArray);
  154.             //GL.EnableClientState(ArrayCap.TextureCoordArray);
  155.  
  156.             //GL.ColorPointer(3, ColorPointerType.Float, Vertex.Stride, new IntPtr((2 * Vector3.SizeInBytes) + Vector2.SizeInBytes));
  157.  
  158.             //GL.VertexAttribPointer(0, 2, TexCoordPointerType.Float, Vertex.Stride, new IntPtr(2 * Vector3.SizeInBytes));
  159.             //if (texIndexLocation >= 0)
  160.             //    GL.VertexAttribPointer(texIndexLocation, 1, VertexAttribPointerType.Byte, false, Vertex.Stride, new IntPtr(2 * Vector3.SizeInBytes + Vertex.ColorSize + 1));
  161.  
  162.             // Tangents
  163.             /*GL.EnableVertexAttribArray(0);
  164.             GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vertex.Stride, new IntPtr(2 * Vector3.SizeInBytes + Vertex.ColorSize + Vector3.SizeInBytes));
  165.             GL.BindAttribLocation(Shader.ProgramHandle, 0, "tangent");
  166.  
  167.             // BiTangents
  168.             GL.EnableVertexAttribArray(1);
  169.             GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, Vertex.Stride, new IntPtr(2 * Vector3.SizeInBytes + Vertex.ColorSize + Vector3.SizeInBytes * 2));
  170.             GL.BindAttribLocation(Shader.ProgramHandle, 1, "bitangent");*/
  171.         }
  172.  
  173.         /// <summary>
  174.         /// Loads the translation and draws the mesh
  175.         /// </summary>
  176.         public virtual void DrawCore(ref RenderOptions renderOptions)
  177.         {
  178.             if (this.UseAlpha)
  179.             {
  180.                 GL.DepthMask(false);
  181.                 GL.Enable(EnableCap.Blend);
  182.             }
  183.  
  184.             GL.MatrixMode(MatrixMode.Modelview);
  185.  
  186.             GL.LoadMatrix(ref Translation);
  187.  
  188.             Tools.OpenGL.CheckError();
  189.  
  190.             int ColorLocation = GL.GetUniformLocation(renderOptions.Shader.ProgramHandle, "def_Color");
  191.             if (ColorLocation >= 0)
  192.                 GL.Uniform4(ColorLocation, renderOptions.Color);
  193.  
  194.             // Bind all textures
  195.             if (Textures.Count > 0)
  196.             {
  197.                 foreach (var entry in Textures)
  198.                 {
  199.                     int mapLocation = GL.GetUniformLocation(renderOptions.Shader.ProgramHandle, entry.Value.Key);
  200.                     if (mapLocation >= 0)
  201.                         entry.Value.Value.Bind(entry.Key, mapLocation);
  202.                 }
  203.             }
  204.  
  205.             if (_indices != null)
  206.                 GL.DrawElements(_primitiveType, _indices.Length, DrawElementsType.UnsignedInt, _indices);
  207.             else
  208.                 GL.DrawArrays(_primitiveType, 0, _vertices.Length);
  209.  
  210.  
  211.             if (Textures.Count > 0)
  212.             {
  213.                 foreach (var entry in Textures)
  214.                 {
  215.                     entry.Value.Value.UnBind(entry.Key);
  216.                 }
  217.             }
  218.  
  219.  
  220.             if (this.UseAlpha)
  221.             {
  222.                 GL.DepthMask(true);
  223.                 GL.Disable(EnableCap.Blend);
  224.             }
  225.         }
  226.         /// <summary>
  227.         /// Ends drawing the mesh (comes after DrawCore)
  228.         /// </summary>
  229.         public virtual void EndDraw(ref RenderOptions renderOptions)
  230.         {
  231.             //Reset vertex buffer
  232.             GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  233.  
  234.             // End
  235.             Finalize(renderOptions);
  236.         }
  237.  
  238.         /// <summary>
  239.         /// Draws the VBO of this mesh to the screen. Use SetData() to generate/update the VBO
  240.         /// </summary>
  241.         public virtual void Draw()
  242.         {
  243.             if (!Visible) return; //invisible duh
  244.  
  245.             var renderOptions = new RenderOptions();
  246.             renderOptions.Color = Color4.White;
  247.  
  248.             BeginDraw(ref renderOptions);
  249.  
  250.             DrawCore(ref renderOptions);
  251.  
  252.             EndDraw(ref renderOptions);
  253.         }
  254.  
  255.         /// <summary>
  256.         /// Draws the VBO of this mesh to the screen. Use SetData() to generate/update the VBO
  257.         /// </summary>
  258.         /// <param name="renderOptions">Rendering options</param>
  259.         public virtual void Draw(RenderOptions renderOptions)
  260.         {
  261.             if (!Visible) return; //invisible duh
  262.  
  263.             BeginDraw(ref renderOptions);
  264.  
  265.             DrawCore(ref renderOptions);
  266.  
  267.             EndDraw(ref renderOptions);
  268.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement