@@ -13,24 +13,75 @@ class Window : GameWindow
1313 {
1414 int _vertexBuffer ;
1515
16+ int _vertexArray ;
17+
18+ int _shader ;
19+
1620 public readonly float [ ] _vertices =
1721 {
18- 0.0f , - 0.5f , 0.0f , // Top
19- - 0.5f , 0.5f , 0.0f , // Bottom-Left
20- 0.5f , 0.5f , 0.0f // Bottom-Right
22+ 0.0f , 0.5f , 0.0f , // Top
23+ - 0.5f , - 0.5f , 0.0f , // Bottom-Left
24+ 0.5f , - 0.5f , 0.0f // Bottom-Right
2125 } ;
2226 public Window ( GameWindowSettings gameWindowSettings , NativeWindowSettings nativeWindowSettings ) : base ( gameWindowSettings , nativeWindowSettings ) { }
27+
28+ static int CompileShader ( string uri , ShaderType type )
29+ {
30+ int shader = GL . CreateShader ( type ) ;
31+
32+ GL . ShaderSource ( shader , File . ReadAllText ( uri ) ) ; // Import the source code of the shader
33+ GL . CompileShader ( shader ) ;
34+
35+ GL . GetShader ( shader , ShaderParameter . CompileStatus , out int compileStatus ) ; // compileStatus is 0 if compile error
36+ if ( compileStatus == 0 )
37+ {
38+ Console . WriteLine ( "{0}: {1}" , type . ToString ( ) , GL . GetShaderInfoLog ( shader ) ) ;
39+ throw new Exception ( ) ; // TODO: Remove this exception
40+ }
41+
42+ return shader ;
43+ }
44+
45+ static int CreateShader ( string uriVertexShader , string uriFragementShader )
46+ {
47+ int program = GL . CreateProgram ( ) ;
48+
49+ int vertexShader = CompileShader ( uriVertexShader , ShaderType . VertexShader ) ;
50+ int fragmentShader = CompileShader ( uriFragementShader , ShaderType . FragmentShader ) ;
51+
52+ // TODO: Handling error
53+
54+ GL . AttachShader ( program , vertexShader ) ;
55+ GL . AttachShader ( program , fragmentShader ) ;
56+
57+ GL . LinkProgram ( program ) ; // Put the shaders in their respective processor
58+ GL . ValidateProgram ( program ) ; // Check if everything correct and store the information on the logs
59+ // TODO: Explain what's the difference with GL.ShaderInfoLog
60+
61+ GL . DeleteShader ( vertexShader ) ;
62+ GL . DeleteShader ( fragmentShader ) ;
63+
64+ return program ;
65+ }
2366 protected override void OnLoad ( )
2467 {
2568 base . OnLoad ( ) ;
69+
70+ GL . ClearColor ( 0.1f , 0.1f , 0.1f , 1.0f ) ;
71+
2672 _vertexBuffer = GL . GenBuffer ( ) ;
2773 GL . BindBuffer ( BufferTarget . ArrayBuffer , _vertexBuffer ) ;
28-
2974 GL . BufferData ( BufferTarget . ArrayBuffer , _vertices . Length * sizeof ( float ) , _vertices , BufferUsageHint . StaticDraw ) ;
3075
76+ _vertexArray = GL . GenVertexArray ( ) ; // TODO: Why a vertex array is necessary ?
77+ GL . BindVertexArray ( _vertexArray ) ;
78+
3179 GL . VertexAttribPointer ( 0 , 3 , VertexAttribPointerType . Float , false , 3 * sizeof ( float ) , 0 ) ;
3280 GL . EnableVertexAttribArray ( 0 ) ;
3381
82+ _shader = CreateShader ( "Shaders/shader.vert" , "Shaders/shader.frag" ) ;
83+
84+ GL . UseProgram ( _shader ) ;
3485 }
3586 protected override void OnResize ( ResizeEventArgs e )
3687 {
@@ -52,12 +103,28 @@ protected override void OnUpdateFrame(FrameEventArgs e)
52103 protected override void OnRenderFrame ( FrameEventArgs e )
53104 {
54105 base . OnRenderFrame ( e ) ;
106+
107+ GL . Clear ( ClearBufferMask . ColorBufferBit ) ; // Clear the color
108+
109+ GL . UseProgram ( _shader ) ;
110+ GL . BindVertexArray ( _vertexArray ) ;
111+
55112 GL . DrawArrays ( PrimitiveType . Triangles , 0 , 3 ) ;
113+
114+ SwapBuffers ( ) ;
56115 }
57116
58117 protected override void OnUnload ( )
59118 {
60119 base . OnUnload ( ) ;
120+
121+ GL . BindBuffer ( BufferTarget . ArrayBuffer , 0 ) ;
122+ GL . BindVertexArray ( _vertexArray ) ;
123+ GL . UseProgram ( 0 ) ;
124+
125+ GL . DeleteProgram ( _shader ) ;
126+ GL . DeleteBuffer ( _vertexBuffer ) ;
127+ GL . DeleteVertexArray ( _vertexArray ) ;
61128 }
62129 }
63130}
0 commit comments