Created
January 7, 2015 19:51
-
-
Save talyian/04b28e9d520cac219fcb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "System.Drawing" | |
#r "OpenTK" | |
open OpenTK | |
open OpenTK.Graphics | |
open OpenTK.Graphics.OpenGL4 | |
let window = new GameWindow (400,400, | |
GraphicsMode.Default, "", | |
GameWindowFlags.Default, | |
DisplayDevice.Default, | |
4, 1, GraphicsContextFlags.ForwardCompatible) | |
let vao = | |
let vao = GL.GenVertexArray() | |
GL.BindVertexArray(vao) | |
let vbo = GL.GenBuffer() | |
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo) | |
let data = [| | |
// Position Color Texcoords | |
-0.5f; 0.5f; 1.0f; 0.0f; 0.0f; 0.0f; 0.0f; // Top-left | |
0.5f; 0.5f; 0.0f; 1.0f; 0.0f; 1.0f; 0.0f; // Top-right | |
0.5f; -0.5f; 0.0f; 0.0f; 1.0f; 1.0f; 1.0f; // Bottom-right | |
-0.5f; -0.5f; 1.0f; 1.0f; 1.0f; 0.0f; 1.0f // Bottom-left | |
|] | |
GL.BufferData(BufferTarget.ArrayBuffer, | |
nativeint (4*data.Length), data, | |
BufferUsageHint.StaticDraw) | |
let ebo = GL.GenBuffer() | |
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ebo) | |
let elements = [| 0;1;2;2;3;0 |] | |
GL.BufferData(BufferTarget.ElementArrayBuffer, | |
nativeint (4 * elements.Length), elements, | |
BufferUsageHint.StaticDraw) | |
vao | |
let vs_source = @" | |
#version 150 core | |
in vec2 position; | |
in vec3 color; | |
in vec2 texcoord; | |
out vec2 uv; | |
void main() { | |
uv = texcoord; | |
gl_Position = vec4(position, 0, 1); | |
}" | |
let fs_source = " | |
#version 150 core | |
uniform sampler2D tex; | |
in vec2 uv; | |
out vec4 col; | |
void main() { | |
col = texture(tex, uv); | |
}" | |
let shaderprogram = | |
let vs, fs = | |
GL.CreateShader(ShaderType.VertexShader), | |
GL.CreateShader(ShaderType.FragmentShader) | |
GL.ShaderSource(vs, vs_source) | |
GL.CompileShader(vs) | |
GL.ShaderSource(fs, fs_source) | |
GL.CompileShader(fs) | |
GL.GetShaderInfoLog(fs) |> printfn "%s" | |
GL.GetShaderInfoLog(vs) |> printfn "%s" | |
let id = GL.CreateProgram() | |
GL.AttachShader(id, vs) | |
GL.AttachShader(id, fs) | |
GL.LinkProgram(id); GL.UseProgram(id); | |
GL.GetProgramInfoLog(id) |> printfn "%s" | |
id | |
let textures = | |
let t1, t2 = GL.GenTexture(), GL.GenTexture() | |
GL.ActiveTexture(TextureUnit.Texture0) | |
GL.BindTexture(TextureTarget.Texture2D, t1) | |
let r = new System.Random(0) | |
let data = Array.init 10000 (fun i -> r.Next()) | |
GL.TexImage2D(TextureTarget.Texture2D, 0, | |
PixelInternalFormat.Rgba, 100, 100, 0, | |
PixelFormat.Rgba, PixelType.UnsignedByte, data); | |
GL.TexParameter(TextureTarget.Texture2D, | |
TextureParameterName.TextureMinFilter, | |
(float32 TextureMinFilter.Nearest)) | |
GL.TexParameter(TextureTarget.Texture2D, | |
TextureParameterName.TextureMagFilter, | |
(float32 TextureMagFilter.Nearest)) | |
let pos = GL.GetAttribLocation(shaderprogram, "position") | |
GL.EnableVertexAttribArray(pos) | |
GL.VertexAttribPointer(pos, 2, VertexAttribPointerType.Float, | |
false, 7 * 4, (nativeint 0)); | |
let col = GL.GetAttribLocation(shaderprogram, "color") | |
GL.EnableVertexAttribArray(col) | |
GL.VertexAttribPointer(pos, 3, VertexAttribPointerType.Float, | |
false, 7 * 4, (nativeint 8)); | |
let tex = GL.GetAttribLocation(shaderprogram, "texcoord") | |
GL.EnableVertexAttribArray(tex) | |
GL.VertexAttribPointer(tex, 2, VertexAttribPointerType.Float, | |
false, 7 * 4, (nativeint (5 * 4))); | |
let render e = | |
GL.ClearColor(Color4.Gray) | |
GL.Clear(ClearBufferMask.ColorBufferBit) | |
GL.DrawElements(PrimitiveType.Triangles, 6, | |
DrawElementsType.UnsignedInt, | |
(nativeint 0)) | |
window.SwapBuffers() | |
window.RenderFrame.Add render | |
window.Run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment