| 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 18 | #include <filamentapp/Sphere.h> |
| 19 | |
| 20 | #include <filament/Engine.h> |
| 21 | #include <filament/IndexBuffer.h> |
| 22 | #include <filament/Material.h> |
| 23 | #include <filament/RenderableManager.h> |
| 24 | #include <filament/TransformManager.h> |
| 25 | #include <filament/VertexBuffer.h> |
| 26 | #include <utils/EntityManager.h> |
| 27 | #include <math/norm.h> |
| 28 | |
| 29 | #include <geometry/SurfaceOrientation.h> |
| 30 | |
| 31 | #include <filamentapp/IcoSphere.h> |
| 32 | |
| 33 | using namespace filament; |
| 34 | using namespace filament::math; |
| 35 | using namespace utils; |
| 36 | |
| 37 | |
| 38 | struct Geometry { |
| 39 | IcoSphere sphere = IcoSphere{ 2 }; |
| 40 | std::vector<filament::math::short4> tangents; |
| 41 | filament::VertexBuffer* vertexBuffer = nullptr; |
| 42 | filament::IndexBuffer* indexBuffer = nullptr; |
| 43 | }; |
| 44 | |
| 45 | // note: this will be leaked since we don't have a good time to free it. |
| 46 | // this should be a cache indexed on the sphere's subdivisions |
| 47 | static Geometry* gGeometry = nullptr; |
| 48 | |
| 49 | Sphere::Sphere(Engine& engine, Material const* material, bool culling) |
| 50 | : mEngine(engine) { |
| 51 | Geometry* geometry = gGeometry; |
| 52 | |
| 53 | static_assert(sizeof(IcoSphere::Triangle) == sizeof(IcoSphere::Index) * 3, |
| 54 | "indices are not packed"); |
| 55 | |
| 56 | if (geometry == nullptr) { |
| 57 | geometry = gGeometry = new Geometry; |
| 58 | |
| 59 | auto const& indices = geometry->sphere.getIndices(); |
| 60 | auto const& vertices = geometry->sphere.getVertices(); |
| 61 | uint32_t indexCount = (uint32_t)(indices.size() * 3); |
| 62 | |
| 63 | geometry->tangents.resize(vertices.size()); |
| 64 | auto* quats = geometry::SurfaceOrientation::Builder() |
| 65 | .vertexCount(vertices.size()) |
| 66 | .normals(vertices.data(), sizeof(float3)) |
| 67 | .build(); |
| 68 | quats->getQuats((short4*) geometry->tangents.data(), vertices.size(), |
| 69 | sizeof(filament::math::short4)); |
| 70 | delete quats; |
| 71 | |
| 72 | // todo produce correct u,v |
| 73 | |
| 74 | geometry->vertexBuffer = VertexBuffer::Builder() |
| 75 | .vertexCount((uint32_t)vertices.size()) |
| 76 | .bufferCount(2) |
| 77 | .attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3) |
| 78 | .attribute(VertexAttribute::TANGENTS, 1, VertexBuffer::AttributeType::SHORT4) |
| 79 | .normalized(VertexAttribute::TANGENTS) |
| 80 | .build(engine); |
| 81 | |
| 82 | geometry->vertexBuffer->setBufferAt(engine, 0, |
| 83 | VertexBuffer::BufferDescriptor(vertices.data(), vertices.size() * sizeof(float3))); |
| 84 | |
| 85 | geometry->vertexBuffer->setBufferAt(engine, 1, |
| 86 | VertexBuffer::BufferDescriptor(geometry->tangents.data(), geometry->tangents.size() * sizeof(filament::math::short4))); |
| 87 | |
| 88 | |
| 89 | geometry->indexBuffer = IndexBuffer::Builder() |
| 90 | .bufferType(IndexBuffer::IndexType::USHORT) |
| 91 | .indexCount(indexCount) |
| 92 | .build(engine); |
| 93 | |
| 94 | geometry->indexBuffer->setBuffer(engine, |
| 95 | IndexBuffer::BufferDescriptor(indices.data(), indexCount * sizeof(IcoSphere::Index))); |
| 96 | } |
| 97 | |
| 98 | if (material) { |
| 99 | mMaterialInstance = material->createInstance(); |
| 100 | } |
| 101 | |
| 102 | utils::EntityManager& em = utils::EntityManager::get(); |
| 103 | mRenderable = em.create(); |
| 104 | RenderableManager::Builder(1) |
| 105 | .boundingBox({{0}, {1}}) |
| 106 | .material(0, mMaterialInstance) |
| 107 | .geometry(0, RenderableManager::PrimitiveType::TRIANGLES, geometry->vertexBuffer, geometry->indexBuffer) |
| 108 | .culling(culling) |
| 109 | .build(engine, mRenderable); |
| 110 | } |
| 111 | |
| 112 | Sphere::~Sphere() { |
| 113 | mEngine.destroy(mMaterialInstance); |
| 114 | mEngine.destroy(mRenderable); |
| 115 | utils::EntityManager& em = utils::EntityManager::get(); |
| 116 | em.destroy(mRenderable); |
| 117 | } |
| 118 | |
| 119 | Sphere& Sphere::setPosition(filament::math::float3 const& position) noexcept { |
| 120 | auto& tcm = mEngine.getTransformManager(); |
| 121 | auto ci = tcm.getInstance(mRenderable); |
| 122 | mat4f model = tcm.getTransform(ci); |
| 123 | model[3].xyz = position; |
| 124 | tcm.setTransform(ci, model); |
| 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | Sphere& Sphere::setRadius(float radius) noexcept { |
| 129 | auto& tcm = mEngine.getTransformManager(); |
| 130 | auto ci = tcm.getInstance(mRenderable); |
| 131 | mat4f model = tcm.getTransform(ci); |
| 132 | model[0].x = radius; |
| 133 | model[1].y = radius; |
| 134 | model[2].z = radius; |
| 135 | tcm.setTransform(ci, model); |
| 136 | return *this; |
| 137 | } |