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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw(sleep);
use OpenGL ':old', ':glutfunctions', ':glutconstants';
my $where = (@ARGV ? 'far away' : 'near Earth');
print "Camera position: $where.\n";
print "Give arguments to position camera far away.\n";
my $field_of_view = (@ARGV ? 20 : 100); # Degrees
my $minviewdist = 0.1;
my $maxviewdist = 1000;
my $mercury = 0;
my $venus = 0;
my $earth = 0;
my $earthMoon = 0;
my $earthspin = 0;
my $mars = 0;
my $jupiter = 230; # Make them visible
my $jups1 = 0;
my $jups2 = 0;
my $jups3 = 0;
my $jups4 = 0;
my $saturn = 250; # Make them visible
sub resizeHandler { # Not auto yet
my ($wind_w, $wind_h) = @_;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( $field_of_view, $wind_w/$wind_h,
$minviewdist, $maxviewdist );
glMatrixMode( GL_MODELVIEW );
}
sub display {
# clear the canvas
glClearColor(0,0,0,1);
glDrawBuffer(GL_FRONT_AND_BACK);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glDrawBuffer(GL_BACK);
glLoadIdentity();
if (@ARGV) {
# move the squares back a bit
glTranslatef( 0, 0, -500 );
# and add a tilt
glRotatef(-70, 1,0,0);
} else {
# place viewpoint on the surface of the planet
glRotatef(30, 1,0,0);
glTranslatef( 0, -5, 2 );
glRotatef(-$earthspin, 0,1,0);
glTranslatef( 0, 0, -60 );
glRotatef(-90, 1,0,0);
glRotatef(90-$earth, 0,0,1);
}
# Sun light
glLightfv(GL_LIGHT0, GL_POSITION, pack "f4", 0, 0, 0, 1);
#glLightfv(GL_LIGHT1, GL_DIFFUSE, pack "f4", 1, 1, 1, 1);
#glLightfv(GL_LIGHT0, GL_DIFFUSE, pack "f4", 0, 0, 0, 1);
# Same for specular?
# Moon light
glPushMatrix();
# Position the moon
glRotatef($earth,0,0,1);
glTranslatef( -60, 0, 0 );
glRotatef($earthMoon,0,0,1);
glTranslatef( -10, 0, 0 );
glLightfv(GL_LIGHT1, GL_POSITION, pack "f4", 0, 0, 0, 1);
my $moon_bright = 0.1*(1 + cos($earthMoon*3.1415926/180));
glLightfv(GL_LIGHT1, GL_AMBIENT,
pack "f4", $moon_bright, $moon_bright, $moon_bright, 1);
glPopMatrix();
# Sun
# glColor3f(1,1,1);
glMaterialfv( GL_FRONT, GL_EMISSION, pack 'f4', 1, 1, 0, 1);
glutSolidSphere(20,10,10);
glMaterialfv( GL_FRONT, GL_EMISSION, pack 'f4', 0, 0, 0, 1);
glPushMatrix();
# Position the mercury
glRotatef($mercury,0,0,1);
glTranslatef( -30, 0, 0 );
glColor3f(0.6, 0.6, 0.6);
glutSolidSphere(2,10,10);
glPopMatrix();
glPushMatrix();
# Position the venus
glRotatef($venus,0,0,1);
glTranslatef( -40, 0, 0 );
glColor3f(0,1,0);
glutSolidSphere(5,10,10);
glPopMatrix();
glPushMatrix();
# Position the earth
glRotatef($earth,0,0,1);
glTranslatef( -60, 0, 0 );
glColor3f(0,0,1);
glPushMatrix();
# Rotate the planet earth
glRotatef($earthspin,0,0,1);
glEnable( GL_LIGHT1 );
glutSolidSphere(5,10,10);
glDisable( GL_LIGHT1 );
glPopMatrix();
# Position the earth's moon
glRotatef($earthMoon,0,0,1);
glTranslatef( -10, 0, 0 );
glColor3f(0.5,0.5,0.5);
glutSolidSphere(1,10,10);
glPopMatrix();
glPushMatrix();
# Position the mars
glRotatef($mars,0,0,1);
glTranslatef( -80, 0, 0 );
glColor3f(1,0,0);
glutSolidSphere(4,10,10);
glPopMatrix();
glPushMatrix();
# Position the jupiter
glRotatef($jupiter,0,0,1);
glTranslatef( -120, 0, 0 );
glColor3f(0.6,0.6,0.6);
glutSolidSphere(9,10,10);
glPushMatrix();
# Position the jup's moon1
glRotatef($jups1,0,0,1);
glTranslatef( -6, 0, 0 );
glColor3f(0.5,0.5,0.5);
glutSolidSphere(1,10,10);
glPopMatrix();
glPushMatrix();
# Position the jup's moon2
glRotatef($jups2,0,0,1);
glTranslatef( -8, 0, 0 );
glColor3f(0.5,0.5,0.5);
glutSolidSphere(1,10,10);
glPopMatrix();
glPushMatrix();
# Position the jup's moon3
glRotatef($jups3,0,0,1);
glTranslatef( -10, 0, 0 );
glColor3f(0.5,0.5,0.5);
glutSolidSphere(1,10,10);
glPopMatrix();
glPushMatrix();
# Position the jup's moon4
glRotatef($jups4,0,0,1);
glTranslatef( -12, 0, 0 );
glColor3f(0.5,0.5,0.5);
glutSolidSphere(1,10,10);
glPopMatrix();
glPopMatrix();
glPushMatrix();
# Position the saturn
glRotatef($saturn,0,0,1);
glTranslatef( -150, 0, 0 );
glColor3f(0.5,0.5,0.5);
glutSolidSphere(8,10,10);
glRotatef(-$saturn,0,0,1);
glRotatef(14, 0.707, -0.707, 0); # What is the actual tilt?
glScalef(1,1,0.2); # Giving smaller values makes it white?!
eval {glutSolidTorus(4.5,13,10,10)};
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
glutInit();
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow('Planets Example');
glutDisplayFunc(\&display);
glutReshapeFunc(\&resizeHandler);
#glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnable( GL_DEPTH_TEST );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_COLOR_MATERIAL );
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, pack 'f4', 0.1, 0.1, 0.1, 1);
while ( sleep(0.1) ) {
glutMainLoopEvent();
$mercury += 1/0.24;
$mercury -= 360 if $mercury >= 360;
$venus += 1/0.62;
$venus -= 360 if $venus >= 360;
$earthMoon += 12.36; # Relative to sun-earth line
$earthMoon -= 360 if $earthMoon >= 360;
$earthspin += 3.65; # 1/100 of real
$earthspin -= 360 if $earthspin >= 360;
$earth += 1;
$earth -= 360 if $earth >= 360;
$venus += 1/0.62;
$venus -= 360 if $venus >= 360;
$mars += 1/1.88; # Wrong
$mars -= 360 if $mars >= 360;
$jupiter += 1/11.86;
$jupiter -= 360 if $jupiter >= 360;
$jups1 += -1/11.86 + 365.2/1.77;
$jups1 -= 360 if $jups1 >= 360;
$jups2 += -1/11.86 + 365.2/3.55;
$jups2 -= 360 if $jups2 >= 360;
$jups3 += -1/11.86 + 365.2/7.16;
$jups3 -= 360 if $jups3 >= 360;
$jups4 += -1/11.86 + 365.2/16.69;
$jups4 -= 360 if $jups4 >= 360;
$saturn += 1/29.46;
$saturn -= 360 if $saturn >= 360;
glutPostRedisplay();
}
|