Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. using OpenGL;
  6.  
  7. namespace pEngine.Core.Graphics.Renderer
  8. {
  9.     public class Renderer
  10.     {
  11.         /// <summary>
  12.         /// Creates a new instance of <see cref="Renderer"/>.
  13.         /// </summary>
  14.         public Renderer()
  15.         {
  16.  
  17.         }
  18.  
  19.         /// <summary>
  20.         /// Initialize all OpenGL related resources.
  21.         /// </summary>
  22.         public void Initialize()
  23.         {
  24.             InizializzaLaMerda();
  25.         }
  26.  
  27.         #region Modules
  28.  
  29.         #endregion
  30.  
  31.         #region Ti prego cancellami
  32.  
  33.         string vs = @"
  34.                             #version 330 core
  35.  
  36.                             layout(location = 0) in vec3 vertexPosition_modelspace;
  37.  
  38.                             void main()
  39.                             {
  40.  
  41.                                 gl_Position.xyz = vertexPosition_modelspace;
  42.                                 gl_Position.w = 1.0;
  43.                             }
  44.         ";
  45.  
  46.         string fs = @"
  47.                             #version 330 core
  48.  
  49.                             out vec3 color;
  50.  
  51.                             void main()
  52.                             {
  53.                                 color = vec3(1,0,0);
  54.                             }
  55.         ";
  56.  
  57.         uint vao;
  58.         uint vertexbuffer;
  59.         uint programID;
  60.  
  61.         float[] g_vertex_buffer_data = new float[9]
  62.             {
  63.                 -1.0f, -1.0f, 0.0f,
  64.                 1.0f, -1.0f, 0.0f,
  65.                 0.0f,  1.0f, 0.0f,
  66.             };
  67.  
  68.         void InizializzaLaMerda()
  69.         {
  70.  
  71.             vao = Gl.GenVertexArray();
  72.             Gl.BindVertexArray(vao);
  73.  
  74.             programID = LoadShader(vs, fs);
  75.  
  76.             // Generate 1 buffer, put the resulting identifier in vertexbuffer
  77.             vertexbuffer = Gl.GenBuffer();
  78.             // The following commands will talk about our 'vertexbuffer' buffer
  79.             Gl.BindBuffer(BufferTargetARB.ArrayBuffer, vertexbuffer);
  80.             // Give our vertices to OpenGL.
  81.             Gl.BufferData(BufferTargetARB.ArrayBuffer, sizeof(float) * 9, g_vertex_buffer_data, BufferUsageARB.StaticDraw);
  82.         }
  83.  
  84.         uint LoadShader(string vertContent, string fragContent)
  85.         {
  86.             uint VertexShaderID = Gl.CreateShader(Gl.VERTEX_SHADER);
  87.             uint FragmentShaderID = Gl.CreateShader(Gl.FRAGMENT_SHADER);
  88.  
  89.             Gl.ShaderSource(VertexShaderID, new string[] { vertContent });
  90.             Gl.CompileShader(VertexShaderID);
  91.  
  92.             Gl.ShaderSource(FragmentShaderID, new string[] { fragContent });
  93.             Gl.CompileShader(FragmentShaderID);
  94.  
  95.             uint programId = Gl.CreateProgram();
  96.  
  97.             Gl.AttachShader(programId, VertexShaderID);
  98.             Gl.AttachShader(programId, FragmentShaderID);
  99.             Gl.LinkProgram(programId);
  100.  
  101.             Gl.DetachShader(programId, VertexShaderID);
  102.             Gl.DetachShader(programId, FragmentShaderID);
  103.  
  104.             Gl.DeleteShader(VertexShaderID);
  105.             Gl.DeleteShader(FragmentShaderID);
  106.  
  107.             return programId;
  108.         }
  109.  
  110.         #endregion
  111.  
  112.         #region Rendering
  113.  
  114.         /// <summary>
  115.         /// Takes all frame assets and draw it
  116.         /// on the backbuffer.
  117.         /// </summary>
  118.         /// <param name="assets">Assets to draw.</param>
  119.         public void DrawFrame(List<Asset> assets)
  120.         {
  121.             Gl.UseProgram(programID);
  122.  
  123.             // 1rst attribute buffer : vertices
  124.             Gl.EnableVertexAttribArray(0);
  125.             Gl.BindBuffer(BufferTargetARB.ArrayBuffer, vertexbuffer);
  126.             Gl.VertexAttribPointer(
  127.                0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
  128.                3,                  // size
  129.                Gl.FLOAT,           // type
  130.                false,           // normalized?
  131.                0,                  // stride
  132.                IntPtr.Zero            // array buffer offset
  133.             );
  134.             // Draw the triangle !
  135.             Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
  136.             Gl.DisableVertexAttribArray(0);
  137.         }
  138.  
  139.         #endregion
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement