File: vbotest.cpp

package info (click to toggle)
filament 1.9.25%2Bdfsg3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,404 kB
  • sloc: cpp: 762,244; asm: 77,287; ansic: 31,754; python: 9,629; javascript: 7,293; cs: 4,310; yacc: 3,650; sh: 2,269; java: 1,549; pascal: 419; makefile: 376; xml: 94; ruby: 88; lisp: 9
file content (98 lines) | stat: -rw-r--r-- 3,670 bytes parent folder | download | duplicates (3)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <filament/IndexBuffer.h>
#include <filament/Material.h>
#include <filament/RenderableManager.h>
#include <filament/Scene.h>
#include <filament/VertexBuffer.h>
#include <filament/View.h>

#include <filamentapp/FilamentApp.h>

#include <utils/EntityManager.h>

#include "generated/resources/resources.h"

using namespace filament;

struct App {
    VertexBuffer* vb;
    IndexBuffer* ib;
    Material* mat;
    Camera* cam;
    utils::Entity camera;
    utils::Entity renderable;
};

static constexpr filament::math::float2 POSITIONS[] { {.5, 0}, {-.5, .5}, {-.5, -.5} };
static constexpr uint32_t COLORS[] { 0xffff0000u, 0xff00ff00u, 0xff0000ffu };
static constexpr uint16_t TRIANGLE_INDICES[] { 0, 1, 2 };

int main(int argc, char** argv) {
    Config config;
    config.title = "vbotest";

    // Aggregate positions and colors into a single buffer without interleaving.
    std::vector<uint8_t> vbo(sizeof(POSITIONS) + sizeof(COLORS));
    memcpy(vbo.data(), POSITIONS, sizeof(POSITIONS));
    memcpy(vbo.data() + sizeof(POSITIONS), COLORS, sizeof(COLORS));

    App app;
    auto setup = [&app, &vbo](Engine* engine, View* view, Scene* scene) {

        // Populate vertex buffer.
        app.vb = VertexBuffer::Builder().vertexCount(3).bufferCount(1)
                .attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT2, 0, 8)
                .attribute(VertexAttribute::COLOR, 0, VertexBuffer::AttributeType::UBYTE4, 24, 4)
                .normalized(VertexAttribute::COLOR).build(*engine);
        app.vb->setBufferAt(*engine, 0, VertexBuffer::BufferDescriptor(vbo.data(), vbo.size(), 0));

        // Populate index buffer.
        app.ib = IndexBuffer::Builder().indexCount(3).bufferType(IndexBuffer::IndexType::USHORT)
                .build(*engine);
        app.ib->setBuffer(*engine, IndexBuffer::BufferDescriptor(TRIANGLE_INDICES, 6, 0));

        // Construct material.
        app.mat = Material::Builder()
                .package(RESOURCES_BAKEDCOLOR_DATA, RESOURCES_BAKEDCOLOR_SIZE).build(*engine);

        // Construct renderable.
        RenderableManager::Builder(1)
                .boundingBox({{ -1, -1, -1 }, { 1, 1, 1 }})
                .material(0, app.mat->getDefaultInstance())
                .geometry(0, RenderableManager::PrimitiveType::TRIANGLES, app.vb, app.ib, 0, 3)
                .build(*engine, app.renderable = utils::EntityManager::get().create());
        scene->addEntity(app.renderable);

        // Replace the FilamentApp camera with identity.
        app.camera = utils::EntityManager::get().create();
        view->setCamera(app.cam = engine->createCamera(app.camera));
    };

    auto cleanup = [&app](Engine* engine, View*, Scene*) {
        engine->destroy(app.renderable);
        engine->destroy(app.mat);
        engine->destroy(app.vb);
        engine->destroy(app.ib);
        engine->destroyCameraComponent(app.camera);
        utils::EntityManager::get().destroy(app.camera);
    };

    FilamentApp::get().run(config, setup, cleanup);

    return 0;
}