49 lines
1.6 KiB
C++
49 lines
1.6 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "SimpleSpinningCube.hpp"
|
|
#include "component/display/MeshRenderer.hpp"
|
|
#include "component/display/material/SimpleTexturedMaterial.hpp"
|
|
#include "display/mesh/CubeMesh.hpp"
|
|
#include "component/SimpleComponent.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
std::shared_ptr<SceneItem> Dawn::createSimpleSpinningCube(Scene &s) {
|
|
// Create the scene item.
|
|
auto cubeItem = s.createSceneItem();
|
|
|
|
// Create a simple cube mesh.
|
|
auto cubeMesh = std::make_shared<Mesh>();
|
|
cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
|
|
CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0);
|
|
|
|
// Add a renderer to the scene item.
|
|
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
|
|
cubeMeshRenderer->mesh = cubeMesh;
|
|
|
|
// Add a material to the scene item.
|
|
auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
|
|
cubeMaterial->setColor(COLOR_MAGENTA);
|
|
|
|
// Add a simple event listener component to the scene item.
|
|
addSimpleComponent(cubeItem, [](auto &cmp, auto &events) {
|
|
// Note that add component cannot receive a self reference, so we must
|
|
// capture the component by reference instead.
|
|
events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&](
|
|
float_t delta
|
|
) {
|
|
// Since we captured within the lambda, we can access the component
|
|
// directly.
|
|
auto item = cmp.getItem();
|
|
item->setLocalRotation(
|
|
item->getLocalRotation() *
|
|
glm::quat(glm::vec3(1, 1, 0) * delta)
|
|
);
|
|
}));
|
|
});
|
|
|
|
return cubeItem;
|
|
} |