-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.adept
57 lines (44 loc) · 1.7 KB
/
model.adept
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
// ---------------------------- model.adept ----------------------------
// Module for handling OpenGL models
// ---------------------------------------------------------------------
import 'glfw/glfw.adept'
import 'sys/cstdio.adept'
import 'sys/cstdlib.adept'
import 'adept/filetypes/bitmap.adept'
struct Model (vao GLuint, vertices_vbo GLuint, uvs_vbo GLuint,
texture_id GLuint, triangle_count int)
func create(this *Model, points *float, points_length usize,
uvs *float, uvs_length usize) void {
this.triangle_count = cast int(points_length / 3ui)
this.vertices_vbo = 0ui
glGenBuffers(1, &this.vertices_vbo)
glBindBuffer(GL_ARRAY_BUFFER, this.vertices_vbo)
glBufferData(GL_ARRAY_BUFFER, cast int(sizeof float * points_length), points, GL_STATIC_DRAW)
this.uvs_vbo = 0ui
glGenBuffers(1, &this.uvs_vbo)
glBindBuffer(GL_ARRAY_BUFFER, this.uvs_vbo)
glBufferData(GL_ARRAY_BUFFER, cast int(sizeof float * uvs_length), uvs, GL_STATIC_DRAW)
glGenVertexArrays(1, &this.vao)
glBindVertexArray(this.vao)
glEnableVertexAttribArray(0ui)
glBindBuffer(GL_ARRAY_BUFFER, this.vertices_vbo)
glVertexAttribPointer(0ui, 3, GL_FLOAT, GL_FALSE, 0, null)
glEnableVertexAttribArray(1ui)
glBindBuffer(GL_ARRAY_BUFFER, this.uvs_vbo)
glVertexAttribPointer(1ui, 2, GL_FLOAT, GL_FALSE, 0, null)
glBindBuffer(GL_ARRAY_BUFFER, 0ui)
glBindVertexArray(0ui)
}
func draw(this *Model) void {
glBindVertexArray(this.vao)
glDrawArrays(GL_TRIANGLES, 0, this.triangle_count)
}
func destroy(this *Model) void {
glDeleteBuffers(1, &this.vertices_vbo)
glDeleteBuffers(1, &this.uvs_vbo)
glDeleteVertexArrays(1, &this.vao)
}
func bindTexture(texture_id GLuint) void {
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texture_id)
}