double ComputeCylinderRadius (const Point3d & p1, const Point3d & p2, const Point3d & p3, const Point3d & p4) { Vec3d v12(p1, p2); Vec3d v13(p1, p3); Vec3d v14(p1, p4); Vec3d n1 = Cross (v12, v13); Vec3d n2 = Cross (v14, v12); double n1l = n1.Length(); double n2l = n2.Length(); n1 /= n1l; n2 /= n2l; double v12len = v12.Length(); double h1 = n1l / v12len; double h2 = n2l / v12len; /* (*testout) << "n1 = " << n1 << " n2 = " << n2 << "h1 = " << h1 << " h2 = " << h2 << endl; */ return ComputeCylinderRadius (n1, n2, h1, h2); }
void Hud::Draw (Zeni::Time::Second_Type elapsedTime) { HeroComponent & hero = HeroComponent::GetInstance(); double heroHealth = hero.GetHealth(); double heroShields = hero.GetShields(); double healthWidth = 200.0f; double healthHeight = 30.0f; Zeni::Point2f bgPosition1 (590.0f, 40.0f); Zeni::Point2f bgPosition2 (bgPosition1.x, bgPosition1.y + healthHeight); Zeni::Point2f bgPosition3 (bgPosition1.x + healthWidth, bgPosition1.y + healthHeight); Zeni::Point2f bgPosition4 (bgPosition1.x + healthWidth, bgPosition1.y); Zeni::Point2f healthPosition1 = bgPosition1; Zeni::Point2f healthPosition2 = bgPosition2; Zeni::Point2f healthPosition3 (bgPosition1.x + healthWidth * heroHealth / 1000.0f, bgPosition1.y + healthHeight); Zeni::Point2f healthPosition4 (bgPosition1.x + healthWidth * heroHealth / 1000.0f, bgPosition1.y); Zeni::Point2f shieldPosition1 = bgPosition1; Zeni::Point2f shieldPosition2 = bgPosition2; Zeni::Point2f shieldPosition3 (bgPosition1.x + healthWidth * heroShields / 100.0f, bgPosition1.y + healthHeight); Zeni::Point2f shieldPosition4 (bgPosition1.x + healthWidth * heroShields / 100.0f, bgPosition1.y); int score = hero.GetScore(); std::stringstream ss4; ss4 << score; Zeni::get_Fonts()["score"].render_text (ss4.str(), Zeni::Point2f (20.0f, 550.0f), Zeni::get_Colors()["score"]); ++frameCount; std::stringstream ss ("FPS: "); ss << fps; //Zeni::get_Fonts()["fps"].render_text (ss.str(), Zeni::Point2f(), Zeni::get_Colors()["fps"]); const std::vector<ProjectileFactory*>& heroWeapons = hero.GetWeapons(); size_t numWeapons = heroWeapons.size(); int selectedWeapon = hero.GetSelectedWeaponIndex(); double corner = 800.0f - 30.0f * numWeapons; Zeni::Color enabled = Zeni::get_Colors()["weapon_enabled"]; Zeni::Color disabled = Zeni::get_Colors()["weapon_disabled"]; for (int i = 0; i < numWeapons; ++i) { Zeni::Vertex2f_Texture vertex1 (Zeni::Point2f(corner + 30.0f * i, 0.0f), Zeni::Point2f(0.0f, 0.0f)); Zeni::Vertex2f_Texture vertex2 (Zeni::Point2f(corner + 30.0f * i, 30.0f), Zeni::Point2f(0.0f, 1.0f)); Zeni::Vertex2f_Texture vertex3 (Zeni::Point2f(corner + 30.0f * (i + 1), 30.0f), Zeni::Point2f(1.0f, 1.0f)); Zeni::Vertex2f_Texture vertex4 (Zeni::Point2f(corner + 30.0f * (i + 1), 0.0f), Zeni::Point2f(1.0f, 0.0f)); Zeni::Quadrilateral<Zeni::Vertex2f_Texture> q (vertex1, vertex2, vertex3, vertex4); Zeni::Material backing(i == selectedWeapon ? "selected_weapon" : "weapon"); q.lend_Material (&backing); Zeni::get_Video().render (q); double r = selectedWeapon == i ? weaponRotation : 0.0f; Zeni::render_image ( heroWeapons[i]->GetTexture(), Zeni::Point2f(corner + 30 * i + 5.0f, 5.0f), Zeni::Point2f(corner + 30 * (i + 1.0f) - 5.0f, 25.0), r, 1.0f, Zeni::Point2f(corner + 30 * i + 15.0f, 15.0f), false, heroWeapons[i]->IsReady() ? enabled : disabled); } int heroAmmo = heroWeapons[selectedWeapon]->GetAmmo(); std::stringstream ss3; ss3 << heroAmmo; Zeni::get_Fonts()["ammo"].render_text (ss3.str(), Zeni::Point2f(corner - 5.0f, 0.0f), Zeni::get_Colors()["ammo"], Zeni::ZENI_RIGHT); Zeni::Vertex2f_Texture v9 (bgPosition1, Zeni::Point2f (0.0f, 0.0f)); Zeni::Vertex2f_Texture v10 (bgPosition2, Zeni::Point2f (0.0f, 1.0f)); Zeni::Vertex2f_Texture v11 (bgPosition3, Zeni::Point2f (1.0f, 1.0f)); Zeni::Vertex2f_Texture v12 (bgPosition4, Zeni::Point2f (1.0f, 0.0f)); Zeni::Quadrilateral<Zeni::Vertex2f_Texture> q3 (v9, v10, v11, v12); Zeni::Material healthbar1("healthbar1"); q3.lend_Material (&healthbar1); Zeni::get_Video().render (q3); Zeni::Vertex2f_Texture v13 (healthPosition1, Zeni::Point2f (0.0f, 0.0f)); Zeni::Vertex2f_Texture v14 (healthPosition2, Zeni::Point2f (0.0f, 1.0f)); Zeni::Vertex2f_Texture v15 (healthPosition3, Zeni::Point2f (heroHealth / 1000.0f, 1.0f)); Zeni::Vertex2f_Texture v16 (healthPosition4, Zeni::Point2f (heroHealth / 1000.0f, 0.0f)); Zeni::Quadrilateral<Zeni::Vertex2f_Texture> q4 (v13, v14, v15, v16); Zeni::Material healthbar2("healthbar2"); q4.lend_Material (&healthbar2); Zeni::get_Video().render (q4); Zeni::Vertex2f_Texture v17 (shieldPosition1, Zeni::Point2f (0.0f, 0.0f)); Zeni::Vertex2f_Texture v18 (shieldPosition2, Zeni::Point2f (0.0f, 1.0f)); Zeni::Vertex2f_Texture v19 (shieldPosition3, Zeni::Point2f (heroShields / 100.0f, 1.0f)); Zeni::Vertex2f_Texture v20 (shieldPosition4, Zeni::Point2f (heroShields / 100.0f, 0.0f)); Zeni::Quadrilateral<Zeni::Vertex2f_Texture> q5 (v17, v18, v19, v20); Zeni::Material healthbar3("healthbar3"); q5.lend_Material (&healthbar3); Zeni::get_Video().render (q5); double timeRemaining = GameTimer::GetInstance().GetRemainingTime(); Zeni::Color timerTextColor = timeRemaining < 10.0f ? Zeni::get_Colors()["low_time"] : Zeni::get_Colors()["time"]; Zeni::render_image ( "Timer", Zeni::Point2f (620.0f, 540.0f), Zeni::Point2f (670.0f, 590.0f), false, timerTextColor); std::stringstream ss2; int minutes = (int)timeRemaining / 60; ss2 << minutes << ":" << std::fixed << std::setprecision(2) << timeRemaining - minutes * 60; Zeni::get_Fonts()["time"].render_text (ss2.str(), Zeni::Point2f(680.0f, 550.0f), timerTextColor); }
osg::Drawable *ClampNode::createBrick(void) const { // Get the brick Clamp* clamp = static_cast<Clamp*>(_lego); // Get brick color QColor color = clamp->getColor(); // Get clamp bounding box clamp->calculateBoundingBox(); BoundingBox bb = clamp->getBoundingBox(); // Get integer sizes int width = bb.getWidth(); int length = bb.getLength(); int height = bb.getHeight(); // Get real position, according to tile size double mw = (-width)*Lego::length_unit/2; double mwpm = (-width)*Lego::length_unit/2+Lego::height_unit/2; double mwp = (-width)*Lego::length_unit/2+0.93*Lego::height_unit; double pw = (width)*Lego::length_unit/2; double pwm = (width)*Lego::length_unit/2-Lego::height_unit/2; double ml = (-length)*Lego::length_unit/2; double mlp = (-length+0.5)*Lego::length_unit/2; double pl = (length)*Lego::length_unit/2; double plm = (length-0.5)*Lego::length_unit/2; double mh = (-height)*Lego::height_unit/2; double mhp = (-height)*Lego::height_unit/2+2*Lego::plot_top_height; double mhpm = (-height)*Lego::height_unit/2+Lego::plot_top_height; double phm = (height)*Lego::height_unit/2-Lego::height_unit/2; double phmp = (height)*Lego::height_unit/2-0.5*Lego::height_unit/2; // Create 3 vertices osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array; osg::Vec3 v0(ml, mw, mh); osg::Vec3 v1(pl, mw, mh); osg::Vec3 v2(pl, pw, mh); osg::Vec3 v3(ml, pw, mh); osg::Vec3 v4(ml, pw, mhp); osg::Vec3 v5(pl, pw, mhp); osg::Vec3 v6(pl, mw, mhp); osg::Vec3 v7(ml, mw, mhp); osg::Vec3 v8(mlp, mw, mhp); osg::Vec3 v9(mlp, mw, phm); osg::Vec3 v10(ml, mw, phm); osg::Vec3 v11(ml, mwp, phmp); osg::Vec3 v12(mlp, mwp, phmp); osg::Vec3 v13(mlp, pw, mhp); osg::Vec3 v14(plm, mw, mhp); osg::Vec3 v15(plm, mw, phm); osg::Vec3 v16(pl, mw, phm); osg::Vec3 v17(pl, mwp, phmp); osg::Vec3 v18(plm, mwp, phmp); osg::Vec3 v19(plm, pw, mhp); osg::Vec3 v20(mlp, mwpm, mh); osg::Vec3 v21(plm, mwpm, mh); osg::Vec3 v22(plm, pwm, mh); osg::Vec3 v23(mlp, pwm, mh); osg::Vec3 v24(mlp, mwpm, mhpm); osg::Vec3 v25(plm, mwpm, mhpm); osg::Vec3 v26(plm, pwm, mhpm); osg::Vec3 v27(mlp, pwm, mhpm); // Create 1 faces, 0 faces are quads splitted into two triangles // NB: Down face is transparent, we don't even create it // Bottom vertices->push_back(v3); vertices->push_back(v2); vertices->push_back(v1); vertices->push_back(v0); // Bottom hole vertices->push_back(v20); vertices->push_back(v21); vertices->push_back(v22); vertices->push_back(v23); // Bottom far vertices->push_back(v24); vertices->push_back(v25); vertices->push_back(v26); vertices->push_back(v27); // Front face vertices->push_back(v2); vertices->push_back(v3); vertices->push_back(v4); vertices->push_back(v5); // Back face vertices->push_back(v0); vertices->push_back(v1); vertices->push_back(v6); vertices->push_back(v7); // Left bottom face vertices->push_back(v0); vertices->push_back(v3); vertices->push_back(v4); vertices->push_back(v7); // Right bottom face vertices->push_back(v1); vertices->push_back(v2); vertices->push_back(v5); vertices->push_back(v6); // Top face vertices->push_back(v4); vertices->push_back(v5); vertices->push_back(v6); vertices->push_back(v7); // Left part back vertices->push_back(v7); vertices->push_back(v8); vertices->push_back(v9); vertices->push_back(v10); // Left part left ext vertices->push_back(v4); vertices->push_back(v7); vertices->push_back(v10); vertices->push_back(v11); // Left part front vertices->push_back(v4); vertices->push_back(v11); vertices->push_back(v12); vertices->push_back(v13); // Left part left int vertices->push_back(v8); vertices->push_back(v9); vertices->push_back(v12); vertices->push_back(v13); // Right part back vertices->push_back(v6); vertices->push_back(v14); vertices->push_back(v15); vertices->push_back(v16); // Left part left ext vertices->push_back(v5); vertices->push_back(v6); vertices->push_back(v16); vertices->push_back(v17); // Left part front vertices->push_back(v5); vertices->push_back(v17); vertices->push_back(v18); vertices->push_back(v19); // Left part left int vertices->push_back(v14); vertices->push_back(v15); vertices->push_back(v18); vertices->push_back(v19); // Bottom front vertices->push_back(v20); vertices->push_back(v21); vertices->push_back(v25); vertices->push_back(v24); // Bottom right vertices->push_back(v21); vertices->push_back(v22); vertices->push_back(v26); vertices->push_back(v25); // Bottom back vertices->push_back(v22); vertices->push_back(v23); vertices->push_back(v27); vertices->push_back(v26); // Bottom left vertices->push_back(v23); vertices->push_back(v20); vertices->push_back(v24); vertices->push_back(v27); // Create tile geometry osg::ref_ptr<osg::Geometry> clampGeometry = new osg::Geometry; // Match vertices clampGeometry->setVertexArray(vertices); // Create colors osg::Vec4 osgColor(static_cast<float>(color.red())/255.0, static_cast<float>(color.green())/255.0, static_cast<float>(color.blue())/255.0, 1.0); osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array; // Every face has the same color, so there is only one color colors->push_back(osgColor); // Match color clampGeometry->setColorArray(colors); clampGeometry->setColorBinding(osg::Geometry::BIND_OVERALL); // Create normals osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array; normals->push_back(osg::Vec3(0, 0, -1)); normals->push_back(osg::Vec3(0, 0, -1)); normals->push_back(osg::Vec3(0, 1, 0)); normals->push_back(osg::Vec3(0, -1, 0)); normals->push_back(osg::Vec3(-1, 0, 0)); normals->push_back(osg::Vec3(1, 0, 0)); normals->push_back(osg::Vec3(0, 0, 1)); normals->push_back(osg::Vec3(0, -1, 0)); normals->push_back(osg::Vec3(-1, 0, 0)); double w = pw - mwp; double h = phmp - mhp; double norm = std::sqrt(w*w + h*h); normals->push_back(osg::Vec3(0, h/norm, w/norm)); normals->push_back(osg::Vec3(1, 0, 0)); normals->push_back(osg::Vec3(0, -1, 0)); normals->push_back(osg::Vec3(1, 0, 0)); normals->push_back(osg::Vec3(0, h/norm, w/norm)); normals->push_back(osg::Vec3(-1, 0, 0)); normals->push_back(osg::Vec3(0, 1, 0)); normals->push_back(osg::Vec3(-1, 0, 0)); normals->push_back(osg::Vec3(0, -1, 0)); normals->push_back(osg::Vec3(1, 0, 0)); // Match normals clampGeometry->setNormalArray(normals); clampGeometry->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE); // Define 1 GL_QUADS with 1*4 vertices, corresponding to bottom part clampGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0*4, 4)); // Define 1 GL_QUADS with 1*4 vertices, corresponding to 1 hole in bottom part clampGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 1*4, 4)); // Retesslate to create hole osgUtil::Tessellator tesslator; tesslator.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); tesslator.setWindingType(osgUtil::Tessellator::TESS_WINDING_ODD); tesslator.retessellatePolygons(*clampGeometry); // Create 17 GL_QUADS, i.e. 18*4 vertices clampGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 2*4, 18*4)); // Return the tile whithout plot return clampGeometry.release(); }