Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Game : GameWindow
- {
- public Mesh<float> triangleMesh;
- public Game(int width, int height, string title)
- : base(width, height, GraphicsMode.Default, title)
- {
- }
- protected override void OnUpdateFrame(FrameEventArgs e)
- {
- var input = Keyboard.GetState();
- if (input.IsKeyDown(Key.Escape))
- {
- Exit();
- }
- base.OnUpdateFrame(e);
- }
- protected override void OnRenderFrame(FrameEventArgs e)
- {
- GL.Clear(ClearBufferMask.ColorBufferBit);
- triangleMesh.DrawMesh();
- SwapBuffers();
- base.OnRenderFrame(e);
- }
- float[] vertices =
- {
- //Position Texture coordinates
- 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
- 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
- -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
- -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
- };
- uint[] indices =
- {
- 0, 1, 3,
- 1, 2, 3
- };
- protected void Test()
- {
- var path = @"C:\Users\sn0wy\Desktop\Projects\OpenRK\OpenTKLearn\Shaders";
- Shader shader = new Shader(path+@"\shader.vert", path+@"\shader.frag");
- //Texture texture = new Texture(@"C:\Users\sn0wy\Desktop\Projects\OpenRK\OpenTKLearn\Resources\moyai_1f5ff.png");
- triangleMesh = new Mesh<float>(vertices, indices, shader);
- }
- protected override void OnLoad(EventArgs e)
- {
- GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- Test();
- base.OnLoad(e);
- }
- protected override void OnUnload(EventArgs e)
- {
- base.OnUnload(e);
- }
- }
- public class Shader : IDisposable
- {
- public readonly int Handle;
- private int _vertexShader;
- private int _fragmentShader;
- public Shader(string vertexPath, string fragmentPath)
- {
- CompileShader(vertexPath, ShaderType.VertexShader);
- CompileShader(fragmentPath, ShaderType.FragmentShader);
- Handle = GL.CreateProgram();
- GL.AttachShader(Handle, _vertexShader);
- GL.AttachShader(Handle, _fragmentShader);
- GL.LinkProgram(Handle);
- }
- public void Use()
- {
- GL.UseProgram(Handle);
- }
- private int CompileShader(string shaderPath, ShaderType shaderType)
- {
- string shaderSource;
- using (StreamReader reader = new StreamReader(shaderPath, Encoding.UTF8))
- {
- shaderSource = reader.ReadToEnd();
- }
- var shader = GL.CreateShader(shaderType);
- GL.ShaderSource(shader, shaderSource);
- GL.CompileShader(shader);
- string infoLogVert = GL.GetShaderInfoLog(shader);
- if (infoLogVert != System.String.Empty)
- Trace.WriteLine(infoLogVert);
- return shader;
- }
- public int GetVertexAttributeLocation(string str)
- {
- return GL.GetAttribLocation(_vertexShader, str);
- }
- public int GetFragmentAttributeLocation(string str)
- {
- return GL.GetAttribLocation(_fragmentShader, str);
- }
- #region Dispose
- private bool disposedValue = false;
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue)
- {
- GL.DeleteProgram(Handle);
- disposedValue = true;
- }
- }
- ~Shader()
- {
- GL.DeleteProgram(Handle);
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- #endregion
- }
- public class Mesh<T> : IDisposable where T : unmanaged
- {
- public uint VBOHandler { get; protected set; }
- public uint VAOHandler { get; protected set; }
- public uint EBOHandler { get; protected set; }
- public T[] Vertices { get; protected set; }
- public uint[] Indicies { get; protected set; }
- public Shader Shader;
- public Texture Texture;
- public Mesh(T[] vertices, uint[] indices, Shader shader)
- {
- Shader = shader;
- Vertices = vertices;
- Indicies = indices;
- GenerateVBO();
- GenerateEBO();
- Shader.Use();
- Texture = new Texture("Resources/maxresdefault.jpg");
- Texture.Use();
- GenerateVAO();
- EnableVertexAttributes();
- }
- public void DrawMesh()
- {
- GL.BindVertexArray(VAOHandler);
- Texture.Use();
- Shader.Use();
- GL.DrawElements(PrimitiveType.Triangles, Indicies.Length, DrawElementsType.UnsignedInt, 0);
- }
- private void GenerateVBO()
- {
- VBOHandler = (uint)GL.GenBuffer();
- GL.BindBuffer(BufferTarget.ArrayBuffer, VBOHandler);
- GL.BufferData(BufferTarget.ArrayBuffer, Vertices.Length * sizeof(float), Vertices, BufferUsageHint.StaticDraw);
- }
- private void GenerateEBO()
- {
- EBOHandler = (uint)GL.GenBuffer();
- GL.BindBuffer(BufferTarget.ElementArrayBuffer, EBOHandler);
- GL.BufferData(BufferTarget.ElementArrayBuffer, Indicies.Length * sizeof(uint), Indicies, BufferUsageHint.StaticDraw);
- }
- private void GenerateVAO()
- {
- VAOHandler = (uint)GL.GenVertexArray();
- GL.BindVertexArray(VAOHandler);
- GL.BindBuffer(BufferTarget.ArrayBuffer, VAOHandler);
- GL.BufferData(BufferTarget.ArrayBuffer, Vertices.Length * sizeof(float), Vertices, BufferUsageHint.StaticDraw);
- }
- private void EnableVertexAttributes()
- {
- var vertexLocation = Shader.GetVertexAttributeLocation("aPosition");
- GL.EnableVertexAttribArray(vertexLocation);
- GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);
- var texCoordLocation = Shader.GetVertexAttributeLocation("aTexCoord");
- GL.EnableVertexAttribArray(texCoordLocation);
- GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));
- }
- public void Dispose()
- {
- GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
- GL.DeleteBuffer(VBOHandler);
- GL.DeleteBuffer(EBOHandler);
- GL.DeleteBuffer(VAOHandler);
- }
- }
- public class Texture
- {
- public readonly int Handle;
- private Image<Rgba32> _image;
- private List<byte> _pixels;
- public float[] abc;
- public Texture(string path)
- {
- GetImage(path);
- Handle = GL.GenTexture();
- Use();
- //GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
- GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8,
- _image.Width, _image.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, _pixels.ToArray());
- // GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
- //optional: GenerateMitmaps();
- }
- public void Use()
- {
- GL.BindTexture(TextureTarget.Texture2D, Handle);
- }
- private void GetImage(string path)
- {
- _image = Image.Load<Rgba32>(path);
- _image.Mutate(x => x.Flip(FlipMode.Vertical));
- var a = new List<Rgba32>();
- for(int i = 0; i<_image.Height; i++)
- {
- var array = _image.GetPixelRowSpan(i).ToArray();
- for(int j = 0; j<_image.Width; j++)
- {
- a.Add(array[j]);
- }
- }
- Rgba32[] tempPixels = _image.GetPixelRowSpan(0).ToArray();
- _pixels = new List<byte>();
- foreach (Rgba32 p in a)
- {
- _pixels.Add(p.R);
- _pixels.Add(p.G);
- _pixels.Add(p.B);
- _pixels.Add(p.A);
- }
- Trace.WriteLine(_pixels.Count);
- //Trace.WriteLine(tempPixels);
- }
- private void GenerateMitmaps()
- {
- // GL.GenerateMipmap();
- }
- }
- Vertex:
- #version 420 core
- layout(location = 0) in vec3 aPosition;
- layout(location = 1) in vec2 aTexCoord;
- out vec2 texCoord;
- void main(void)
- {
- texCoord = aTexCoord;
- gl_Position = vec4(aPosition, 1.0);
- }
- Fragment:
- #version 420 core
- out vec4 outputColor;
- in vec2 texCoord;
- uniform sampler2D texture0;
- void main()
- {
- outputColor = texture(texture0, texCoord);
- }
Add Comment
Please, Sign In to add comment