-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisuals.cpp
More file actions
443 lines (313 loc) · 8.44 KB
/
visuals.cpp
File metadata and controls
443 lines (313 loc) · 8.44 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <stdio.h>
#include <fstream>
#include <string.h>
#include <iostream>
#include <sstream>
#include <math.h>
#include <stdlib.h>
#include "GL/glut.h"
#include "visuals.h"
model md;
int starMatrix[2000];
/////////hasko////////
float sun = 1;
float transun = 15.0;
static float ty = 0.0;
static float tx = 0.0;
static float tz = 0.0;
static bool animate = false;
////////////
//int codes for the planets
const int OtherPlanetCode = 0;
const int RedPlanetCode = 1;
const int GreenPlanetCode = 2;
const int BluePlanetCode = 3;
static float rotatePlanet = 0.0f;
static float rot = 0.0f;
using namespace std;
void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//new zoom in,zoom out function
glTranslatef(0.0f, 0.0f, tz);
//implementing the rotation of the camera
glRotatef(tx, 1.0, 0.0, 0.0);
glRotatef(ty, 0.0, 1.0, 0.0);
//different way of moving checking to see more ways of using the camera
//gluLookAt(100*pow(cos(t),2),100*pow(cos(t),2),100*pow(sin(t),2),0,0,-100,0,1,0);
glTranslatef(0.0f, 0.0f, -100.0f);
//creating stars
glPushMatrix();
stars(transun);
glPopMatrix();
//creating the sun
glPushMatrix();
glScalef(0.8f,0.8f, 0.8f);
star(transun);
glPopMatrix();
//creating first planet
glPushMatrix();
glScalef(0.01f, 0.01f, 0.01f);
CreatePlanet(RedPlanetCode);
glPopMatrix();
//creating second planet
glPushMatrix();
glScalef(0.02f, 0.02f, 0.02f);
CreatePlanet(GreenPlanetCode);
glPopMatrix();
//creating third planet
glPushMatrix();
glScalef(0.007f, 0.007f, 0.007f);
CreatePlanet(BluePlanetCode);
glPopMatrix();
//creating fourth planet
glPushMatrix();
glScalef(0.014f, 0.014f, 0.014f);
CreatePlanet(OtherPlanetCode);
glPopMatrix();
glutSwapBuffers();
}
void Resize(int w, int h)
{
if (h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f, (float)w / (float)h, 1.0f, 10000.0f);
}
//function for planet creation
void CreatePlanet(int colour)
{
if (colour == RedPlanetCode) //code for red planet
{
glPushMatrix();
glRotatef(rot, 0.0f, 0.5f, 0.0f); // around sun
glTranslatef(2000, 0, 0);
glRotatef(rotatePlanet, 0.0f, 0.5f, 0.0f); //around itself
glColor3f(1.0f, 0.0f, 0.0f);
DisplayModel(md);
glPopMatrix();
}
else if (colour == GreenPlanetCode) // code for green planet
{
glPushMatrix();
glRotatef(-rot, 0.0f, 0.5f, 0.0f);
glTranslatef(-3000, 0, 0);
glRotatef(rotatePlanet, 0.0f, 0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
DisplayModel(md);
glPopMatrix();
}
else if (colour == BluePlanetCode) // code for blue planet
{
glPushMatrix();
glRotatef(rot, 0.5f, 0.0f, 0.0f);
glTranslatef(0, 5000, 0);
glRotatef(rotatePlanet, 0.0f, 0.5f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
DisplayModel(md);
glPopMatrix();
}
else // code for other planet
{
glPushMatrix();
glRotatef(-rot, 0.5f, 0.0f, 0.0f);
glTranslatef(0, -6000, 0);
glRotatef(rotatePlanet, 0.0f, 0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 1.0f);
DisplayModel(md);
glPopMatrix();
}
}
void Setup()
{
ReadFile(&md); //reading file
//Parameter handling
glShadeModel(GL_SMOOTH);
//enable blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); //renders a fragment if its z value is less or equal of the stored value
glClearDepth(1);
// polygon rendering mode
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
//Set up light source
GLfloat light_position[] = { 0.0, 0.0, -70.0, 1 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//turn on if you want some more lighting!!! (uncomment next line)
//GLfloat ambientLight[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat diffuseLight[] = { 0.8, 0.8, 0.8, 1.0 };
//turn on if you want some more lighting!!! (uncomment next line)
//glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
////
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glFrontFace(GL_CCW);
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void Idle()
{
if (animate)
{
//calculations for the planets rotation and the growdth and shrinking of the sun
rot += 5.5f;
rotatePlanet += 1.0f;
float dt = 0.01f;
float g = 10;
if (transun < 11) sun = -sun;
else sun = sun - g*dt;
transun = transun + sun*dt;
/////
}
glutPostRedisplay();
}
//keyboard function for input////////
void Keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'q':exit(0);
break;
case 'd'://t += 0.1;
ty += 4.5f;
break;
case 'a'://t -= 0.1;
ty -= 4.5f;
break;
case 'w':
tx += 4.5f;
break;
case 's':
tx -= 4.5f;
break;
case 'g':
animate = !animate;
break;
case 't':
tz += 5.1f;
break;
case 'y':
tz -= 5.1f;
break;
default:
break;
}
glutPostRedisplay();
}
//changed readfile function for the new planet.obj file
void ReadFile(model *md)
{
ifstream obj_file("planet.obj");
if (obj_file.fail())
exit(1);
string stringLine;
int vertexPositionCounter = 0;
int facesCounter = 0;
int normalsCounter = 0;
while (getline(obj_file,stringLine))
{
if (stringLine.substr(0, 2) == "v ")
{
istringstream s(stringLine.substr(2));
s >> md->obj_points[vertexPositionCounter].x;
s >> md->obj_points[vertexPositionCounter].y;
s >> md->obj_points[vertexPositionCounter].z;
vertexPositionCounter++;
}
else if (stringLine.substr(0, 3) == "vn ")
{
istringstream s(stringLine.substr(3));
s >> md->obj_normalls[normalsCounter].x;
s >> md->obj_normalls[normalsCounter].y;
s >> md->obj_normalls[normalsCounter].z;
normalsCounter++;
}
else if (stringLine.substr(0, 2) == "f ")
{
istringstream s(stringLine.substr(2));
//getting first vertex position
s >> md->obj_faces[facesCounter].vtx[0];
s.ignore(10, '/');
s.ignore(10, '/');
s >> md->obj_faces[facesCounter].vtn[0];
//ignoring space to get the next vertex position
s.ignore(10, ' ');
//getting next vertex position
s >> md->obj_faces[facesCounter].vtx[1];
s.ignore(10, '/');
s.ignore(10, '/');
s >> md->obj_faces[facesCounter].vtn[1];
//ignoring space to get the next vertex position
s.ignore(10, ' ');
//getting next vertex position
s >> md->obj_faces[facesCounter].vtx[2];
s.ignore(10, '/');
s.ignore(10, '/');
s >> md->obj_faces[facesCounter].vtn[2];
facesCounter++;
}
}
//setting the number of vertex positions,normalls and faces
md->vertices = vertexPositionCounter;
md->normalls = normalsCounter;
md->faces = facesCounter;
obj_file.close();
}
//added extra vertex normall display
void DisplayModel(model md)
{
glPushMatrix();
glBegin(GL_TRIANGLES);
for (int i = 0; i < md.faces; i++)
{
glVertex3f(md.obj_points[md.obj_faces[i].vtx[0] - 1].x, md.obj_points[md.obj_faces[i].vtx[0] - 1].y, md.obj_points[md.obj_faces[i].vtx[0] - 1].z);
glVertex3f(md.obj_points[md.obj_faces[i].vtx[1] - 1].x, md.obj_points[md.obj_faces[i].vtx[1] - 1].y, md.obj_points[md.obj_faces[i].vtx[1] - 1].z);
glVertex3f(md.obj_points[md.obj_faces[i].vtx[2] - 1].x, md.obj_points[md.obj_faces[i].vtx[2] - 1].y, md.obj_points[md.obj_faces[i].vtx[2] - 1].z);
glNormal3f(md.obj_normalls[md.obj_faces[i].vtn[0] - 1].x, md.obj_normalls[md.obj_faces[i].vtn[0] - 1].y, md.obj_normalls[md.obj_faces[i].vtn[0] - 1].z);
glNormal3f(md.obj_normalls[md.obj_faces[i].vtn[1] - 1].x, md.obj_normalls[md.obj_faces[i].vtn[1] - 1].y, md.obj_normalls[md.obj_faces[i].vtn[1] - 1].z);
glNormal3f(md.obj_normalls[md.obj_faces[i].vtn[2] - 1].x, md.obj_normalls[md.obj_faces[i].vtn[2] - 1].y, md.obj_normalls[md.obj_faces[i].vtn[2] - 1].z);
}
glEnd();
glPopMatrix();
}
//function for star creation
void star(float transun)
{
glColor3f(0.6f, 0.5f, 0.1f);
glutSolidSphere(10.0, 30, 24);
glPushMatrix();
glColor4f(1, 1, 0,(transun - 11) / 4);
glutSolidSphere(transun, 30, 15);
glPopMatrix();
}
//function for stars
void stars(float transun)
{
int i;
for (i = 0; i < 2000; i = i + 3) {
glPushMatrix();
glTranslatef(starMatrix[i], starMatrix[i + 1], starMatrix[i + 2]);
glTranslatef(0.0f, 0.0f, -100.0f);
glScalef(0.02, 0.02, 0.02);
star(transun);
glPopMatrix();
}
}
//function to fill the starsMatrix
void pinakas()
{
int i, a;
for (i = 0; i < 2000; i++) {
a = rand() % 600 - 300;
starMatrix[i] = a;
}
}