本文整理汇总了C++中vector3d::Length方法的典型用法代码示例。如果您正苦于以下问题:C++ vector3d::Length方法的具体用法?C++ vector3d::Length怎么用?C++ vector3d::Length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类vector3d
的用法示例。
在下文中一共展示了vector3d::Length方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Render
void SpaceStation::Render(const vector3d &viewCoords, const matrix4x4d &viewTransform)
{
LmrObjParams ¶ms = GetLmrObjParams();
params.label = GetLabel().c_str();
SetLmrTimeParams();
for (int i=0; i<MAX_DOCKING_PORTS; i++) {
params.animStages[ANIM_DOCKING_BAY_1 + i] = m_shipDocking[i].stage;
params.animValues[ANIM_DOCKING_BAY_1 + i] = m_shipDocking[i].stagePos;
}
RenderLmrModel(viewCoords, viewTransform);
/* don't render city if too far away */
if (viewCoords.Length() > 1000000.0) return;
// find planet Body*
Planet *planet;
{
Body *_planet = GetFrame()->m_astroBody;
if ((!_planet) || !_planet->IsType(Object::PLANET)) {
// orbital spaceport -- don't make city turds
} else {
planet = static_cast<Planet*>(_planet);
if (!m_adjacentCity) {
m_adjacentCity = new CityOnPlanet(planet, this, m_sbody->seed);
}
m_adjacentCity->Render(this, viewCoords, viewTransform);
}
}
}
示例2: Render
// Renders space station and adjacent city if applicable
// For orbital starports: renders as normal
// For surface starports:
// Lighting: Calculates available light for model and splits light between directly and ambiently lit
// Lighting is done by manipulating global lights or setting uniforms in atmospheric models shader
void SpaceStation::Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform)
{
Body *b = GetFrame()->GetBody();
assert(b);
if (!b->IsType(Object::PLANET)) {
// orbital spaceport -- don't make city turds or change lighting based on atmosphere
RenderModel(r, camera, viewCoords, viewTransform);
}
else {
std::vector<Graphics::Light> oldLights;
Color oldAmbient;
SetLighting(r, camera, oldLights, oldAmbient);
Planet *planet = static_cast<Planet*>(b);
/* don't render city if too far away */
if (viewCoords.Length() < 1000000.0){
if (!m_adjacentCity) {
m_adjacentCity = new CityOnPlanet(planet, this, m_sbody->seed);
}
m_adjacentCity->Render(r, camera, this, viewCoords, viewTransform);
}
RenderModel(r, camera, viewCoords, viewTransform, false);
ResetLighting(r, oldLights, oldAmbient);
}
}
示例3: AIGetLeadDir
// returns direction in ship's frame from this ship to target lead position
vector3d Ship::AIGetLeadDir(const Body *target, const vector3d& targaccel, int gunindex)
{
assert(target);
if (ScopedTable(m_equipSet).CallMethod<int>("OccupiedSpace", "laser_front") == 0)
return target->GetPositionRelTo(this).Normalized();
const vector3d targpos = target->GetPositionRelTo(this);
const vector3d targvel = target->GetVelocityRelTo(this);
// todo: should adjust targpos for gunmount offset
double projspeed = 0;
Properties().Get(gunindex?"laser_rear_speed":"laser_front_speed", projspeed);
vector3d leadpos;
// avoid a divide-by-zero floating point exception (very nearly zero is ok)
if( !is_zero_exact(projspeed) ) {
// first attempt
double projtime = targpos.Length() / projspeed;
leadpos = targpos + targvel*projtime + 0.5*targaccel*projtime*projtime;
// second pass
projtime = leadpos.Length() / projspeed;
leadpos = targpos + targvel*projtime + 0.5*targaccel*projtime*projtime;
} else {
// default
leadpos = targpos;
}
return leadpos.Normalized();
}
示例4: UpdateVBOs
void UpdateVBOs() {
PROFILE_SCOPED()
//create buffer and upload data
Graphics::VertexBufferDesc vbd;
vbd.attrib[0].semantic = Graphics::ATTRIB_POSITION;
vbd.attrib[0].format = Graphics::ATTRIB_FORMAT_FLOAT3;
vbd.attrib[1].semantic = Graphics::ATTRIB_NORMAL;
vbd.attrib[1].format = Graphics::ATTRIB_FORMAT_FLOAT3;
vbd.numVertices = ctx->NUMVERTICES();
vbd.usage = Graphics::BUFFER_USAGE_STATIC;
m_vertexBuffer.reset(Pi::renderer->CreateVertexBuffer(vbd));
GasPatchContext::VBOVertex* vtxPtr = m_vertexBuffer->Map<GasPatchContext::VBOVertex>(Graphics::BUFFER_MAP_WRITE);
assert(m_vertexBuffer->GetDesc().stride == sizeof(GasPatchContext::VBOVertex));
const Sint32 edgeLen = ctx->edgeLen;
const double frac = ctx->frac;
for (Sint32 y=0; y<edgeLen; y++) {
for (Sint32 x=0; x<edgeLen; x++) {
const vector3d p = GetSpherePoint(x*frac, y*frac);
const vector3d pSubCentroid = p - clipCentroid;
clipRadius = std::max(clipRadius, p.Length());
vtxPtr->pos = vector3f(pSubCentroid);
vtxPtr->norm = vector3f(p);
++vtxPtr; // next vertex
}
}
m_vertexBuffer->Unmap();
}
示例5: AIGetLeadDir
// returns direction in ship's frame from this ship to target lead position
vector3d Ship::AIGetLeadDir(const Body *target, const vector3d& targaccel, int gunindex)
{
assert(target);
if (m_equipment.Get(Equip::SLOT_LASER) == Equip::NONE)
return target->GetPositionRelTo(this).Normalized();
const vector3d targpos = target->GetPositionRelTo(this);
const vector3d targvel = target->GetVelocityRelTo(this);
// todo: should adjust targpos for gunmount offset
const int laser = Equip::types[m_equipment.Get(Equip::SLOT_LASER, gunindex)].tableIndex;
const double projspeed = Equip::lasers[laser].speed;
vector3d leadpos;
// avoid a divide-by-zero floating point exception (very nearly zero is ok)
if( !is_zero_exact(projspeed) ) {
// first attempt
double projtime = targpos.Length() / projspeed;
leadpos = targpos + targvel*projtime + 0.5*targaccel*projtime*projtime;
// second pass
projtime = leadpos.Length() / projspeed;
leadpos = targpos + targvel*projtime + 0.5*targaccel*projtime*projtime;
} else {
// default
leadpos = targpos;
}
return leadpos.Normalized();
}
示例6: GetFlipMode
static int GetFlipMode(Ship *ship, const vector3d &relpos, const vector3d &relvel)
{
double dist = relpos.Length();
double vel = relvel.Dot(relpos) / dist;
if (vel > 0.0 && vel*vel > 1.7 * ship->GetAccelRev() * dist) return 2;
if (dist > 100000000.0) return 1; // arbitrary
return 0;
}
示例7: DrawAtmosphereSurface
static void DrawAtmosphereSurface(Graphics::Renderer *renderer,
const matrix4x4d &modelView, const vector3d &campos, float rad, Graphics::Material *mat)
{
const int LAT_SEGS = 20;
const int LONG_SEGS = 20;
vector3d yaxis = campos.Normalized();
vector3d zaxis = vector3d(1.0,0.0,0.0).Cross(yaxis).Normalized();
vector3d xaxis = yaxis.Cross(zaxis);
const matrix4x4d invrot = matrix4x4d::MakeRotMatrix(xaxis, yaxis, zaxis).InverseOf();
renderer->SetTransform(modelView * matrix4x4d::ScaleMatrix(rad, rad, rad) * invrot);
// what is this? Well, angle to the horizon is:
// acos(planetRadius/viewerDistFromSphereCentre)
// and angle from this tangent on to atmosphere is:
// acos(planetRadius/atmosphereRadius) ie acos(1.0/1.01244blah)
double endAng = acos(1.0/campos.Length())+acos(1.0/rad);
double latDiff = endAng / double(LAT_SEGS);
double rot = 0.0;
float sinCosTable[LONG_SEGS+1][2];
for (int i=0; i<=LONG_SEGS; i++, rot += 2.0*M_PI/double(LONG_SEGS)) {
sinCosTable[i][0] = float(sin(rot));
sinCosTable[i][1] = float(cos(rot));
}
/* Tri-fan above viewer */
Graphics::VertexArray va(Graphics::ATTRIB_POSITION);
va.Add(vector3f(0.f, 1.f, 0.f));
for (int i=0; i<=LONG_SEGS; i++) {
va.Add(vector3f(
sin(latDiff)*sinCosTable[i][0],
cos(latDiff),
-sin(latDiff)*sinCosTable[i][1]));
}
renderer->DrawTriangles(&va, mat, Graphics::TRIANGLE_FAN);
/* and wound latitudinal strips */
double lat = latDiff;
for (int j=1; j<LAT_SEGS; j++, lat += latDiff) {
Graphics::VertexArray v(Graphics::ATTRIB_POSITION);
float cosLat = cos(lat);
float sinLat = sin(lat);
float cosLat2 = cos(lat+latDiff);
float sinLat2 = sin(lat+latDiff);
for (int i=0; i<=LONG_SEGS; i++) {
v.Add(vector3f(sinLat*sinCosTable[i][0], cosLat, -sinLat*sinCosTable[i][1]));
v.Add(vector3f(sinLat2*sinCosTable[i][0], cosLat2, -sinLat2*sinCosTable[i][1]));
}
renderer->DrawTriangles(&v, mat, Graphics::TRIANGLE_STRIP);
}
}
示例8: Render
void SpaceStation::Render(const vector3d &viewCoords, const matrix4x4d &viewTransform)
{
/* Well this is nice... */
static int poo=0;
if (!poo) {
poo = 1;
LmrGetModelsWithTag("advert", s_advertModels);
}
// it is silly to do this every render call
//
// random advert models in pFlag[16 .. 19]
// station name in pText[0]
// docking port in pText[1]
MTRand rand;
rand.seed(m_sbody->seed);
LmrObjParams ¶ms = GetLmrObjParams();
/* random advert models */
params.argStrings[4] = s_advertModels[rand.Int32(s_advertModels.size())]->GetName();
params.argStrings[5] = s_advertModels[rand.Int32(s_advertModels.size())]->GetName();
params.argStrings[6] = s_advertModels[rand.Int32(s_advertModels.size())]->GetName();
params.argStrings[7] = s_advertModels[rand.Int32(s_advertModels.size())]->GetName();
params.argStrings[0] = GetLabel().c_str();
SetLmrTimeParams();
for (int i=0; i<MAX_DOCKING_PORTS; i++) {
params.argDoubles[ARG_STATION_BAY1_STAGE + i] = double(m_shipDocking[i].stage);
params.argDoubles[ARG_STATION_BAY1_POS + i] = m_shipDocking[i].stagePos;
}
RenderLmrModel(viewCoords, viewTransform);
/* don't render city if too far away */
if (viewCoords.Length() > 1000000.0) return;
// find planet Body*
Planet *planet;
{
Body *_planet = GetFrame()->m_astroBody;
if ((!_planet) || !_planet->IsType(Object::PLANET)) {
// orbital spaceport -- don't make city turds
} else {
planet = static_cast<Planet*>(_planet);
if (!m_adjacentCity) {
m_adjacentCity = new CityOnPlanet(planet, this, m_sbody->seed);
}
m_adjacentCity->Render(this, viewCoords, viewTransform);
}
}
}
示例9: while
void Space::BodyNearFinder::GetBodiesMaybeNear(const vector3d &pos, double dist, BodyNearList &bodies) const
{
if (m_bodyDist.empty()) return;
const double len = pos.Length();
std::vector<BodyDist>::const_iterator min = std::lower_bound(m_bodyDist.begin(), m_bodyDist.end(), len-dist);
std::vector<BodyDist>::const_iterator max = std::upper_bound(min, m_bodyDist.end(), len+dist);
while (min != max) {
bodies.push_back((*min).body);
++min;
}
}
示例10: l_space_spawn_ship_near
/*
* Function: SpawnShipNear
*
* Create a ship and place it in space near the given <Body>.
*
* > ship = Space.SpawnShip(type, body, min, max, hyperspace)
*
* Parameters:
*
* type - the name of the ship
*
* body - the <Body> near which the ship should be spawned
*
* min - minimum distance from the body to place the ship, in Km
*
* max - maximum distance to place the ship
*
* hyperspace - optional table containing hyperspace entry information. If
* this is provided the ship will not spawn directly. Instead,
* a hyperspace cloud will be created that the ship will exit
* from. The table contains two elements, a <SystemPath> for
* the system the ship is travelling from, and the due
* date/time that the ship should emerge from the cloud.
*
* Return:
*
* ship - a <Ship> object for the new ship
*
* Example:
*
* > -- spawn a ship 10km from the player
* > local ship = Ship.SpawnNear("viper_police_craft", Game.player, 10, 10)
*
* Availability:
*
* alpha 10
*
* Status:
*
* experimental
*/
static int l_space_spawn_ship_near(lua_State *l)
{
if (!Pi::game)
luaL_error(l, "Game is not started");
LUA_DEBUG_START(l);
const char *type = luaL_checkstring(l, 1);
if (! ShipType::Get(type))
luaL_error(l, "Unknown ship type '%s'", type);
Body *nearbody = LuaObject<Body>::CheckFromLua(2);
float min_dist = luaL_checknumber(l, 3);
float max_dist = luaL_checknumber(l, 4);
SystemPath *path = 0;
double due = -1;
_unpack_hyperspace_args(l, 5, path, due);
Ship *ship = new Ship(type);
assert(ship);
Body *thing = _maybe_wrap_ship_with_cloud(ship, path, due);
// XXX protect against spawning inside the body
Frame * newframe = nearbody->GetFrame();
const vector3d newPosition = (MathUtil::RandomPointOnSphere(min_dist, max_dist)* 1000.0) + nearbody->GetPosition();
// If the frame is rotating and the chosen position is too far, use non-rotating parent.
// Otherwise the ship will be given a massive initial velocity when it's bumped out of the
// rotating frame in the next update
if (newframe->IsRotFrame() && newframe->GetRadius() < newPosition.Length()) {
assert(newframe->GetParent());
newframe = newframe->GetParent();
}
thing->SetFrame(newframe);;
thing->SetPosition(newPosition);
thing->SetVelocity(vector3d(0,0,0));
Pi::game->GetSpace()->AddBody(thing);
LuaObject<Ship>::PushToLua(ship);
LUA_DEBUG_END(l, 1);
return 1;
}
示例11: CheckCollision
// check whether ship is at risk of colliding with frame body on current path
// return values:
//0 - no collision
//1 - below feature height
//2 - unsafe escape from effect radius
//3 - unsafe entry to effect radius
//4 - probable path intercept
static int CheckCollision(Ship *ship, const vector3d &pathdir, double pathdist, const vector3d &tpos, double endvel, double r)
{
// ship is in obstructor's frame anyway, so is tpos
if (pathdist < 100.0) return 0;
Body *body = ship->GetFrame()->GetBodyFor();
if (!body) return 0;
vector3d spos = ship->GetPosition();
double tlen = tpos.Length(), slen = spos.Length();
double fr = MaxFeatureRad(body);
// if target inside, check if direct entry is safe (30 degree)
if (tlen < r) {
double af = (tlen > fr) ? 0.5 * (1 - (tlen-fr) / (r-fr)) : 0.5;
if (pathdir.Dot(tpos) > -af*tlen)
if (slen < fr) return 1; else return 3;
else return 0;
}
// if ship inside, check for max feature height and direct escape (30 degree)
if (slen < r) {
if (slen < fr) return 1;
double af = (slen > fr) ? 0.5 * (1 - (slen-fr) / (r-fr)) : 0.5;
if (pathdir.Dot(spos) < af*slen) return 2; else return 0;
}
// now for the intercept calc
// find closest point to obstructor
double tanlen = -spos.Dot(pathdir);
if (tanlen < 0 || tanlen > pathdist) return 0; // closest point outside path
vector3d perpdir = (tanlen*pathdir + spos).Normalized();
double perpspeed = ship->GetVelocity().Dot(perpdir);
double parspeed = ship->GetVelocity().Dot(pathdir);
if (parspeed < 0) parspeed = 0; // shouldn't break any important case
if (perpspeed > 0) perpspeed = 0; // prevent attempts to speculatively fly through planets
// find time that ship will pass through that point
// get velocity as if accelerating from start or end, pick smallest
double ivelsqr = endvel*endvel + 2*ship->GetAccelFwd()*(pathdist-tanlen); // could put endvel in here
double fvelsqr = parspeed*parspeed + 2*ship->GetAccelFwd()*tanlen;
double tanspeed = sqrt(ivelsqr < fvelsqr ? ivelsqr : fvelsqr);
double time = tanlen / (0.5 * (parspeed + tanspeed)); // actually correct?
double dist = spos.Dot(perpdir) + perpspeed*time; // spos.perpdir should be positive
if (dist < r) return 4;
return 0;
}
示例12: _UpdateVBOs
void GeoPatch::_UpdateVBOs() {
if (m_needUpdateVBOs) {
m_needUpdateVBOs = false;
if (!m_vbo) glGenBuffersARB(1, &m_vbo);
glBindBufferARB(GL_ARRAY_BUFFER, m_vbo);
glBufferDataARB(GL_ARRAY_BUFFER, sizeof(GeoPatchContext::VBOVertex)*ctx->NUMVERTICES(), 0, GL_DYNAMIC_DRAW);
double xfrac=0.0, yfrac=0.0;
double *pHts = heights.Get();
const vector3f *pNorm = &normals[0];
const Color3ub *pColr = &colors[0];
GeoPatchContext::VBOVertex *pData = ctx->vbotemp;
for (int y=0; y<ctx->edgeLen; y++) {
xfrac = 0.0;
for (int x=0; x<ctx->edgeLen; x++) {
const double height = *pHts;
const vector3d p = (GetSpherePoint(xfrac, yfrac) * (height + 1.0)) - clipCentroid;
clipRadius = std::max(clipRadius, p.Length());
pData->x = float(p.x);
pData->y = float(p.y);
pData->z = float(p.z);
++pHts; // next height
pData->nx = pNorm->x;
pData->ny = pNorm->y;
pData->nz = pNorm->z;
++pNorm; // next normal
pData->col[0] = pColr->r;
pData->col[1] = pColr->g;
pData->col[2] = pColr->b;
pData->col[3] = 255;
++pColr; // next colour
++pData; // next vertex
xfrac += ctx->frac;
}
yfrac += ctx->frac;
}
glBufferDataARB(GL_ARRAY_BUFFER, sizeof(GeoPatchContext::VBOVertex)*ctx->NUMVERTICES(), ctx->vbotemp, GL_DYNAMIC_DRAW);
glBindBufferARB(GL_ARRAY_BUFFER, 0);
}
}
示例13: PutOrbit
void SystemView::PutOrbit(const Orbit *orbit, const vector3d &offset, const Color &color, double planetRadius)
{
int num_vertices = 0;
vector3f vts[100];
for (int i = 0; i < int(COUNTOF(vts)); ++i) {
const double t = double(i) / double(COUNTOF(vts));
const vector3d pos = orbit->EvenSpacedPosTrajectory(t);
vts[i] = vector3f(offset + pos * double(m_zoom));
++num_vertices;
if (pos.Length() < planetRadius)
break;
}
if (num_vertices > 1) {
// don't close the loop for hyperbolas and parabolas and crashed ellipses
if ((orbit->GetEccentricity() > 1.0) || (num_vertices < int(COUNTOF(vts))))
m_renderer->DrawLines(num_vertices, vts, color, LINE_STRIP);
else
m_renderer->DrawLines(num_vertices, vts, color, LINE_LOOP);
}
}
示例14: UpdateFrame
void Body::UpdateFrame()
{
if (!(m_flags & FLAG_CAN_MOVE_FRAME)) return;
// falling out of frames
if (m_frame->GetRadius() < GetPosition().Length()) {
Frame *newFrame = GetFrame()->GetParent();
if (newFrame) { // don't fall out of root frame
Output("%s leaves frame %s\n", GetLabel().c_str(), GetFrame()->GetLabel().c_str());
SwitchToFrame(newFrame);
return;
}
}
// entering into frames
for (Frame* kid : m_frame->GetChildren()) {
const vector3d pos = GetPositionRelTo(kid);
if (pos.Length() >= kid->GetRadius()) continue;
SwitchToFrame(kid);
Output("%s enters frame %s\n", GetLabel().c_str(), kid->GetLabel().c_str());
break;
}
}
示例15: DrawSpike
void Camera::DrawSpike(double rad, const vector3d &viewCoords, const matrix4x4d &viewTransform)
{
// draw twinkly star-thing on faraway objects
// XXX this seems like a good case for drawing in 2D - use projected position, then the
// "face the camera dammit" bits can be skipped
if (!m_renderer) return;
const double newdist = m_zNear + 0.5f * (m_zFar - m_zNear);
const double scale = newdist / viewCoords.Length();
matrix4x4d trans = matrix4x4d::Identity();
trans.Translate(scale*viewCoords.x, scale*viewCoords.y, scale*viewCoords.z);
// face the camera dammit
vector3d zaxis = viewCoords.Normalized();
vector3d xaxis = vector3d(0,1,0).Cross(zaxis).Normalized();
vector3d yaxis = zaxis.Cross(xaxis);
matrix4x4d rot = matrix4x4d::MakeInvRotMatrix(xaxis, yaxis, zaxis);
trans = trans * rot;
m_renderer->SetDepthTest(false);
m_renderer->SetBlendMode(BLEND_ALPHA_ONE);
// XXX this is supposed to pick a correct light colour for the object twinkle.
// Not quite correct, since it always uses the first light
GLfloat col[4];
glGetLightfv(GL_LIGHT0, GL_DIFFUSE, col);
col[3] = 1.f;
static VertexArray va(ATTRIB_POSITION | ATTRIB_DIFFUSE);
va.Clear();
const Color center(col[0], col[1], col[2], col[2]);
const Color edges(col[0], col[1], col[2], 0.f);
//center
va.Add(vector3f(0.f), center);
const float spikerad = float(scale*rad);
// bezier with (0,0,0) control points
{
const vector3f p0(0,spikerad,0), p1(spikerad,0,0);
float t=0.1f; for (int i=1; i<10; i++, t+= 0.1f) {
const vector3f p = (1-t)*(1-t)*p0 + t*t*p1;
va.Add(p, edges);
}
}
{
const vector3f p0(spikerad,0,0), p1(0,-spikerad,0);
float t=0.1f; for (int i=1; i<10; i++, t+= 0.1f) {
const vector3f p = (1-t)*(1-t)*p0 + t*t*p1;
va.Add(p, edges);
}
}
{
const vector3f p0(0,-spikerad,0), p1(-spikerad,0,0);
float t=0.1f; for (int i=1; i<10; i++, t+= 0.1f) {
const vector3f p = (1-t)*(1-t)*p0 + t*t*p1;
va.Add(p, edges);
}
}
{
const vector3f p0(-spikerad,0,0), p1(0,spikerad,0);
float t=0.1f; for (int i=1; i<10; i++, t+= 0.1f) {
const vector3f p = (1-t)*(1-t)*p0 + t*t*p1;
va.Add(p, edges);
}
}
glPushMatrix();
m_renderer->SetTransform(trans);
m_renderer->DrawTriangles(&va, Graphics::vtxColorMaterial, TRIANGLE_FAN);
m_renderer->SetBlendMode(BLEND_SOLID);
m_renderer->SetDepthTest(true);
glPopMatrix();
}