// // Update the position of the moon image in the sky // void CMoonImage::Reposition (sgVec3 p, double theta, double lst, double lat, double ra, double dec, double spin) { sgMat4 LST, LAT, RA, DEC, D, SCALE, ECLIPTIC, SPIN; sgVec3 axis; sgVec3 v; // Create scaling matrix for moon illusion (appears larger near horizon) float scale = 1.0f; sgMakeIdentMat4 (SCALE); float maxMagnification = 0.5f; float minThreshold = DegToRad (80.0f); float maxThreshold = DegToRad (95.0f); float span = maxThreshold - minThreshold; if ((theta >= minThreshold) && (theta <= maxThreshold)) { sgMat4 I; sgMakeIdentMat4 (I); scale = 1.0f + (maxMagnification * (theta - minThreshold) / span); sgScaleMat4 (SCALE, I, scale); } // Rotation matrix for latitude sgSetVec3 (axis, -1.0f, 0, 0); sgMakeRotMat4 (LAT, 90.0f-(float)lat, axis); // Rotation matrix for local sidereal time, converted from h to deg sgSetVec3 (axis, 0, 0, -1.0f); sgMakeRotMat4 (LST, ((float)lst * 15), axis); // Rotation matrix for right ascension sgSetVec3 (axis, 0, 0, 1); sgMakeRotMat4 (RA, RadToDeg ((float)ra), axis); // Rotation matrix for declination sgSetVec3 (axis, 1, 0, 0); sgMakeRotMat4 (DEC, 90.0f - RadToDeg ((float)dec), axis); // Translate moon distance sgSetVec3 (v, 0, 0, distance); sgMakeTransMat4 (D, v); // Rotate to align moon equator with ecliptic sgSetVec3 (axis, 1.0f, 0, 0); sgMakeRotMat4 (ECLIPTIC, 90.0f, axis); /// Rotate the moon image accurately towards the sun position sgSetVec3 (axis, 0, 0, 1); sgMakeRotMat4 (SPIN, spin, axis); // Combine all transforms sgMakeIdentMat4 (T); sgPreMultMat4 (T, LAT); sgPreMultMat4 (T, LST); sgPreMultMat4 (T, RA); sgPreMultMat4 (T, DEC); sgPreMultMat4 (T, D); sgPreMultMat4 (T, ECLIPTIC); sgPreMultMat4 (T, SPIN); }
float getHOT(ssgRoot *root, float x, float y) { sgVec3 test_vec; sgMat4 invmat; sgMakeIdentMat4(invmat); invmat[3][0] = -x; invmat[3][1] = -y; invmat[3][2] = 0.0f ; test_vec [0] = 0.0f; test_vec [1] = 0.0f; test_vec [2] = 100000.0f; ssgHit *results; int num_hits = ssgHOT (root, test_vec, invmat, &results); float hot = -1000000.0f; for (int i = 0; i < num_hits; i++) { ssgHit *h = &results[i]; float hgt = - h->plane[3] / h->plane[2]; if (hgt >= hot) hot = hgt; } return hot; }
// TODO: more efficient solution, this one is slow. float grGetHOT(float x, float y) { sgVec3 test_vec; sgMat4 invmat; sgMakeIdentMat4(invmat); invmat[3][0] = -x; invmat[3][1] = -y; invmat[3][2] = 0.0f ; test_vec [0] = 0; test_vec [1] = 0; test_vec [2] = 100000.0f; ssgHit *results; int num_hits = ssgHOT (TheScene, test_vec, invmat, &results); float hot = -1000000.0f; for (int i = 0; i < num_hits; i++) { ssgHit *h = &results[i]; float hgt = (h->plane[2] == 0.0 ? 0.0 : - h->plane[3] / h->plane[2]); if (hgt >= hot) { hot = hgt; } } return hot; }
void save_database(void) { int i; tFace *curFace; sgMat4 m; ssgBranch *b = new ssgBranch(); sgMakeIdentMat4(m); for (i = 0; i < NbRows; i++) { curFace = GF_TAILQ_FIRST(&(Row[i].faces)); while (curFace) { if (curFace->isPresent) { curFace->branch->setTransform(m); ssgFlatten(curFace->branch); ssgStripify(curFace->branch); } curFace = GF_TAILQ_NEXT(curFace, link); } } b->addKid(Root); ssgFlatten(Root); ssgStripify(Root); myssgSaveAC(OutputFileName, b, SkinFileName); }
/** \brief Draw the airplane * * This method actually does not draw anything. It only * updates the aircraft's transformation node in the * scenegraph with the aircraft's current position and * orientation. The actual drawing is done by the global * ssgCullAndDraw() call. * * \param airplane pointer to the airplane's FDM object */ void CRRCAirplaneLaRCSimSSG::draw(FDMBase* airplane) { CRRCMath::Vector3 pos = airplane->getPos(); sgMat4 m; sgMakeIdentMat4(m); m[3][0] = pos.r[1]; m[3][1] = -1 * pos.r[2]; m[3][2] = -1 * pos.r[0]; makeOGLRotMat4(m, airplane->getPhi(), airplane->getTheta(), airplane->getPsi()); model_trans->setTransform(m); }
HD_TilingTerrain::HD_TilingTerrain(ssgRoot * SceneGraph) { for ( int i = 0 ; i <= SIZE_GRID_PLANES; i++ ) { for ( int j = 0 ; j <= SIZE_GRID_PLANES; j++ ) { tile_table[i][j] = new ssgVertexArray(); } } sgMat4 xform; sgMakeIdentMat4(xform); tiling_terrain(SceneGraph,xform); }
/** * Create a CRRCControlSurfaceAnimation object * * Initialize the animation from an * <animation type="ControlSurface"> tag */ CRRCControlSurfaceAnimation::CRRCControlSurfaceAnimation(SimpleXMLTransfer *xml) : CRRCAnimation(new ssgTransform()), fallback_data(0.0f), eventAdapter(this, &CRRCControlSurfaceAnimation::axisValueCallback, Event::Input), aileron(0.0f), elevator(0.0f), rudder(0.0f), throttle(0.0f), spoiler(0.0f), flap(0.0f), retract(0.0f), pitch(0.0f) { bool failed = false; // evaluate <object> tag SimpleXMLTransfer *map = xml->getChild("object", true); symbolic_name = map->getString("name", "no_name_set"); max_angle = (float)(map->getDouble("max_angle", 0.0) * SG_RADIANS_TO_DEGREES); abs_max_angle = (float)fabs((double)max_angle); // find hinges and evaluate all <control> tags int num_controls = 0; int num_hinges = 0; for (int i = 0; i < xml->getChildCount(); i++) { SimpleXMLTransfer *child = xml->getChildAt(i); if (child->getName() == "hinge") { // found a <hinge> child sgVec3 pos; pos[SG_X] = (float)(-1 * child->getDouble("y", 0.0)); pos[SG_Y] = (float)(-1 * child->getDouble("x", 0.0)); pos[SG_Z] = (float)(-1 * child->getDouble("z", 0.0)); if (num_hinges == 0) { sgCopyVec3(hinge_1, pos); } else if (num_hinges == 1) { sgCopyVec3(hinge_2, pos); } num_hinges++; } else if (child->getName() == "control") { // found a <control> child // The "*2" factor for each gain value scales the control input // values from -0.5...+0.5 to -1.0...+1.0. This saves one // float multiplication per mapping in the runtime update() routine. std::string mapping = child->getString("mapping", "NOTHING"); float gain = (float)child->getDouble("gain", 1.0); if (mapping == "ELEVATOR") { datasource.push_back(&elevator); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "AILERON") { datasource.push_back(&aileron); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "THROTTLE") { datasource.push_back(&throttle); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "RUDDER") { datasource.push_back(&rudder); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "FLAP") { datasource.push_back(&flap); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "SPOILER") { datasource.push_back(&spoiler); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "RETRACT") { datasource.push_back(&retract); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "PITCH") { datasource.push_back(&pitch); source_gain.push_back(gain * 2); num_controls++; } else { std::cerr << "ControlSurfaceAnimation: ignoring <control> tag without mapping." << std::endl; } } } if (num_controls < 1) { std::cerr << "ControlSurfaceAnimation: found animation without proper <control> tag. Animation disabled." << std::endl; failed = true; } if (num_hinges < 2) { std::cerr << "ControlSurfaceAnimation: Must specify exactly two hinges!" << std::endl; failed = true; } else { if (num_hinges > 2) { std::cerr << "ControlSurfaceAnimation: Must specify exactly two hinges!" << std::endl; std::cerr << "ControlSurfaceAnimation: Ignoring excessive hinge tag(s)." << std::endl; } sgSubVec3(axis, hinge_2, hinge_1); if (sgLengthVec3(axis) < 0.001) { std::cerr << "ControlSurfaceAnimation: Insufficient spacing between hinges!" << std::endl; failed = true; } } if (failed) { std::cerr << "ControlSurfaceAnimation: Animation setup failed." << std::endl; // set to some non-critical defaults datasource.resize(1); datasource[0] = &fallback_data; source_gain.resize(1); source_gain[0] = 1.0; sgSetVec3(hinge_1, 0.0f, 0.0f, 0.0f); sgSetVec3(hinge_2, 1.0f, 0.0f, 0.0f); sgSubVec3(axis, hinge_2, hinge_1); } sgMakeIdentMat4(move_to_origin); move_to_origin[3][0] = -hinge_1[0]; move_to_origin[3][1] = -hinge_1[1]; move_to_origin[3][2] = -hinge_1[2]; sgMakeTransMat4(move_back, hinge_1); realInit(); }
void calc_coord(void) { sgMat4 m, m2; int i, j; tFace *curFace; float width, height; float scale, offX, offY; int largerRow; float maxWidth; int col; ssgBranch *branch; ColWidth = (float*)calloc(NbMaxCols, sizeof(float)); fprintf(stderr, "After Rotation:\n"); largerRow = 0; maxWidth = 0; for (i = 0; i < NbRows; i++) { curFace = GF_TAILQ_FIRST(&(Row[i].faces)); while (curFace) { if (curFace->isPresent) { branch = curFace->branch->getParent(0); if (branch->isAKindOf(_SSG_TYPE_BASETRANSFORM)) { ((ssgBaseTransform*)branch)->getTransform(m2); curFace->align[0] *= m2[3][0]; curFace->align[1] *= m2[3][1]; curFace->align[2] *= m2[3][2]; fprintf(stderr, "Align face %s : %f %f %f\n", curFace->faceName, curFace->align[0], curFace->align[1], curFace->align[2]); } sgMakeTransMat4(m, curFace->align); sgMakeIdentMat4(m2); for (j = 0; j < 3; j++) { m2[j][j] = curFace->lscale[j]; } sgPostMultMat4(m, m2); sgMakeRotMat4(m2, curFace->xform.hpr); sgPostMultMat4(m, m2); sgCopyMat4(curFace->mat, m); sgXformPnt3(curFace->sbbmin, curFace->lbbmin, m); sgXformPnt3(curFace->sbbmax, curFace->lbbmax, m); fprintf(stderr, " Face %s : %f %f %f --- %f %f %f\n", curFace->faceName, curFace->sbbmin[0], curFace->sbbmin[1], curFace->sbbmin[2], curFace->sbbmax[0], curFace->sbbmax[1], curFace->sbbmax[2]); curFace->lwidth = 2.0 * MAX(fabs(curFace->sbbmin[0]), fabs(curFace->sbbmax[0])); curFace->lheight = 2.0 * MAX(fabs(curFace->sbbmin[2]), fabs(curFace->sbbmax[2])); //curFace->lwidth = fabs(curFace->sbbmin[0] - curFace->sbbmax[0]); //curFace->lheight = fabs(curFace->sbbmin[2] - curFace->sbbmax[2]); Row[i].lwidth += curFace->lwidth; Row[i].lheight = MAX(Row[i].lheight, curFace->lheight); } curFace = GF_TAILQ_NEXT(curFace, link); } if (Row[i].lwidth > maxWidth) { maxWidth = Row[i].lwidth; largerRow = i; } } height = 0; fprintf(stderr, "After Scaling:\n"); for (i = 0; i < NbRows; i++) { height += Row[i].lheight; } width = maxWidth; scale = (float)ImgSize / MAX(width, height); curFace = GF_TAILQ_FIRST(&(Row[largerRow].faces)); i = 0; fprintf(stderr, "Columns : "); while (curFace) { ColWidth[i] = curFace->lwidth * scale; fprintf(stderr, "%.2f ", ColWidth[i]); curFace = GF_TAILQ_NEXT(curFace, link); i++; } fprintf(stderr, "\n"); fprintf(stderr, "Total Width = %.2f Height = %.2f\n", width, height); fprintf(stderr, "Image Width = %.2f Height = %.2f\n", width*scale, height*scale); offY = - (float)ImgSize / 2.0; for (i = 0; i < NbRows; i++) { curFace = GF_TAILQ_FIRST(&(Row[i].faces)); offY += Row[i].lheight*scale / 2.0; col = 0; offX = - (float)ImgSize / 2.0; while (curFace) { if (curFace->isPresent) { sgCopyMat4(m, curFace->mat); curFace->texScale = scale; sgMakeIdentMat4(m2); for (j = 0; j < 3; j++) { m2[j][j] = scale; } sgPostMultMat4(m, m2); sgXformPnt3(curFace->sbbmin, curFace->lbbmin, m); sgXformPnt3(curFace->sbbmax, curFace->lbbmax, m); offX += ColWidth[col] / 2.0; curFace->offset[0] = offX; curFace->offset[2] = offY; offX += ColWidth[col] / 2.0; sgMakeTransMat4(m2, curFace->offset); sgPostMultMat4(m, m2); sgCopyMat4(curFace->mat, m); curFace->branch->setTransform(m); } else { offX += ColWidth[col]; } col++; curFace = GF_TAILQ_NEXT(curFace, link); } offY += Row[i].lheight*scale / 2.0; } }
// // Update the position of the moon image in the sky // void CMoonImage::Reposition (sgVec3 p, double theta, double lst, double lat, double ra, double dec, double spin) { sgMat4 LST, LAT, RA, DEC, D, SCALE, ECLIPTIC, SPIN; sgVec3 axis; sgVec3 v; // Create scaling matrix for moon illusion (appears larger near horizon) float scale = 1.0f; sgMakeIdentMat4 (SCALE); float maxMagnification = 0.5f; float minThreshold = DegToRad (80.0f); float maxThreshold = DegToRad (95.0f); float span = maxThreshold - minThreshold; if ((theta >= minThreshold) && (theta <= maxThreshold)) { sgMat4 I; sgMakeIdentMat4 (I); scale = 1.0f + (maxMagnification * (theta - minThreshold) / span); sgScaleMat4 (SCALE, I, scale); } // Rotation matrix for latitude sgSetVec3 (axis, -1.0f, 0, 0); sgMakeRotMat4 (LAT, 90.0f-(float)lat, axis); // Rotation matrix for local sidereal time, converted from h to deg sgSetVec3 (axis, 0, 0, -1.0f); sgMakeRotMat4 (LST, ((float)lst * 15), axis); // Rotation matrix for right ascension sgSetVec3 (axis, 0, 0, 1); sgMakeRotMat4 (RA, RadToDeg ((float)ra), axis); // Rotation matrix for declination sgSetVec3 (axis, 1, 0, 0); sgMakeRotMat4 (DEC, 90.0f - RadToDeg ((float)dec), axis); // Translate moon distance sgSetVec3 (v, 0, 0, distance); sgMakeTransMat4 (D, v); // Rotate to align moon equator with ecliptic sgSetVec3 (axis, 1.0f, 0, 0); sgMakeRotMat4 (ECLIPTIC, 90.0f, axis); /// Rotate the moon image accurately towards the sun position sgSetVec3 (axis, 0, 0, 1); sgMakeRotMat4 (SPIN, spin, axis); // Combine all transforms sgMakeIdentMat4 (T); sgPreMultMat4 (T, LAT); sgPreMultMat4 (T, LST); sgPreMultMat4 (T, RA); sgPreMultMat4 (T, DEC); sgPreMultMat4 (T, D); sgPreMultMat4 (T, ECLIPTIC); sgPreMultMat4 (T, SPIN); /* char debug[256]; double jd = CTimeManager::Instance().GetJulianDate(); SDateTime dt = CTimeManager::Instance().GetLocalDateTime (); sprintf (debug, "JD=%f D=%d/%d/%d T=%d:%d RA=%f Dec=%f", jd, dt.date.year, dt.date.month, dt.date.day, dt.time.hour, dt.time.minute, RadToDeg(ra), RadToDeg(dec)); DrawNoticeToUser (debug, 1); */ }