Initial claude dump
This commit is contained in:
+95
-20
@@ -1,3 +1,5 @@
|
||||
#include <imgui.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
@@ -7,8 +9,12 @@
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <sophia/application.hpp>
|
||||
#include <sophia/components.hpp>
|
||||
#include <sophia/entity.hpp>
|
||||
#include <sophia/event.hpp>
|
||||
#include <sophia/key_event.hpp>
|
||||
#include <sophia/scene_view.hpp>
|
||||
#include <sophia/ui/panel.hpp>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -16,6 +22,24 @@
|
||||
struct Boid {
|
||||
glm::vec2 position;
|
||||
glm::vec2 velocity;
|
||||
sophia::Entity entity;
|
||||
};
|
||||
|
||||
// Shows a render target filling the whole panel.
|
||||
class ViewportPanel : public sophia::Panel {
|
||||
public:
|
||||
ViewportPanel(sophia::UI& ui, sophia::RenderTargetHandle target)
|
||||
: Panel{{.name = "Boids",
|
||||
.windowFlags = ImGuiWindowFlags_NoScrollbar |
|
||||
ImGuiWindowFlags_NoScrollWithMouse}},
|
||||
ui{ui},
|
||||
target{target} {}
|
||||
|
||||
void onUpdate(const sophia::FrameInfo&) override { ui.image(target); }
|
||||
|
||||
private:
|
||||
sophia::UI& ui;
|
||||
sophia::RenderTargetHandle target;
|
||||
};
|
||||
|
||||
constexpr uint32_t DEFAULT_NUMBER_OF_BOIDS = 25;
|
||||
@@ -81,28 +105,53 @@ class Boids2D : public sophia::Application {
|
||||
boid.velocity = glm::vec2{std::cos(theta), std::sin(theta)} * MAX_SPEED;
|
||||
}
|
||||
|
||||
// Boid shape in local space, nose pointing along +x; each boid is drawn
|
||||
// as an instance of this one mesh with a per-boid transform.
|
||||
std::vector<sophia::CanvasVertex> vertices{
|
||||
{{BOID_LENGTH * 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}},
|
||||
{{-BOID_LENGTH * 0.5f, BOID_WIDTH * 0.5f}, {1.0f, 1.0f, 1.0f}},
|
||||
{{-BOID_LENGTH * 0.5f, -BOID_WIDTH * 0.5f}, {1.0f, 1.0f, 1.0f}},
|
||||
};
|
||||
boidMesh = resources().create<sophia::Mesh>(std::move(vertices));
|
||||
boidTexture = createBoidTexture();
|
||||
|
||||
sceneHandle = scenes().create();
|
||||
sophia::Scene& scene = scenes().get(sceneHandle);
|
||||
|
||||
cameraEntity = scene.createEntity("Camera");
|
||||
cameraEntity.addComponent<sophia::CameraComponent>();
|
||||
|
||||
std::uniform_real_distribution<float> tint{0.0f, 1.0f};
|
||||
for (Boid& boid : boids) {
|
||||
boid.entity = scene.createEntity("Boid");
|
||||
boid.entity.addComponent<sophia::SpriteComponent>(sophia::SpriteComponent{
|
||||
.texture = boidTexture,
|
||||
.color = glm::vec4{glm::mix(glm::vec3{0.55f, 0.8f, 1.0f},
|
||||
glm::vec3{1.0f, 0.85f, 0.55f}, tint(rng)),
|
||||
1.0f},
|
||||
.size = {BOID_LENGTH, BOID_WIDTH}});
|
||||
}
|
||||
|
||||
target = resources().create<sophia::RenderTarget>();
|
||||
views().create({.scene = sceneHandle,
|
||||
.target = target,
|
||||
.camera = cameraEntity,
|
||||
.clearColor = {0.01f, 0.01f, 0.02f, 1.0f}});
|
||||
|
||||
panels().emplace<ViewportPanel>(ui(), target);
|
||||
panels().setDefaultLayout({.center = {"Boids"}});
|
||||
}
|
||||
|
||||
void onUpdate(const sophia::FrameInfo& frameInfo) override {
|
||||
float scrollDistance =
|
||||
SCROLL_SPEED * static_cast<float>(frameInfo.deltaTime);
|
||||
if (scrollLeft) camera.position.x -= scrollDistance;
|
||||
if (scrollRight) camera.position.x += scrollDistance;
|
||||
if (scrollUp) camera.position.y -= scrollDistance;
|
||||
if (scrollDown) camera.position.y += scrollDistance;
|
||||
glm::vec3& cameraPosition =
|
||||
cameraEntity.getComponent<sophia::TransformComponent>().position;
|
||||
if (scrollLeft) cameraPosition.x -= scrollDistance;
|
||||
if (scrollRight) cameraPosition.x += scrollDistance;
|
||||
if (scrollUp) cameraPosition.y -= scrollDistance;
|
||||
if (scrollDown) cameraPosition.y += scrollDistance;
|
||||
|
||||
// One world unit per pixel, whatever size the viewport is.
|
||||
cameraEntity.getComponent<sophia::CameraComponent>().ortho.height =
|
||||
resources().getExtent(target).y;
|
||||
|
||||
updateBoids(static_cast<float>(frameInfo.deltaTime));
|
||||
|
||||
for (const Boid& boid : boids) {
|
||||
drawBoid(boid);
|
||||
for (Boid& boid : boids) {
|
||||
syncTransform(boid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,17 +261,40 @@ class Boids2D : public sophia::Application {
|
||||
if (position.y < -WORLD_HALF_HEIGHT) position.y = WORLD_HALF_HEIGHT;
|
||||
}
|
||||
|
||||
void drawBoid(const Boid& boid) {
|
||||
void syncTransform(Boid& boid) {
|
||||
glm::vec2 direction = boid.velocity;
|
||||
float speed = glm::length(direction);
|
||||
direction = speed > 1e-5f ? direction / speed : glm::vec2{0.0f, -1.0f};
|
||||
float angle = std::atan2(direction.y, direction.x);
|
||||
|
||||
glm::mat4 transform =
|
||||
glm::translate(glm::mat4{1.f}, glm::vec3(boid.position, 0.0f)) *
|
||||
glm::rotate(glm::mat4{1.f}, angle, glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
auto& transform = boid.entity.getComponent<sophia::TransformComponent>();
|
||||
transform.position = glm::vec3(boid.position, 0.0f);
|
||||
transform.rotation = glm::angleAxis(angle, glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
}
|
||||
|
||||
drawMesh(boidMesh, transform);
|
||||
// Boid shape, nose pointing along +u, as an anti-aliased white triangle on
|
||||
// a transparent background; each boid's sprite tints it.
|
||||
sophia::TextureHandle createBoidTexture() {
|
||||
constexpr uint32_t WIDTH = 64;
|
||||
constexpr uint32_t HEIGHT = 40;
|
||||
|
||||
std::vector<uint8_t> pixels(WIDTH * HEIGHT * 4);
|
||||
for (uint32_t y = 0; y < HEIGHT; ++y) {
|
||||
for (uint32_t x = 0; x < WIDTH; ++x) {
|
||||
float u = (static_cast<float>(x) + 0.5f) / WIDTH;
|
||||
float v = (static_cast<float>(y) + 0.5f) / HEIGHT;
|
||||
// Distance inside the triangle's edge, in pixels.
|
||||
float inside = (0.5f * (1.0f - u) - std::abs(v - 0.5f)) * HEIGHT;
|
||||
float alpha = std::clamp(inside + 0.5f, 0.0f, 1.0f);
|
||||
|
||||
uint8_t* pixel = &pixels[(y * WIDTH + x) * 4];
|
||||
pixel[0] = pixel[1] = pixel[2] = 255;
|
||||
pixel[3] = static_cast<uint8_t>(alpha * 255.0f);
|
||||
}
|
||||
}
|
||||
|
||||
return resources().create<sophia::Texture>(WIDTH, HEIGHT,
|
||||
std::move(pixels));
|
||||
}
|
||||
|
||||
void setScrollKeyHeld(sophia::KeyCode keycode, bool held) {
|
||||
@@ -235,7 +307,10 @@ class Boids2D : public sophia::Application {
|
||||
std::vector<Boid> boids;
|
||||
std::vector<std::vector<uint32_t>> grid{
|
||||
static_cast<size_t>(GRID_WIDTH * GRID_HEIGHT)};
|
||||
sophia::MeshHandle boidMesh{};
|
||||
sophia::TextureHandle boidTexture{};
|
||||
sophia::SceneHandle sceneHandle{};
|
||||
sophia::RenderTargetHandle target{};
|
||||
sophia::Entity cameraEntity;
|
||||
bool scrollLeft = false;
|
||||
bool scrollRight = false;
|
||||
bool scrollUp = false;
|
||||
|
||||
Reference in New Issue
Block a user