| 1 |
/* |
| 2 |
* Copyright (C) 2019 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "Wireframe.h" |
| 18 |
#include "FFilamentAsset.h" |
| 19 |
|
| 20 |
#include <filament/Box.h> |
| 21 |
#include <filament/Engine.h> |
| 22 |
#include <filament/MaterialEnums.h> |
| 23 |
#include <filament/RenderableManager.h> |
| 24 |
#include <filament/TransformManager.h> |
| 25 |
|
| 26 |
#include <utils/EntityManager.h> |
| 27 |
|
| 28 |
#include <math/vec3.h> |
| 29 |
#include <math/mat4.h> |
| 30 |
|
| 31 |
#include <functional> |
| 32 |
|
| 33 |
using namespace filament; |
| 34 |
using namespace filament::math; |
| 35 |
using namespace std; |
| 36 |
using namespace utils; |
| 37 |
|
| 38 |
static const auto FREE_CALLBACK = [](void* mem, size_t, void*) { free(mem); }; |
| 39 |
|
| 40 |
namespace gltfio { |
| 41 |
|
| 42 |
struct FFilamentAsset; |
| 43 |
|
| 44 |
Wireframe::Wireframe(FFilamentAsset* asset) : mAsset(asset) { |
| 45 |
Engine* engine = mAsset->mEngine; |
| 46 |
TransformManager& tm = engine->getTransformManager(); |
| 47 |
RenderableManager& rm = engine->getRenderableManager(); |
| 48 |
|
| 49 |
// Traverse the transform hierachy and count renderables. |
| 50 |
function<uint32_t(utils::Entity)> count; |
| 51 |
count = [&count, &tm, &rm](Entity node) { |
| 52 |
uint32_t n = rm.getInstance(node) ? 1 : 0; |
| 53 |
auto transformable = tm.getInstance(node); |
| 54 |
vector<Entity> children(tm.getChildCount(transformable)); |
| 55 |
tm.getChildren(transformable, children.data(), children.size()); |
| 56 |
for (auto ce : children) { |
| 57 |
n += count(ce); |
| 58 |
} |
| 59 |
return n; |
| 60 |
}; |
| 61 |
size_t renderableCount = count(mAsset->mRoot); |
| 62 |
size_t vertCount = renderableCount * 8; |
| 63 |
size_t indCount = renderableCount * 24; |
| 64 |
float3* verts = (float3*) malloc(sizeof(float3) * vertCount); |
| 65 |
uint32_t* inds = (uint32_t*) malloc(sizeof(uint32_t) * indCount); |
| 66 |
|
| 67 |
// Traverse the hierarchy a second time to populate the vertex & index buffers. |
| 68 |
function<void(utils::Entity)> create; |
| 69 |
float3* pvert = verts; |
| 70 |
uint32_t* pindx = inds; |
| 71 |
create = [&create, &pvert, &pindx, &rm, &tm, verts](Entity node) { |
| 72 |
auto transformable = tm.getInstance(node); |
| 73 |
auto renderable = rm.getInstance(node); |
| 74 |
if (renderable) { |
| 75 |
Box aabb = rm.getAxisAlignedBoundingBox(renderable); |
| 76 |
float3 minpt = aabb.getMin(); |
| 77 |
float3 maxpt = aabb.getMax(); |
| 78 |
mat4f worldTransform = tm.getWorldTransform(transformable); |
| 79 |
|
| 80 |
// Write coordinates for the 8 corners of the cuboid. |
| 81 |
pvert[0] = (worldTransform * float4(minpt.x, minpt.y, minpt.z, 1.0)).xyz; |
| 82 |
pvert[1] = (worldTransform * float4(minpt.x, minpt.y, maxpt.z, 1.0)).xyz; |
| 83 |
pvert[2] = (worldTransform * float4(minpt.x, maxpt.y, minpt.z, 1.0)).xyz; |
| 84 |
pvert[3] = (worldTransform * float4(minpt.x, maxpt.y, maxpt.z, 1.0)).xyz; |
| 85 |
pvert[4] = (worldTransform * float4(maxpt.x, minpt.y, minpt.z, 1.0)).xyz; |
| 86 |
pvert[5] = (worldTransform * float4(maxpt.x, minpt.y, maxpt.z, 1.0)).xyz; |
| 87 |
pvert[6] = (worldTransform * float4(maxpt.x, maxpt.y, minpt.z, 1.0)).xyz; |
| 88 |
pvert[7] = (worldTransform * float4(maxpt.x, maxpt.y, maxpt.z, 1.0)).xyz; |
| 89 |
|
| 90 |
uint32_t i = pvert - verts; |
| 91 |
pvert += 8; |
| 92 |
|
| 93 |
// Generate 4 lines around face at -X. |
| 94 |
pindx[0] = i + 0; |
| 95 |
pindx[1] = i + 1; |
| 96 |
pindx[2] = i + 1; |
| 97 |
pindx[3] = i + 3; |
| 98 |
pindx[4] = i + 3; |
| 99 |
pindx[5] = i + 2; |
| 100 |
pindx[6] = i + 2; |
| 101 |
pindx[7] = i + 0; |
| 102 |
// Generate 4 lines around face at +X. |
| 103 |
pindx[ 8] = i + 4; |
| 104 |
pindx[ 9] = i + 5; |
| 105 |
pindx[10] = i + 5; |
| 106 |
pindx[11] = i + 7; |
| 107 |
pindx[12] = i + 7; |
| 108 |
pindx[13] = i + 6; |
| 109 |
pindx[14] = i + 6; |
| 110 |
pindx[15] = i + 4; |
| 111 |
// Generate 2 horizontal lines at -Z. |
| 112 |
pindx[16] = i + 0; |
| 113 |
pindx[17] = i + 4; |
| 114 |
pindx[18] = i + 2; |
| 115 |
pindx[19] = i + 6; |
| 116 |
// Generate 2 horizontal lines at +Z. |
| 117 |
pindx[20] = i + 1; |
| 118 |
pindx[21] = i + 5; |
| 119 |
pindx[22] = i + 3; |
| 120 |
pindx[23] = i + 7; |
| 121 |
pindx += 24; |
| 122 |
} |
| 123 |
|
| 124 |
vector<Entity> children(tm.getChildCount(transformable)); |
| 125 |
tm.getChildren(transformable, children.data(), children.size()); |
| 126 |
for (auto ce : children) { |
| 127 |
create(ce); |
| 128 |
} |
| 129 |
}; |
| 130 |
create(mAsset->mRoot); |
| 131 |
|
| 132 |
mVertexBuffer = VertexBuffer::Builder() |
| 133 |
.bufferCount(1) |
| 134 |
.vertexCount(vertCount) |
| 135 |
.attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3) |
| 136 |
.build(*engine); |
| 137 |
|
| 138 |
mIndexBuffer = IndexBuffer::Builder() |
| 139 |
.indexCount(indCount) |
| 140 |
.bufferType(IndexBuffer::IndexType::UINT) |
| 141 |
.build(*engine); |
| 142 |
|
| 143 |
mVertexBuffer->setBufferAt(*engine, 0, VertexBuffer::BufferDescriptor( |
| 144 |
verts, mVertexBuffer->getVertexCount() * sizeof(float3), FREE_CALLBACK)); |
| 145 |
|
| 146 |
mIndexBuffer->setBuffer(*engine, IndexBuffer::BufferDescriptor( |
| 147 |
inds, mIndexBuffer->getIndexCount() * sizeof(uint32_t), FREE_CALLBACK)); |
| 148 |
|
| 149 |
mEntity = EntityManager::get().create(); |
| 150 |
|
| 151 |
RenderableManager::Builder(1) |
| 152 |
.culling(false) |
| 153 |
.castShadows(false) |
| 154 |
.receiveShadows(false) |
| 155 |
.geometry(0, RenderableManager::PrimitiveType::LINES, mVertexBuffer, mIndexBuffer) |
| 156 |
.build(*engine, mEntity); |
| 157 |
} |
| 158 |
|
| 159 |
Wireframe::~Wireframe() { |
| 160 |
Engine* engine = mAsset->mEngine; |
| 161 |
engine->destroy(mEntity); |
| 162 |
engine->destroy(mVertexBuffer); |
| 163 |
engine->destroy(mIndexBuffer); |
| 164 |
} |
| 165 |
|
| 166 |
} // namsepace gltfio |