This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
/
playground
executable file
·204 lines (144 loc) · 2.94 KB
/
playground
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
#!/usr/bin/env ruby -I lib -w
require 'environment'
class SpaceWars < Controller
on "w" do
spaceship.size *= 1.1
end
on "s" do
spaceship.size *= 0.9
end
on "a" do
spaceship.turn_left
end
on "d" do
spaceship.turn_right
end
on "=" do
$camera.zoom_in
end
on "-" do
$camera.zoom_out
end
on "h" do
$camera.roll(-0.1)
end
on "j" do
$camera.pitch( 0.1)
end
on "k" do
$camera.pitch(-0.1)
end
on "l" do
$camera.roll( 0.1)
end
on "q" do
exit
end
def spaceship
@spaceship ||= Spaceship.new
end
def on_tick
spaceship.animate
end
end
class Spaceship
attr_accessor :size
def initialize
@size = 0.5
end
def animate
#rotator.yaw!(0.01)
end
def rotation
rotator.matrix
end
def turn_left
rotator.roll!(0.1)
end
def turn_right
rotator.roll!(-0.1)
end
private
def rotator
@rotator ||= Rotator.new
end
end
class Fuselage < View
def draw
glMultMatrix(@model.rotation)
texture = $window.textures.find(:earthmap)
quadro = gluNewQuadric();
gluQuadricNormals(quadro, GLU_SMOOTH);
gluQuadricTexture(quadro, GL_TRUE);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, texture);
gluSphere(quadro, 50000, 48, 48);
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(quadro);
end
end
class Sun
def rotation
rotator.matrix
end
private
def rotator
@rotator ||= Rotator.new
end
end
class SunView < View
def draw
glMultMatrix(@model.rotation)
texture = $window.textures.find(:sunmap_bmp)
quadro = gluNewQuadric();
gluQuadricNormals(quadro, GLU_SMOOTH);
gluQuadricTexture(quadro, GL_TRUE);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, texture);
gluSphere(quadro, 500000, 48, 48);
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(quadro);
end
end
class Camera
def initialize(zoom = 0)
@zoom = zoom
end
def zoom_in
@zoom *= 0.9
end
def zoom_out
@zoom *= 1.1
end
def roll(r)
rotator.roll!(r)
end
def pitch(r)
rotator.pitch!(r)
end
def rotation
rotator.matrix + rotator.translate(0, 0, @zoom)
end
private
def rotator
@rotator ||= Rotator.new
end
end
class CameraView < View
def draw!
glMultMatrix(@model.rotation)
end
end
space_wars = SpaceWars.new
window = Window.new(space_wars)
$camera = Camera.new(-4)
$camera.pitch(Math::PI/2)
window.views << CameraView.new($camera)
window.views << Fuselage.new(space_wars.spaceship)
# window.views << SunView.new(Sun.new)
$window = window
#not sure where textures should be loaded. For one they _must_ be loaded after the window is generated.
#Perhaps a model should have his own stack of textures. But Im not sure if GenTextures can be called multiple times without wiping the stack.
window.start