Node::Ptr createTransparentObject(float scale, float rotationY, file::AssetLibrary::Ptr assets) { assert(assets); assert(NUM_OBJECTS > 0); auto randomAxis = Vector3::create((float)rand(), (float)rand(), (float)rand())->normalize(); const float randomAng = 2.0f * (float)PI * rand() / (float)RAND_MAX; return scene::Node::create() ->addComponent(Transform::create( Matrix4x4::create() ->appendRotation(randomAng, randomAxis) ->appendScale(scale) ->appendTranslation(1.0f) ->appendRotationY(rotationY) )) ->addComponent(Surface::create( assets->geometry("cube"), material::BasicMaterial::create() ->diffuseColor(Color::hslaToRgba(0.5f * rotationY / (float)PI, 1.0f, 0.5f, 0.5f)) ->isTransparent(true, true) ->triangleCulling(render::TriangleCulling::BACK), assets->effect("effect/Basic.effect") )); }
void addStar(Node::Ptr root, file::AssetLibrary::Ptr assets, std::vector<Star>& stars) { const unsigned int MAX_NUM_STARS = 20; if (stars.size() == MAX_NUM_STARS) { std::cerr << "cannot add more stars." << std::endl; return; } unsigned int numBranches = 4 + rand() % 4; float outRadius = 0.9f + 0.2f * (rand() / float(RAND_MAX) - 0.5f); float inRadius = outRadius * (0.5f + 0.1f * (rand() / float(RAND_MAX) - 0.5f)); auto starNode = Node::create("star_" + std::to_string(stars.size())) ->addComponent(Surface::create( createStarLineGeometry(numBranches, inRadius, outRadius, assets->context()), material::Material::create("material") ->set("diffuseColor", Color::hslaToRgba(rand() / float(RAND_MAX), 0.75f, 0.6f, 1.0f)) ->set("lineThickness", 1.0f + 3.0f * (rand() / float(RAND_MAX))), assets->effect("line") )) ->addComponent(Transform::create(Matrix4x4::create()->appendRotationZ(2.0f * float(M_PI) * rand() / (float) RAND_MAX))); stars.push_back(Star()); stars.back().node = starNode; stars.back().angRate = 0.01f + 0.05f * (rand() / float(RAND_MAX)); root->addChild(stars.back().node); }
Node::Ptr createPhysicsObject(unsigned int id, file::AssetLibrary::Ptr assets, bool isCube) { const float mass = MIN_MASS + (rand() / (float)RAND_MAX) * (MAX_MASS - MIN_MASS); const float size = MIN_SCALE + (rand() / (float)RAND_MAX) * (MAX_SCALE - MIN_SCALE); const float startX = MIN_DROP_POS->x() + (rand() / (float)RAND_MAX) * (MAX_DROP_POS->x() - MIN_DROP_POS->x()); const float startY = MIN_DROP_POS->y() + (rand() / (float)RAND_MAX) * (MAX_DROP_POS->y() - MIN_DROP_POS->y()); const float startZ = MIN_DROP_POS->z() + (rand() / (float)RAND_MAX) * (MAX_DROP_POS->z() - MIN_DROP_POS->z()); const float halfSize = 0.5f * size; const float hue = (id % 10) * 0.1f; auto diffColor = Color::hslaToRgba(hue, 1.0f, 0.5f, 1.0f); auto specColor = Color::hslaToRgba(hue, 1.0f, 0.8f, 1.0f); const float shininess = 2.0f * (rand() / (float)RAND_MAX) * 6.0f; bullet::Collider::Ptr collider = nullptr; if (isCube) { auto boxColliderData = bullet::ColliderData::create( mass, bullet::BoxShape::create(halfSize, halfSize, halfSize) ); collider = bullet::Collider::create(boxColliderData); } else { auto sphColliderData = bullet::ColliderData::create( mass, bullet::SphereShape::create(halfSize) ); collider = bullet::Collider::create(sphColliderData); } return scene::Node::create("node_" + std::to_string(id)) ->addComponent(Transform::create( Matrix4x4::create() ->appendScale(size) ->appendTranslation(startX, startY, startZ) )) ->addComponent(Surface::create( assets->geometry(isCube ? "cube" : "sphere"), material::PhongMaterial::create() ->specularColor(specColor) ->shininess(shininess) ->diffuseColor(diffColor), assets->effect("phong") )) ->addComponent(collider); }
void generateStars(unsigned int numStars, file::AssetLibrary::Ptr assets, std::vector<Node::Ptr>& starNodes) { if (assets == nullptr) throw std::invalid_argument("assets"); starNodes.clear(); starNodes.resize(numStars, nullptr); for (unsigned int i = 0; i < numStars; ++i) { starNodes[i] = Node::create("star_" + std::to_string(i)) ->addComponent(Transform::create()) ->addComponent(Surface::create( assets->geometry("smallStar"), Material::create() ->set("diffuseColor", generateColor()) ->set("colorMask", true) ->set("depthMask", false) ->set("depthFunc", CompareMode::ALWAYS) ->set("stencilFunc", CompareMode::EQUAL) ->set("stencilRef", 1) ->set("stencilMask", (unsigned int)0xff) ->set("stencilFailOp", StencilOperation::KEEP), assets->effect("basic") )); const float minX = -1.0f; const float rangeX = 1.0f - minX; const float minY = -1.0f; const float rangeY = 1.0f - minY; starNodes[i]->component<Transform>()->matrix() ->appendScale(0.25f, 0.25f, 0.25f) ->appendRotationZ(2.0f * (float)PI * (rand() / (float)RAND_MAX)) ->appendTranslation( minX + (rand() / (float)RAND_MAX)*rangeX, minY + (rand() / (float)RAND_MAX)*rangeY, 0.0f ); } }