Added code to load galaxies from a file.

This commit is contained in:
Chris Laurel 2001-04-20 04:23:22 +00:00
parent aee5979262
commit 2e9f479558
2 changed files with 189 additions and 27 deletions

View file

@ -7,18 +7,52 @@
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <algorithm>
#include "celestia.h"
#include "mathlib.h"
#include "util.h"
#include "astro.h"
#include "galaxy.h"
#include "perlin.h"
#include "parser.h"
using namespace std;
static bool formsInitialized = false;
static vector<Point3f>* spiralPoints = NULL;
static vector<Point3f>* ellipticalPoints = NULL;
static vector<Point3f>* irregularPoints = NULL;
static GalacticForm* spiralForm = NULL;
static GalacticForm* ellipticalForm = NULL;
static GalacticForm* irregularForm = NULL;
static GalacticForm** ellipticalForms = NULL;
static void InitializeForms();
struct GalaxyTypeName
{
char* name;
Galaxy::GalaxyType type;
};
static GalaxyTypeName GalaxyTypeNames[] =
{
{ "S0", Galaxy::S0 },
{ "Sa", Galaxy::Sa },
{ "Sb", Galaxy::Sb },
{ "Sc", Galaxy::Sc },
{ "SBa", Galaxy::SBa },
{ "SBb", Galaxy::SBb },
{ "SBc", Galaxy::SBc },
{ "E0", Galaxy::E0 },
{ "E1", Galaxy::E1 },
{ "E2", Galaxy::E2 },
{ "E3", Galaxy::E3 },
{ "E4", Galaxy::E4 },
{ "E5", Galaxy::E5 },
{ "E6", Galaxy::E6 },
{ "E7", Galaxy::E7 },
{ "Irr", Galaxy::Irr },
};
Galaxy::Galaxy() :
name(""),
@ -93,8 +127,15 @@ void Galaxy::setType(Galaxy::GalaxyType _type)
case Irr:
form = irregularForm;
break;
default:
form = ellipticalForm;
case E0:
case E1:
case E2:
case E3:
case E4:
case E5:
case E6:
case E7:
form = ellipticalForms[type - E0];
break;
}
}
@ -112,8 +153,8 @@ void InitializeForms()
int i;
// Initialize the spiral form--this is a big hack
spiralForm = new GalacticForm();
spiralForm->reserve(galaxySize);
spiralPoints = new vector<Point3f>();
spiralPoints->reserve(galaxySize);
for (i = 0; i < galaxySize; i++)
{
float r = Mathf::frand();
@ -129,26 +170,140 @@ void InitializeForms()
float x = r * (float) cos(theta);
float z = r * (float) sin(theta);
float y = Mathf::sfrand() * 0.1f * 1.0f / (1 + 2 * r);
spiralForm->insert(spiralForm->end(), Point3f(x, y, z));
spiralPoints->insert(spiralPoints->end(), Point3f(x, y, z));
}
spiralForm = new GalacticForm();
spiralForm->points = spiralPoints;
spiralForm->scale = Vec3f(1, 1, 1);
irregularForm = new GalacticForm();
irregularForm->reserve(galaxySize);
irregularPoints = new vector<Point3f>;
irregularPoints->reserve(galaxySize);
i = 0;
while (i < galaxySize)
{
Point3f p(Mathf::sfrand(), Mathf::sfrand(), Mathf::sfrand());
float r = p.distanceTo(Point3f(0, 0, 0));
float r = p.distanceFromOrigin();
if (r < 1)
{
float prob = (1 - r) * (fractalsum(Point3f(p.x + 5, p.y + 5, p.z + 5), 8) + 1) * 0.5f;
if (Mathf::frand() < prob)
{
irregularForm->insert(irregularForm->end(), p);
irregularPoints->insert(irregularPoints->end(), p);
i++;
}
}
}
irregularForm = new GalacticForm();
irregularForm->points = irregularPoints;
irregularForm->scale = Vec3f(1, 1, 1);
ellipticalPoints = new vector<Point3f>();
ellipticalPoints->reserve(galaxySize);
i = 0;
while (i < galaxySize)
{
Point3f p(Mathf::sfrand(), Mathf::sfrand(), Mathf::sfrand());
float r = p.distanceFromOrigin();
if (r < 1)
{
if (Mathf::frand() < cube(1 - r))
{
ellipticalPoints->insert(ellipticalPoints->end(), p);
i++;
}
}
}
ellipticalForms = new GalacticForm*[8];
for (int eform = 0; eform <= 7; eform++)
{
ellipticalForms[eform] = new GalacticForm();
ellipticalForms[eform]->points = ellipticalPoints;
ellipticalForms[eform]->scale = Vec3f(1.0f, 1.0f - (float) eform / 8.0f, 1.0f);
}
formsInitialized = true;
}
GalaxyList* ReadGalaxyList(istream& in)
{
GalaxyList* galaxies = new GalaxyList();
Tokenizer tokenizer(&in);
Parser parser(&tokenizer);
while (tokenizer.nextToken() != Tokenizer::TokenEnd)
{
if (tokenizer.getTokenType() != Tokenizer::TokenString)
{
DPRINTF("Error parsing galaxies file.\n");
for_each(galaxies->begin(), galaxies->end(), deleteFunc<Galaxy*>());
delete galaxies;
return NULL;
}
Galaxy* galaxy = new Galaxy();
galaxy->setName(tokenizer.getStringValue());
Value* galaxyParamsValue = parser.readValue();
if (galaxyParamsValue == NULL || galaxyParamsValue->getType() != Value::HashType)
{
DPRINTF("Error parsing galaxy entry %s\n", galaxy->getName().c_str());
for_each(galaxies->begin(), galaxies->end(), deleteFunc<Galaxy*>());
delete galaxy;
return NULL;
}
Hash* galaxyParams = galaxyParamsValue->getHash();
// Get position
Vec3d position(0.0, 0.0, 0.0);
if (galaxyParams->getVector("Position", position))
{
galaxy->setPosition(Point3d(position.x, position.y, position.z));
}
else
{
double distance = 1.0;
double RA = 0.0;
double dec = 0.0;
galaxyParams->getNumber("Distance", distance);
galaxyParams->getNumber("RA", RA);
galaxyParams->getNumber("Dec", dec);
Point3f p = astro::equatorialToCelestialCart(RA, dec, distance);
galaxy->setPosition(Point3d(p.x, p.y, p.z));
cout << galaxy->getName() << ": " << p.x << ", " << p.y << ", " << p.z << '\n';
}
// Get orientation
Vec3d axis(1.0, 0.0, 0.0);
double angle = 0.0;
galaxyParams->getVector("Axis", axis);
galaxyParams->getNumber("Angle", angle);
Quatf q(1);
q.setAxisAngle(Vec3f((float) axis.x, (float) axis.y, (float) axis.z),
(float) angle);
galaxy->setOrientation(q);
double radius = 0.0;
galaxyParams->getNumber("Radius", radius);
galaxy->setRadius((float) radius);
string typeName;
galaxyParams->getString("Type", typeName);
Galaxy::GalaxyType type = Galaxy::Irr;
for (int i = 0; i < sizeof(GalaxyTypeNames) / sizeof(GalaxyTypeNames[0]); i++)
{
if (GalaxyTypeNames[i].name == typeName)
{
type = GalaxyTypeNames[i].type;
break;
}
}
galaxy->setType(type);
galaxies->insert(galaxies->end(), galaxy);
}
return galaxies;
}

View file

@ -12,32 +12,37 @@
#include <vector>
#include <string>
#include <iostream>
#include "vecmath.h"
#include "quaternion.h"
typedef std::vector<Point3f> GalacticForm;
struct GalacticForm
{
std::vector<Point3f>* points;
Vec3f scale;
};
class Galaxy
{
public:
enum GalaxyType {
S0,
Sa,
Sb,
Sc,
SBa,
SBb,
SBc,
E0,
E1,
E2,
E3,
E4,
E5,
E6,
E7,
Irr
S0 = 0,
Sa = 1,
Sb = 2,
Sc = 3,
SBa = 4,
SBb = 5,
SBc = 6,
E0 = 7,
E1 = 8,
E2 = 9,
E3 = 10,
E4 = 11,
E5 = 12,
E6 = 13,
E7 = 14,
Irr = 15,
};
public:
@ -68,4 +73,6 @@ class Galaxy
typedef std::vector<Galaxy*> GalaxyList;
GalaxyList* ReadGalaxyList(std::istream& in);
#endif // _GALAXY_H_