97ce2f060653b01ac266dce00d388692b5d06478
[demo] / src / vulkan / mesh-vk.cc
1 #include <vulkan/vulkan.h>
2 #include "mesh-vk.h"
3
4 MeshVK::MeshVK()
5 {
6 }
7
8 MeshVK::MeshVK(const MeshVK &mesh)
9 {
10         indices = mesh.indices;
11         vertices = mesh.vertices;
12         normals = mesh.normals;
13         tex_coords = mesh.tex_coords;
14 }
15
16 MeshVK &MeshVK::operator=(const MeshVK &mesh)
17 {
18         if(this == &mesh)
19                 return *this;
20
21         /* what the copy constructor does */
22         indices = mesh.indices;
23         vertices = mesh.vertices;
24         normals = mesh.normals;
25         tex_coords = mesh.tex_coords;
26
27         return *this;
28 }
29
30 MeshVK::~MeshVK()
31 {
32         vku_destroy_buffer(vk_vertices);
33         vku_destroy_buffer(vk_normals);
34         vku_destroy_buffer(vk_tex_coords);
35         vku_destroy_buffer(vk_indices);
36
37         vertices.clear();
38         normals.clear();
39         tex_coords.clear();
40         indices.clear();
41 }
42
43 bool MeshVK::update_vertex_data()
44 {
45         if(vertices.empty()) {
46                 printf("empty vertices!\n");
47                 return false;
48         }
49
50         /* create the buffers */
51
52         if(!(vk_vertices = vku_create_buffer(vertices.size() * sizeof(Vec3),
53                                         VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
54                 fprintf(stderr, "Failed to create the buffer for the vertices.\n");
55                 return false;
56         }
57
58         if(!(vk_normals = vku_create_buffer(normals.size() * sizeof(Vec3),
59                                         VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
60                 vku_destroy_buffer(vk_vertices);
61
62                 fprintf(stderr, "Failed to create the buffer for the normals.\n");
63                 return false;
64         }
65
66         if(!(vk_tex_coords = vku_create_buffer(tex_coords.size() * sizeof(Vec2),
67                                         VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
68                 vku_destroy_buffer(vk_vertices);
69                 vku_destroy_buffer(vk_normals);
70
71                 fprintf(stderr,
72                                 "Failed to create the buffer for the texture coordinates.\n");
73                 return false;
74         }
75
76         if(!(vk_indices = vku_create_buffer(indices.size() * 2,
77                                         VK_BUFFER_USAGE_INDEX_BUFFER_BIT))) {
78                 vku_destroy_buffer(vk_vertices);
79                 vku_destroy_buffer(vk_normals);
80                 vku_destroy_buffer(vk_tex_coords);
81
82                 fprintf(stderr, "Failed to create the indices buffer.\n");
83                 return false;
84         }
85
86         /* map the buffers */
87
88         //TODO this is going to change when we allocate one mem block for all data,
89         //for the moment we map each buffer memory block and we set the data
90
91         return true;
92 }
93
94 void MeshVK::draw() const
95 {
96 }
97
98 void MeshVK::draw_normals(float scale) const
99 {
100 }