-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.cpp
172 lines (144 loc) · 4.99 KB
/
game.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
//============================================================================
// Hassan Shahzad
// 18i-0441
// FAST-NUCES
// Computer Graphics: 3D Solar System Scene
// Contact at : chhxnshah@gmail.com
//============================================================================
#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include<string>
using namespace std;
float xRotated = 90.0, yRotated = 0.0, zRotated = 0.0;
//________________________________________________________________________________________________________________________________
//defining some MACROS
#define FPS 1000 // frame per seconds
// define another constant to hold ASCII for Escape key.
#define KEY_ESC 27
//________________________________________________________________________________________________________________________________
void reshapeFunc(int x, int y)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (GLdouble)x / (GLdouble)y, 0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y);
}
//________________________________________________________________________________________________________________________________
void Draw_Spheres()
{
glClearColor(0/*Red Component*/, 0.0/*Green Component*/,
0.0/*Blue Component*/, 0 /*Alpha component*/); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
glMatrixMode (GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-15.0);
// SUN
glColor4f(1.0,1.0,0.0,0.0); //Yellow color
glPushMatrix();
glTranslatef(-12.5,0.0,0.0);
glutSolidSphere(4.0,50,50);
glPopMatrix();
// Mercury
glColor3f(0.0,0.1,0.1); //Dark Grey color
glPushMatrix();
glTranslatef(-7,0.0,0.0);
glRotatef(60.0,1,0,0);
glRotatef(zRotated*1.5,0,0,1);
glutSolidSphere(0.4,15,10);
glPopMatrix();
// Venus
glColor4f(1.0,0.5,0.0,0.0); //Orange Brown color
glPushMatrix();
glTranslatef(-5.5,0.0,0.0);
glRotatef(60.0,1,0,0);
glRotatef(zRotated,0,0,1);
glutSolidSphere(0.7,20,20);
glPopMatrix();
//Earth
glColor3f(0.1,0.2,0.8); //Blue color
glPushMatrix();
glTranslatef(-3.8,0.0,0.0);
glRotatef(60.0,1,0,0);
glRotatef(zRotated*3,0,0,1);
glutSolidSphere(0.7,20,20);
glPopMatrix();
//Mars
glColor3f(0.8,0.2,0.1); //Red color
glPushMatrix();
glTranslatef(-2.3,0.0,0.0);
glRotatef(125.0,1,0,0);
glRotatef(zRotated*4.0,0,0,1);
glutSolidSphere(0.5,20,15);
glPopMatrix();
//Jupiter
glColor3f(1.0,0.5,0.0); //Orange color
glPushMatrix();
glTranslatef(-0.5,0.0,0.0);
glRotatef(125.0,1,0,0);
glRotatef(zRotated*7.0,0,0,1);
glutSolidSphere(1.0,20,40);
glPopMatrix();
// Saturn
glColor3f(0.0,0.5,0.5); //Bluish Green
glPushMatrix();
glTranslatef(2.0,0.0,0.0);
glRotatef(60.0,1,0,0);
glRotatef(zRotated*6.5,0,0,1);
glutSolidSphere(1.0,20,30);
glPopMatrix();
//Uranus
glColor3f(0.5,1.0,1.0); //Cyan Color,
glPushMatrix();
glTranslatef(4.0,0.0,0.0);
glRotatef(125.0,1,0,0);
glRotatef(zRotated*5.0,0,0,1);
glutSolidSphere(0.7,20,20);
glPopMatrix();
//Neptune
glColor3f(0.1,0.0,1.0); //Purple Color,
glPushMatrix();
glTranslatef(6.0,0.0,0.0);
glRotatef(60.0,1,0,0);
glRotatef(zRotated*5.5,0,0,1);
glutSolidSphere(0.7,20,20);
glPopMatrix();
glutSwapBuffers();
}
//________________________________________________________________________________________________________________________________
void idleFunc()
{
zRotated+=0.1;
glutPostRedisplay();
}
//________________________________________________________________________________________________________________________________
void SetCanvasSize(int width, int height) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
}
//________________________________________________________________________________________________________________________________
void PrintableKeys(unsigned char key, int x, int y) {
if (key == KEY_ESC/* Escape key ASCII*/) {
exit(1); // exit the program when escape key is pressed.
}
}
//________________________________________________________________________________________________________________________________
int main(int argc, char*argv[]) {
int width = 1300, height = 800; // i have set my window size to be 800 x 600
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("Computer Graphics: 3D Solar System Scene"); // set the title of our game window
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
SetCanvasSize(width, height); // set the number of pixels...
glutDisplayFunc(Draw_Spheres); // tell library which function to call for drawing Canvas.
glutReshapeFunc(reshapeFunc);
glutIdleFunc(idleFunc);
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
glutMainLoop();
return 1;
}