diff options
Diffstat (limited to 'src/asset')
| -rw-r--r-- | src/asset/asset_cache.c | 4 | ||||
| -rw-r--r-- | src/asset/model.c | 157 |
2 files changed, 46 insertions, 115 deletions
diff --git a/src/asset/asset_cache.c b/src/asset/asset_cache.c index 191a5fd..dfaf7c6 100644 --- a/src/asset/asset_cache.c +++ b/src/asset/asset_cache.c | |||
| @@ -1,15 +1,15 @@ | |||
| 1 | #include "asset_cache.h" | 1 | #include "asset_cache.h" |
| 2 | 2 | ||
| 3 | #include "animation_impl.h" | ||
| 3 | #include "memory.h" | 4 | #include "memory.h" |
| 4 | #include "model.h" | 5 | #include "model.h" |
| 5 | #include "scene/animation_impl.h" | ||
| 6 | #include "scene/model_impl.h" | 6 | #include "scene/model_impl.h" |
| 7 | #include "scene/node_impl.h" | 7 | #include "scene/node_impl.h" |
| 8 | #include "texture.h" | 8 | #include "texture.h" |
| 9 | 9 | ||
| 10 | #include <gfx/asset.h> | 10 | #include <gfx/asset.h> |
| 11 | #include <gfx/gfx.h> | 11 | #include <gfx/gfx.h> |
| 12 | #include <gfx/scene/node.h> | 12 | #include <gfx/scene.h> |
| 13 | #include <gfx_assert.h> | 13 | #include <gfx_assert.h> |
| 14 | 14 | ||
| 15 | #include <cstring.h> | 15 | #include <cstring.h> |
diff --git a/src/asset/model.c b/src/asset/model.c index df116fb..2ee3cd1 100644 --- a/src/asset/model.c +++ b/src/asset/model.c | |||
| @@ -85,13 +85,11 @@ | |||
| 85 | #include "gfx_assert.h" | 85 | #include "gfx_assert.h" |
| 86 | #include "scene/model_impl.h" | 86 | #include "scene/model_impl.h" |
| 87 | 87 | ||
| 88 | #include <gfx/animation.h> | ||
| 88 | #include <gfx/core.h> | 89 | #include <gfx/core.h> |
| 89 | #include <gfx/gfx.h> | 90 | #include <gfx/gfx.h> |
| 90 | #include <gfx/render/llr.h> | 91 | #include <gfx/render/llr.h> |
| 91 | #include <gfx/scene/animation.h> | 92 | #include <gfx/scene.h> |
| 92 | #include <gfx/scene/camera.h> | ||
| 93 | #include <gfx/scene/node.h> | ||
| 94 | #include <gfx/scene/object.h> | ||
| 95 | #include <gfx/sizes.h> | 93 | #include <gfx/sizes.h> |
| 96 | #include <gfx/util/shader.h> | 94 | #include <gfx/util/shader.h> |
| 97 | 95 | ||
| @@ -1580,7 +1578,6 @@ static void load_nodes( | |||
| 1580 | 1578 | ||
| 1581 | // Add SceneObject, Camera or Lights. | 1579 | // Add SceneObject, Camera or Lights. |
| 1582 | // TODO: Handle lights once they are implemented in the gfx library. | 1580 | // TODO: Handle lights once they are implemented in the gfx library. |
| 1583 | assert(!nodes[n]); | ||
| 1584 | if (node->mesh) { | 1581 | if (node->mesh) { |
| 1585 | const cgltf_size mesh_index = node->mesh - data->meshes; | 1582 | const cgltf_size mesh_index = node->mesh - data->meshes; |
| 1586 | assert(mesh_index < data->meshes_count); | 1583 | assert(mesh_index < data->meshes_count); |
| @@ -1715,39 +1712,21 @@ static Model* load_scene( | |||
| 1715 | LOGD("Filepath: %s", mstring_cstr(filepath)); | 1712 | LOGD("Filepath: %s", mstring_cstr(filepath)); |
| 1716 | LOGD("Directory: %s", mstring_cstr(&directory)); | 1713 | LOGD("Directory: %s", mstring_cstr(&directory)); |
| 1717 | 1714 | ||
| 1718 | Buffer** tangent_buffers = 0; | ||
| 1719 | Buffer** buffers = 0; | ||
| 1720 | LoadTextureCmd* load_texture_cmds = 0; | ||
| 1721 | const Texture** textures = 0; // Textures are owned by asset cache. | ||
| 1722 | Material** materials = 0; | ||
| 1723 | Geometry** geometries = 0; | ||
| 1724 | Mesh** meshes = 0; | ||
| 1725 | AnimaDesc* anima_desc = 0; | ||
| 1726 | SceneObject** scene_objects = 0; | ||
| 1727 | SceneNode** scene_nodes = 0; | ||
| 1728 | Anima* anima = 0; | ||
| 1729 | SceneNode* root_node = 0; | ||
| 1730 | Model* model = 0; | ||
| 1731 | |||
| 1732 | tangent_buffers = calloc(num_tangent_buffers, sizeof(Buffer*)); | ||
| 1733 | buffers = calloc(data->buffers_count, sizeof(Buffer*)); | ||
| 1734 | textures = calloc(data->textures_count, sizeof(Texture*)); | ||
| 1735 | materials = calloc(data->materials_count, sizeof(Material*)); | ||
| 1736 | geometries = calloc(primitive_count, sizeof(Geometry*)); | ||
| 1737 | meshes = calloc(primitive_count, sizeof(Mesh*)); | ||
| 1738 | scene_objects = calloc(data->meshes_count, sizeof(SceneObject*)); | ||
| 1739 | scene_nodes = calloc(data->nodes_count, sizeof(SceneNode*)); | ||
| 1740 | // A glTF scene does not necessarily have textures. Materials can be given | 1715 | // A glTF scene does not necessarily have textures. Materials can be given |
| 1741 | // as constants, for example. | 1716 | // as constants, for example. |
| 1742 | if (data->textures_count > 0) { | 1717 | // gfx textures are owned by asset cache. |
| 1743 | load_texture_cmds = calloc(data->textures_count, sizeof(LoadTextureCmd)); | 1718 | Buffer* tangent_buffers[num_tangent_buffers]; |
| 1744 | } | 1719 | Buffer* buffers[data->buffers_count]; |
| 1745 | 1720 | LoadTextureCmd load_texture_cmds[data->textures_count]; | |
| 1746 | if (!buffers || !tangent_buffers || | 1721 | const Texture* textures[data->textures_count]; |
| 1747 | ((data->textures_count > 0) && !load_texture_cmds) || !textures || | 1722 | Material* materials[data->materials_count]; |
| 1748 | !materials || !geometries || !meshes || !scene_objects || !scene_nodes) { | 1723 | Geometry* geometries[primitive_count]; |
| 1749 | goto cleanup; | 1724 | Mesh* meshes[primitive_count]; |
| 1750 | } | 1725 | SceneObject* scene_objects[data->meshes_count]; |
| 1726 | SceneNode* scene_nodes[data->nodes_count]; | ||
| 1727 | Anima* anima = 0; | ||
| 1728 | SceneNode* root_node = 0; | ||
| 1729 | Model* model = 0; | ||
| 1751 | 1730 | ||
| 1752 | if ((num_tangent_buffers > 0) && | 1731 | if ((num_tangent_buffers > 0) && |
| 1753 | !load_tangent_buffers( | 1732 | !load_tangent_buffers( |
| @@ -1780,22 +1759,18 @@ static Model* load_scene( | |||
| 1780 | // This is an anima node if the scene has skins; otherwise it is a logical | 1759 | // This is an anima node if the scene has skins; otherwise it is a logical |
| 1781 | // node. | 1760 | // node. |
| 1782 | if (data->skins_count > 0) { | 1761 | if (data->skins_count > 0) { |
| 1783 | anima_desc = calloc(1, sizeof(AnimaDesc)); | ||
| 1784 | if (!anima_desc) { | ||
| 1785 | goto cleanup; | ||
| 1786 | } | ||
| 1787 | |||
| 1788 | const cgltf_size base = find_base_joint_index(data); | 1762 | const cgltf_size base = find_base_joint_index(data); |
| 1789 | 1763 | ||
| 1790 | anima_desc->num_skeletons = data->skins_count; | 1764 | AnimaDesc anima_desc = |
| 1791 | anima_desc->num_animations = data->animations_count; | 1765 | (AnimaDesc){.num_skeletons = data->skins_count, |
| 1792 | anima_desc->num_joints = load_skins(data, buffers, base, anima_desc); | 1766 | .num_animations = data->animations_count, |
| 1793 | load_animations(data, base, anima_desc); | 1767 | .num_joints = load_skins(data, buffers, base, &anima_desc)}; |
| 1768 | load_animations(data, base, &anima_desc); | ||
| 1794 | 1769 | ||
| 1795 | compute_joint_bounding_boxes( | 1770 | compute_joint_bounding_boxes( |
| 1796 | data, anima_desc->num_joints, anima_desc->joints); | 1771 | data, anima_desc.num_joints, anima_desc.joints); |
| 1797 | 1772 | ||
| 1798 | anima = gfx_make_anima(anima_desc); | 1773 | anima = gfx_make_anima(&anima_desc); |
| 1799 | root_node = gfx_make_anima_node(anima); | 1774 | root_node = gfx_make_anima_node(anima); |
| 1800 | } else { | 1775 | } else { |
| 1801 | root_node = gfx_make_node(); | 1776 | root_node = gfx_make_node(); |
| @@ -1812,86 +1787,42 @@ static Model* load_scene( | |||
| 1812 | cleanup: | 1787 | cleanup: |
| 1813 | // The arrays of resources are no longer needed. The resources themselves are | 1788 | // The arrays of resources are no longer needed. The resources themselves are |
| 1814 | // destroyed only if this function fails. | 1789 | // destroyed only if this function fails. |
| 1815 | if (tangent_buffers) { | 1790 | if (!success) { |
| 1816 | if (!success) { | 1791 | for (cgltf_size i = 0; i < num_tangent_buffers; ++i) { |
| 1817 | for (cgltf_size i = 0; i < num_tangent_buffers; ++i) { | 1792 | if (tangent_buffers[i]) { |
| 1818 | if (tangent_buffers[i]) { | 1793 | gfx_destroy_buffer(gfxcore, &tangent_buffers[i]); |
| 1819 | gfx_destroy_buffer(gfxcore, &tangent_buffers[i]); | ||
| 1820 | } | ||
| 1821 | } | 1794 | } |
| 1822 | } | 1795 | } |
| 1823 | free(tangent_buffers); | 1796 | for (cgltf_size i = 0; i < data->buffers_count; ++i) { |
| 1824 | } | 1797 | if (buffers[i]) { |
| 1825 | if (buffers) { | 1798 | gfx_destroy_buffer(gfxcore, &buffers[i]); |
| 1826 | if (!success) { | ||
| 1827 | for (cgltf_size i = 0; i < data->buffers_count; ++i) { | ||
| 1828 | if (buffers[i]) { | ||
| 1829 | gfx_destroy_buffer(gfxcore, &buffers[i]); | ||
| 1830 | } | ||
| 1831 | } | 1799 | } |
| 1832 | } | 1800 | } |
| 1833 | free(buffers); | 1801 | for (cgltf_size i = 0; i < data->materials_count; ++i) { |
| 1834 | } | 1802 | if (materials[i]) { |
| 1835 | if (load_texture_cmds) { | 1803 | gfx_destroy_material(&materials[i]); |
| 1836 | free(load_texture_cmds); | ||
| 1837 | } | ||
| 1838 | if (textures) { | ||
| 1839 | free(textures); | ||
| 1840 | } | ||
| 1841 | if (materials) { | ||
| 1842 | if (!success) { | ||
| 1843 | for (cgltf_size i = 0; i < data->materials_count; ++i) { | ||
| 1844 | if (materials[i]) { | ||
| 1845 | gfx_destroy_material(&materials[i]); | ||
| 1846 | } | ||
| 1847 | } | 1804 | } |
| 1848 | } | 1805 | } |
| 1849 | free(materials); | 1806 | for (size_t i = 0; i < primitive_count; ++i) { |
| 1850 | } | 1807 | if (geometries[i]) { |
| 1851 | if (geometries) { | 1808 | gfx_destroy_geometry(gfxcore, &geometries[i]); |
| 1852 | if (!success) { | ||
| 1853 | for (size_t i = 0; i < primitive_count; ++i) { | ||
| 1854 | if (geometries[i]) { | ||
| 1855 | gfx_destroy_geometry(gfxcore, &geometries[i]); | ||
| 1856 | } | ||
| 1857 | } | 1809 | } |
| 1858 | } | 1810 | } |
| 1859 | free(geometries); | 1811 | for (size_t i = 0; i < primitive_count; ++i) { |
| 1860 | } | 1812 | if (meshes[i]) { |
| 1861 | if (meshes) { | 1813 | gfx_destroy_mesh(&meshes[i]); |
| 1862 | if (!success) { | ||
| 1863 | for (size_t i = 0; i < primitive_count; ++i) { | ||
| 1864 | if (meshes[i]) { | ||
| 1865 | gfx_destroy_mesh(&meshes[i]); | ||
| 1866 | } | ||
| 1867 | } | 1814 | } |
| 1868 | } | 1815 | } |
| 1869 | free(meshes); | 1816 | for (cgltf_size i = 0; i < data->meshes_count; ++i) { |
| 1870 | } | 1817 | if (scene_objects[i]) { |
| 1871 | if (anima_desc) { | 1818 | gfx_destroy_object(&scene_objects[i]); |
| 1872 | free(anima_desc); | ||
| 1873 | } | ||
| 1874 | if (scene_objects) { | ||
| 1875 | if (!success) { | ||
| 1876 | for (cgltf_size i = 0; i < data->meshes_count; ++i) { | ||
| 1877 | if (scene_objects[i]) { | ||
| 1878 | gfx_destroy_object(&scene_objects[i]); | ||
| 1879 | } | ||
| 1880 | } | 1819 | } |
| 1881 | } | 1820 | } |
| 1882 | free(scene_objects); | 1821 | for (cgltf_size i = 0; i < data->nodes_count; ++i) { |
| 1883 | } | 1822 | if (scene_nodes[i]) { |
| 1884 | if (scene_nodes) { | 1823 | gfx_destroy_node(&scene_nodes[i]); |
| 1885 | if (!success) { | ||
| 1886 | for (cgltf_size i = 0; i < data->nodes_count; ++i) { | ||
| 1887 | if (scene_nodes[i]) { | ||
| 1888 | gfx_destroy_node(&scene_nodes[i]); | ||
| 1889 | } | ||
| 1890 | } | 1824 | } |
| 1891 | } | 1825 | } |
| 1892 | free(scene_nodes); | ||
| 1893 | } | ||
| 1894 | if (!success) { | ||
| 1895 | if (root_node) { | 1826 | if (root_node) { |
| 1896 | gfx_destroy_node(&root_node); // Node owns the anima. | 1827 | gfx_destroy_node(&root_node); // Node owns the anima. |
| 1897 | } else if (anima) { | 1828 | } else if (anima) { |
