summaryrefslogtreecommitdiff
path: root/libs/pwyazh/mesh.h
blob: 9443a13497a45a6c75f8b12f908e3515a9631858 (plain)
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
#ifndef MESH_H
#define MESH_H

Vertex
vertex(V3F pos, V3F normal, V2F tex_coords)
{
    Vertex result;
    result.pos = pos;
    result.normal = normal;
    result.tex_coords = tex_coords;
    return(result);
}

Mesh *
mesh_init(Arena *arena,
          Vertex *vertices, U32 vertex_count,
          U32 *indices, U32 index_count)
{
    Mesh *mesh = 0;
    U32 vbo, ebo;
    
    if (!vertices)
    {
        printf("[ERROR]: Vertices is null\n");
        return(mesh);
    }
    if (!indices)
    {
        printf("[ERROR]: Indices is null\n");
        return(mesh);
    }
    
    Assert(arena);
    mesh = arena_push_size(arena, sizeof(Mesh));
    
    mesh->vertices = vertices;
    mesh->vertex_count = vertex_count;
    
    mesh->indices = indices;
    mesh->index_count = index_count;
    
    glGenVertexArrays(1, &mesh->vao);
    glBindVertexArray(mesh->vao);
    
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, vertex_count*sizeof(Vertex), vertices, GL_STATIC_DRAW);
    
    glGenBuffers(1, &ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_count*sizeof(U32), indices, GL_STATIC_DRAW);
    
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
                          (void *)(OffsetOfMember(Vertex, pos)));
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
                          (void *)(OffsetOfMember(Vertex, normal)));
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
                          (void *)(OffsetOfMember(Vertex, tex_coords)));
    
    glBindVertexArray(0);
    
    return(mesh);
}

void
mesh_draw(Mesh *mesh)
{
    glBindVertexArray(mesh->vao);
    glDrawElements(GL_TRIANGLES, mesh->index_count, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

#endif /* MESH_H */