# Getting started

## Setup

### C / C++

<span style="white-space: pre-wrap;"> </span>**ufbx**<span style="white-space: pre-wrap;">はシングルソースライブラリなので、必要なのは2つのファイルだけ（ </span>`<span class="editor-theme-code">ufbx.c</span>`<span style="white-space: pre-wrap;"> </span><span style="color: rgb(36, 41, 47); background-color: rgb(252, 252, 252);">and</span><span style="white-space: pre-wrap;"> </span>`<span class="editor-theme-code">ufbx.h</span>`）です。最も簡単な方法は、[https://github.com/bqqbarbhg/ufbx](https://github.com/bqqbarbhg/ufbx)からダウンロードすることです。マスターブランチには、ライブラリの最新の安定バージョンが含まれています。  
単一の**ヘッダー**ライブラリとは異なり、残りのコードと一緒にコンパイルする必要があります。あるいは、#include "ufbx.c"で単一のファイルでコンパイルすることも可能です。  
*****ufbx**** *は**libc 外部に依存しませんが、C 標準数学ライブラリ`<span class="editor-theme-code">-</span>``<span class="editor-theme-code">lm</span>`をリンクするために渡す必要がある場合があります。

```
gcc -lm ufbx.c main.c -o main
clang -lm ufbx.c main.c -o main
```

## シーンの読み込み

<span style="white-space: pre-wrap;"> </span>**ufbx**を使い始めるには、まずシーンをロードする必要があります。シーンをロードした後は、返された[`<span class="editor-theme-code">ufbx_scene</span>`](https://ufbx.github.io/reference#ufbx_scene)構造体を調べるだけで、かなり先の処理を行うことができます。ファイルからシーンをロードし、シーン階層内のオブジェクトを表すすべてのノードの名前を出力してみましょう。

```c++
#include <stdio.h>
#include "ufbx.h"

int main()
{
    ufbx_scene *scene = ufbx_load_file("my_scene.fbx", nullptr, nullptr);

    for (ufbx_node *node : scene->nodes) {
        printf("%s\n", node->name.data);
    }

    ufbx_free_scene(scene);
    return 0;
}
```

上記の例では、シーンをデフォルトオプションでロードし、エラー処理に関してはかなり厳格に行いました。これを修正するには、ロード中に発生したエラーに関する情報を取得するために[`<span class="editor-theme-code">ufbx_error</span>`](https://ufbx.github.io/reference#ufbx_error)を使用します。  
オプションを渡すこともできます[`<span class="editor-theme-code">ufbx_load_opts</span>`](https://ufbx.github.io/reference#ufbx_load_opts)。FBXシーンには様々な座標系と単位系があり、**ufbxは**読み込み時にそれらを正規化することをサポートしています。ここでは、1メートル単位の右手Y軸上座標系を要求しています。

```c++
#include <stdio.h>
#include "ufbx.h"

int main()
{
    ufbx_load_opts opts = { };
    opts.target_axes = ufbx_axes_right_handed_y_up;
    opts.target_unit_meters = 1.0f;

    ufbx_error error;
    ufbx_scene *scene = ufbx_load_file("my_scene.fbx", &opts, &error);
    if (!scene) {
        fprintf(stderr, "Failed to load scene: %s\n", error.description.data);
        return 1;
    }

    for (ufbx_node *node : scene->nodes) {
        printf("%s\n", node->name.data);
    }

    ufbx_free_scene(scene);
    return 0;
}
```

必要に応じて、 を使用してメモリからシーンをロードし[`<span class="editor-theme-code">ufbx_load_memory</span>`](https://ufbx.github.io/reference#ufbx_load_memory)`<span class="editor-theme-code">()</span>`たり、 を使用してカスタム ストリームをロードすることもできます[`<span class="editor-theme-code">ufbx_load_stream</span>`](https://ufbx.github.io/reference#ufbx_load_stream)`<span class="editor-theme-code">()</span>`。

## データ型

<span style="white-space: pre-wrap;"> </span>**ufbx は**シーンをデータとして表現することを目的としているため、返されたデータを調べるだけで多くのことを行うことができます[`<span class="editor-theme-code">ufbx_scene</span>`](https://ufbx.github.io/reference#ufbx_scene)。  
多くのCライブラリとは異なり、**ufbxは**<span style="white-space: pre-wrap;">長さ情報のない生のポインタを公開しないため、C以外の言語でも境界チェックが可能です。ufbx内の可変長データはすべて、 </span>**以下**の型で表現されます。

```c++
// UTF-8 encoded string of `length` bytes, always NULL terminated
struct ufbx_string {
    const char *data;
    size_t length;
};

// Arbitrary binary data of `size` bytes
struct ufbx_blob {
    const char *data;
    size_t size;
};

// List of `count` objects of type T
struct ufbx_T_list {
    T *data;
    size_t count;

    // Bounds-checked indexing and iterator support in C++
    T &operator[](size_t index) const;
    T *begin() const;
    T *end() const;
};
```

[`<span class="editor-theme-code">ufbx_string</span>`](https://ufbx.github.io/reference#ufbx_string)、[`<span class="editor-theme-code">ufbx_blob</span>`](https://ufbx.github.io/reference#ufbx_blob)<span style="white-space: pre-wrap;">、または </span>`<span class="editor-theme-code">ufbx_T_list</span>`<span style="white-space: pre-wrap;"> に含まれていないすべてのポインタは、</span>  
****1つのオブジェクトを指す****<span style="white-space: pre-wrap;"> か、あるいは </span>[`<span class="editor-theme-code">ufbx_nullable</span>`](https://ufbx.github.io/reference#ufbx_nullable)<span style="white-space: pre-wrap;">が指定されている場合には </span>****NULL****<span style="white-space: pre-wrap;"> である可能性があります。</span>  
逆に言えば、[`<span class="editor-theme-code">ufbx_nullable</span>`](https://ufbx.github.io/reference#ufbx_nullable)が****付いていないポインタ****<span style="white-space: pre-wrap;">は、必ず </span>****1つの有効なオブジェクト****<span style="white-space: pre-wrap;"> を指すことが保証されています。</span>

---

デバッグ時にこれらの構造体を視覚的に確認しやすくするために、[`<span class="editor-theme-code">ufbx.natvis</span>`](https://github.com/bqqbarbhg/ufbx/blob/master/misc/ufbx.natvis)<span style="white-space: pre-wrap;"> をダウンロードして利用できます。</span>  
<span style="white-space: pre-wrap;">これは少なくとも </span>****MSVC****<span style="white-space: pre-wrap;">（Visual Studio）および </span>****VS Code****<span style="white-space: pre-wrap;"> でサポートされています。</span>