51 lines
1.6 KiB
C++
51 lines
1.6 KiB
C++
#include <glm/gtc/quaternion.hpp>
|
|
#include <sophia/application.hpp>
|
|
#include <sophia/components.hpp>
|
|
#include <sophia/entity.hpp>
|
|
#include <sophia/primitives.hpp>
|
|
#include <sophia/scene_view.hpp>
|
|
|
|
// A spinning cube seen through a perspective camera.
|
|
class Cube : public sophia::Application {
|
|
public:
|
|
Cube() : Application{{.width = 800, .height = 600, .name = "Cube"}} {
|
|
debugUI().showFrameStats(true);
|
|
}
|
|
|
|
void onCreate() override {
|
|
sophia::SceneHandle sceneHandle = scenes().create();
|
|
sophia::Scene& scene = scenes().get(sceneHandle);
|
|
|
|
sophia::Entity camera = scene.createCamera();
|
|
camera.getComponent<sophia::CameraComponent>().projection =
|
|
sophia::CameraComponent::Projection::Perspective;
|
|
camera.getComponent<sophia::TransformComponent>().position = {0.f, 0.f,
|
|
3.f};
|
|
|
|
// Unlit, so color each face by its normal to tell them apart.
|
|
sophia::MeshData data = sophia::primitives::cube();
|
|
for (sophia::MeshVertex& vertex : data.vertices) {
|
|
vertex.color = glm::vec4{vertex.normal * 0.5f + 0.5f, 1.f};
|
|
}
|
|
|
|
cube = scene.createEntity("Cube");
|
|
cube.addComponent<sophia::MeshComponent>(sophia::MeshComponent{
|
|
.handle = resources().create<sophia::Mesh>(data)});
|
|
|
|
sceneViews().create({
|
|
.scene = sceneHandle,
|
|
});
|
|
}
|
|
|
|
void onUpdate(const sophia::FrameInfo& frameInfo) override {
|
|
auto angle = static_cast<float>(frameInfo.elapsedTime);
|
|
cube.getComponent<sophia::TransformComponent>().rotation =
|
|
glm::angleAxis(angle, glm::normalize(glm::vec3{1.f, 1.f, 0.f}));
|
|
}
|
|
|
|
private:
|
|
sophia::Entity cube;
|
|
};
|
|
|
|
SOPHIA_MAIN(Cube)
|