Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Preparation for mesh drawing. (Sets all the GL Stuff)
- /// </summary>
- private static void Prepare(RenderOptions renderOptions)
- {
- //Clean up textures
- GL.ActiveTexture(TextureUnit.Texture3);
- GL.BindTexture(TextureTarget.Texture2D, 0);
- GL.ActiveTexture(TextureUnit.Texture2);
- GL.BindTexture(TextureTarget.Texture2D, 0);
- GL.ActiveTexture(TextureUnit.Texture1);
- GL.BindTexture(TextureTarget.Texture2D, 0);
- GL.ActiveTexture(TextureUnit.Texture0);
- GL.BindTexture(TextureTarget.Texture2D, 0);
- GL.ActiveTexture(TextureUnit.Texture0);
- //Set Wireframe
- if (renderOptions.Wireframe)
- GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
- GL.EnableClientState(ArrayCap.VertexArray);
- GL.EnableClientState(ArrayCap.NormalArray);
- GL.EnableClientState(ArrayCap.TextureCoordArray);
- GL.Enable(EnableCap.Texture2D);
- //GL.EnableClientState(ArrayCap.ColorArray);
- /*if (renderOptions.Color)
- {
- //Color
- GL.Disable(EnableCap.Texture2D);
- }
- else
- {
- //Texture
- }*/
- if (renderOptions.Shader != null)
- renderOptions.Shader.Bind();
- if (renderOptions.SetUniform != null)
- renderOptions.SetUniform(renderOptions.Shader);
- }
- /// <summary>
- /// Finalizes the draw cycle. (Sets all GL things to default again)
- /// </summary>
- /// <param name="renderOptions"></param>
- private static void Finalize(RenderOptions renderOptions)
- {
- //Unbind Shader
- if (renderOptions.Shader != null)
- renderOptions.Shader.UnBind();
- //Disabel Vertex attributes
- GL.DisableVertexAttribArray(0); // Tangents
- GL.DisableVertexAttribArray(1); // BiTangents
- //Reset Client states
- GL.DisableClientState(ArrayCap.VertexArray);
- GL.DisableClientState(ArrayCap.NormalArray);
- GL.DisableClientState(ArrayCap.ColorArray);
- GL.ClientActiveTexture(TextureUnit.Texture3);
- GL.DisableClientState(ArrayCap.TextureCoordArray);
- GL.ClientActiveTexture(TextureUnit.Texture2);
- GL.DisableClientState(ArrayCap.TextureCoordArray);
- GL.ClientActiveTexture(TextureUnit.Texture1);
- GL.DisableClientState(ArrayCap.TextureCoordArray);
- GL.ClientActiveTexture(TextureUnit.Texture0);
- GL.DisableClientState(ArrayCap.TextureCoordArray);
- //Reset Transform Matrix
- var ident = Matrix4.Identity;
- GL.LoadMatrix(ref ident);
- //Reset Wireframe
- if (renderOptions.Wireframe)
- GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
- //Reset Active Textures
- GL.ClientActiveTexture(TextureUnit.Texture0);
- GL.ActiveTexture(TextureUnit.Texture0);
- //Check for errors
- Tools.OpenGL.CheckError();
- }
- /// <summary>
- /// Starts drawing the mesh (comes before DrawCore)
- /// </summary>
- public virtual void BeginDraw(ref RenderOptions renderOptions)
- {
- // Set shader when not specified
- if (renderOptions.Shader == null)
- if (this.Shader == null)
- {
- renderOptions.Shader = DefaultShader;
- }
- else
- {
- renderOptions.Shader = this.Shader;
- }
- // Set uniform-action when not specified
- if (renderOptions.SetUniform == null)
- renderOptions.SetUniform = SetShaderParameters;
- //var texIndexLocation = renderOptions.Shader.GetAttribLocation("TextureIndex");
- //if (texIndexLocation >= 0)
- // GL.EnableVertexAttribArray(0);
- // Begin
- Prepare(renderOptions);
- GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);
- GL.VertexPointer(3, VertexPointerType.Float, Vertex.Stride, 0);
- GL.NormalPointer(NormalPointerType.Float, Vertex.Stride, Vector3.SizeInBytes);
- GL.ClientActiveTexture(TextureUnit.Texture0);
- GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, 2 * Vector3.SizeInBytes);
- GL.EnableClientState(ArrayCap.TextureCoordArray);
- GL.ClientActiveTexture(TextureUnit.Texture1);
- GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, (2 * Vector3.SizeInBytes) + (Vector2.SizeInBytes));
- GL.EnableClientState(ArrayCap.TextureCoordArray);
- GL.ClientActiveTexture(TextureUnit.Texture2);
- GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, (2 * Vector3.SizeInBytes) + (Vector2.SizeInBytes * 2));
- GL.EnableClientState(ArrayCap.TextureCoordArray);
- GL.ClientActiveTexture(TextureUnit.Texture3);
- GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, (2 * Vector3.SizeInBytes) + (Vector2.SizeInBytes * 3));
- GL.EnableClientState(ArrayCap.TextureCoordArray);
- //GL.VertexAttribPointer()
- //GL.EnableClientState(ArrayCap.TextureCoordArray);
- // GL.EnableClientState(ArrayCap.TextureCoordArray);
- //GL.EnableClientState(ArrayCap.TextureCoordArray);
- //GL.ColorPointer(3, ColorPointerType.Float, Vertex.Stride, new IntPtr((2 * Vector3.SizeInBytes) + Vector2.SizeInBytes));
- //GL.VertexAttribPointer(0, 2, TexCoordPointerType.Float, Vertex.Stride, new IntPtr(2 * Vector3.SizeInBytes));
- //if (texIndexLocation >= 0)
- // GL.VertexAttribPointer(texIndexLocation, 1, VertexAttribPointerType.Byte, false, Vertex.Stride, new IntPtr(2 * Vector3.SizeInBytes + Vertex.ColorSize + 1));
- // Tangents
- /*GL.EnableVertexAttribArray(0);
- GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vertex.Stride, new IntPtr(2 * Vector3.SizeInBytes + Vertex.ColorSize + Vector3.SizeInBytes));
- GL.BindAttribLocation(Shader.ProgramHandle, 0, "tangent");
- // BiTangents
- GL.EnableVertexAttribArray(1);
- GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, Vertex.Stride, new IntPtr(2 * Vector3.SizeInBytes + Vertex.ColorSize + Vector3.SizeInBytes * 2));
- GL.BindAttribLocation(Shader.ProgramHandle, 1, "bitangent");*/
- }
- /// <summary>
- /// Loads the translation and draws the mesh
- /// </summary>
- public virtual void DrawCore(ref RenderOptions renderOptions)
- {
- if (this.UseAlpha)
- {
- GL.DepthMask(false);
- GL.Enable(EnableCap.Blend);
- }
- GL.MatrixMode(MatrixMode.Modelview);
- GL.LoadMatrix(ref Translation);
- Tools.OpenGL.CheckError();
- int ColorLocation = GL.GetUniformLocation(renderOptions.Shader.ProgramHandle, "def_Color");
- if (ColorLocation >= 0)
- GL.Uniform4(ColorLocation, renderOptions.Color);
- // Bind all textures
- if (Textures.Count > 0)
- {
- foreach (var entry in Textures)
- {
- int mapLocation = GL.GetUniformLocation(renderOptions.Shader.ProgramHandle, entry.Value.Key);
- if (mapLocation >= 0)
- entry.Value.Value.Bind(entry.Key, mapLocation);
- }
- }
- if (_indices != null)
- GL.DrawElements(_primitiveType, _indices.Length, DrawElementsType.UnsignedInt, _indices);
- else
- GL.DrawArrays(_primitiveType, 0, _vertices.Length);
- if (Textures.Count > 0)
- {
- foreach (var entry in Textures)
- {
- entry.Value.Value.UnBind(entry.Key);
- }
- }
- if (this.UseAlpha)
- {
- GL.DepthMask(true);
- GL.Disable(EnableCap.Blend);
- }
- }
- /// <summary>
- /// Ends drawing the mesh (comes after DrawCore)
- /// </summary>
- public virtual void EndDraw(ref RenderOptions renderOptions)
- {
- //Reset vertex buffer
- GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
- // End
- Finalize(renderOptions);
- }
- /// <summary>
- /// Draws the VBO of this mesh to the screen. Use SetData() to generate/update the VBO
- /// </summary>
- public virtual void Draw()
- {
- if (!Visible) return; //invisible duh
- var renderOptions = new RenderOptions();
- renderOptions.Color = Color4.White;
- BeginDraw(ref renderOptions);
- DrawCore(ref renderOptions);
- EndDraw(ref renderOptions);
- }
- /// <summary>
- /// Draws the VBO of this mesh to the screen. Use SetData() to generate/update the VBO
- /// </summary>
- /// <param name="renderOptions">Rendering options</param>
- public virtual void Draw(RenderOptions renderOptions)
- {
- if (!Visible) return; //invisible duh
- BeginDraw(ref renderOptions);
- DrawCore(ref renderOptions);
- EndDraw(ref renderOptions);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement