cscの作法 その373

# 概要
cscの作法、調べてみた。
opentkのgamewindow使ってみた。
Shader使ってみた。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/bc769149-739f-d3f8-c0f0-4dae1fcb1f07.png)

# サンプルコード

```
using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;

namespace App
{
	class Gwin: GameWindow {
		double counter = 0;
		private readonly float[] Vertices = {
			-0.5f, -0.5f, 0.0f, 
			 0.5f, -0.5f, 0.0f, 
			 0.0f,  0.5f, 0.0f,
		};
		private int[] VertexBufferObjects = new int[1];
		private int[] VertexArrayObjects = new int[1];
		private readonly string VertexShaderSourceCode = "#version 330 core\r\n" +
			"layout(location = 0) in vec3 InPosition;\r\n" +
			"void main() {\r\n" +
			"	gl_Position = vec4(InPosition, 1.0);\r\n" +
			"}";
		private readonly string FragmentShaderSourceCode = "#version 330 core\r\n" +
			"out vec4 FragColor;\r\n" +
			"void main() {\r\n" +
			"	FragColor = vec4(1.0, 1.0, 1.0, 1.0);\r\n" +
			"}";
		private int VertexShader;
		private int FragmentShader;
		private int Program;
		private int PositionLocation;
		public Gwin(int width, int height, GraphicsMode mode, string title): base(width, height, mode, title) {
			this.VSync = VSyncMode.On;
			InitializeBuffer();
			InitializeShader();
			InitializeProgram();
			InitializeVertexArrayObject();
		}
		protected override void OnUpdateFrame(FrameEventArgs args) {
			base.OnUpdateFrame(args);
		}
		protected override void OnRenderFrame(FrameEventArgs e) {
			base.OnRenderFrame(e);
			counter += e.Time;
			GL.Clear(ClearBufferMask.ColorBufferBit);
			GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObjects[0]);
			GL.BindVertexArray(VertexArrayObjects[0]);
			GL.UseProgram(Program);
			GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
			this.SwapBuffers();
		}
		private void InitializeBuffer() {
			GL.GenBuffers(1, VertexBufferObjects);
			GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObjects[0]);
			GL.BufferData(BufferTarget.ArrayBuffer, 4 * 9, Vertices, BufferUsageHint.StaticDraw);
			GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
		}
		private void InitializeShader() {
			VertexShader = CreateShader(ShaderType.VertexShader, VertexShaderSourceCode);
			FragmentShader = CreateShader(ShaderType.FragmentShader, FragmentShaderSourceCode);
		}
		private int CreateShader(ShaderType shaderType, string sourceCode) {
			int shader = GL.CreateShader(shaderType);
			GL.ShaderSource(shader, sourceCode);
			GL.CompileShader(shader);
			string log = GL.GetShaderInfoLog(shader);
			return shader;
		}
		private void InitializeProgram() {
			Program = GL.CreateProgram();
			GL.AttachShader(Program, VertexShader);
			GL.AttachShader(Program, FragmentShader);
			GL.LinkProgram(Program);
			GL.UseProgram(Program);
			PositionLocation = GL.GetAttribLocation(Program, "InPosition");
			GL.UseProgram(0);
		}
		private void InitializeVertexArrayObject() {
			GL.GenVertexArrays(1, VertexArrayObjects);
			GL.BindVertexArray(VertexArrayObjects[0]);
			GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObjects[0]);
			GL.EnableVertexAttribArray(PositionLocation);
			GL.VertexAttribPointer(PositionLocation, 3, VertexAttribPointerType.Float, false, 12, 0);
			GL.EnableVertexAttribArray(0);
			GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
			GL.BindVertexArray(0);
		}
		protected override void OnResize(EventArgs e) {
			GL.Viewport(0, 0, Width, Height);
		}
		protected override void OnUnload(EventArgs e) {
			base.OnUnload(e);
			GL.DeleteVertexArrays(1, VertexArrayObjects);
			GL.DeleteBuffers(1, VertexBufferObjects);
			GL.DeleteProgram(Program);
		}
	}
	class Program {
		static void Main(string[] args) {
			Gwin window = new Gwin(500, 500, OpenTK.Graphics.GraphicsMode.Default, "Test");
			window.Run();
		}
	}
}





```


以上。