-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextImage.cs
More file actions
83 lines (69 loc) · 3 KB
/
TextImage.cs
File metadata and controls
83 lines (69 loc) · 3 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using OpenTK.Graphics.OpenGL4;
using System.Diagnostics;
using OpenTK.Mathematics;
namespace MedViewer
{
class TextImage
{
private readonly float[] _vertices =
{
// Position Texture coordinates
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
};
private readonly uint[] _indices =
{
0, 1, 3,
1, 2, 3
};
private int vbo_index;
private int vbo_vertex;
private int vao;
public Matrix4 transform = Matrix4.Identity;
public void ConstructVAO()
{
vao = GL.GenVertexArray();
GL.BindVertexArray(vao);
vbo_vertex = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_vertex);
GL.BufferData(BufferTarget.ArrayBuffer, _vertices.Length * sizeof(float), _vertices, BufferUsageHint.StaticDraw);
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
vbo_index = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo_index);
GL.BufferData(BufferTarget.ElementArrayBuffer, _indices.Length * sizeof(uint),_indices, BufferUsageHint.StaticDraw);
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
}
public void BindToShader(Shader shader)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_vertex);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo_index);
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
var vertexLocation = shader.GetAttribLocation("aPosition");
GL.EnableVertexAttribArray(vertexLocation);
GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);
var texCoordLocation = shader.GetAttribLocation("aTexCoord");
GL.EnableVertexAttribArray(texCoordLocation);
GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
}
public void Draw()
{
GL.BindVertexArray(vao);
GL.DrawElements(PrimitiveType.Triangles, _indices.Length, DrawElementsType.UnsignedInt, 0);
Debug.Assert(GL.GetError() == OpenTK.Graphics.OpenGL4.ErrorCode.NoError);
GL.BindVertexArray(0);
}
public void Delete()
{
GL.DeleteBuffer(vbo_vertex);
GL.DeleteBuffer(vbo_index);
GL.DeleteVertexArray(vao);
}
}
}