Guest User

Untitled

a guest
Jun 20th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.74 KB | None | 0 0
  1. public class Game : GameWindow
  2.     {
  3.         public Mesh<float> triangleMesh;
  4.        
  5.         public Game(int width, int height, string title)
  6.             : base(width, height, GraphicsMode.Default, title)
  7.         {
  8.            
  9.         }
  10.        
  11.         protected override void OnUpdateFrame(FrameEventArgs e)
  12.         {
  13.             var input = Keyboard.GetState();
  14.            
  15.             if (input.IsKeyDown(Key.Escape))
  16.             {
  17.                 Exit();
  18.             }
  19.  
  20.             base.OnUpdateFrame(e);
  21.         }
  22.  
  23.         protected override void OnRenderFrame(FrameEventArgs e)
  24.         {
  25.             GL.Clear(ClearBufferMask.ColorBufferBit);
  26.            
  27.             triangleMesh.DrawMesh();
  28.            
  29.             SwapBuffers();
  30.             base.OnRenderFrame(e);
  31.         }
  32.  
  33.         float[] vertices =
  34.         {
  35.             //Position          Texture coordinates
  36.             0.5f,  0.5f, 0.0f, 1.0f, 1.0f, // top right
  37.             0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
  38.             -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
  39.             -0.5f,  0.5f, 0.0f, 0.0f, 1.0f  // top left
  40.         };
  41.            
  42.         uint[] indices =
  43.         {
  44.             0, 1, 3,
  45.             1, 2, 3
  46.         };
  47.        
  48.         protected void Test()
  49.         {
  50.  
  51.             var path = @"C:\Users\sn0wy\Desktop\Projects\OpenRK\OpenTKLearn\Shaders";
  52.             Shader shader = new Shader(path+@"\shader.vert", path+@"\shader.frag");
  53.             //Texture texture = new Texture(@"C:\Users\sn0wy\Desktop\Projects\OpenRK\OpenTKLearn\Resources\moyai_1f5ff.png");
  54.            
  55.             triangleMesh = new Mesh<float>(vertices, indices, shader);
  56.         }
  57.        
  58.        
  59.         protected override void OnLoad(EventArgs e)
  60.         {
  61.             GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  62.            
  63.             Test();
  64.            
  65.             base.OnLoad(e);
  66.         }
  67.  
  68.         protected override void OnUnload(EventArgs e)
  69.         {
  70.             base.OnUnload(e);
  71.         }
  72.     }
  73.  
  74. public class Shader : IDisposable
  75.     {
  76.         public readonly int Handle;
  77.         private int _vertexShader;
  78.         private int _fragmentShader;
  79.        
  80.         public Shader(string vertexPath, string fragmentPath)
  81.         {
  82.             CompileShader(vertexPath, ShaderType.VertexShader);
  83.             CompileShader(fragmentPath, ShaderType.FragmentShader);
  84.            
  85.             Handle = GL.CreateProgram();
  86.  
  87.             GL.AttachShader(Handle, _vertexShader);
  88.             GL.AttachShader(Handle, _fragmentShader);
  89.  
  90.             GL.LinkProgram(Handle);
  91.         }
  92.  
  93.         public void Use()
  94.         {
  95.             GL.UseProgram(Handle);
  96.         }
  97.        
  98.         private int CompileShader(string shaderPath, ShaderType shaderType)
  99.         {
  100.             string shaderSource;
  101.  
  102.             using (StreamReader reader = new StreamReader(shaderPath, Encoding.UTF8))
  103.             {
  104.                 shaderSource = reader.ReadToEnd();
  105.             }
  106.            
  107.             var shader = GL.CreateShader(shaderType);
  108.            
  109.             GL.ShaderSource(shader, shaderSource);
  110.             GL.CompileShader(shader);
  111.  
  112.             string infoLogVert = GL.GetShaderInfoLog(shader);
  113.             if (infoLogVert != System.String.Empty)
  114.                 Trace.WriteLine(infoLogVert);
  115.  
  116.             return shader;
  117.         }
  118.        
  119.         public int GetVertexAttributeLocation(string str)
  120.         {
  121.             return GL.GetAttribLocation(_vertexShader, str);
  122.         }
  123.         public int GetFragmentAttributeLocation(string str)
  124.         {
  125.             return GL.GetAttribLocation(_fragmentShader, str);
  126.         }
  127.  
  128.         #region Dispose
  129.        
  130.         private bool disposedValue = false;
  131.  
  132.         protected virtual void Dispose(bool disposing)
  133.         {
  134.             if (!disposedValue)
  135.             {
  136.                 GL.DeleteProgram(Handle);
  137.  
  138.                 disposedValue = true;
  139.             }
  140.         }
  141.  
  142.         ~Shader()
  143.         {
  144.             GL.DeleteProgram(Handle);
  145.         }
  146.  
  147.         public void Dispose()
  148.         {
  149.             Dispose(true);
  150.             GC.SuppressFinalize(this);
  151.         }
  152.        
  153.         #endregion
  154.     }
  155.  
  156.  
  157. public class Mesh<T> : IDisposable where T : unmanaged
  158.     {
  159.         public uint VBOHandler { get; protected set; }
  160.         public uint VAOHandler { get; protected set; }
  161.         public uint EBOHandler { get; protected set; }
  162.         public T[] Vertices { get; protected set; }
  163.  
  164.         public uint[] Indicies { get; protected set; }
  165.  
  166.         public Shader Shader;
  167.         public Texture Texture;
  168.  
  169.         public Mesh(T[] vertices, uint[] indices, Shader shader)
  170.         {
  171.             Shader = shader;
  172.             Vertices = vertices;
  173.             Indicies = indices;
  174.  
  175.             GenerateVBO();
  176.             GenerateEBO();
  177.  
  178.             Shader.Use();
  179.             Texture = new Texture("Resources/maxresdefault.jpg");
  180.             Texture.Use();
  181.  
  182.             GenerateVAO();
  183.             EnableVertexAttributes();
  184.         }
  185.  
  186.         public void DrawMesh()
  187.         {
  188.             GL.BindVertexArray(VAOHandler);
  189.            
  190.             Texture.Use();
  191.             Shader.Use();
  192.            
  193.             GL.DrawElements(PrimitiveType.Triangles, Indicies.Length, DrawElementsType.UnsignedInt, 0);
  194.         }
  195.  
  196.         private void GenerateVBO()
  197.         {
  198.             VBOHandler = (uint)GL.GenBuffer();
  199.            
  200.             GL.BindBuffer(BufferTarget.ArrayBuffer, VBOHandler);
  201.             GL.BufferData(BufferTarget.ArrayBuffer, Vertices.Length * sizeof(float), Vertices, BufferUsageHint.StaticDraw);
  202.         }
  203.  
  204.         private void GenerateEBO()
  205.         {
  206.             EBOHandler = (uint)GL.GenBuffer();
  207.            
  208.             GL.BindBuffer(BufferTarget.ElementArrayBuffer, EBOHandler);
  209.             GL.BufferData(BufferTarget.ElementArrayBuffer, Indicies.Length * sizeof(uint), Indicies, BufferUsageHint.StaticDraw);
  210.         }
  211.        
  212.         private void GenerateVAO()
  213.         {
  214.             VAOHandler = (uint)GL.GenVertexArray();
  215.  
  216.             GL.BindVertexArray(VAOHandler);
  217.             GL.BindBuffer(BufferTarget.ArrayBuffer, VAOHandler);
  218.             GL.BufferData(BufferTarget.ArrayBuffer, Vertices.Length * sizeof(float), Vertices, BufferUsageHint.StaticDraw);
  219.         }
  220.  
  221.         private void EnableVertexAttributes()
  222.         {
  223.             var vertexLocation = Shader.GetVertexAttributeLocation("aPosition");
  224.             GL.EnableVertexAttribArray(vertexLocation);
  225.             GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);
  226.            
  227.             var texCoordLocation = Shader.GetVertexAttributeLocation("aTexCoord");
  228.             GL.EnableVertexAttribArray(texCoordLocation);
  229.             GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));
  230.         }
  231.  
  232.         public void Dispose()
  233.         {
  234.             GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  235.  
  236.             GL.DeleteBuffer(VBOHandler);
  237.             GL.DeleteBuffer(EBOHandler);
  238.             GL.DeleteBuffer(VAOHandler);
  239.         }
  240.     }
  241.  
  242. public class Texture
  243.     {
  244.         public readonly int Handle;
  245.        
  246.         private Image<Rgba32> _image;
  247.         private List<byte> _pixels;
  248.         public float[] abc;
  249.  
  250.         public Texture(string path)
  251.         {
  252.             GetImage(path);
  253.            
  254.             Handle = GL.GenTexture();
  255.             Use();
  256.            
  257.             //GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
  258.            
  259.             GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
  260.             GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  261.            
  262.             GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8,
  263.                 _image.Width, _image.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, _pixels.ToArray());
  264.            
  265.            
  266.             // GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
  267.             //optional: GenerateMitmaps();
  268.         }
  269.  
  270.         public void Use()
  271.         {
  272.             GL.BindTexture(TextureTarget.Texture2D, Handle);
  273.         }
  274.        
  275.         private void GetImage(string path)
  276.         {
  277.             _image = Image.Load<Rgba32>(path);
  278.            
  279.             _image.Mutate(x => x.Flip(FlipMode.Vertical));
  280.  
  281.             var a = new List<Rgba32>();
  282.  
  283.             for(int i = 0; i<_image.Height; i++)
  284.             {
  285.                 var array = _image.GetPixelRowSpan(i).ToArray();
  286.                 for(int j = 0; j<_image.Width; j++)
  287.                 {
  288.                     a.Add(array[j]);
  289.                 }
  290.             }
  291.  
  292.  
  293.             Rgba32[] tempPixels = _image.GetPixelRowSpan(0).ToArray();
  294.            
  295.             _pixels = new List<byte>();
  296.  
  297.             foreach (Rgba32 p in a)
  298.             {
  299.                 _pixels.Add(p.R);
  300.                 _pixels.Add(p.G);
  301.                 _pixels.Add(p.B);
  302.                 _pixels.Add(p.A);
  303.             }
  304.            
  305.             Trace.WriteLine(_pixels.Count);
  306.             //Trace.WriteLine(tempPixels);
  307.         }
  308.  
  309.         private void GenerateMitmaps()
  310.         {
  311.            // GL.GenerateMipmap();
  312.         }
  313.     }
  314.  
  315.  
  316. Vertex:
  317.  
  318. #version 420 core
  319.  
  320. layout(location = 0) in vec3 aPosition;
  321.  
  322. layout(location = 1) in vec2 aTexCoord;
  323.  
  324. out vec2 texCoord;
  325.  
  326. void main(void)
  327. {
  328.     texCoord = aTexCoord;
  329.  
  330.     gl_Position = vec4(aPosition, 1.0);
  331. }
  332.  
  333.  
  334. Fragment:
  335.  
  336. #version 420 core
  337.  
  338. out vec4 outputColor;
  339.  
  340. in vec2 texCoord;
  341.  
  342. uniform sampler2D texture0;
  343.  
  344. void main()
  345. {
  346.     outputColor = texture(texture0, texCoord);
  347. }
Add Comment
Please, Sign In to add comment