1 #include <vulkan/vulkan.h>
9 MeshVK::MeshVK(const MeshVK &mesh)
11 indices = mesh.indices;
12 vertices = mesh.vertices;
13 normals = mesh.normals;
14 tex_coords = mesh.tex_coords;
17 MeshVK &MeshVK::operator=(const MeshVK &mesh)
22 /* what the copy constructor does */
23 indices = mesh.indices;
24 vertices = mesh.vertices;
25 normals = mesh.normals;
26 tex_coords = mesh.tex_coords;
33 vku_destroy_buffer(vk_vertices);
34 vku_destroy_buffer(vk_normals);
35 vku_destroy_buffer(vk_tex_coords);
36 vku_destroy_buffer(vk_indices);
44 bool MeshVK::update_vertex_data()
46 if(vertices.empty()) {
47 printf("empty vertices!\n");
51 /* create the buffers */
53 int vsz = vertices.size() * sizeof(Vec3);
54 if(!(vk_vertices = vku_create_buffer(vsz,
55 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
56 fprintf(stderr, "Failed to create the buffer for the vertices.\n");
60 int nsz = normals.size() * sizeof(Vec3);
61 if(!(vk_normals = vku_create_buffer(nsz,
62 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
63 vku_destroy_buffer(vk_vertices);
65 fprintf(stderr, "Failed to create the buffer for the normals.\n");
69 int tsz = tex_coords.size() * sizeof(Vec2);
70 if(!(vk_tex_coords = vku_create_buffer(tsz,
71 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
72 vku_destroy_buffer(vk_vertices);
73 vku_destroy_buffer(vk_normals);
76 "Failed to create the buffer for the texture coordinates.\n");
80 int isz = indices.size() * 2;
81 if(!(vk_indices = vku_create_buffer(isz,
82 VK_BUFFER_USAGE_INDEX_BUFFER_BIT))) {
83 vku_destroy_buffer(vk_vertices);
84 vku_destroy_buffer(vk_normals);
85 vku_destroy_buffer(vk_tex_coords);
87 fprintf(stderr, "Failed to create the indices buffer.\n");
91 /* write the buffers */
93 if(!vku_write_memory(vk_vertices->mem_pool, vsz, (void*)vertices.data())) {
94 fprintf(stderr, "Failed to write the vertices on GPU.\n");
97 if(!vku_write_memory(vk_normals->mem_pool, nsz, (void*)normals.data())) {
98 fprintf(stderr, "Failed to write the normalson GPU.\n");
101 if(!vku_write_memory(vk_tex_coords->mem_pool, tsz,
102 (void*)tex_coords.data())) {
103 fprintf(stderr, "Failed to write the texture coordinates on GPU.\n");
106 if(!vku_write_memory(vk_indices->mem_pool, isz, (void*)indices.data())) {
107 fprintf(stderr, "Failed to write the indices on GPU.\n");
114 void MeshVK::draw() const
118 void MeshVK::draw_normals(float scale) const