| 1 | /* |
| 2 | * Copyright (C) 2015 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 <filamentapp/Cube.h> |
| 18 | |
| 19 | #include <utils/EntityManager.h> |
| 20 | #include <filament/VertexBuffer.h> |
| 21 | #include <filament/IndexBuffer.h> |
| 22 | #include <filament/RenderableManager.h> |
| 23 | #include <filament/TransformManager.h> |
| 24 | |
| 25 | using namespace filament::math; |
| 26 | using namespace filament; |
| 27 | |
| 28 | const uint32_t Cube::mIndices[] = { |
| 29 | // solid |
| 30 | 2,0,1, 2,1,3, // far |
| 31 | 6,4,5, 6,5,7, // near |
| 32 | 2,0,4, 2,4,6, // left |
| 33 | 3,1,5, 3,5,7, // right |
| 34 | 0,4,5, 0,5,1, // bottom |
| 35 | 2,6,7, 2,7,3, // top |
| 36 | |
| 37 | // wire-frame |
| 38 | 0,1, 1,3, 3,2, 2,0, // far |
| 39 | 4,5, 5,7, 7,6, 6,4, // near |
| 40 | 0,4, 1,5, 3,7, 2,6, |
| 41 | }; |
| 42 | |
| 43 | const filament::math::float3 Cube::mVertices[] = { |
| 44 | { -1, -1, 1}, // 0. left bottom far |
| 45 | { 1, -1, 1}, // 1. right bottom far |
| 46 | { -1, 1, 1}, // 2. left top far |
| 47 | { 1, 1, 1}, // 3. right top far |
| 48 | { -1, -1, -1}, // 4. left bottom near |
| 49 | { 1, -1, -1}, // 5. right bottom near |
| 50 | { -1, 1, -1}, // 6. left top near |
| 51 | { 1, 1, -1}}; // 7. right top near |
| 52 | |
| 53 | |
| 54 | Cube::Cube(Engine& engine, filament::Material const* material, float3 linearColor, bool culling) : |
| 55 | mEngine(engine), |
| 56 | mMaterial(material) { |
| 57 | |
| 58 | mVertexBuffer = VertexBuffer::Builder() |
| 59 | .vertexCount(8) |
| 60 | .bufferCount(1) |
| 61 | .attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3) |
| 62 | .build(engine); |
| 63 | |
| 64 | mIndexBuffer = IndexBuffer::Builder() |
| 65 | .indexCount(12*2 + 3*2*6) |
| 66 | .build(engine); |
| 67 | |
| 68 | if (mMaterial) { |
| 69 | mMaterialInstanceSolid = mMaterial->createInstance(); |
| 70 | mMaterialInstanceWireFrame = mMaterial->createInstance(); |
| 71 | mMaterialInstanceSolid->setParameter("color", RgbaType::LINEAR, |
| 72 | LinearColorA{linearColor.r, linearColor.g, linearColor.b, 0.05f}); |
| 73 | mMaterialInstanceWireFrame->setParameter("color", RgbaType::LINEAR, |
| 74 | LinearColorA{linearColor.r, linearColor.g, linearColor.b, 0.25f}); |
| 75 | } |
| 76 | |
| 77 | mVertexBuffer->setBufferAt(engine, 0, |
| 78 | VertexBuffer::BufferDescriptor( |
| 79 | mVertices, mVertexBuffer->getVertexCount() * sizeof(mVertices[0]))); |
| 80 | |
| 81 | mIndexBuffer->setBuffer(engine, |
| 82 | IndexBuffer::BufferDescriptor( |
| 83 | mIndices, mIndexBuffer->getIndexCount() * sizeof(uint32_t))); |
| 84 | |
| 85 | utils::EntityManager& em = utils::EntityManager::get(); |
| 86 | mSolidRenderable = em.create(); |
| 87 | RenderableManager::Builder(1) |
| 88 | .boundingBox({{ 0, 0, 0 }, |
| 89 | { 1, 1, 1 }}) |
| 90 | .material(0, mMaterialInstanceSolid) |
| 91 | .geometry(0, RenderableManager::PrimitiveType::TRIANGLES, mVertexBuffer, mIndexBuffer, 0, 3*2*6) |
| 92 | .priority(7) |
| 93 | .culling(culling) |
| 94 | .build(engine, mSolidRenderable); |
| 95 | |
| 96 | mWireFrameRenderable = em.create(); |
| 97 | RenderableManager::Builder(1) |
| 98 | .boundingBox({{ 0, 0, 0 }, |
| 99 | { 1, 1, 1 }}) |
| 100 | .material(0, mMaterialInstanceWireFrame) |
| 101 | .geometry(0, RenderableManager::PrimitiveType::LINES, mVertexBuffer, mIndexBuffer, WIREFRAME_OFFSET, 24) |
| 102 | .priority(6) |
| 103 | .culling(culling) |
| 104 | .build(engine, mWireFrameRenderable); |
| 105 | } |
| 106 | |
| 107 | void Cube::mapFrustum(filament::Engine& engine, Camera const* camera) { |
| 108 | // the Camera far plane is at infinity, but we want it closer for display |
| 109 | const mat4 vm(camera->getModelMatrix()); |
| 110 | mat4 p(vm * inverse(camera->getCullingProjectionMatrix())); |
| 111 | return mapFrustum(engine, p); |
| 112 | } |
| 113 | |
| 114 | void Cube::mapFrustum(filament::Engine& engine, filament::math::mat4 const& transform) { |
| 115 | // the Camera far plane is at infinity, but we want it closer for display |
| 116 | mat4f p(transform); |
| 117 | auto& tcm = engine.getTransformManager(); |
| 118 | tcm.setTransform(tcm.getInstance(mSolidRenderable), p); |
| 119 | tcm.setTransform(tcm.getInstance(mWireFrameRenderable), p); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void Cube::mapAabb(filament::Engine& engine, filament::Box const& box) { |
| 124 | mat4 p = mat4::translation(box.center) * mat4::scaling(box.halfExtent); |
| 125 | return mapFrustum(engine, p); |
| 126 | } |
| 127 | |
| 128 | Cube::~Cube() { |
| 129 | mEngine.destroy(mVertexBuffer); |
| 130 | mEngine.destroy(mIndexBuffer); |
| 131 | mEngine.destroy(mMaterialInstanceSolid); |
| 132 | mEngine.destroy(mMaterialInstanceWireFrame); |
| 133 | // We don't own the material, only instances |
| 134 | mEngine.destroy(mSolidRenderable); |
| 135 | mEngine.destroy(mWireFrameRenderable); |
| 136 | |
| 137 | utils::EntityManager& em = utils::EntityManager::get(); |
| 138 | em.destroy(mSolidRenderable); |
| 139 | em.destroy(mWireFrameRenderable); |
| 140 | } |