Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 204 lines (167 sloc) 6.8 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
#include "ObjectViewerView.h"
#include "WorldView.h"
#include "Pi.h"
#include "Frame.h"
#include "Player.h"
#include "Space.h"
#include "GeoSphere.h"
#include "terrain/Terrain.h"
#include "Planet.h"
#include "Light.h"
#include "graphics/Renderer.h"

#if WITH_OBJECTVIEWER

ObjectViewerView::ObjectViewerView(): View()
{
SetTransparency(true);
viewingDist = 1000.0f;
m_camRot = matrix4x4d::Identity();

m_infoLabel = new Gui::Label("");
Add(m_infoLabel, 2, Gui::Screen::GetHeight()-66-Gui::Screen::GetFontHeight());

m_vbox = new Gui::VBox();
Add(m_vbox, 580, 2);

m_vbox->PackEnd(new Gui::Label("Mass (earths):"));
m_sbodyMass = new Gui::TextEntry();
m_vbox->PackEnd(m_sbodyMass);

m_vbox->PackEnd(new Gui::Label("Radius (earths):"));
m_sbodyRadius = new Gui::TextEntry();
m_vbox->PackEnd(m_sbodyRadius);

m_vbox->PackEnd(new Gui::Label("Integer seed:"));
m_sbodySeed = new Gui::TextEntry();
m_vbox->PackEnd(m_sbodySeed);

m_vbox->PackEnd(new Gui::Label("Volatile gases (>= 0):"));
m_sbodyVolatileGas = new Gui::TextEntry();
m_vbox->PackEnd(m_sbodyVolatileGas);

m_vbox->PackEnd(new Gui::Label("Volatile liquid (0-1):"));
m_sbodyVolatileLiquid = new Gui::TextEntry();
m_vbox->PackEnd(m_sbodyVolatileLiquid);

m_vbox->PackEnd(new Gui::Label("Volatile ices (0-1):"));
m_sbodyVolatileIces = new Gui::TextEntry();
m_vbox->PackEnd(m_sbodyVolatileIces);

m_vbox->PackEnd(new Gui::Label("Life (0-1):"));
m_sbodyLife = new Gui::TextEntry();
m_vbox->PackEnd(m_sbodyLife);

m_vbox->PackEnd(new Gui::Label("Volcanicity (0-1):"));
m_sbodyVolcanicity = new Gui::TextEntry();
m_vbox->PackEnd(m_sbodyVolcanicity);

m_vbox->PackEnd(new Gui::Label("Crust metallicity (0-1):"));
m_sbodyMetallicity = new Gui::TextEntry();
m_vbox->PackEnd(m_sbodyMetallicity);

Gui::LabelButton *b = new Gui::LabelButton(new Gui::Label("Change planet terrain type"));
b->onClick.connect(sigc::mem_fun(this, &ObjectViewerView::OnChangeTerrain));
m_vbox->PackEnd(b);

Gui::HBox *hbox = new Gui::HBox();

b = new Gui::LabelButton(new Gui::Label("Prev Seed"));
b->onClick.connect(sigc::mem_fun(this, &ObjectViewerView::OnPrevSeed));
hbox->PackEnd(b);

b = new Gui::LabelButton(new Gui::Label("Random Seed"));
b->onClick.connect(sigc::mem_fun(this, &ObjectViewerView::OnRandomSeed));
hbox->PackEnd(b);

b = new Gui::LabelButton(new Gui::Label("Next Seed"));
b->onClick.connect(sigc::mem_fun(this, &ObjectViewerView::OnNextSeed));
hbox->PackEnd(b);

m_vbox->PackEnd(hbox);
}

void ObjectViewerView::Draw3D()
{
m_renderer->ClearScreen();
float znear, zfar;
m_renderer->GetNearFarRange(znear, zfar);
m_renderer->SetPerspectiveProjection(75.f, Pi::GetScrAspect(), znear, zfar);
m_renderer->SetTransform(matrix4x4f::Identity());

Light light;
light.SetType(Light::LIGHT_DIRECTIONAL);

if (Pi::MouseButtonState(SDL_BUTTON_RIGHT)) {
int m[2];
Pi::GetMouseMotion(m);
m_camRot = matrix4x4d::RotateXMatrix(-0.002*m[1]) *
matrix4x4d::RotateYMatrix(-0.002*m[0]) * m_camRot;
}

Body *body = Pi::player->GetNavTarget();
if (body) {
if (body->IsType(Object::STAR))
light.SetPosition(vector3f(0.f));
else {
light.SetPosition(vector3f(0.577f));
}
m_renderer->SetLights(1, &light);

body->Render(m_renderer, vector3d(0,0,-viewingDist), m_camRot);
}
}

void ObjectViewerView::OnSwitchTo()
{
m_camRot = matrix4x4d::Identity();
}

void ObjectViewerView::Update()
{
if (Pi::KeyState(SDLK_EQUALS)) viewingDist *= 0.99f;
if (Pi::KeyState(SDLK_MINUS)) viewingDist *= 1.01f;
viewingDist = Clamp(viewingDist, 10.0f, 1e12f);

char buf[128];
Body *body = Pi::player->GetNavTarget();
if(body && (body != lastTarget)) {
// Reset view distance for new target.
viewingDist = body->GetBoundingRadius() * 2.0f;
lastTarget = body;

if (body->IsType(Object::TERRAINBODY)) {
TerrainBody *tbody = static_cast<TerrainBody*>(body);
const SBody *sbody = tbody->GetSBody();
m_sbodyVolatileGas->SetText(stringf("%0{f.3}", sbody->m_volatileGas.ToFloat()));
m_sbodyVolatileLiquid->SetText(stringf("%0{f.3}", sbody->m_volatileLiquid.ToFloat()));
m_sbodyVolatileIces->SetText(stringf("%0{f.3}", sbody->m_volatileIces.ToFloat()));
m_sbodyLife->SetText(stringf("%0{f.3}", sbody->m_life.ToFloat()));
m_sbodyVolcanicity->SetText(stringf("%0{f.3}", sbody->m_volcanicity.ToFloat()));
m_sbodyMetallicity->SetText(stringf("%0{f.3}", sbody->m_metallicity.ToFloat()));
m_sbodySeed->SetText(stringf("%0{i}", int(sbody->seed)));
m_sbodyMass->SetText(stringf("%0{f}", sbody->mass.ToFloat()));
m_sbodyRadius->SetText(stringf("%0{f}", sbody->radius.ToFloat()));
}
}
snprintf(buf, sizeof(buf), "View dist: %s Object: %s", format_distance(viewingDist).c_str(), (body ? body->GetLabel().c_str() : "<none>"));
m_infoLabel->SetText(buf);

if (body->IsType(Object::TERRAINBODY)) m_vbox->ShowAll();
else m_vbox->HideAll();
}

void ObjectViewerView::OnChangeTerrain()
{
const fixed volatileGas = fixed(65536.0*atof(m_sbodyVolatileGas->GetText().c_str()), 65536);
const fixed volatileLiquid = fixed(65536.0*atof(m_sbodyVolatileLiquid->GetText().c_str()), 65536);
const fixed volatileIces = fixed(65536.0*atof(m_sbodyVolatileIces->GetText().c_str()), 65536);
const fixed life = fixed(65536.0*atof(m_sbodyLife->GetText().c_str()), 65536);
const fixed volcanicity = fixed(65536.0*atof(m_sbodyVolcanicity->GetText().c_str()), 65536);
const fixed metallicity = fixed(65536.0*atof(m_sbodyMetallicity->GetText().c_str()), 65536);
const fixed mass = fixed(65536.0*atof(m_sbodyMass->GetText().c_str()), 65536);
const fixed radius = fixed(65536.0*atof(m_sbodyRadius->GetText().c_str()), 65536);

// XXX this is horrendous, but probably safe for the moment. all bodies,
// terrain, whatever else holds a const pointer to the same toplevel
// sbody. one day objectviewer should be far more contained and not
// actually modify the space
Body *body = Pi::player->GetNavTarget();
SBody *sbody = const_cast<SBody*>(body->GetSBody());

sbody->seed = atoi(m_sbodySeed->GetText().c_str());
sbody->radius = radius;
sbody->mass = mass;
sbody->m_metallicity = metallicity;
sbody->m_volatileGas = volatileGas;
sbody->m_volatileLiquid = volatileLiquid;
sbody->m_volatileIces = volatileIces;
sbody->m_volcanicity = volcanicity;
sbody->m_life = life;

// force reload
static_cast<TerrainBody*>(body)->GetGeoSphere()->OnChangeDetailLevel();
}

void ObjectViewerView::OnRandomSeed()
{
m_sbodySeed->SetText(stringf("%0{i}", int(Pi::rng.Int32())));
OnChangeTerrain();
}

void ObjectViewerView::OnNextSeed()
{
m_sbodySeed->SetText(stringf("%0{i}", atoi(m_sbodySeed->GetText().c_str()) + 1));
OnChangeTerrain();
}

void ObjectViewerView::OnPrevSeed()
{
m_sbodySeed->SetText(stringf("%0{i}", atoi(m_sbodySeed->GetText().c_str()) - 1));
OnChangeTerrain();
}

#endif
Something went wrong with that request. Please try again.