-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpriteRenderer.fs
67 lines (52 loc) · 1.92 KB
/
SpriteRenderer.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module OpenGl.SpriteRenderer
open System.Numerics
open Silk.NET.OpenGL
open System
(*
let private vertices = [|
0.0f; 1.0f; 0.0f; 1.0f
1.0f; 0.0f; 1.0f; 0.0f
0.0f; 0.0f; 0.0f; 0.0f
0.0f; 1.0f; 0.0f; 1.0f
1.0f; 1.0f; 1.0f; 1.0f
1.0f; 0.0f; 1.0f; 0.0f
|]*)
let private vertices = [|
1.0f ; 1.0f ; 1.0f ; 1.0f
1.0f ; 0.0f ; 1.0f ; 0.0f
0.0f ; 0.0f ; 0.0f ; 0.0f
0.0f ; 1.0f ; 0.0f ; 1.0f
|]
(*
let vertices = [|
0.5f ; 0.5f ; 1.0f ; 1.0f
0.5f ; -0.5f ; 1.0f ; 0.0f
-0.5f ; -0.5f ; 0.0f ; 0.0f
-0.5f ; 0.5f ; 0.0f ; 1.0f
|]*)
let private indices = [|
0u ; 1u ; 3u
1u ; 2u ; 3u
|]
let create gl consoleWidth consoleHeight =
let vbo = BufferObject.create gl vertices BufferTargetARB.ArrayBuffer
let ebo = BufferObject.create gl indices BufferTargetARB.ElementArrayBuffer
let vao = VertexArrayObject.create gl vbo ebo
VertexArrayObject.vertexAttributePointer vao 0u 2 VertexAttribPointerType.Float 4u 0
VertexArrayObject.vertexAttributePointer vao 1u 2 VertexAttribPointerType.Float 4u 2
let shader = Shader.createShader gl "sprite"
let projectionMatrix = Matrix4x4.CreateOrthographicOffCenter(0.0f, consoleWidth, consoleHeight, 0.0f, -1.0f, 1.0f)
let render (position:Vector2) (size:Vector2) (texture:Texture.T) =
let model =
Matrix4x4.Identity *
Matrix4x4.CreateScale(Vector3(size.X, size.Y, 1.0f)) *
Matrix4x4.CreateTranslation(Vector3(position.X, position.Y, 0.0f))
Shader.useShader shader
Texture.bindWithSlot TextureUnit.Texture0 texture
Shader.setUniformInt shader "image" 0
Shader.setMatrix4x4 shader "projection" projectionMatrix
Shader.setMatrix4x4 shader "model" model
Shader.setVector3f shader "spriteColor" (Vector3(1.0f, 1.0f, 1.0f))
VertexArrayObject.bind vao
gl.DrawElements (PrimitiveType.Triangles, indices.Length |> uint32, DrawElementsType.UnsignedInt, IntPtr.Zero.ToPointer())
render