├── .gitattributes ├── .gitignore ├── Example01 ├── App.config ├── Example01.fsproj ├── Program.fs └── packages.config ├── Example02 ├── App.config ├── Example02.fsproj ├── Program.fs └── packages.config ├── Example03 ├── App.config ├── Example03.fsproj ├── Program.fs └── packages.config ├── Example04 ├── App.config ├── Example04.fsproj ├── Program.fs └── packages.config ├── Example05 ├── App.config ├── Example05.fsproj ├── Program.fs └── packages.config ├── Example06 ├── App.config ├── Example06.fsproj ├── Program.fs └── packages.config ├── Example07 ├── App.config ├── Example07.fsproj ├── Program.fs └── packages.config ├── Example08 ├── App.config ├── Example08.fsproj ├── Program.fs ├── packages.config └── texture.bmp ├── Example09 ├── App.config ├── Example09.fsproj ├── Program.fs ├── packages.config └── texture.bmp ├── LICENSE.md ├── OpenTK Examples.sln └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Nuget 170 | ############# 171 | packages/ 172 | 173 | 174 | ############# 175 | ## Windows detritus 176 | ############# 177 | 178 | # Windows image file caches 179 | Thumbs.db 180 | ehthumbs.db 181 | 182 | # Folder config file 183 | Desktop.ini 184 | 185 | # Recycle Bin used on file shares 186 | $RECYCLE.BIN/ 187 | 188 | # Mac crap 189 | .DS_Store 190 | 191 | 192 | ############# 193 | ## Python 194 | ############# 195 | 196 | *.py[co] 197 | 198 | # Packages 199 | *.egg 200 | *.egg-info 201 | dist/ 202 | build/ 203 | eggs/ 204 | parts/ 205 | var/ 206 | sdist/ 207 | develop-eggs/ 208 | .installed.cfg 209 | 210 | # Installer logs 211 | pip-log.txt 212 | 213 | # Unit test / coverage reports 214 | .coverage 215 | .tox 216 | 217 | #Translations 218 | *.mo 219 | 220 | #Mr Developer 221 | .mr.developer.cfg 222 | -------------------------------------------------------------------------------- /Example01/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Example01/Example01.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | 626332a3-544b-4810-a138-40c6841b384e 9 | Exe 10 | Example01 11 | Example01 12 | v4.0 13 | Example01 14 | 15 | 16 | true 17 | full 18 | false 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | 3 23 | AnyCPU 24 | bin\Debug\Example01.XML 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | true 31 | bin\Release\ 32 | TRACE 33 | 3 34 | AnyCPU 35 | bin\Release\Example01.XML 36 | true 37 | 38 | 39 | 40 | 41 | True 42 | 43 | 44 | ..\packages\OpenTK.1.1.1508.5724\lib\NET40\OpenTK.dll 45 | True 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 11 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /Example01/Program.fs: -------------------------------------------------------------------------------- 1 | open OpenTK 2 | open OpenTK.Graphics.OpenGL4 3 | 4 | type Window(width, height, mode, title, options) = 5 | inherit GameWindow(width, height, mode, title, options) 6 | 7 | // Function for initialization. 8 | override this.OnLoad(e) = 9 | // The background will just cleared with blue color. 10 | GL.ClearColor(0.0f, 0.0f, 1.0f, 0.0f) 11 | base.OnLoad(e) 12 | 13 | // Function is called before first update and every time when the window is resized. 14 | override this.OnResize(e) = 15 | GL.Viewport(0, 0, this.Width, this.Height) 16 | base.OnResize(e) 17 | 18 | // Function to render and display content. 19 | override this.OnRenderFrame(e) = 20 | // Now, the background is painted blue. 21 | GL.Clear(ClearBufferMask.ColorBufferBit) 22 | // Swap the front and back buffers 23 | this.Context.SwapBuffers() 24 | base.OnRenderFrame(e) 25 | 26 | [] 27 | let main argv = 28 | use window = new Window(640, 480, Graphics.GraphicsMode.Default, "Example Window", GameWindowFlags.Default) 29 | window.Run() 30 | 0 31 | -------------------------------------------------------------------------------- /Example01/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Example02/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Example02/Example02.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | ab324615-a07a-4cec-a14a-9b8dfb5dfbf4 9 | Exe 10 | Example02 11 | Example02 12 | v4.0 13 | Example02 14 | 15 | 16 | true 17 | full 18 | false 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | 3 23 | AnyCPU 24 | bin\Debug\Example02.XML 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | true 31 | bin\Release\ 32 | TRACE 33 | 3 34 | AnyCPU 35 | bin\Release\Example02.XML 36 | true 37 | 38 | 39 | 40 | 41 | True 42 | 43 | 44 | ..\packages\OpenTK.1.1.1508.5724\lib\NET40\OpenTK.dll 45 | True 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 11 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /Example02/Program.fs: -------------------------------------------------------------------------------- 1 | open OpenTK 2 | open OpenTK.Graphics.OpenGL4 3 | 4 | type Window(width, height, mode, title, options) = 5 | inherit GameWindow(width, height, mode, title, options) 6 | 7 | let vertexSource = """ 8 | #version 330 9 | 10 | layout (location = 0) in vec4 position; 11 | 12 | void main(void) 13 | { 14 | gl_Position = position; 15 | } 16 | """ 17 | 18 | let fragmentSource = """ 19 | #version 330 20 | 21 | out vec4 outputColor; 22 | 23 | void main(void) 24 | { 25 | outputColor = vec4(1.0, 0.0, 0.0, 1.0); 26 | } 27 | """ 28 | 29 | // Points of a triangle in normalized device coordinates. 30 | let points = [| -0.5f; 0.0f; 0.0f; 1.0f; 0.5f; 0.0f; 0.0f; 1.0f; 0.0f; 0.5f; 0.0f; 1.0f |] 31 | 32 | let mutable vertexShader = 0 33 | let mutable fragmentShader = 0 34 | let mutable program = 0 35 | 36 | let mutable verticesVbo = 0 37 | let mutable vao = 0 38 | 39 | override this.OnLoad(e) = 40 | // Load the source of the vertex and fragment shader. 41 | vertexShader <- 42 | let shader = GL.CreateShader(ShaderType.VertexShader) 43 | GL.ShaderSource(shader, vertexSource) 44 | GL.CompileShader(shader) 45 | shader 46 | 47 | fragmentShader <- 48 | let shader = GL.CreateShader(ShaderType.FragmentShader) 49 | GL.ShaderSource(shader, fragmentSource) 50 | GL.CompileShader(shader) 51 | shader 52 | 53 | // Build the program. 54 | program <- 55 | let program = GL.CreateProgram() 56 | GL.AttachShader(program, vertexShader) 57 | GL.AttachShader(program, fragmentShader) 58 | GL.LinkProgram(program) 59 | program 60 | 61 | // Create and bind the VBO for the vertices. 62 | verticesVbo <- 63 | let buffer = GL.GenBuffer() 64 | GL.BindBuffer(BufferTarget.ArrayBuffer, buffer) 65 | buffer 66 | 67 | // Create the VAO for the program. 68 | vao <- 69 | let array = GL.GenVertexArray() 70 | GL.BindVertexArray(array) 71 | array 72 | 73 | // Transfer the vertices from CPU to GPU. 74 | GL.BufferData(BufferTarget.ArrayBuffer, nativeint(3 * 4 * sizeof), points, BufferUsageHint.StaticDraw) 75 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 76 | 77 | // Use the program. 78 | GL.UseProgram(program) 79 | 80 | // Retrieve the vertex location in the program. 81 | let vertexPosition = GL.GetAttribLocation(program, "position") 82 | 83 | // Bind the only used VBO in this example. 84 | GL.BindBuffer(BufferTarget.ArrayBuffer, verticesVbo) 85 | GL.VertexAttribPointer(vertexPosition, 4, VertexAttribPointerType.Float, false, 0, 0) 86 | GL.EnableVertexAttribArray(vertexPosition) 87 | 88 | GL.ClearColor(0.0f, 0.0f, 1.0f, 0.0f) 89 | base.OnLoad(e) 90 | 91 | // Function to delete resources 92 | override this.OnUnload(e) = 93 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 94 | GL.DeleteBuffer(verticesVbo) 95 | GL.DeleteVertexArray(vao) 96 | 97 | GL.UseProgram(0) 98 | GL.DeleteProgram(program) 99 | GL.DeleteShader(vertexShader) 100 | GL.DeleteShader(fragmentShader) 101 | base.OnUnload(e) 102 | 103 | override this.OnResize(e) = 104 | GL.Viewport(0, 0, this.Width, this.Height) 105 | base.OnResize(e) 106 | 107 | override this.OnRenderFrame(e) = 108 | GL.Clear(ClearBufferMask.ColorBufferBit) 109 | 110 | // This draws the triangle, having three points. 111 | GL.DrawArrays(PrimitiveType.Triangles, 0, 3); 112 | 113 | this.Context.SwapBuffers() 114 | base.OnRenderFrame(e) 115 | 116 | [] 117 | let main argv = 118 | use window = new Window(640, 480, Graphics.GraphicsMode.Default, "Example Window", GameWindowFlags.Default) 119 | window.Run() 120 | 0 121 | -------------------------------------------------------------------------------- /Example02/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Example03/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Example03/Example03.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | 2b7b8811-0b23-4b57-a13f-d203699a5ff2 9 | Exe 10 | Example03 11 | Example03 12 | v4.0 13 | Example03 14 | 15 | 16 | true 17 | full 18 | false 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | 3 23 | AnyCPU 24 | bin\Debug\Example03.XML 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | true 31 | bin\Release\ 32 | TRACE 33 | 3 34 | AnyCPU 35 | bin\Release\Example03.XML 36 | true 37 | 38 | 39 | 40 | 41 | True 42 | 43 | 44 | ..\packages\OpenTK.1.1.1508.5724\lib\NET40\OpenTK.dll 45 | True 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 11 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /Example03/Program.fs: -------------------------------------------------------------------------------- 1 | open OpenTK 2 | open OpenTK.Graphics.OpenGL4 3 | 4 | type Window(width, height, mode, title, options) = 5 | inherit GameWindow(width, height, mode, title, options) 6 | 7 | let points = [| 8 | -0.5f; 0.0f; 0.0f; 1.0f; 1.0f; 0.0f; 0.0f; 1.0f; 9 | 0.5f; 0.0f; 0.0f; 1.0f; 0.0f; 1.0f; 0.0f; 1.0f; 10 | 0.0f; 0.5f; 0.0f; 1.0f; 0.0f; 0.0f; 1.0f; 1.0f; |] 11 | 12 | let vertexSource = """ 13 | #version 330 14 | 15 | layout (location = 0) in vec4 position; 16 | layout (location = 1) in vec4 color; 17 | 18 | smooth out vec4 blendColor; 19 | 20 | void main(void) 21 | { 22 | gl_Position = position; 23 | blendColor = color; 24 | } 25 | """ 26 | 27 | let fragmentSource = """ 28 | #version 330 29 | 30 | smooth in vec4 blendColor; 31 | 32 | out vec4 outputColor; 33 | 34 | void main(void) 35 | { 36 | outputColor = blendColor; 37 | } 38 | """ 39 | 40 | let mutable vertexShader = 0 41 | let mutable fragmentShader = 0 42 | let mutable program = 0 43 | 44 | let mutable verticesVbo = 0 45 | let mutable vao = 0 46 | 47 | override this.OnLoad(e) = 48 | vertexShader <- 49 | let shader = GL.CreateShader(ShaderType.VertexShader) 50 | GL.ShaderSource(shader, vertexSource) 51 | GL.CompileShader(shader) 52 | shader 53 | 54 | fragmentShader <- 55 | let shader = GL.CreateShader(ShaderType.FragmentShader) 56 | GL.ShaderSource(shader, fragmentSource) 57 | GL.CompileShader(shader) 58 | shader 59 | 60 | program <- 61 | let program = GL.CreateProgram() 62 | GL.AttachShader(program, vertexShader) 63 | GL.AttachShader(program, fragmentShader) 64 | GL.LinkProgram(program) 65 | program 66 | 67 | 68 | verticesVbo <- 69 | let buffer = GL.GenBuffer() 70 | GL.BindBuffer(BufferTarget.ArrayBuffer, buffer) 71 | buffer 72 | 73 | vao <- 74 | let array = GL.GenVertexArray() 75 | GL.BindVertexArray(array) 76 | array 77 | 78 | // Transfer the vertices from CPU to GPU. 79 | GL.BufferData(BufferTarget.ArrayBuffer, nativeint(3 * 8 * sizeof), points, BufferUsageHint.StaticDraw) 80 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 81 | 82 | // Use the program. 83 | GL.UseProgram(program) 84 | let vertexPosition = GL.GetAttribLocation(program, "position"); 85 | let vertexColor = GL.GetAttribLocation(program, "color"); 86 | 87 | GL.BindBuffer(BufferTarget.ArrayBuffer, verticesVbo) 88 | 89 | GL.VertexAttribPointer(vertexPosition, 4, VertexAttribPointerType.Float, false, sizeof * 8, 0) 90 | GL.EnableVertexAttribArray(vertexPosition) 91 | 92 | GL.VertexAttribPointer(vertexColor, 4, VertexAttribPointerType.Float, false, sizeof * 8, sizeof * 4) 93 | GL.EnableVertexAttribArray(vertexColor) 94 | 95 | GL.ClearColor(0.0f, 0.0f, 1.0f, 0.0f) 96 | 97 | override this.OnUnload(e) = 98 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 99 | GL.DeleteBuffer(verticesVbo) 100 | GL.DeleteVertexArray(vao) 101 | 102 | GL.UseProgram(0) 103 | GL.DeleteProgram(program) 104 | GL.DeleteShader(vertexShader) 105 | GL.DeleteShader(fragmentShader) 106 | base.OnUnload(e) 107 | 108 | override this.OnResize(e) = 109 | GL.Viewport(0, 0, this.Width, this.Height) 110 | base.OnResize(e) 111 | 112 | override this.OnRenderFrame(e) = 113 | GL.Clear(ClearBufferMask.ColorBufferBit) 114 | 115 | // This draws the triangle, having three points. 116 | GL.DrawArrays(PrimitiveType.Triangles, 0, 3); 117 | 118 | this.Context.SwapBuffers() 119 | base.OnRenderFrame(e) 120 | 121 | [] 122 | let main argv = 123 | use window = new Window(640, 480, Graphics.GraphicsMode.Default, "Example Window", GameWindowFlags.Default) 124 | window.Run() 125 | 0 126 | -------------------------------------------------------------------------------- /Example03/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Example04/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Example04/Example04.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | 33aea5c9-d268-4318-818d-9f4af2c30000 9 | Exe 10 | Example04 11 | Example04 12 | v4.0 13 | Example04 14 | 15 | 16 | true 17 | full 18 | false 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | 3 23 | AnyCPU 24 | bin\Debug\Example04.XML 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | true 31 | bin\Release\ 32 | TRACE 33 | 3 34 | AnyCPU 35 | bin\Release\Example04.XML 36 | true 37 | 38 | 39 | 40 | 41 | True 42 | 43 | 44 | ..\packages\OpenTK.1.1.1508.5724\lib\NET40\OpenTK.dll 45 | True 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 11 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /Example04/Program.fs: -------------------------------------------------------------------------------- 1 | open OpenTK 2 | open OpenTK.Graphics.OpenGL4 3 | 4 | type Vertex = 5 | struct 6 | val Position : Vector4 7 | val Color : Vector4 8 | 9 | new(position : Vector4, color : Vector4) = { Position = position; Color = color } 10 | end 11 | 12 | type Window(width, height, mode, title, options) = 13 | inherit GameWindow(width, height, mode, title, options) 14 | 15 | let points = 16 | [| 17 | Vertex(Vector4(-0.5f, 0.0f, 0.0f, 1.0f), Vector4(1.0f, 0.0f, 0.0f, 1.0f)) 18 | Vertex(Vector4(0.5f, 0.0f, 0.0f, 1.0f), Vector4(0.0f, 1.0f, 0.0f, 1.0f)) 19 | Vertex(Vector4(0.0f, 0.5f, 0.0f, 1.0f), Vector4(0.0f, 0.0f, 1.0f, 1.0f)) 20 | |] 21 | 22 | 23 | let vertexSource = """ 24 | #version 330 25 | 26 | layout (location = 0) in vec4 position; 27 | layout (location = 1) in vec4 color; 28 | 29 | smooth out vec4 blendColor; 30 | 31 | void main(void) 32 | { 33 | gl_Position = position; 34 | blendColor = color; 35 | } 36 | """ 37 | 38 | let fragmentSource = """ 39 | #version 330 40 | 41 | smooth in vec4 blendColor; 42 | 43 | out vec4 outputColor; 44 | 45 | void main(void) 46 | { 47 | outputColor = blendColor; 48 | } 49 | """ 50 | 51 | let createShader typ source = 52 | let shader = GL.CreateShader(typ) 53 | GL.ShaderSource(shader, source) 54 | GL.CompileShader(shader) 55 | shader 56 | 57 | let mutable vertexShader = 0 58 | let mutable fragmentShader = 0 59 | let mutable program = 0 60 | 61 | let mutable verticesVbo = 0 62 | let mutable vao = 0 63 | 64 | override this.OnLoad(e) = 65 | vertexShader <- createShader ShaderType.VertexShader vertexSource 66 | 67 | fragmentShader <- createShader ShaderType.FragmentShader fragmentSource 68 | 69 | program <- 70 | let program = GL.CreateProgram() 71 | GL.AttachShader(program, vertexShader) 72 | GL.AttachShader(program, fragmentShader) 73 | GL.LinkProgram(program) 74 | program 75 | 76 | 77 | verticesVbo <- 78 | let buffer = GL.GenBuffer() 79 | GL.BindBuffer(BufferTarget.ArrayBuffer, buffer) 80 | buffer 81 | 82 | vao <- 83 | let array = GL.GenVertexArray() 84 | GL.BindVertexArray(array) 85 | array 86 | 87 | // Transfer the vertices from CPU to GPU. 88 | GL.BufferData(BufferTarget.ArrayBuffer, nativeint(3 * sizeof), points, BufferUsageHint.StaticDraw) 89 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 90 | 91 | // Use the program. 92 | GL.UseProgram(program) 93 | let vertexPosition = GL.GetAttribLocation(program, "position"); 94 | let vertexColor = GL.GetAttribLocation(program, "color"); 95 | 96 | GL.BindBuffer(BufferTarget.ArrayBuffer, verticesVbo) 97 | 98 | GL.VertexAttribPointer(vertexPosition, 4, VertexAttribPointerType.Float, false, sizeof, 0) 99 | GL.EnableVertexAttribArray(vertexPosition) 100 | 101 | GL.VertexAttribPointer(vertexColor, 4, VertexAttribPointerType.Float, false, sizeof, sizeof) 102 | GL.EnableVertexAttribArray(vertexColor) 103 | 104 | GL.ClearColor(0.0f, 0.0f, 1.0f, 0.0f) 105 | 106 | override this.OnUnload(e) = 107 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 108 | GL.DeleteBuffer(verticesVbo) 109 | GL.DeleteVertexArray(vao) 110 | 111 | GL.UseProgram(0) 112 | GL.DeleteProgram(program) 113 | GL.DeleteShader(vertexShader) 114 | GL.DeleteShader(fragmentShader) 115 | base.OnUnload(e) 116 | 117 | override this.OnResize(e) = 118 | GL.Viewport(0, 0, this.Width, this.Height) 119 | base.OnResize(e) 120 | 121 | override this.OnRenderFrame(e) = 122 | GL.Clear(ClearBufferMask.ColorBufferBit) 123 | 124 | // This draws the triangle, having three points. 125 | GL.DrawArrays(PrimitiveType.Triangles, 0, 3); 126 | 127 | this.Context.SwapBuffers() 128 | base.OnRenderFrame(e) 129 | 130 | [] 131 | let main argv = 132 | use window = new Window(640, 480, Graphics.GraphicsMode.Default, "Example Window", GameWindowFlags.Default) 133 | window.Run() 134 | 0 135 | -------------------------------------------------------------------------------- /Example04/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Example05/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Example05/Example05.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | e45e180b-d0aa-43a5-a08f-e51ab5fbde76 9 | Exe 10 | Example05 11 | Example05 12 | v4.0 13 | Example05 14 | 15 | 16 | true 17 | full 18 | false 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | 3 23 | AnyCPU 24 | bin\Debug\Example05.XML 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | true 31 | bin\Release\ 32 | TRACE 33 | 3 34 | AnyCPU 35 | bin\Release\Example05.XML 36 | true 37 | 38 | 39 | 40 | 41 | True 42 | 43 | 44 | ..\packages\OpenTK.1.1.1508.5724\lib\NET40\OpenTK.dll 45 | True 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 11 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /Example05/Program.fs: -------------------------------------------------------------------------------- 1 | open OpenTK 2 | open OpenTK.Graphics 3 | open OpenTK.Graphics.OpenGL4 4 | 5 | type Vertex = 6 | struct 7 | val Position : Vector4 8 | val ColorA : Color4 9 | val ColorB : Color4 10 | 11 | new(position : Vector4, colorA : Color4, colorB : Color4) = 12 | { Position = position; ColorA = colorA; ColorB = colorB } 13 | end 14 | 15 | type Window(width, height, mode, title, options) = 16 | inherit GameWindow(width, height, mode, title, options) 17 | 18 | let points = 19 | [| 20 | Vertex(Vector4(-0.5f, 0.0f, 0.0f, 1.0f), OpenTK.Graphics.Color4.Red, OpenTK.Graphics.Color4.Green) 21 | Vertex(Vector4(0.5f, 0.0f, 0.0f, 1.0f), OpenTK.Graphics.Color4.Green, OpenTK.Graphics.Color4.Blue) 22 | Vertex(Vector4(0.f, 0.5f, 0.0f, 1.0f), OpenTK.Graphics.Color4.Blue, OpenTK.Graphics.Color4.Red) 23 | |] 24 | 25 | let vertexSource = """ 26 | #version 330 27 | 28 | uniform float time; 29 | uniform float loop; 30 | 31 | layout (location = 0) in vec4 position; 32 | layout (location = 1) in vec4 colorA; 33 | layout (location = 2) in vec4 colorB; 34 | 35 | smooth out vec4 fragColorA; 36 | smooth out vec4 fragColorB; 37 | 38 | void main(void) 39 | { 40 | float timeScale = 3.14159f * 2.0f / loop; 41 | float t = mod(time, loop); 42 | vec4 offset = vec4( 43 | cos(t * timeScale) * 0.5f, 44 | sin(t * timeScale) * 0.5f, 45 | 0.0f, 46 | 0.0f); 47 | 48 | gl_Position = position + offset; 49 | fragColorA = colorA; 50 | fragColorB = colorB; 51 | } 52 | """ 53 | 54 | let fragmentSource = """ 55 | #version 330 56 | 57 | uniform float time; 58 | uniform float loop; 59 | 60 | smooth in vec4 fragColorA; 61 | smooth in vec4 fragColorB; 62 | 63 | out vec4 outputColor; 64 | 65 | void main(void) 66 | { 67 | float t = mod(time, loop) / loop; 68 | t = max(1 - abs((t * 2) - 1), 0); 69 | outputColor = mix(fragColorA, fragColorB, t); 70 | } 71 | """ 72 | 73 | let createShader typ source = 74 | let shader = GL.CreateShader(typ) 75 | GL.ShaderSource(shader, source) 76 | GL.CompileShader(shader) 77 | let status = GL.GetShader(shader, ShaderParameter.CompileStatus) 78 | if status <> 0 then 79 | shader 80 | else 81 | failwith (GL.GetShaderInfoLog(shader)) 82 | 83 | let mutable vertexShader = 0 84 | let mutable fragmentShader = 0 85 | let mutable program = 0 86 | 87 | let mutable timeLocation = 0 88 | let mutable loopLocation = 0 89 | 90 | let mutable verticesVbo = 0 91 | let mutable vao = 0 92 | 93 | // variable to keep track of total time 94 | let mutable time = 0.0f 95 | 96 | override this.OnLoad(e) = 97 | vertexShader <- createShader ShaderType.VertexShader vertexSource 98 | 99 | fragmentShader <- createShader ShaderType.FragmentShader fragmentSource 100 | 101 | program <- 102 | let program = GL.CreateProgram() 103 | GL.AttachShader(program, vertexShader) 104 | GL.AttachShader(program, fragmentShader) 105 | GL.LinkProgram(program) 106 | let status = GL.GetProgram(program, GetProgramParameterName.LinkStatus) 107 | if status <> 0 then 108 | program 109 | else 110 | failwith (GL.GetProgramInfoLog(program)) 111 | 112 | verticesVbo <- 113 | let buffer = GL.GenBuffer() 114 | GL.BindBuffer(BufferTarget.ArrayBuffer, buffer) 115 | buffer 116 | 117 | vao <- 118 | let array = GL.GenVertexArray() 119 | GL.BindVertexArray(array) 120 | array 121 | 122 | // Get the uniform locations 123 | timeLocation <- GL.GetUniformLocation(program, "time") 124 | loopLocation <- GL.GetUniformLocation(program, "loop") 125 | 126 | // Transfer the vertices from CPU to GPU. 127 | GL.BufferData(BufferTarget.ArrayBuffer, nativeint(3 * sizeof), points, BufferUsageHint.StaticDraw) 128 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 129 | 130 | // Use the program. 131 | GL.UseProgram(program) 132 | let vertexPosition = GL.GetAttribLocation(program, "position"); 133 | let vertexColorA = GL.GetAttribLocation(program, "colorA"); 134 | let vertexColorB = GL.GetAttribLocation(program, "colorB"); 135 | 136 | GL.BindBuffer(BufferTarget.ArrayBuffer, verticesVbo) 137 | 138 | GL.VertexAttribPointer(vertexPosition, 4, VertexAttribPointerType.Float, false, sizeof, 0) 139 | GL.EnableVertexAttribArray(vertexPosition) 140 | 141 | GL.VertexAttribPointer(vertexColorA, 4, VertexAttribPointerType.Float, false, sizeof, sizeof) 142 | GL.EnableVertexAttribArray(vertexColorA) 143 | 144 | GL.VertexAttribPointer(vertexColorB, 4, VertexAttribPointerType.Float, false, sizeof, sizeof + sizeof) 145 | GL.EnableVertexAttribArray(vertexColorB) 146 | 147 | GL.ClearColor(0.0f, 0.0f, 1.0f, 0.0f) 148 | 149 | override this.OnUnload(e) = 150 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 151 | GL.DeleteBuffer(verticesVbo) 152 | GL.DeleteVertexArray(vao) 153 | 154 | GL.UseProgram(0) 155 | GL.DeleteProgram(program) 156 | GL.DeleteShader(vertexShader) 157 | GL.DeleteShader(fragmentShader) 158 | base.OnUnload(e) 159 | 160 | override this.OnResize(e) = 161 | GL.Viewport(0, 0, this.Width, this.Height) 162 | base.OnResize(e) 163 | 164 | override this.OnRenderFrame(e) = 165 | GL.Clear(ClearBufferMask.ColorBufferBit) 166 | 167 | // This draws the triangle, having three points. 168 | GL.DrawArrays(PrimitiveType.Triangles, 0, 3); 169 | 170 | this.Context.SwapBuffers() 171 | base.OnRenderFrame(e) 172 | 173 | override this.OnUpdateFrame(e) = 174 | time <- time + float32(e.Time) 175 | GL.Uniform1(timeLocation, time) 176 | GL.Uniform1(loopLocation, 4.0f) 177 | 178 | base.OnUpdateFrame(e) 179 | 180 | [] 181 | let main argv = 182 | use window = new Window(640, 480, Graphics.GraphicsMode.Default, "Example Window", GameWindowFlags.Default) 183 | window.Run() 184 | 0 185 | -------------------------------------------------------------------------------- /Example05/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Example06/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Example06/Example06.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | bd7badfb-6eb0-43f3-91ed-ba0c0fdbb68e 9 | Exe 10 | Example06 11 | Example06 12 | v4.0 13 | Example06 14 | 15 | 16 | true 17 | full 18 | false 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | 3 23 | AnyCPU 24 | bin\Debug\Example06.XML 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | true 31 | bin\Release\ 32 | TRACE 33 | 3 34 | AnyCPU 35 | bin\Release\Example06.XML 36 | true 37 | 38 | 39 | 40 | 41 | True 42 | 43 | 44 | ..\packages\OpenTK.1.1.1508.5724\lib\NET40\OpenTK.dll 45 | True 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 11 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /Example06/Program.fs: -------------------------------------------------------------------------------- 1 | open OpenTK 2 | open OpenTK.Graphics 3 | open OpenTK.Graphics.OpenGL4 4 | 5 | type Vertex = 6 | struct 7 | val Position : Vector4 8 | val Color : Color4 9 | 10 | new(position : Vector4, color : Color4) = 11 | { Position = position; Color = color } 12 | end 13 | 14 | type Window(width, height, mode, title, options) = 15 | inherit GameWindow(width, height, mode, title, options) 16 | 17 | let quad color p0 p1 p2 p3 = 18 | [| p0; p1; p2; p2; p1; p3 |] 19 | |> Array.map (fun (p : Vector3) -> Vertex(Vector4(p.X, p.Y, p.Z, 1.0f), color)) 20 | 21 | let points = 22 | [| 23 | // Front quad 24 | quad Color4.Red 25 | <| Vector3(-1.0f, -1.0f, -1.0f) 26 | <| Vector3(-1.0f, 1.0f, -1.0f) 27 | <| Vector3( 1.0f, -1.0f, -1.0f) 28 | <| Vector3( 1.0f, 1.0f, -1.0f) 29 | 30 | // Right quad 31 | quad Color4.Green 32 | <| Vector3(1.0f, -1.0f, -1.0f) 33 | <| Vector3(1.0f, 1.0f, -1.0f) 34 | <| Vector3(1.0f, -1.0f, 1.0f) 35 | <| Vector3(1.0f, 1.0f, 1.0f) 36 | 37 | // Left quad 38 | quad Color4.Blue 39 | <| Vector3(-1.0f, -1.0f, -1.0f) 40 | <| Vector3(-1.0f, -1.0f, 1.0f) 41 | <| Vector3(-1.0f, 1.0f, -1.0f) 42 | <| Vector3(-1.0f, 1.0f, 1.0f) 43 | 44 | // Back quad 45 | quad Color4.White 46 | <| Vector3(-1.0f, -1.0f, 1.0f) 47 | <| Vector3( 1.0f, -1.0f, 1.0f) 48 | <| Vector3(-1.0f, 1.0f, 1.0f) 49 | <| Vector3( 1.0f, 1.0f, 1.0f) 50 | 51 | // Top quad 52 | quad Color4.Pink 53 | <| Vector3(-1.0f, 1.0f, 1.0f) 54 | <| Vector3( 1.0f, 1.0f, 1.0f) 55 | <| Vector3(-1.0f, 1.0f, -1.0f) 56 | <| Vector3( 1.0f, 1.0f, -1.0f) 57 | 58 | // Bottom quad 59 | quad Color4.Purple 60 | <| Vector3(-1.0f, -1.0f, 1.0f) 61 | <| Vector3(-1.0f, -1.0f, -1.0f) 62 | <| Vector3( 1.0f, -1.0f, 1.0f) 63 | <| Vector3( 1.0f, -1.0f, -1.0f) 64 | |] |> Array.concat 65 | 66 | let vertexSource = """ 67 | #version 330 68 | 69 | uniform mat4 projection; 70 | 71 | layout (location = 0) in vec4 position; 72 | layout (location = 1) in vec4 color; 73 | 74 | smooth out vec4 fragColor; 75 | 76 | void main(void) 77 | { 78 | gl_Position = projection * position; 79 | fragColor = color; 80 | } 81 | """ 82 | 83 | let fragmentSource = """ 84 | #version 330 85 | 86 | smooth in vec4 fragColor; 87 | 88 | out vec4 outputColor; 89 | 90 | void main(void) 91 | { 92 | outputColor = fragColor; 93 | } 94 | """ 95 | 96 | let createShader typ source = 97 | let shader = GL.CreateShader(typ) 98 | GL.ShaderSource(shader, source) 99 | GL.CompileShader(shader) 100 | let status = GL.GetShader(shader, ShaderParameter.CompileStatus) 101 | if status <> 0 then 102 | shader 103 | else 104 | failwith (GL.GetShaderInfoLog(shader)) 105 | 106 | let mutable vertexShader = 0 107 | let mutable fragmentShader = 0 108 | let mutable program = 0 109 | 110 | let mutable projectionLocation = 0 111 | 112 | let mutable verticesVbo = 0 113 | let mutable vao = 0 114 | 115 | let mutable rotation = 0.0f 116 | let mutable yTranslate = 0.0f 117 | let mutable yBounce = true 118 | 119 | override this.OnLoad(e) = 120 | vertexShader <- createShader ShaderType.VertexShader vertexSource 121 | 122 | fragmentShader <- createShader ShaderType.FragmentShader fragmentSource 123 | 124 | program <- 125 | let program = GL.CreateProgram() 126 | GL.AttachShader(program, vertexShader) 127 | GL.AttachShader(program, fragmentShader) 128 | GL.LinkProgram(program) 129 | let status = GL.GetProgram(program, GetProgramParameterName.LinkStatus) 130 | if status <> 0 then 131 | program 132 | else 133 | failwith (GL.GetProgramInfoLog(program)) 134 | 135 | verticesVbo <- 136 | let buffer = GL.GenBuffer() 137 | GL.BindBuffer(BufferTarget.ArrayBuffer, buffer) 138 | buffer 139 | 140 | vao <- 141 | let array = GL.GenVertexArray() 142 | GL.BindVertexArray(array) 143 | array 144 | 145 | // Get the uniform locations 146 | projectionLocation <- GL.GetUniformLocation(program, "projection") 147 | 148 | // Transfer the vertices from CPU to GPU. 149 | GL.BufferData(BufferTarget.ArrayBuffer, nativeint(points.Length * sizeof), points, BufferUsageHint.StaticDraw) 150 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 151 | 152 | // Use the program. 153 | GL.UseProgram(program) 154 | let vertexPosition = GL.GetAttribLocation(program, "position"); 155 | let vertexColor = GL.GetAttribLocation(program, "color"); 156 | 157 | GL.BindBuffer(BufferTarget.ArrayBuffer, verticesVbo) 158 | 159 | GL.VertexAttribPointer(vertexPosition, 4, VertexAttribPointerType.Float, false, sizeof, 0) 160 | GL.EnableVertexAttribArray(vertexPosition) 161 | 162 | GL.VertexAttribPointer(vertexColor, 4, VertexAttribPointerType.Float, false, sizeof, sizeof) 163 | GL.EnableVertexAttribArray(vertexColor) 164 | 165 | 166 | // Enable face culling 167 | GL.Enable(EnableCap.CullFace) 168 | GL.CullFace(CullFaceMode.Back) 169 | GL.FrontFace(FrontFaceDirection.Ccw) 170 | 171 | GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f) 172 | 173 | override this.OnUnload(e) = 174 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 175 | GL.DeleteBuffer(verticesVbo) 176 | GL.DeleteVertexArray(vao) 177 | 178 | GL.UseProgram(0) 179 | GL.DeleteProgram(program) 180 | GL.DeleteShader(vertexShader) 181 | GL.DeleteShader(fragmentShader) 182 | base.OnUnload(e) 183 | 184 | override this.OnResize(e) = 185 | GL.Viewport(0, 0, this.Width, this.Height) 186 | base.OnResize(e) 187 | 188 | override this.OnRenderFrame(e) = 189 | GL.Clear(ClearBufferMask.ColorBufferBit) 190 | 191 | rotation <- if rotation >= 360.0f then rotation - 360.0f else rotation + (10.0f * float32(e.Time)) 192 | yBounce <- if yBounce then yTranslate >= -4.0f else yTranslate >= 4.0f 193 | yTranslate <- yTranslate + (if yBounce then -1.0f else +1.0f) * float32(e.Time) 194 | 195 | // Set projection matrix 196 | let projectionMatrix = 197 | Matrix4.CreateRotationY(MathHelper.DegreesToRadians(rotation)) * 198 | Matrix4.CreateTranslation(0.0f, yTranslate, -4.0f) * 199 | Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90.0f), float32(this.ClientSize.Width) / float32(this.ClientSize.Height), 0.1f, 10.f) 200 | 201 | GL.UniformMatrix4(projectionLocation, false, ref projectionMatrix) 202 | 203 | GL.DrawArrays(PrimitiveType.Triangles, 0, points.Length) 204 | 205 | this.Context.SwapBuffers() 206 | base.OnRenderFrame(e) 207 | 208 | [] 209 | let main argv = 210 | use window = new Window(640, 480, Graphics.GraphicsMode.Default, "Example Window", GameWindowFlags.Default) 211 | window.Run() 212 | 0 213 | -------------------------------------------------------------------------------- /Example06/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Example07/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Example07/Example07.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | 162bbf58-3d22-459b-ad2b-ed66dc6fa106 9 | Exe 10 | Example07 11 | Example07 12 | v4.5 13 | Example07 14 | 15 | 16 | true 17 | full 18 | false 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | 3 23 | AnyCPU 24 | bin\Debug\Example07.XML 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | true 31 | bin\Release\ 32 | TRACE 33 | 3 34 | AnyCPU 35 | bin\Release\Example07.XML 36 | true 37 | 38 | 39 | 40 | 41 | True 42 | 43 | 44 | ..\packages\OpenTK.1.1.1589.5941\lib\NET40\OpenTK.dll 45 | True 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 11 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /Example07/Program.fs: -------------------------------------------------------------------------------- 1 | open OpenTK 2 | open OpenTK.Graphics.OpenGL4 3 | 4 | type Vertex = 5 | struct 6 | val Position : Vector4 7 | val UV : Vector2 8 | 9 | new(position : Vector4, uv : Vector2) = { Position = position; UV = uv } 10 | end 11 | 12 | type Window(width, height, mode, title, options) = 13 | inherit GameWindow(width, height, mode, title, options) 14 | 15 | let quad p0 p1 p2 p3 = 16 | [| p0; p1; p2; p2; p1; p3 |] 17 | 18 | let points = 19 | [| 20 | quad 21 | <| Vertex(Vector4(-0.5f, -0.5f, 0.0f, 1.0f), Vector2(0.0f, 0.0f)) 22 | <| Vertex(Vector4( 0.5f, -0.5f, 0.0f, 1.0f), Vector2(1.0f, 0.0f)) 23 | <| Vertex(Vector4(-0.5f, 0.5f, 0.0f, 1.0f), Vector2(0.0f, 1.0f)) 24 | <| Vertex(Vector4( 0.5f, 0.5f, 0.0f, 1.0f), Vector2(1.0f, 1.0f)) 25 | |] |> Array.concat 26 | 27 | 28 | let vertexSource = """ 29 | #version 330 30 | 31 | layout (location = 0) in vec4 position; 32 | layout (location = 1) in vec2 uv; 33 | 34 | out vec2 fragmentUV; 35 | 36 | void main(void) 37 | { 38 | gl_Position = position; 39 | fragmentUV = uv; 40 | } 41 | """ 42 | 43 | let fragmentSource = """ 44 | #version 330 45 | 46 | in vec2 fragmentUV; 47 | 48 | uniform sampler2D textureSampler; 49 | 50 | out vec4 outputColor; 51 | 52 | void main(void) 53 | { 54 | outputColor = texture(textureSampler, fragmentUV); 55 | } 56 | """ 57 | 58 | let createShader typ source = 59 | let shader = GL.CreateShader(typ) 60 | GL.ShaderSource(shader, source) 61 | GL.CompileShader(shader) 62 | let status = GL.GetShader(shader, ShaderParameter.CompileStatus) 63 | if status <> 0 then 64 | shader 65 | else 66 | failwith (GL.GetShaderInfoLog(shader)) 67 | 68 | let mutable vertexShader = 0 69 | let mutable fragmentShader = 0 70 | let mutable program = 0 71 | 72 | let mutable verticesVbo = 0 73 | let mutable vao = 0 74 | 75 | let mutable texture = 0 76 | 77 | override this.OnLoad(e) = 78 | vertexShader <- createShader ShaderType.VertexShader vertexSource 79 | 80 | fragmentShader <- createShader ShaderType.FragmentShader fragmentSource 81 | 82 | program <- 83 | let program = GL.CreateProgram() 84 | GL.AttachShader(program, vertexShader) 85 | GL.AttachShader(program, fragmentShader) 86 | GL.LinkProgram(program) 87 | let status = GL.GetProgram(program, GetProgramParameterName.LinkStatus) 88 | if status <> 0 then 89 | program 90 | else 91 | failwith (GL.GetProgramInfoLog(program)) 92 | 93 | verticesVbo <- 94 | let buffer = GL.GenBuffer() 95 | GL.BindBuffer(BufferTarget.ArrayBuffer, buffer) 96 | buffer 97 | 98 | vao <- 99 | let array = GL.GenVertexArray() 100 | GL.BindVertexArray(array) 101 | array 102 | 103 | // Transfer the vertices from CPU to GPU. 104 | GL.BufferData(BufferTarget.ArrayBuffer, nativeint(points.Length * sizeof), points, BufferUsageHint.StaticDraw) 105 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 106 | 107 | // Create a new 2x2 texture with one mip level 108 | texture <- GL.GenTexture() 109 | GL.BindTexture(TextureTarget.Texture2D, texture) 110 | GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 2, 2, 0, PixelFormat.Rgb, PixelType.Float, 111 | [| 112 | 1.0f;0.1f;0.1f; 113 | 0.1f;1.0f;0.1f; 114 | 0.1f;0.1f;1.0f; 115 | 1.0f;1.0f;1.0f; 116 | |]) 117 | // Set texture wrapping and mip level bounds 118 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge) 119 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge) 120 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0) 121 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 0) 122 | 123 | // Use the program. 124 | GL.UseProgram(program) 125 | let vertexPosition = GL.GetAttribLocation(program, "position"); 126 | let vertexUv = GL.GetAttribLocation(program, "uv"); 127 | 128 | GL.BindBuffer(BufferTarget.ArrayBuffer, verticesVbo) 129 | 130 | GL.VertexAttribPointer(vertexPosition, 4, VertexAttribPointerType.Float, false, sizeof, 0) 131 | GL.EnableVertexAttribArray(vertexPosition) 132 | 133 | GL.VertexAttribPointer(vertexUv, 2, VertexAttribPointerType.Float, false, sizeof, sizeof) 134 | GL.EnableVertexAttribArray(vertexUv) 135 | 136 | GL.ClearColor(0.0f, 0.0f, 1.0f, 0.0f) 137 | 138 | override this.OnUnload(e) = 139 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 140 | GL.DeleteBuffer(verticesVbo) 141 | GL.DeleteVertexArray(vao) 142 | 143 | GL.BindTexture(TextureTarget.Texture2D, 0) 144 | GL.DeleteTexture(texture) 145 | 146 | GL.UseProgram(0) 147 | GL.DeleteProgram(program) 148 | GL.DeleteShader(vertexShader) 149 | GL.DeleteShader(fragmentShader) 150 | base.OnUnload(e) 151 | 152 | override this.OnResize(e) = 153 | GL.Viewport(0, 0, this.Width, this.Height) 154 | base.OnResize(e) 155 | 156 | override this.OnRenderFrame(e) = 157 | GL.Clear(ClearBufferMask.ColorBufferBit) 158 | 159 | GL.ActiveTexture(TextureUnit.Texture0) 160 | GL.BindTexture(TextureTarget.Texture2D, texture) 161 | let textureLocation = GL.GetUniformLocation(program, "textureSampler") 162 | GL.ProgramUniform1(program, textureLocation, 0) 163 | 164 | GL.DrawArrays(PrimitiveType.Triangles, 0, points.Length); 165 | 166 | this.Context.SwapBuffers() 167 | base.OnRenderFrame(e) 168 | 169 | [] 170 | let main argv = 171 | use window = new Window(640, 480, Graphics.GraphicsMode.Default, "Example Window", GameWindowFlags.Default) 172 | window.Run() 173 | 0 174 | -------------------------------------------------------------------------------- /Example07/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Example08/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Example08/Example08.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | d99ad474-c134-4733-aced-29c7e4bd7661 9 | Exe 10 | Example08 11 | Example08 12 | v4.5 13 | Example08 14 | 15 | 16 | true 17 | full 18 | false 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | 3 23 | AnyCPU 24 | bin\Debug\Example08.XML 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | true 31 | bin\Release\ 32 | TRACE 33 | 3 34 | AnyCPU 35 | bin\Release\Example08.XML 36 | true 37 | 38 | 39 | 11 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | PreserveNewest 48 | 49 | 50 | 51 | 52 | 53 | True 54 | 55 | 56 | ..\packages\OpenTK.1.1.1589.5941\lib\NET40\OpenTK.dll 57 | True 58 | 59 | 60 | 61 | 62 | 63 | 64 | 71 | -------------------------------------------------------------------------------- /Example08/Program.fs: -------------------------------------------------------------------------------- 1 | open OpenTK 2 | open OpenTK.Graphics 3 | open OpenTK.Graphics.OpenGL4 4 | 5 | type Vertex = 6 | struct 7 | val Position : Vector4 8 | val UV : Vector2 9 | 10 | new(position : Vector4, uv : Vector2) = { Position = position; UV = uv } 11 | end 12 | 13 | type Window(width, height, mode, title, options) = 14 | inherit GameWindow(width, height, mode, title, options) 15 | 16 | let quad p0 p1 p2 p3 = 17 | [| p0; p1; p2; p2; p1; p3 |] 18 | 19 | let points = 20 | [| 21 | // Front quad 22 | quad 23 | <| Vertex(Vector4(-1.0f, -1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 24 | <| Vertex(Vector4(-1.0f, 1.0f, -1.0f, 1.0f), Vector2(0.0f, 1.0f)) 25 | <| Vertex(Vector4( 1.0f, -1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 26 | <| Vertex(Vector4( 1.0f, 1.0f, -1.0f, 1.0f), Vector2(1.0f, 1.0f)) 27 | 28 | // Right quad 29 | quad 30 | <| Vertex(Vector4(1.0f, -1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 31 | <| Vertex(Vector4(1.0f, 1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 32 | <| Vertex(Vector4(1.0f, -1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 33 | <| Vertex(Vector4(1.0f, 1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 34 | 35 | // Left quad 36 | quad 37 | <| Vertex(Vector4(-1.0f, -1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 38 | <| Vertex(Vector4(-1.0f, -1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 39 | <| Vertex(Vector4(-1.0f, 1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 40 | <| Vertex(Vector4(-1.0f, 1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 41 | 42 | // Back quad 43 | quad 44 | <| Vertex(Vector4(-1.0f, -1.0f, 1.0f, 1.0f), Vector2(0.0f, 0.0f)) 45 | <| Vertex(Vector4( 1.0f, -1.0f, 1.0f, 1.0f), Vector2(1.0f, 0.0f)) 46 | <| Vertex(Vector4(-1.0f, 1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 47 | <| Vertex(Vector4( 1.0f, 1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 48 | 49 | // Top quad 50 | quad 51 | <| Vertex(Vector4(-1.0f, 1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 52 | <| Vertex(Vector4( 1.0f, 1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 53 | <| Vertex(Vector4(-1.0f, 1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 54 | <| Vertex(Vector4( 1.0f, 1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 55 | 56 | // Bottom quad 57 | quad 58 | <| Vertex(Vector4(-1.0f, -1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 59 | <| Vertex(Vector4(-1.0f, -1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 60 | <| Vertex(Vector4( 1.0f, -1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 61 | <| Vertex(Vector4( 1.0f, -1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 62 | |] |> Array.concat 63 | 64 | let vertexSource = """ 65 | #version 330 66 | 67 | uniform mat4 projection; 68 | 69 | layout (location = 0) in vec4 position; 70 | layout (location = 1) in vec2 uv; 71 | 72 | out vec2 fragmentUV; 73 | 74 | void main(void) 75 | { 76 | gl_Position = projection * position; 77 | fragmentUV = uv; 78 | } 79 | """ 80 | 81 | let fragmentSource = """ 82 | #version 330 83 | 84 | in vec2 fragmentUV; 85 | 86 | uniform sampler2D textureSampler; 87 | 88 | out vec4 outputColor; 89 | 90 | void main(void) 91 | { 92 | outputColor = texture(textureSampler, fragmentUV); 93 | } 94 | """ 95 | 96 | let createShader typ source = 97 | let shader = GL.CreateShader(typ) 98 | GL.ShaderSource(shader, source) 99 | GL.CompileShader(shader) 100 | let status = GL.GetShader(shader, ShaderParameter.CompileStatus) 101 | if status <> 0 then 102 | shader 103 | else 104 | failwith (GL.GetShaderInfoLog(shader)) 105 | 106 | let mutable vertexShader = 0 107 | let mutable fragmentShader = 0 108 | let mutable program = 0 109 | 110 | let mutable projectionLocation = 0 111 | 112 | let mutable verticesVbo = 0 113 | let mutable vao = 0 114 | 115 | let mutable rotation = 0.0f 116 | let mutable yTranslate = 0.0f 117 | let mutable yBounce = true 118 | 119 | let mutable texture = 0 120 | 121 | override this.OnLoad(e) = 122 | vertexShader <- createShader ShaderType.VertexShader vertexSource 123 | 124 | fragmentShader <- createShader ShaderType.FragmentShader fragmentSource 125 | 126 | program <- 127 | let program = GL.CreateProgram() 128 | GL.AttachShader(program, vertexShader) 129 | GL.AttachShader(program, fragmentShader) 130 | GL.LinkProgram(program) 131 | let status = GL.GetProgram(program, GetProgramParameterName.LinkStatus) 132 | if status <> 0 then 133 | program 134 | else 135 | failwith (GL.GetProgramInfoLog(program)) 136 | 137 | verticesVbo <- 138 | let buffer = GL.GenBuffer() 139 | GL.BindBuffer(BufferTarget.ArrayBuffer, buffer) 140 | buffer 141 | 142 | vao <- 143 | let array = GL.GenVertexArray() 144 | GL.BindVertexArray(array) 145 | array 146 | 147 | // Get the uniform locations 148 | projectionLocation <- GL.GetUniformLocation(program, "projection") 149 | 150 | // Transfer the vertices from CPU to GPU. 151 | GL.BufferData(BufferTarget.ArrayBuffer, nativeint(points.Length * sizeof), points, BufferUsageHint.StaticDraw) 152 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 153 | 154 | // Read texture file 155 | use bitmap = new System.Drawing.Bitmap("texture.bmp") 156 | let texture_data = 157 | Array.init (bitmap.Width * bitmap.Height) (fun i -> 158 | let x = i % bitmap.Width 159 | let y = i / bitmap.Height 160 | let pixel = bitmap.GetPixel(x, y) 161 | [|pixel.R; pixel.G; pixel.B;|] 162 | ) |> Array.concat 163 | 164 | // Create a new texture 165 | texture <- GL.GenTexture() 166 | GL.BindTexture(TextureTarget.Texture2D, texture) 167 | GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, texture_data) 168 | // Set texture wrapping and mip level bounds 169 | GL.GenerateMipmap(GenerateMipmapTarget.Texture2D) 170 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge) 171 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge) 172 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.LinearMipmapLinear); 173 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.LinearMipmapLinear); 174 | 175 | // Use the program. 176 | GL.UseProgram(program) 177 | let vertexPosition = GL.GetAttribLocation(program, "position"); 178 | let vertexUv = GL.GetAttribLocation(program, "uv"); 179 | 180 | GL.BindBuffer(BufferTarget.ArrayBuffer, verticesVbo) 181 | 182 | GL.VertexAttribPointer(vertexPosition, 4, VertexAttribPointerType.Float, false, sizeof, 0) 183 | GL.EnableVertexAttribArray(vertexPosition) 184 | 185 | GL.VertexAttribPointer(vertexUv, 2, VertexAttribPointerType.Float, false, sizeof, sizeof) 186 | GL.EnableVertexAttribArray(vertexUv) 187 | 188 | // Enable face culling 189 | GL.Enable(EnableCap.CullFace) 190 | GL.CullFace(CullFaceMode.Back) 191 | GL.FrontFace(FrontFaceDirection.Ccw) 192 | 193 | GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f) 194 | 195 | override this.OnUnload(e) = 196 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 197 | GL.DeleteBuffer(verticesVbo) 198 | GL.DeleteVertexArray(vao) 199 | 200 | GL.BindTexture(TextureTarget.Texture2D, 0) 201 | GL.DeleteTexture(texture) 202 | 203 | GL.UseProgram(0) 204 | GL.DeleteProgram(program) 205 | GL.DeleteShader(vertexShader) 206 | GL.DeleteShader(fragmentShader) 207 | base.OnUnload(e) 208 | 209 | override this.OnResize(e) = 210 | GL.Viewport(0, 0, this.Width, this.Height) 211 | base.OnResize(e) 212 | 213 | override this.OnRenderFrame(e) = 214 | GL.Clear(ClearBufferMask.ColorBufferBit) 215 | 216 | rotation <- if rotation >= 360.0f then rotation - 360.0f else rotation + (10.0f * float32(e.Time)) 217 | yBounce <- if yBounce then yTranslate >= -4.0f else yTranslate >= 4.0f 218 | yTranslate <- yTranslate + (if yBounce then -1.0f else +1.0f) * float32(e.Time) 219 | 220 | // Set projection matrix 221 | let projectionMatrix = 222 | Matrix4.CreateRotationY(MathHelper.DegreesToRadians(rotation)) * 223 | Matrix4.CreateTranslation(0.0f, yTranslate, -4.0f) * 224 | Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90.0f), float32(this.ClientSize.Width) / float32(this.ClientSize.Height), 0.1f, 10.f) 225 | 226 | GL.UniformMatrix4(projectionLocation, false, ref projectionMatrix) 227 | 228 | // Set texture 229 | GL.ActiveTexture(TextureUnit.Texture0) 230 | GL.BindTexture(TextureTarget.Texture2D, texture) 231 | let textureLocation = GL.GetUniformLocation(program, "textureSampler") 232 | GL.ProgramUniform1(program, textureLocation, 0) 233 | 234 | GL.DrawArrays(PrimitiveType.Triangles, 0, points.Length) 235 | 236 | this.Context.SwapBuffers() 237 | base.OnRenderFrame(e) 238 | 239 | [] 240 | let main argv = 241 | use window = new Window(640, 480, Graphics.GraphicsMode.Default, "Example Window", GameWindowFlags.Default) 242 | window.Run() 243 | 0 244 | -------------------------------------------------------------------------------- /Example08/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Example08/texture.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Frassle/OpenTKExamples/b0751e4fbf780f2ddbd83589ef0336759de6261b/Example08/texture.bmp -------------------------------------------------------------------------------- /Example09/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Example09/Example09.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | 1c62efe6-b110-47ba-b156-b0082a8ef02c 9 | Exe 10 | Example09 11 | Example09 12 | v4.5 13 | Example09 14 | 15 | 16 | true 17 | full 18 | false 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | 3 23 | AnyCPU 24 | bin\Debug\Example09.XML 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | true 31 | bin\Release\ 32 | TRACE 33 | 3 34 | AnyCPU 35 | bin\Release\Example09.XML 36 | true 37 | 38 | 39 | 11 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | PreserveNewest 48 | 49 | 50 | 51 | 52 | 53 | True 54 | 55 | 56 | ..\packages\OpenTK.1.1.1589.5941\lib\NET40\OpenTK.dll 57 | True 58 | 59 | 60 | 61 | 62 | 63 | 64 | 71 | -------------------------------------------------------------------------------- /Example09/Program.fs: -------------------------------------------------------------------------------- 1 | open OpenTK 2 | open OpenTK.Graphics 3 | open OpenTK.Graphics.OpenGL4 4 | 5 | type Vertex = 6 | struct 7 | val Position : Vector4 8 | val UV : Vector2 9 | 10 | new(position : Vector4, uv : Vector2) = { Position = position; UV = uv } 11 | end 12 | 13 | type Window(width, height, mode, title, options) = 14 | inherit GameWindow(width, height, mode, title, options) 15 | 16 | let quad p0 p1 p2 p3 = 17 | [| p0; p1; p2; p3 |] 18 | 19 | let points = 20 | [| 21 | // Front quad 22 | quad 23 | <| Vertex(Vector4(-1.0f, -1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 24 | <| Vertex(Vector4(-1.0f, 1.0f, -1.0f, 1.0f), Vector2(0.0f, 1.0f)) 25 | <| Vertex(Vector4( 1.0f, -1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 26 | <| Vertex(Vector4( 1.0f, 1.0f, -1.0f, 1.0f), Vector2(1.0f, 1.0f)) 27 | 28 | // Right quad 29 | quad 30 | <| Vertex(Vector4(1.0f, -1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 31 | <| Vertex(Vector4(1.0f, 1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 32 | <| Vertex(Vector4(1.0f, -1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 33 | <| Vertex(Vector4(1.0f, 1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 34 | 35 | // Left quad 36 | quad 37 | <| Vertex(Vector4(-1.0f, -1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 38 | <| Vertex(Vector4(-1.0f, -1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 39 | <| Vertex(Vector4(-1.0f, 1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 40 | <| Vertex(Vector4(-1.0f, 1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 41 | 42 | // Back quad 43 | quad 44 | <| Vertex(Vector4(-1.0f, -1.0f, 1.0f, 1.0f), Vector2(0.0f, 0.0f)) 45 | <| Vertex(Vector4( 1.0f, -1.0f, 1.0f, 1.0f), Vector2(1.0f, 0.0f)) 46 | <| Vertex(Vector4(-1.0f, 1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 47 | <| Vertex(Vector4( 1.0f, 1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 48 | 49 | // Top quad 50 | quad 51 | <| Vertex(Vector4(-1.0f, 1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 52 | <| Vertex(Vector4( 1.0f, 1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 53 | <| Vertex(Vector4(-1.0f, 1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 54 | <| Vertex(Vector4( 1.0f, 1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 55 | 56 | // Bottom quad 57 | quad 58 | <| Vertex(Vector4(-1.0f, -1.0f, 1.0f, 1.0f), Vector2(0.0f, 1.0f)) 59 | <| Vertex(Vector4(-1.0f, -1.0f, -1.0f, 1.0f), Vector2(0.0f, 0.0f)) 60 | <| Vertex(Vector4( 1.0f, -1.0f, 1.0f, 1.0f), Vector2(1.0f, 1.0f)) 61 | <| Vertex(Vector4( 1.0f, -1.0f, -1.0f, 1.0f), Vector2(1.0f, 0.0f)) 62 | |] |> Array.concat 63 | 64 | let indexQuad (index : uint16) = 65 | let i0 = index 66 | let i1 = index + 1us 67 | let i2 = index + 2us 68 | let i3 = index + 3us 69 | [| i0; i1; i2; i2; i1; i3 |] 70 | 71 | let elements = 72 | [| 73 | indexQuad 0us 74 | indexQuad 4us 75 | indexQuad 8us 76 | indexQuad 12us 77 | indexQuad 16us 78 | indexQuad 20us 79 | |] |> Array.concat 80 | 81 | let vertexSource = """ 82 | #version 330 83 | 84 | uniform mat4 projection; 85 | 86 | layout (location = 0) in vec4 position; 87 | layout (location = 1) in vec2 uv; 88 | 89 | out vec2 fragmentUV; 90 | 91 | void main(void) 92 | { 93 | gl_Position = projection * position; 94 | fragmentUV = uv; 95 | } 96 | """ 97 | 98 | let fragmentSource = """ 99 | #version 330 100 | 101 | in vec2 fragmentUV; 102 | 103 | uniform sampler2D textureSampler; 104 | 105 | out vec4 outputColor; 106 | 107 | void main(void) 108 | { 109 | outputColor = texture(textureSampler, fragmentUV); 110 | } 111 | """ 112 | 113 | let createShader typ source = 114 | let shader = GL.CreateShader(typ) 115 | GL.ShaderSource(shader, source) 116 | GL.CompileShader(shader) 117 | let status = GL.GetShader(shader, ShaderParameter.CompileStatus) 118 | if status <> 0 then 119 | shader 120 | else 121 | failwith (GL.GetShaderInfoLog(shader)) 122 | 123 | let mutable vertexShader = 0 124 | let mutable fragmentShader = 0 125 | let mutable program = 0 126 | 127 | let mutable projectionLocation = 0 128 | let mutable textureLocation = 0 129 | 130 | let mutable verticesVbo = 0 131 | let mutable elementBuffer = 0 132 | let mutable vao = 0 133 | 134 | let mutable rotation = 0.0f 135 | let mutable yTranslate = 0.0f 136 | let mutable yBounce = true 137 | 138 | let mutable texture = 0 139 | 140 | override this.OnLoad(e) = 141 | vertexShader <- createShader ShaderType.VertexShader vertexSource 142 | 143 | fragmentShader <- createShader ShaderType.FragmentShader fragmentSource 144 | 145 | program <- 146 | let program = GL.CreateProgram() 147 | GL.AttachShader(program, vertexShader) 148 | GL.AttachShader(program, fragmentShader) 149 | GL.LinkProgram(program) 150 | let status = GL.GetProgram(program, GetProgramParameterName.LinkStatus) 151 | if status <> 0 then 152 | program 153 | else 154 | failwith (GL.GetProgramInfoLog(program)) 155 | 156 | vao <- 157 | let array = GL.GenVertexArray() 158 | GL.BindVertexArray(array) 159 | GL.BindVertexArray(0) 160 | array 161 | 162 | verticesVbo <- 163 | let buffer = GL.GenBuffer() 164 | GL.BindBuffer(BufferTarget.ArrayBuffer, buffer) 165 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 166 | buffer 167 | 168 | elementBuffer <- 169 | let buffer = GL.GenBuffer() 170 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, buffer) 171 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0) 172 | buffer 173 | 174 | // Get the uniform locations 175 | projectionLocation <- GL.GetUniformLocation(program, "projection") 176 | textureLocation <- GL.GetUniformLocation(program, "textureSampler") 177 | 178 | // Transfer the vertices from CPU to GPU. 179 | GL.BindBuffer(BufferTarget.ArrayBuffer, verticesVbo) 180 | GL.BufferData(BufferTarget.ArrayBuffer, nativeint(points.Length * sizeof), points, BufferUsageHint.StaticDraw) 181 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 182 | 183 | // Transfer the indices from CPU to GPU. 184 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, elementBuffer) 185 | GL.BufferData(BufferTarget.ElementArrayBuffer, nativeint(elements.Length * sizeof), elements, BufferUsageHint.StaticDraw) 186 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0) 187 | 188 | // Read texture file 189 | use bitmap = new System.Drawing.Bitmap("texture.bmp") 190 | let texture_data = 191 | Array.init (bitmap.Width * bitmap.Height) (fun i -> 192 | let x = i % bitmap.Width 193 | let y = i / bitmap.Height 194 | let pixel = bitmap.GetPixel(x, y) 195 | [|pixel.R; pixel.G; pixel.B;|] 196 | ) |> Array.concat 197 | 198 | // Create a new texture 199 | texture <- GL.GenTexture() 200 | GL.BindTexture(TextureTarget.Texture2D, texture) 201 | GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, texture_data) 202 | // Set texture wrapping and mip level bounds 203 | GL.GenerateMipmap(GenerateMipmapTarget.Texture2D) 204 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge) 205 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge) 206 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.LinearMipmapLinear); 207 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.LinearMipmapLinear); 208 | GL.BindTexture(TextureTarget.Texture2D, 0) 209 | 210 | // Setup the vertex array 211 | let vertexPosition = GL.GetAttribLocation(program, "position"); 212 | let vertexUv = GL.GetAttribLocation(program, "uv"); 213 | 214 | GL.BindVertexArray(vao) 215 | GL.BindBuffer(BufferTarget.ArrayBuffer, verticesVbo) 216 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, elementBuffer) 217 | 218 | GL.VertexAttribPointer(vertexPosition, 4, VertexAttribPointerType.Float, false, sizeof, 0) 219 | GL.EnableVertexAttribArray(vertexPosition) 220 | 221 | GL.VertexAttribPointer(vertexUv, 2, VertexAttribPointerType.Float, false, sizeof, sizeof) 222 | GL.EnableVertexAttribArray(vertexUv) 223 | 224 | GL.BindVertexArray(0) 225 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 226 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0) 227 | 228 | // Enable face culling 229 | GL.Enable(EnableCap.CullFace) 230 | GL.CullFace(CullFaceMode.Back) 231 | GL.FrontFace(FrontFaceDirection.Ccw) 232 | 233 | GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f) 234 | 235 | override this.OnUnload(e) = 236 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 237 | GL.DeleteBuffer(verticesVbo) 238 | 239 | GL.BindVertexArray(0) 240 | GL.DeleteVertexArray(vao) 241 | 242 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0) 243 | GL.DeleteBuffer(elementBuffer) 244 | 245 | GL.BindTexture(TextureTarget.Texture2D, 0) 246 | GL.DeleteTexture(texture) 247 | 248 | GL.UseProgram(0) 249 | GL.DeleteProgram(program) 250 | GL.DeleteShader(vertexShader) 251 | GL.DeleteShader(fragmentShader) 252 | base.OnUnload(e) 253 | 254 | override this.OnResize(e) = 255 | GL.Viewport(0, 0, this.Width, this.Height) 256 | base.OnResize(e) 257 | 258 | override this.OnRenderFrame(e) = 259 | GL.Clear(ClearBufferMask.ColorBufferBit) 260 | 261 | rotation <- if rotation >= 360.0f then rotation - 360.0f else rotation + (10.0f * float32(e.Time)) 262 | yBounce <- if yBounce then yTranslate >= -4.0f else yTranslate >= 4.0f 263 | yTranslate <- yTranslate + (if yBounce then -1.0f else +1.0f) * float32(e.Time) 264 | 265 | let projectionMatrix = 266 | Matrix4.CreateRotationY(MathHelper.DegreesToRadians(rotation)) * 267 | Matrix4.CreateTranslation(0.0f, yTranslate, -4.0f) * 268 | Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90.0f), float32(this.ClientSize.Width) / float32(this.ClientSize.Height), 0.1f, 10.f) 269 | 270 | // Set projection matrix 271 | GL.UseProgram(program) 272 | GL.UniformMatrix4(projectionLocation, false, ref projectionMatrix) 273 | 274 | // Set texture 275 | GL.ActiveTexture(TextureUnit.Texture0) 276 | GL.BindTexture(TextureTarget.Texture2D, texture) 277 | GL.ProgramUniform1(program, textureLocation, 0) 278 | 279 | // Bind vertex array object 280 | GL.BindVertexArray(vao) 281 | 282 | // Draw 283 | GL.DrawElements(BeginMode.Triangles, elements.Length, DrawElementsType.UnsignedShort, 0) 284 | 285 | this.Context.SwapBuffers() 286 | base.OnRenderFrame(e) 287 | 288 | [] 289 | let main argv = 290 | use window = new Window(640, 480, Graphics.GraphicsMode.Default, "Example Window", GameWindowFlags.Default) 291 | window.Run() 292 | 0 293 | -------------------------------------------------------------------------------- /Example09/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Example09/texture.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Frassle/OpenTKExamples/b0751e4fbf780f2ddbd83589ef0336759de6261b/Example09/texture.bmp -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) {{{year}}} {{{fullname}}} 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OpenTK Examples.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Example01", "Example01\Example01.fsproj", "{626332A3-544B-4810-A138-40C6841B384E}" 5 | EndProject 6 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Example02", "Example02\Example02.fsproj", "{AB324615-A07A-4CEC-A14A-9B8DFB5DFBF4}" 7 | EndProject 8 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Example03", "Example03\Example03.fsproj", "{2B7B8811-0B23-4B57-A13F-D203699A5FF2}" 9 | EndProject 10 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Example04", "Example04\Example04.fsproj", "{33AEA5C9-D268-4318-818D-9F4AF2C30000}" 11 | EndProject 12 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Example05", "Example05\Example05.fsproj", "{E45E180B-D0AA-43A5-A08F-E51AB5FBDE76}" 13 | EndProject 14 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Example06", "Example06\Example06.fsproj", "{BD7BADFB-6EB0-43F3-91ED-BA0C0FDBB68E}" 15 | EndProject 16 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{19745CF3-7F1E-43B4-BF06-DB6A0E04D767}" 17 | ProjectSection(SolutionItems) = preProject 18 | README.md = README.md 19 | EndProjectSection 20 | EndProject 21 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Example07", "Example07\Example07.fsproj", "{162BBF58-3D22-459B-AD2B-ED66DC6FA106}" 22 | EndProject 23 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Example08", "Example08\Example08.fsproj", "{D99AD474-C134-4733-ACED-29C7E4BD7661}" 24 | EndProject 25 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Example09", "Example09\Example09.fsproj", "{1C62EFE6-B110-47BA-B156-B0082A8EF02C}" 26 | EndProject 27 | Global 28 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 29 | Debug|Any CPU = Debug|Any CPU 30 | Release|Any CPU = Release|Any CPU 31 | EndGlobalSection 32 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 33 | {626332A3-544B-4810-A138-40C6841B384E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 34 | {626332A3-544B-4810-A138-40C6841B384E}.Debug|Any CPU.Build.0 = Debug|Any CPU 35 | {626332A3-544B-4810-A138-40C6841B384E}.Release|Any CPU.ActiveCfg = Release|Any CPU 36 | {626332A3-544B-4810-A138-40C6841B384E}.Release|Any CPU.Build.0 = Release|Any CPU 37 | {AB324615-A07A-4CEC-A14A-9B8DFB5DFBF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 38 | {AB324615-A07A-4CEC-A14A-9B8DFB5DFBF4}.Debug|Any CPU.Build.0 = Debug|Any CPU 39 | {AB324615-A07A-4CEC-A14A-9B8DFB5DFBF4}.Release|Any CPU.ActiveCfg = Release|Any CPU 40 | {AB324615-A07A-4CEC-A14A-9B8DFB5DFBF4}.Release|Any CPU.Build.0 = Release|Any CPU 41 | {2B7B8811-0B23-4B57-A13F-D203699A5FF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 42 | {2B7B8811-0B23-4B57-A13F-D203699A5FF2}.Debug|Any CPU.Build.0 = Debug|Any CPU 43 | {2B7B8811-0B23-4B57-A13F-D203699A5FF2}.Release|Any CPU.ActiveCfg = Release|Any CPU 44 | {2B7B8811-0B23-4B57-A13F-D203699A5FF2}.Release|Any CPU.Build.0 = Release|Any CPU 45 | {33AEA5C9-D268-4318-818D-9F4AF2C30000}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 46 | {33AEA5C9-D268-4318-818D-9F4AF2C30000}.Debug|Any CPU.Build.0 = Debug|Any CPU 47 | {33AEA5C9-D268-4318-818D-9F4AF2C30000}.Release|Any CPU.ActiveCfg = Release|Any CPU 48 | {33AEA5C9-D268-4318-818D-9F4AF2C30000}.Release|Any CPU.Build.0 = Release|Any CPU 49 | {E45E180B-D0AA-43A5-A08F-E51AB5FBDE76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 50 | {E45E180B-D0AA-43A5-A08F-E51AB5FBDE76}.Debug|Any CPU.Build.0 = Debug|Any CPU 51 | {E45E180B-D0AA-43A5-A08F-E51AB5FBDE76}.Release|Any CPU.ActiveCfg = Release|Any CPU 52 | {E45E180B-D0AA-43A5-A08F-E51AB5FBDE76}.Release|Any CPU.Build.0 = Release|Any CPU 53 | {BD7BADFB-6EB0-43F3-91ED-BA0C0FDBB68E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 54 | {BD7BADFB-6EB0-43F3-91ED-BA0C0FDBB68E}.Debug|Any CPU.Build.0 = Debug|Any CPU 55 | {BD7BADFB-6EB0-43F3-91ED-BA0C0FDBB68E}.Release|Any CPU.ActiveCfg = Release|Any CPU 56 | {BD7BADFB-6EB0-43F3-91ED-BA0C0FDBB68E}.Release|Any CPU.Build.0 = Release|Any CPU 57 | {162BBF58-3D22-459B-AD2B-ED66DC6FA106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 58 | {162BBF58-3D22-459B-AD2B-ED66DC6FA106}.Debug|Any CPU.Build.0 = Debug|Any CPU 59 | {162BBF58-3D22-459B-AD2B-ED66DC6FA106}.Release|Any CPU.ActiveCfg = Release|Any CPU 60 | {162BBF58-3D22-459B-AD2B-ED66DC6FA106}.Release|Any CPU.Build.0 = Release|Any CPU 61 | {D99AD474-C134-4733-ACED-29C7E4BD7661}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 62 | {D99AD474-C134-4733-ACED-29C7E4BD7661}.Debug|Any CPU.Build.0 = Debug|Any CPU 63 | {D99AD474-C134-4733-ACED-29C7E4BD7661}.Release|Any CPU.ActiveCfg = Release|Any CPU 64 | {D99AD474-C134-4733-ACED-29C7E4BD7661}.Release|Any CPU.Build.0 = Release|Any CPU 65 | {1C62EFE6-B110-47BA-B156-B0082A8EF02C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 66 | {1C62EFE6-B110-47BA-B156-B0082A8EF02C}.Debug|Any CPU.Build.0 = Debug|Any CPU 67 | {1C62EFE6-B110-47BA-B156-B0082A8EF02C}.Release|Any CPU.ActiveCfg = Release|Any CPU 68 | {1C62EFE6-B110-47BA-B156-B0082A8EF02C}.Release|Any CPU.Build.0 = Release|Any CPU 69 | EndGlobalSection 70 | GlobalSection(SolutionProperties) = preSolution 71 | HideSolutionNode = FALSE 72 | EndGlobalSection 73 | EndGlobal 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | OpenTKExamples 2 | ============== 3 | 4 | Small F# example programs for OpenTK. 5 | 6 | Examples 7 | ============== 8 | 9 | 1. An OpenTK window. 10 | 2. Solid colored triangle. 11 | 3. Triangle with blended colors between vertices. 12 | 4. Same as example 3 but using structs for vertices. 13 | 5. Moving color changing triangle. 14 | 6. Bouncing rotating cube. 15 | 7. Simple textured quad. 16 | 8. Bouncing rotating textured cube. 17 | 9. Same as example 8 but using element arrays. --------------------------------------------------------------------------------