Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using OpenGL;
- namespace pEngine.Core.Graphics.Renderer
- {
- public class Renderer
- {
- /// <summary>
- /// Creates a new instance of <see cref="Renderer"/>.
- /// </summary>
- public Renderer()
- {
- }
- /// <summary>
- /// Initialize all OpenGL related resources.
- /// </summary>
- public void Initialize()
- {
- InizializzaLaMerda();
- }
- #region Modules
- #endregion
- #region Ti prego cancellami
- string vs = @"
- #version 330 core
- layout(location = 0) in vec3 vertexPosition_modelspace;
- void main()
- {
- gl_Position.xyz = vertexPosition_modelspace;
- gl_Position.w = 1.0;
- }
- ";
- string fs = @"
- #version 330 core
- out vec3 color;
- void main()
- {
- color = vec3(1,0,0);
- }
- ";
- uint vao;
- uint vertexbuffer;
- uint programID;
- float[] g_vertex_buffer_data = new float[9]
- {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f,
- };
- void InizializzaLaMerda()
- {
- vao = Gl.GenVertexArray();
- Gl.BindVertexArray(vao);
- programID = LoadShader(vs, fs);
- // Generate 1 buffer, put the resulting identifier in vertexbuffer
- vertexbuffer = Gl.GenBuffer();
- // The following commands will talk about our 'vertexbuffer' buffer
- Gl.BindBuffer(BufferTargetARB.ArrayBuffer, vertexbuffer);
- // Give our vertices to OpenGL.
- Gl.BufferData(BufferTargetARB.ArrayBuffer, sizeof(float) * 9, g_vertex_buffer_data, BufferUsageARB.StaticDraw);
- }
- uint LoadShader(string vertContent, string fragContent)
- {
- uint VertexShaderID = Gl.CreateShader(Gl.VERTEX_SHADER);
- uint FragmentShaderID = Gl.CreateShader(Gl.FRAGMENT_SHADER);
- Gl.ShaderSource(VertexShaderID, new string[] { vertContent });
- Gl.CompileShader(VertexShaderID);
- Gl.ShaderSource(FragmentShaderID, new string[] { fragContent });
- Gl.CompileShader(FragmentShaderID);
- uint programId = Gl.CreateProgram();
- Gl.AttachShader(programId, VertexShaderID);
- Gl.AttachShader(programId, FragmentShaderID);
- Gl.LinkProgram(programId);
- Gl.DetachShader(programId, VertexShaderID);
- Gl.DetachShader(programId, FragmentShaderID);
- Gl.DeleteShader(VertexShaderID);
- Gl.DeleteShader(FragmentShaderID);
- return programId;
- }
- #endregion
- #region Rendering
- /// <summary>
- /// Takes all frame assets and draw it
- /// on the backbuffer.
- /// </summary>
- /// <param name="assets">Assets to draw.</param>
- public void DrawFrame(List<Asset> assets)
- {
- Gl.UseProgram(programID);
- // 1rst attribute buffer : vertices
- Gl.EnableVertexAttribArray(0);
- Gl.BindBuffer(BufferTargetARB.ArrayBuffer, vertexbuffer);
- Gl.VertexAttribPointer(
- 0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
- 3, // size
- Gl.FLOAT, // type
- false, // normalized?
- 0, // stride
- IntPtr.Zero // array buffer offset
- );
- // Draw the triangle !
- Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
- Gl.DisableVertexAttribArray(0);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement