forked from pioneerspacesim/pioneer
/
ObjectViewerView.cpp
175 lines (147 loc) · 5.72 KB
/
ObjectViewerView.cpp
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
#include "ObjectViewerView.h"
#include "WorldView.h"
#include "Pi.h"
#include "Frame.h"
#include "Player.h"
#include "Space.h"
#include "GeoSphere.h"
#include "GeoSphereStyle.h"
#include "Planet.h"
#if 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());
Gui::VBox *vbox = new Gui::VBox();
Add(vbox, 580, 2);
vbox->PackEnd(new Gui::Label("Mass (earths):"));
m_sbodyMass = new Gui::TextEntry();
vbox->PackEnd(m_sbodyMass);
vbox->PackEnd(new Gui::Label("Radius (earths):"));
m_sbodyRadius = new Gui::TextEntry();
vbox->PackEnd(m_sbodyRadius);
vbox->PackEnd(new Gui::Label("Integer seed:"));
m_sbodySeed = new Gui::TextEntry();
vbox->PackEnd(m_sbodySeed);
vbox->PackEnd(new Gui::Label("Volatile gases (>= 0):"));
m_sbodyVolatileGas = new Gui::TextEntry();
vbox->PackEnd(m_sbodyVolatileGas);
vbox->PackEnd(new Gui::Label("Volatile liquid (0-1):"));
m_sbodyVolatileLiquid = new Gui::TextEntry();
vbox->PackEnd(m_sbodyVolatileLiquid);
vbox->PackEnd(new Gui::Label("Volatile ices (0-1):"));
m_sbodyVolatileIces = new Gui::TextEntry();
vbox->PackEnd(m_sbodyVolatileIces);
vbox->PackEnd(new Gui::Label("Life (0-1):"));
m_sbodyLife = new Gui::TextEntry();
vbox->PackEnd(m_sbodyLife);
vbox->PackEnd(new Gui::Label("Volcanicity (0-1):"));
m_sbodyVolcanicity = new Gui::TextEntry();
vbox->PackEnd(m_sbodyVolcanicity);
vbox->PackEnd(new Gui::Label("Crust metallicity (0-1):"));
m_sbodyMetallicity = new Gui::TextEntry();
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::OnChangeGeoSphereStyle));
vbox->PackEnd(b);
}
void ObjectViewerView::Draw3D()
{
static float rot;
rot += 0.1;
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float znear, zfar;
Pi::worldView->GetNearFarClipPlane(&znear, &zfar);
float fracH = znear / Pi::GetScrAspect();
glFrustum(-znear, znear, -fracH, fracH, znear, zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_LIGHT0);
Render::State::SetZnearZfar(znear, zfar);
if (Pi::MouseButtonState(3)) {
int m[2];
Pi::GetMouseMotion(m);
m_camRot = matrix4x4d::RotateXMatrix(-0.002*m[1]) *
matrix4x4d::RotateYMatrix(-0.002*m[0]) * m_camRot;
}
float lightPos[4] = { .577, .577, .577, 0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
Body *body = Pi::player->GetNavTarget();
if (body) {
body->Render(vector3d(0,0,-viewingDist), m_camRot);
}
}
void ObjectViewerView::OnSwitchTo()
{
m_camRot = matrix4x4d::Identity();
}
void ObjectViewerView::Update()
{
if (Pi::KeyState(SDLK_EQUALS)) viewingDist *= 0.99;
if (Pi::KeyState(SDLK_MINUS)) viewingDist *= 1.01;
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::PLANET)) {
Planet *planet = static_cast<Planet*>(body);
const SBody *sbody = planet->GetSBody();
m_sbodyVolatileGas->SetText(stringf(64, "%.3f", sbody->m_volatileGas.ToFloat()));
m_sbodyVolatileLiquid->SetText(stringf(64, "%.3f", sbody->m_volatileLiquid.ToFloat()));
m_sbodyVolatileIces->SetText(stringf(64, "%.3f", sbody->m_volatileIces.ToFloat()));
m_sbodyLife->SetText(stringf(64, "%.3f", sbody->m_life.ToFloat()));
m_sbodyVolcanicity->SetText(stringf(64, "%.3f", sbody->m_volcanicity.ToFloat()));
m_sbodyMetallicity->SetText(stringf(64, "%.3f", sbody->m_metallicity.ToFloat()));
m_sbodySeed->SetText(stringf(64, "%d", sbody->seed));
m_sbodyMass->SetText(stringf(64, "%f", sbody->mass.ToFloat()));
m_sbodyRadius->SetText(stringf(64, "%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);
}
void ObjectViewerView::OnChangeGeoSphereStyle()
{
SBody sbody;
const fixed volatileGas = fixed((int)65536.0*atof(m_sbodyVolatileGas->GetText().c_str()), 65536);
const fixed volatileLiquid = fixed((int)65536.0*atof(m_sbodyVolatileLiquid->GetText().c_str()), 65536);
const fixed volatileIces = fixed((int)65536.0*atof(m_sbodyVolatileIces->GetText().c_str()), 65536);
const fixed life = fixed((int)65536.0*atof(m_sbodyLife->GetText().c_str()), 65536);
const fixed volcanicity = fixed((int)65536.0*atof(m_sbodyVolcanicity->GetText().c_str()), 65536);
const fixed metallicity = fixed((int)65536.0*atof(m_sbodyMetallicity->GetText().c_str()), 65536);
const fixed mass = fixed((int)65536.0*atof(m_sbodyMass->GetText().c_str()), 65536);
const fixed radius = fixed((int)65536.0*atof(m_sbodyRadius->GetText().c_str()), 65536);
sbody.parent = 0;
sbody.name = "Test";
/* These should be the only SBody attributes GeoSphereStyle uses */
sbody.type = SBody::TYPE_PLANET_TERRESTRIAL;
sbody.seed = atoi(m_sbodySeed->GetText().c_str());
sbody.radius = radius;
sbody.mass = mass;
sbody.averageTemp = 273;
sbody.m_metallicity = metallicity;
sbody.m_volatileGas = volatileGas;
sbody.m_volatileLiquid = volatileLiquid;
sbody.m_volatileIces = volatileIces;
sbody.m_volcanicity = volcanicity;
sbody.m_life = life;
sbody.heightMapFilename = 0;
Body *body = Pi::player->GetNavTarget();
if (body->IsType(Object::PLANET)) {
Planet *planet = static_cast<Planet*>(body);
GeoSphere *gs = planet->m_geosphere;
gs->m_style = GeoSphereStyle(&sbody);
// force rebuild
gs->OnChangeDetailLevel();
}
}
#endif