-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlight simulator.cpp
174 lines (144 loc) · 5.07 KB
/
Flight simulator.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
// Flight simulator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// This program is a trivial little flight simulator. You control a ship
// with the keyboard. Use
//
// J and L keys to roll,
// I and K keys to pitch,
// H and ; keys to yaw,
// 8 key to increase speed and the M key to decrease speed.
// W key toggles wireframe mode
// R key generates a new landscape
//
// In this little program you fly around a single fractal landscape. It would
// be best to extend the program so that one could plug in any arbitrary
// scene.
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <glut.h>
#endif
#include "cockpit.h"
#include "landscape.h"
#include "rfHillTerrain.h"
#include <iostream>
// A landscape to fly around, with some parameters that are manipuated by the
// program.
using namespace RobotFrog;
Landscape landscape(400, 243);
// Wireframe view or solid view?
static bool wireframe = false;
void newLandscape1() {
HillTerrain terrain; // you can also specify terrain params here
terrain.SetSeed(12345);
std::cout<<"x"<<terrain.GetHillMin();
terrain.SetHillMin(10);
terrain.SetHillMax(30);
terrain.SetHillSize(10,15);
terrain.SetNumHills(5);
terrain.SetIsland(1);
terrain.Generate();
}
void newLandscape() {
static double rug = ((double)rand()) / RAND_MAX;
landscape.create(rug);
}
// A ship and some functions to control it: Later, we need to add a ship
// controller class so even the navigation controls are pluggable.
static Ship theShip(Point(60, 40, 220));
static Cockpit cockpit(theShip);
void keyboard(unsigned char key, int, int) {
const double deltaSpeed = 0.01;
const double angle = 0.02;
switch(key) {
case 'i': theShip.setSpeed(theShip.getSpeed() + 3*deltaSpeed); break;
case 'k': theShip.setSpeed(theShip.getSpeed() - 3*deltaSpeed); break;
case 't': wireframe = !wireframe; break;
case 'r': newLandscape();
case 'a': theShip.roll(angle); break;
case 'd': theShip.roll(-angle); break;
case 'j': theShip.yaw(angle); break;
case 'l': theShip.yaw(-angle); break;
case 'w': theShip.pitch(-angle); break;
case 's': theShip.pitch(angle); break;
}
}
// Display and Animation: To draw we just clear the window and draw the scene.
// Because our main window is double buffered we have to swap the buffers to
// make the drawing visible. Animation is achieved by successively moving
// the ship and drawing.
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
wireframe ? landscape.drawWireFrame() : landscape.draw();
cockpit.draw();
glFlush();
glutSwapBuffers();
}
// Move the ship one step, recompute the view, and ask to redisplay.
void timer(int v) {
theShip.fly();
Point eye(theShip.getPosition());
Point at(theShip.getPosition() + theShip.getDirection());
Vector up(theShip.getVertical());
glLoadIdentity();
gluLookAt(eye.x, eye.y, eye.z, at.x, at.y, at.z, up.i, up.j, up.k);
glutPostRedisplay();
glutTimerFunc(1000/60, timer, v);
}
// Reshape callback: Make the viewport take up the whole window, recompute the
// camera settings to match the new window shape, and go back to modelview
// matrix mode.
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.05, 300.0);
glMatrixMode(GL_MODELVIEW);
}
// init(): Initialize GLUT and enter the GLUT event loop.
void init() {
srand(9903);
glEnable(GL_DEPTH_TEST);
newLandscape();
newLandscape1();
cockpit.create();
GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat dark[] = { 0.2, 0.15, 0.2, 1.0 };
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat direction[] = { 0.2, 1.0, 0.5, 0.0 };
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialf(GL_FRONT, GL_SHININESS, 30);
glLightfv(GL_LIGHT0, GL_AMBIENT, dark);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_SPECULAR, white);
glLightfv(GL_LIGHT0, GL_POSITION, direction);
glEnable(GL_LIGHTING); // so the renderer considers light
glEnable(GL_LIGHT0); // turn LIGHT0 on
}
// Writes some trivial help text to the console.
void writeHelpToConsole() {
std::cout << "a/d = roll left / right\n";
std::cout << "w/s - pitch down / up\n";
std::cout << "j/l - yaw left / right\n";
std::cout << "i/k - speed up / slow down\n";
std::cout << "t - toggle wireframe mode\n";
std::cout << "r - generate a new landscape\n";
}
// main(): Initialize GLUT and enter the GLUT event loop.
int main(int argc, char** argv) {
writeHelpToConsole();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(80, 80);
glutInitWindowSize(780, 500);
glutCreateWindow("Simple Flight");
glutReshapeFunc(reshape);
glutTimerFunc(100, timer, 0);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
newLandscape1();
init();
std::cout<<"X";
glutMainLoop();
}