#include #include #include #include #include #include #include #include #include using namespace legs; class MySystem : public ISystem { public: MySystem() { int width; int height; g_engine->GetWindow()->GetFramebufferSize(&width, &height); m_camera = std::make_shared(width, height); m_camera->SetPosition({0.0f, -10.0f, 5.0f}); g_engine->SetCamera(m_camera); auto renderer = g_engine->GetRenderer(); auto world = g_engine->GetWorld(); // Create a sky auto sky = std::make_shared(renderer); world->SetSky(sky); // Create some test spheres for (unsigned int x = 0; x < 3; x++) { for (unsigned int y = 0; y < 3; y++) { auto testSphere = SIcosphere(glm::vec3(x, y, 5), 0.5f, 1); std::shared_ptr sphereVertexBuffer; std::shared_ptr sphereIndexBuffer; std::vector sphereVertices; sphereVertices.reserve(testSphere.positions.size()); for (unsigned int i = 0; i < testSphere.positions.size(); i++) { sphereVertices.push_back( {testSphere.positions[i], testSphere.normals[i], glm::vec3(0.5, 0.5, 0.5)} ); } renderer->CreateBuffer( sphereVertexBuffer, VertexBuffer, sphereVertices.data(), sizeof(Vertex_P_N_C), static_cast(sphereVertices.size()) ); renderer->CreateBuffer( sphereIndexBuffer, IndexBuffer, testSphere.indices.data(), sizeof(Index), static_cast(testSphere.indices.size()) ); auto sphere = std::make_shared(); sphere->SetBuffers(sphereVertexBuffer, sphereIndexBuffer); sphere->SetPipeline(RenderPipeline::GEO_P_N_C); world->AddEntity(sphere); m_spheres.push_back(sphere); } } // Create a test plane std::shared_ptr planeVertexBuffer; std::shared_ptr planeIndexBuffer; auto testPlane = SPlane({0.0f, 0.0f, 0.0f}, 20.0f); auto planeVertices = std::array(); for (unsigned int i = 0; i < 4; i++) { auto color = glm::vec3 { i == 0 ? 1.0f : 0.0f, i == 1 ? 1.0f : 0.0f, i == 2 ? 1.0f : 0.0f, }; planeVertices[i] = {testPlane.vertices[i], color}; } renderer->CreateBuffer( planeVertexBuffer, VertexBuffer, planeVertices.data(), sizeof(Vertex_P_C), static_cast(planeVertices.size()) ); renderer->CreateBuffer( planeIndexBuffer, IndexBuffer, testPlane.indices.data(), sizeof(Index), static_cast(testPlane.indices.size()) ); auto plane = std::make_shared(); plane->SetBuffers(planeVertexBuffer, planeIndexBuffer); plane->SetPipeline(RenderPipeline::GEO_P_C); world->AddEntity(plane); } ~MySystem() { } void OnFrame() override { m_camera->HandleInput(g_engine->GetFrameInput()); // TODO: model matrix buffer // Move the spheres around /* for (unsigned int i = 0; i < m_spheres.size(); i++) { auto pos = m_spheres[i]->GetPosition(); pos.z = static_cast(std::sin(legs::Time::Uptime() * i)); m_spheres[i]->SetPosition(pos); } */ // Move the sun across the sky const auto degreesPerSecond = 5.0; auto sunRotation = glm::angleAxis( glm::radians(degreesPerSecond * legs::Time::Uptime()), glm::normalize(glm::dvec3 {1.0, 0.3, 0.2}) ); g_engine->GetWorld()->GetSky()->SunDirection = glm::normalize(sunRotation * glm::dvec3(0.1, 0.2, 1.0)); } void OnTick() override { } private: std::shared_ptr m_camera; std::vector> m_spheres; }; int main(int argc, char** argv) { Log::SetLogLevel(LogLevel::Debug); auto code = LEGS_Init(argc, argv); if (code < 0) { return code; } g_engine->GetWindow()->SetTitle("02_systems"); g_engine->AddSystem(std::make_shared()); return LEGS_Run(); }