Guest User

Untitled

a guest
Apr 10th, 2019
22
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <array>
  3. #include "GL/glew.h"
  4. #include "GL/3dgl.h"
  5. #include "GL/glut.h"
  6. #include "GL/freeglut_ext.h"
  7.  
  8. #pragma comment (lib, "glew32.lib")
  9.  
  10. using namespace std;
  11. using namespace _3dgl;
  12.  
  13. // 3D Models
  14. C3dglModel earth;
  15. C3dglModel minimap;
  16. C3dglModel st_stephens_tower;
  17. C3dglModel eiffel_tower;
  18. C3dglModel colosseum;
  19. C3dglModel sagrada_familia;
  20. C3dglModel pyramids_of_giza;
  21. C3dglModel red_square;
  22. C3dglModel great_wall_of_china;
  23. C3dglModel empire_state_building;
  24.  
  25. // Skybox
  26. C3dglSkyBox skybox;
  27.  
  28. // Textures
  29. GLuint none_texture;
  30. GLuint shadowmap_texture;
  31. GLuint pp_texture[4];
  32. GLuint earth_texture;
  33. GLuint earth_texture_night;
  34. GLuint earth_texture_norm;
  35. GLuint earthwater_texture_norm;
  36. GLuint uppercloud_texture;
  37. GLuint middlecloud_texture;
  38. GLuint st_stephens_tower_texture;
  39. GLuint colosseum_texture;
  40. GLuint sagrada_familia_texture;
  41. GLuint pyramids_of_giza_texture;
  42. GLuint red_square_texture;
  43. GLuint great_wall_of_china_texture;
  44. GLuint empire_state_building_texture;
  45. GLuint snow_texture;
  46.  
  47. // GLSL Objects (Shader Program)
  48. C3dglProgram EarthShader;
  49. C3dglProgram AtmosphereShader;
  50. C3dglProgram ModelShader;
  51. C3dglProgram ParticleShader;
  52. C3dglProgram BlurShader;
  53. C3dglProgram BloomShader;
  54.  
  55. // FBOs
  56. GLuint shadowFBO;
  57.  
  58. // camera position (for first person type camera navigation)
  59. float matrixView[16]; // The View Matrix
  60. float angleTilt = 0; // Tilt Angle
  61. float deltaX = 0, deltaY = 0, deltaZ = 0; // Camera movement values
  62. float inverseModelViewMatrix[16];
  63.  
  64. // camera world-position
  65. float cameraVector[3];
  66. GLfloat cameraMatrix[16];
  67.  
  68. // global matrix
  69. float matrix[16];
  70.  
  71. // screen size
  72. GLuint ScreenWidth = 800;
  73. GLuint ScreenHeight = 600;
  74. // absolute screen height for text rendering
  75. int abs_screenHeight;
  76.  
  77. // particle system variables / buffers
  78. float particleAlpha;
  79. const float PERIOD = 0.000175f;
  80. const float LIFETIME = 50.0;
  81. const int NPARTICLES = (int)(LIFETIME / PERIOD);
  82. GLuint idBufferVelocity;
  83. GLuint idBufferStartTime;
  84. bool isNorth;
  85. bool isSouth;
  86.  
  87. // post processing
  88. GLuint ppFBO[4];
  89. GLuint depth_rb[4];
  90. // screen quad buffer
  91. GLuint finalRenderBuffer;
  92.  
  93. // control variables
  94. // input
  95. float time;
  96. bool middlecloudlayer;
  97. bool uppercloudlayer;
  98. bool atmospherelayer;
  99. // sun
  100. float sunDir[2];
  101. int sunOrigin;
  102. int sunRadius;
  103. float sunTime;
  104. // camera
  105. int viewIndex;
  106. bool lock[2] = {};
  107. int charIndex;
  108. char *charState[] =
  109. {
  110. "Outer Earth View", // 0
  111. "Inner Earth View", // 1
  112. "Europe", // 2
  113. "Africa", // 3
  114. "Asia", // 4
  115. "Oceania", // 5
  116. "North America", // 6
  117. "South America", // 7
  118. "Antarctica", // 8
  119. "Press Spacebar to unlock movement", // 9
  120. "Use WSAD keys to navigate", // 10
  121. "Click and drag to look around", // 11
  122. "1: Toggle lower cloud layer", // 12
  123. "2: Toggle upper cloud layer", // 13
  124. "3: Toggle atmopshere", // 14
  125. "4: Toggle view", // 15
  126. // Europe info
  127. "Area: 10,180,000 km2", // 16
  128. "Population: 741,447,158", // 17
  129. "Nominal GDP: $19 trillion", // 18
  130. "Countries: 50", // 19
  131. "Largest cities:", // 20
  132. "Istanbul, Turkey", // 21
  133. "Moscow, Russia", // 22
  134. "Paris, France", // 23
  135. "London, UK", // 24
  136. "Madrid, Spain", // 25
  137. // Africa info
  138. "Area: 30,370,000 km2", // 26
  139. "Population: 1,225,080,510", // 27
  140. "Nominal GDP: $2.1 trillion", // 28
  141. "Countries: 54", // 29
  142. "Largest cities:", // 30
  143. "Lagos, Nigeria", // 31
  144. "Cairo, Egypt", // 32
  145. "Kinshasa, Democratic Republic of the Congo", // 33
  146. "Johannesburg, South Africa", // 34
  147. "Khartoum, Sudan", // 35
  148. // Asia info
  149. "Area: 44,579,000 km2", // 36
  150. "Population: 4,462,676,731", // 37
  151. "Nominal GDP: $27.2 trillion", // 38
  152. "Countries: 55", // 39
  153. "Largest cities:", // 40
  154. "Guangzhou-Foshan, China", // 41
  155. "Tokyo, Japan", // 42
  156. "Shanghai, China", // 43
  157. "Jakarta, Indonesia", // 44
  158. "Delhi, India", // 45
  159. // Australia/Oceania info
  160. "Area: 8,525,989 km2", // 46
  161. "Population: 40,117,432", // 47
  162. "Nominal GDP: $1.468 trillion", // 48
  163. "Countries: 16", // 49
  164. "Largest cities:", // 50
  165. "Sydney, Australia", // 51
  166. "Melbourne, Australia", // 52
  167. "Brisbane, Australia", // 53
  168. "Perth, Australia", // 54
  169. "Auckland, New Zealand", // 55
  170. // North America info
  171. "Area: 24,709,000 km2", // 56
  172. "Population: 579,024,000", // 57
  173. "Nominal GDP: $21.2 trillion", // 58
  174. "Countries: 23 sovereign states", // 59
  175. "Largest cities:", // 60
  176. "New York City, USA", // 61
  177. "Mexico City, Mexico", // 62
  178. "Los Angeles, USA", // 63
  179. "Chicago, USA", // 64
  180. "Boston, USA", // 65
  181. // South America info
  182. "Area: 17,840,000 km2", // 66
  183. "Population: 420,458,044", // 67
  184. "Nominal GDP: $3.9 trillion", // 68
  185. "Countries: 12", // 69
  186. "Largest cities:", // 70
  187. "Sao Paulo, Brazil", // 71
  188. "Lima, Peru", // 72
  189. "Bogota, Colombia", // 73
  190. "Rio de Janeiro, Brazil", // 74
  191. "Santiago, Chile", // 75
  192. // South America info
  193. "Area: 14,000,000 km2", // 76
  194. "Population: 1,106", // 77
  195. "Nominal GDP: N/A", // 78
  196. "Countries: 0", // 79
  197. "Largest claims of land:", // 80
  198. "5,896,500 km2, Australian", // 81
  199. "2,700,000 km2, Norwegian", // 82
  200. "1,709,400 km2, British", // 83
  201. "1,461,597 km2, Argentine", // 84
  202. "1,250,257 km2, Chilean", // 85
  203.  
  204. };
  205. // font
  206. const int font = (int)GLUT_BITMAP_HELVETICA_18;
  207. const int font2 = (int)GLUT_BITMAP_HELVETICA_10;
  208.  
  209. // prototype
  210. void reshape(int w, int h);
  211.  
  212. bool init()
  213. {
  214. // rendering states
  215. glEnable(GL_DEPTH_TEST); // depth test is necessary for most 3D scenes
  216. glEnable(GL_NORMALIZE); // normalization is needed by AssImp library models
  217. glShadeModel(GL_SMOOTH); // smooth shading mode is the default one; try GL_FLAT here!
  218. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // this is the default one; try GL_LINE!
  219.  
  220. // enable alpha channels
  221. glEnable(GL_BLEND);
  222. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  223.  
  224. // particle system setup
  225. glEnable(GL_POINT_SPRITE);
  226. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
  227. glPointSize(1);
  228.  
  229. // initialise Shaders
  230. C3dglShader VertexShader;
  231. C3dglShader FragmentShader;
  232.  
  233. if (!VertexShader.Create(GL_VERTEX_SHADER)) return false;
  234. if (!VertexShader.LoadFromFile("shaders/particles.vert")) return false;
  235. if (!VertexShader.Compile()) return false;
  236.  
  237. if (!FragmentShader.Create(GL_FRAGMENT_SHADER)) return false;
  238. if (!FragmentShader.LoadFromFile("shaders/particles.frag")) return false;
  239. if (!FragmentShader.Compile()) return false;
  240.  
  241. if (!ParticleShader.Create()) return false;
  242. if (!ParticleShader.Attach(VertexShader)) return false;
  243. if (!ParticleShader.Attach(FragmentShader)) return false;
  244. if (!ParticleShader.Link()) return false;
  245. if (!ParticleShader.Use(true)) return false;
  246.  
  247. if (!VertexShader.Create(GL_VERTEX_SHADER)) return false;
  248. if (!VertexShader.LoadFromFile("shaders/blur.vert")) return false;
  249. if (!VertexShader.Compile()) return false;
  250.  
  251. if (!FragmentShader.Create(GL_FRAGMENT_SHADER)) return false;
  252. if (!FragmentShader.LoadFromFile("shaders/blur.frag")) return false;
  253. if (!FragmentShader.Compile()) return false;
  254.  
  255. if (!BlurShader.Create()) return false;
  256. if (!BlurShader.Attach(VertexShader)) return false;
  257. if (!BlurShader.Attach(FragmentShader)) return false;
  258. if (!BlurShader.Link()) return false;
  259. if (!BlurShader.Use(true)) return false;
  260.  
  261. if (!VertexShader.Create(GL_VERTEX_SHADER)) return false;
  262. if (!VertexShader.LoadFromFile("shaders/bloom.vert")) return false;
  263. if (!VertexShader.Compile()) return false;
  264.  
  265. if (!FragmentShader.Create(GL_FRAGMENT_SHADER)) return false;
  266. if (!FragmentShader.LoadFromFile("shaders/bloom.frag")) return false;
  267. if (!FragmentShader.Compile()) return false;
  268.  
  269. if (!BloomShader.Create()) return false;
  270. if (!BloomShader.Attach(VertexShader)) return false;
  271. if (!BloomShader.Attach(FragmentShader)) return false;
  272. if (!BloomShader.Link()) return false;
  273. if (!BloomShader.Use(true)) return false;
  274.  
  275. if (!VertexShader.Create(GL_VERTEX_SHADER)) return false;
  276. if (!VertexShader.LoadFromFile("shaders/atmosphere.vert")) return false;
  277. if (!VertexShader.Compile()) return false;
  278.  
  279. if (!FragmentShader.Create(GL_FRAGMENT_SHADER)) return false;
  280. if (!FragmentShader.LoadFromFile("shaders/atmosphere.frag")) return false;
  281. if (!FragmentShader.Compile()) return false;
  282.  
  283. if (!AtmosphereShader.Create()) return false;
  284. if (!AtmosphereShader.Attach(VertexShader)) return false;
  285. if (!AtmosphereShader.Attach(FragmentShader)) return false;
  286. if (!AtmosphereShader.Link()) return false;
  287. if (!AtmosphereShader.Use(true)) return false;
  288.  
  289. if (!VertexShader.Create(GL_VERTEX_SHADER)) return false;
  290. if (!VertexShader.LoadFromFile("shaders/earth.vert")) return false;
  291. if (!VertexShader.Compile()) return false;
  292.  
  293. if (!FragmentShader.Create(GL_FRAGMENT_SHADER)) return false;
  294. if (!FragmentShader.LoadFromFile("shaders/earth.frag")) return false;
  295. if (!FragmentShader.Compile()) return false;
  296.  
  297. if (!EarthShader.Create()) return false;
  298. if (!EarthShader.Attach(VertexShader)) return false;
  299. if (!EarthShader.Attach(FragmentShader)) return false;
  300. if (!EarthShader.Link()) return false;
  301. if (!EarthShader.Use(true)) return false;
  302.  
  303. if (!VertexShader.Create(GL_VERTEX_SHADER)) return false;
  304. if (!VertexShader.LoadFromFile("shaders/model.vert")) return false;
  305. if (!VertexShader.Compile()) return false;
  306.  
  307. if (!FragmentShader.Create(GL_FRAGMENT_SHADER)) return false;
  308. if (!FragmentShader.LoadFromFile("shaders/model.frag")) return false;
  309. if (!FragmentShader.Compile()) return false;
  310.  
  311. if (!ModelShader.Create()) return false;
  312. if (!ModelShader.Attach(VertexShader)) return false;
  313. if (!ModelShader.Attach(FragmentShader)) return false;
  314. if (!ModelShader.Link()) return false;
  315. if (!ModelShader.Use(true)) return false;
  316.  
  317. // glut additional setup
  318. glutSetVertexAttribCoord3(EarthShader.GetAttribLocation("aVertex"));
  319. glutSetVertexAttribNormal(EarthShader.GetAttribLocation("aNormal"));
  320.  
  321. // load models
  322. if (!skybox.load("models\\stars.png", "models\\stars.png", "models\\stars.png",
  323. "models\\stars.png", "models\\stars.png", "models\\stars.png")) return false;
  324. if (!earth.load("models\\hugeball_reduced.obj")) return false;
  325. if (!st_stephens_tower.load("models\\st_stephens_tower.obj")) return false;
  326. if (!eiffel_tower.load("models\\eiffel_tower.obj")) return false;
  327. if (!colosseum.load("models\\colosseum.obj")) return false;
  328. if (!sagrada_familia.load("models\\sagrada_familia.obj")) return false;
  329. if (!pyramids_of_giza.load("models\\pyramids_of_giza.obj")) return false;
  330. if (!red_square.load("models\\red_square.obj")) return false;
  331. if (!great_wall_of_china.load("models\\great_wall_of_china.obj")) return false;
  332. if (!empire_state_building.load("models\\empire_state_building.obj")) return false;
  333.  
  334. // loading and binding textures
  335. C3dglBitmap bm, bm2, bm3, bm4, bm5, bm6, bm7, bm8, bm9, bm10, bm11, bm12, bm13, bm14;
  336. bm.Load("models\\2_no_clouds_8k.jpg", GL_RGBA);
  337. if (!bm.GetBits()) return false;
  338. bm2.Load("models\\2_no_clouds_8k_n.jpg", GL_RGBA);
  339. if (!bm2.GetBits()) return false;
  340. bm3.Load("models\\clouds0.tga", GL_RGBA);
  341. if (!bm3.GetBits()) return false;
  342. bm4.Load("models\\clouds2.tga", GL_RGBA);
  343. if (!bm4.GetBits()) return false;
  344. bm5.Load("models\\water_n.jpg", GL_RGBA);
  345. if (!bm5.GetBits()) return false;
  346. bm6.Load("models\\st_stephens_tower_texture.jpg", GL_RGBA);
  347. if (!bm6.GetBits()) return false;
  348. bm7.Load("models\\colosseum_texture.jpg", GL_RGBA);
  349. if (!bm7.GetBits()) return false;
  350. bm8.Load("models\\sagrada_familia_texture.jpg", GL_RGBA);
  351. if (!bm8.GetBits()) return false;
  352. bm9.Load("models\\pyramids_of_giza_texture.jpg", GL_RGBA);
  353. if (!bm9.GetBits()) return false;
  354. bm10.Load("models\\red_square_texture.jpg", GL_RGBA);
  355. if (!bm10.GetBits()) return false;
  356. bm11.Load("models\\5_night_8k.jpg", GL_RGBA);
  357. if (!bm11.GetBits()) return false;
  358. bm12.Load("models\\great_wall_of_china_texture.jpg", GL_RGBA);
  359. if (!bm12.GetBits()) return false;
  360. bm13.Load("models\\empire_state_building_texture.jpg", GL_RGBA);
  361. if (!bm13.GetBits()) return false;
  362. bm14.Load("models\\snow.png", GL_RGBA);
  363. if (!bm14.GetBits()) return false;
  364.  
  365.  
  366. // send particles shader uniforms
  367. std::vector<float> bufferVelocity;
  368. std::vector<float> bufferStartTime;
  369. float time = 0;
  370. float M_PI = 3.14;
  371.  
  372. for (int i = 0; i < NPARTICLES; i++)
  373. {
  374. float theta = (float)M_PI / 6.f * (float)rand() / (float)RAND_MAX;
  375. float phi = (float)M_PI * 2.f * (float)rand() / (float)RAND_MAX;
  376. float x = sin(theta) * cos(phi);
  377. float y = cos(theta);
  378. float z = sin(theta) * sin(phi);
  379. float v = 2 + 0.5f * (float)rand() / (float)RAND_MAX;
  380.  
  381. bufferVelocity.push_back(x * v);
  382. bufferVelocity.push_back(y * v);
  383. bufferVelocity.push_back(z * v);
  384. bufferStartTime.push_back(time);
  385.  
  386. time += PERIOD;
  387. }
  388.  
  389. glGenBuffers(1, &idBufferVelocity);
  390. glBindBuffer(GL_ARRAY_BUFFER, idBufferVelocity);
  391. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * bufferVelocity.size(), &bufferVelocity[0], GL_STATIC_DRAW);
  392. glGenBuffers(1, &idBufferStartTime); glBindBuffer(GL_ARRAY_BUFFER, idBufferStartTime);
  393. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * bufferStartTime.size(), &bufferStartTime[0], GL_STATIC_DRAW);
  394.  
  395. //ParticleShader.SendUniform("gravity", 0.0f, -0.5f, 0.0f);
  396. ParticleShader.SendUniform("particleLifetime", LIFETIME);
  397.  
  398. // channel 0
  399. // earth texture
  400. glActiveTexture(GL_TEXTURE0);
  401. glGenTextures(1, &earth_texture);
  402. glBindTexture(GL_TEXTURE_2D, earth_texture);
  403. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  404. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm.GetWidth(), bm.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm.GetBits());
  405.  
  406. // uppercloud texture
  407. glGenTextures(1, &uppercloud_texture);
  408. glBindTexture(GL_TEXTURE_2D, uppercloud_texture);
  409. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  410. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm3.GetWidth(), bm3.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
  411.  
  412. // middlecloud texture
  413. glGenTextures(1, &middlecloud_texture);
  414. glBindTexture(GL_TEXTURE_2D, middlecloud_texture);
  415. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  416. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm4.GetWidth(), bm4.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm4.GetBits());
  417.  
  418. // st stephens tower texture
  419. glGenTextures(1, &st_stephens_tower_texture);
  420. glBindTexture(GL_TEXTURE_2D, st_stephens_tower_texture);
  421. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  422. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm6.GetWidth(), bm6.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm6.GetBits());
  423.  
  424. // colosseum texture
  425. glGenTextures(1, &colosseum_texture);
  426. glBindTexture(GL_TEXTURE_2D, colosseum_texture);
  427. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  428. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm7.GetWidth(), bm7.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm7.GetBits());
  429.  
  430. // sagrada familia texture
  431. glGenTextures(1, &sagrada_familia_texture);
  432. glBindTexture(GL_TEXTURE_2D, sagrada_familia_texture);
  433. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  434. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm8.GetWidth(), bm8.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm8.GetBits());
  435.  
  436. // pyramids of giza texture
  437. glGenTextures(1, &pyramids_of_giza_texture);
  438. glBindTexture(GL_TEXTURE_2D, pyramids_of_giza_texture);
  439. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  440. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm9.GetWidth(), bm9.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm9.GetBits());
  441.  
  442. // red square texture
  443. glGenTextures(1, &red_square_texture);
  444. glBindTexture(GL_TEXTURE_2D, red_square_texture);
  445. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  446. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm10.GetWidth(), bm10.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm10.GetBits());
  447.  
  448. // great wall of china texture
  449. glGenTextures(1, &great_wall_of_china_texture);
  450. glBindTexture(GL_TEXTURE_2D, great_wall_of_china_texture);
  451. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  452. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm12.GetWidth(), bm12.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm12.GetBits());
  453.  
  454. // empire state building texture
  455. glGenTextures(1, &empire_state_building_texture);
  456. glBindTexture(GL_TEXTURE_2D, empire_state_building_texture);
  457. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  458. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm13.GetWidth(), bm13.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm13.GetBits());
  459.  
  460. // snow texture
  461. glGenTextures(1, &snow_texture);
  462. glBindTexture(GL_TEXTURE_2D, snow_texture);
  463. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  464. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm14.GetWidth(), bm14.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm14.GetBits());
  465.  
  466. //// earth texture flipped
  467. //glGenTextures(1, &earth_texture_flipped);
  468. //glBindTexture(GL_TEXTURE_2D, earth_texture_flipped);
  469. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  470. //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm11.GetWidth(), bm11.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm11.GetBits());
  471.  
  472. // null texture
  473. glGenTextures(1, &none_texture);
  474. glBindTexture(GL_TEXTURE_2D, none_texture);
  475. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  476. BYTE bytes[] = { 255, 255, 255 };
  477. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_BGR, GL_UNSIGNED_BYTE, &bytes);
  478.  
  479. // bloom post-processing textures
  480. for (int i = 0; i < 4; i++)
  481. {
  482. glGenTextures(1, &pp_texture[i]);
  483. glBindTexture(GL_TEXTURE_2D, pp_texture[i]);
  484. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  485. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  486. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  487. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  488. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ScreenWidth * 4, ScreenHeight * 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  489. // FBO attachment
  490. glGenFramebuffers(1, &ppFBO[i]);
  491. glBindFramebuffer(GL_FRAMEBUFFER, ppFBO[i]);
  492. glGenRenderbuffers(1, &depth_rb[i]);
  493. glBindRenderbuffer(GL_RENDERBUFFER, depth_rb[i]);
  494. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, ScreenWidth * 4, ScreenHeight * 4);
  495. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_rb[i]);
  496. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pp_texture[i], 0);
  497. glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
  498. }
  499. // as part of post processing, use a quad for final rendering
  500. float ppScreenQuad[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  501. 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  502. 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
  503. 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
  504. };
  505. glGenBuffers(1, &finalRenderBuffer);
  506. glBindBuffer(GL_ARRAY_BUFFER, finalRenderBuffer);
  507. glBufferData(GL_ARRAY_BUFFER, sizeof(ppScreenQuad), ppScreenQuad, GL_STATIC_DRAW);
  508.  
  509. // shadowmap texture
  510. glGenTextures(1, &shadowmap_texture);
  511. glBindTexture(GL_TEXTURE_2D, shadowmap_texture);
  512. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  513. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  514. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  515. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  516. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  517. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
  518. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, ScreenWidth * 2, ScreenHeight * 2, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
  519. glBindTexture(GL_TEXTURE_2D, 0);
  520. // FBO attachment
  521. glGenFramebuffers(1, &shadowFBO);
  522. glBindFramebuffer(GL_FRAMEBUFFER_EXT, shadowFBO);
  523. glDrawBuffer(GL_NONE);
  524. glReadBuffer(GL_NONE);
  525. glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadowmap_texture, 0);
  526. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  527.  
  528. // channel 1
  529. // earth texture normals
  530. glActiveTexture(GL_TEXTURE1);
  531. glGenTextures(1, &earth_texture_norm);
  532. glBindTexture(GL_TEXTURE_2D, earth_texture_norm);
  533. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  534. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm2.GetWidth(), bm2.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm2.GetBits());
  535.  
  536. // channel 2
  537. // earth water texture normals
  538. glActiveTexture(GL_TEXTURE2);
  539. glGenTextures(1, &earthwater_texture_norm);
  540. glBindTexture(GL_TEXTURE_2D, earthwater_texture_norm);
  541. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  542. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm5.GetWidth(), bm5.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm5.GetBits());
  543.  
  544. // channel 3
  545. // earth texture night
  546. glActiveTexture(GL_TEXTURE3);
  547. glGenTextures(1, &earth_texture_night);
  548. glBindTexture(GL_TEXTURE_2D, earth_texture_night);
  549. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  550. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm11.GetWidth(), bm11.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm11.GetBits());
  551.  
  552. EarthShader.SendUniform("texture0", 0);
  553. EarthShader.SendUniform("textureNormal", 1);
  554. EarthShader.SendUniform("textureWaterNormal", 2);
  555. EarthShader.SendUniform("texture3", 3);
  556. EarthShader.SendUniform("NormalMap", 0); // boolean toggle for normal map
  557. AtmosphereShader.SendUniform("texture0", 0);
  558. ModelShader.SendUniform("texture0", 0);
  559. ModelShader.SendUniform("textureNormal", 1);
  560. ModelShader.SendUniform("NormalMap", 0); // boolean toggle for normal map
  561. BlurShader.SendUniform("texture0", 0);
  562. BloomShader.SendUniform("texture0", 4);
  563. BloomShader.SendUniform("texture1", 5);
  564. BloomShader.SendUniform("exposure", 1);
  565.  
  566. // setup ambient light
  567. EarthShader.SendUniform("lightAmbient.on", 1);
  568. EarthShader.SendUniform("lightAmbient.color", 1.0f, 1.0f, 1.0f);
  569. AtmosphereShader.SendUniform("lightAmbient.on", 1);
  570. AtmosphereShader.SendUniform("lightAmbient.color", 1.0f, 1.0f, 1.0f);
  571. ModelShader.SendUniform("lightAmbient.on", 1);
  572. ModelShader.SendUniform("lightAmbient.color", 1.0f, 1.0f, 1.0f);
  573.  
  574. // setup directional light
  575. //EarthShader.SendUniform("lightDir.on", 1);
  576. //EarthShader.SendUniform("lightDir.direction", -1.0f, 0.0f, 0.0f);
  577. //EarthShader.SendUniform("lightDir.diffuse", 1.2f, 1.2f, 1.2f);
  578. //EarthShader.SendUniform("lightDir.specular", 8.0, 8.0, 8.0);
  579. //AtmosphereShader.SendUniform("lightDir.on", 1);
  580. //AtmosphereShader.SendUniform("lightDir.direction", -1.0f, 0.0f, 0.0f);
  581. //AtmosphereShader.SendUniform("lightDir.diffuse", 1.2f, 1.2f, 1.2f);
  582. //ModelShader.SendUniform("lightDir.on", 1);
  583. //ModelShader.SendUniform("lightDir.direction", -1.0f, 0.0f, 0.0f);
  584. //ModelShader.SendUniform("lightDir.diffuse", 1.2f, 1.2f, 1.2f);
  585.  
  586. // point light
  587. EarthShader.SendUniform("lightPoint.on", 1);
  588. EarthShader.SendUniform("lightPoint.position", -2000.0f, 0.0f, 0.0f);
  589. EarthShader.SendUniform("lightPoint.diffuse", 1.6f, 1.6f, 1.6f);
  590. EarthShader.SendUniform("lightPoint.specular", 8.0, 8.0, 8.0);
  591. EarthShader.SendUniform("shininess", 30.0);
  592. AtmosphereShader.SendUniform("lightPoint.on", 1);
  593. AtmosphereShader.SendUniform("lightPoint.position", -2000.0f, 0.0f, 0.0f);
  594. AtmosphereShader.SendUniform("lightPoint.diffuse", 1.2f, 1.2f, 1.2f);
  595. ModelShader.SendUniform("lightPoint.on", 1);
  596. ModelShader.SendUniform("lightPoint.position", -2000.0f, 0.0f, 0.0f);
  597. ModelShader.SendUniform("lightPoint.diffuse", 1.2f, 1.2f, 1.2f);
  598. //AtmosphereShader.SendUniform("lightPoint.specular", 8.0, 8.0, 8.0);
  599.  
  600. // init variables
  601. particleAlpha = 1.0;
  602. sunDir[0] = -2000.0;
  603. sunDir[1] = 0.0f;
  604. sunOrigin = 0;
  605. sunRadius = 2000;
  606. middlecloudlayer = true;
  607. uppercloudlayer = true;
  608. atmospherelayer = true;
  609. lock[0] = true;
  610. lock[1] = true;
  611. charIndex = 0;
  612. viewIndex = 0;
  613. bool ppb_horizontal = true;
  614. bool ppb_first_iteration = true;
  615.  
  616. // fog
  617. //Program.SendUniform("fogColor", 0.10f, 0.10f, 0.10f);
  618. //Program.SendUniform("fogThickness", 0.07); // higher value for more thickness
  619.  
  620. // set-up view matrix
  621. glMatrixMode(GL_MODELVIEW);
  622. angleTilt = 15;
  623. glLoadIdentity();
  624. glRotatef(angleTilt, 1, 0, 0);
  625. gluLookAt(-1008.5f, 279.973f, 63.586f,
  626. 0.0f, 279.973f, 63.586f,
  627. 0.0, 1.0, 0.0);
  628. glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
  629.  
  630. return true;
  631. }
  632.  
  633. void done()
  634. {
  635. }
  636.  
  637. // misc functions
  638. void getCamPos()
  639. {
  640. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  641. //cameraVector[0] = -(cameraMatrix[0] * cameraMatrix[12] + cameraMatrix[1] * cameraMatrix[13] + cameraMatrix[2] * cameraMatrix[14]); // x
  642. //cameraVector[1] = -(cameraMatrix[4] * cameraMatrix[12] + cameraMatrix[5] * cameraMatrix[13] + cameraMatrix[6] * cameraMatrix[14]); // y
  643. //cameraVector[2] = -(cameraMatrix[8] * cameraMatrix[12] + cameraMatrix[9] * cameraMatrix[13] + cameraMatrix[10] * cameraMatrix[14]); // z
  644. gluInvertMatrix(matrix, inverseModelViewMatrix);
  645. cameraVector[0] = inverseModelViewMatrix[12];
  646. cameraVector[1] = inverseModelViewMatrix[13];
  647. cameraVector[2] = inverseModelViewMatrix[14];
  648. }
  649.  
  650. void getCamPosDisplay()
  651. {
  652. cout << "x, y, z \n";
  653. cout << cameraVector[0]; cout << "f, ";
  654. cout << cameraVector[1]; cout << "f, ";
  655. cout << cameraVector[2]; cout << "f, ";
  656. cout << "\n";
  657. }
  658.  
  659. void set2DMode()
  660. {
  661. glMatrixMode(GL_PROJECTION);
  662. glLoadIdentity();
  663. glOrtho(-1200, 1200, -1200, 1200, 0.5, 2300);
  664. }
  665.  
  666. void set3DMode(float w, float h)
  667. {
  668. glMatrixMode(GL_PROJECTION);
  669. glLoadIdentity();
  670. gluPerspective(120, (float)w / (float)h, 0.5, 1200);
  671. }
  672.  
  673. //void theSwitcheroo(bool boolarray[], int index)
  674. //{
  675. // for (int i = 0; i < sizeof(boolarray); i++) boolarray[i] = false;
  676. // boolarray[index] = true;
  677. //}
  678.  
  679. // text rendering
  680. //void setOrthographicProjection()
  681. //{
  682. // GLint viewport[4];
  683. // glGetIntegerv(GL_VIEWPORT, viewport);
  684. // int w = viewport[2];
  685. // int h = viewport[3];
  686. //
  687. // glMatrixMode(GL_PROJECTION);
  688. // glPushMatrix();
  689. // glLoadIdentity();
  690. // gluOrtho2D(0, w, 0, h);
  691. // glScalef(1, -1, 1);
  692. // glTranslatef(0, -h, 0);
  693. // glMatrixMode(GL_MODELVIEW);
  694. //}
  695.  
  696. //void resetPerspectiveProjection()
  697. //{
  698. // glMatrixMode(GL_PROJECTION);
  699. // glPopMatrix();
  700. // glMatrixMode(GL_MODELVIEW);
  701. //}
  702.  
  703. void prepareBitmapString(float x, float y, void *font)
  704. {
  705. glWindowPos2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
  706. for (char *p = charState[charIndex]; *p != '\0'; p++) {
  707. glutBitmapCharacter(font, *p);
  708. }
  709. }
  710.  
  711. void renderBitmapString(float r, float g, float b, float x, float y, void *font)
  712. {
  713. glColor3d(r, g, b);
  714. //setOrthographicProjection();
  715. //glPushMatrix();
  716. //glLoadIdentity();
  717. prepareBitmapString(x, y, font);
  718. //glPopMatrix();
  719. //resetPerspectiveProjection();
  720. }
  721.  
  722. // earth shading
  723. void renderEarth()
  724. {
  725. EarthShader.Use();
  726.  
  727. glActiveTexture(GL_TEXTURE0);
  728. glBindTexture(GL_TEXTURE_2D, earth_texture);
  729. glActiveTexture(GL_TEXTURE1);
  730. glBindTexture(GL_TEXTURE_2D, earth_texture_norm);
  731. glActiveTexture(GL_TEXTURE2);
  732. glBindTexture(GL_TEXTURE_2D, earthwater_texture_norm);
  733. glActiveTexture(GL_TEXTURE3);
  734. glBindTexture(GL_TEXTURE_2D, earth_texture_night);
  735. EarthShader.SendUniform("NormalMap", 1);
  736. EarthShader.SendUniform("materialDiffuse", 1.0f, 1.0f, 1.0f);
  737. EarthShader.SendUniform("materialAmbient", 0.1f, 0.1f, 0.1f);
  738. EarthShader.SendUniform("materialSpecular", 1.0f, 1.0f, 1.0f);
  739. glPushMatrix();
  740. glRotatef((sunTime * 100), 0.0f, 1.0f, 0.0f); // rotate the earth anti-clockwise to the skybox (skybox also rotates the earth model (?))
  741. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  742. EarthShader.SendUniform("matrixModelView", matrix);
  743. earth.render();
  744. glPopMatrix();
  745. EarthShader.SendUniform("NormalMap", 0);
  746. }
  747.  
  748. // atmosphere shading
  749. void renderUpperCloudLayer()
  750. {
  751. AtmosphereShader.Use();
  752.  
  753. glActiveTexture(GL_TEXTURE0);
  754. glBindTexture(GL_TEXTURE_2D, uppercloud_texture);
  755. AtmosphereShader.SendUniform("materialDiffuse", 0.7f, 0.7f, 0.7f);
  756. AtmosphereShader.SendUniform("materialAmbient", 0.5f, 0.5f, 0.5f);
  757. AtmosphereShader.SendUniform("upper", 1);
  758. glPushMatrix();
  759. glScalef(1.11f, 1.11f, 1.11f);
  760. glRotatef(sunTime * 100, 0.0f, 1.0f, 0.0f);
  761. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  762. AtmosphereShader.SendUniform("matrixModelView", matrix);
  763. earth.render();
  764. glPopMatrix();
  765. AtmosphereShader.SendUniform("upper", 0);
  766. }
  767.  
  768. void renderMiddleCloudLayer()
  769. {
  770. AtmosphereShader.Use();
  771.  
  772. glActiveTexture(GL_TEXTURE0);
  773. glBindTexture(GL_TEXTURE_2D, middlecloud_texture);
  774. AtmosphereShader.SendUniform("materialDiffuse", 0.7f, 0.7f, 0.7f);
  775. AtmosphereShader.SendUniform("materialAmbient", 0.5f, 0.5f, 0.5f);
  776. AtmosphereShader.SendUniform("middle", 1);
  777. glPushMatrix();
  778. glScalef(1.105f, 1.105f, 1.105f);
  779. glRotatef(sunTime * 100, 0.0f, 1.0f, 0.0f);
  780. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  781. AtmosphereShader.SendUniform("matrixModelView", matrix);
  782. earth.render();
  783. glPopMatrix();
  784. AtmosphereShader.SendUniform("middle", 0);
  785. }
  786.  
  787. void renderAtmosphereLayer()
  788. {
  789. AtmosphereShader.Use();
  790.  
  791. AtmosphereShader.SendUniform("materialDiffuse", 0.5f, 0.5f, 0.5f);
  792. AtmosphereShader.SendUniform("materialAmbient", 0.102f * 2.0, 0.175f * 2.0, 0.255f * 2.0);
  793. glPushMatrix();
  794. glScalef(1.115f, 1.115f, 1.115f);
  795. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  796. AtmosphereShader.SendUniform("matrixModelView", matrix);
  797. earth.render();
  798. glPopMatrix();
  799. }
  800.  
  801. // model shading
  802. void renderSkybox()
  803. {
  804. ModelShader.Use();
  805.  
  806. ModelShader.SendUniform("materialDiffuse", 0.0f, 0.0f, 0.0f);
  807. ModelShader.SendUniform("materialAmbient", 5.0f, 5.0f, 5.0f);
  808. glPushMatrix();
  809. glRotatef(-(sunTime * 100), 0.0f, 1.0f, 0.0f);
  810. skybox.render();
  811. glPopMatrix();
  812. }
  813.  
  814. void renderStStephensTower()
  815. {
  816. ModelShader.Use();
  817.  
  818. glActiveTexture(GL_TEXTURE0);
  819. glBindTexture(GL_TEXTURE_2D, st_stephens_tower_texture);
  820. ModelShader.SendUniform("materialDiffuse", 0.5f, 0.5f, 0.5f);
  821. ModelShader.SendUniform("materialAmbient", 0.5f, 0.5f, 0.5f);
  822. glPushMatrix();
  823. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  824. ModelShader.SendUniform("matrixModelView", matrix);
  825. st_stephens_tower.render();
  826. glPopMatrix();
  827. }
  828.  
  829. void renderEiffelTower()
  830. {
  831. ModelShader.Use();
  832.  
  833. glActiveTexture(GL_TEXTURE0);
  834. glBindTexture(GL_TEXTURE_2D, none_texture);
  835. ModelShader.SendUniform("materialDiffuse", 0.5f, 0.5f, 0.5f);
  836. ModelShader.SendUniform("materialAmbient", 0.255f, 0.204f, 0.153f);
  837. glPushMatrix();
  838. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  839. ModelShader.SendUniform("matrixModelView", matrix);
  840. eiffel_tower.render();
  841. glPopMatrix();
  842. }
  843.  
  844. void renderColosseum()
  845. {
  846. ModelShader.Use();
  847.  
  848. glActiveTexture(GL_TEXTURE0);
  849. glBindTexture(GL_TEXTURE_2D, colosseum_texture);
  850. ModelShader.SendUniform("materialDiffuse", 0.5f, 0.5f, 0.5f);
  851. ModelShader.SendUniform("materialAmbient", 0.255f, 0.204f, 0.153f);
  852. glPushMatrix();
  853. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  854. ModelShader.SendUniform("matrixModelView", matrix);
  855. colosseum.render();
  856. glPopMatrix();
  857. }
  858.  
  859. void renderSagradaFamilia()
  860. {
  861. ModelShader.Use();
  862.  
  863. glActiveTexture(GL_TEXTURE0);
  864. glBindTexture(GL_TEXTURE_2D, sagrada_familia_texture);
  865. ModelShader.SendUniform("materialDiffuse", 0.5f, 0.5f, 0.5f);
  866. ModelShader.SendUniform("materialAmbient", 0.255f, 0.204f, 0.153f);
  867. glPushMatrix();
  868. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  869. ModelShader.SendUniform("matrixModelView", matrix);
  870. sagrada_familia.render();
  871. glPopMatrix();
  872. }
  873.  
  874. void renderPyramidsOfGiza()
  875. {
  876. ModelShader.Use();
  877.  
  878. glActiveTexture(GL_TEXTURE0);
  879. glBindTexture(GL_TEXTURE_2D, pyramids_of_giza_texture);
  880. ModelShader.SendUniform("materialDiffuse", 0.5f, 0.5f, 0.5f);
  881. ModelShader.SendUniform("materialAmbient", 0.255f, 0.204f, 0.153f);
  882. glPushMatrix();
  883. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  884. ModelShader.SendUniform("matrixModelView", matrix);
  885. pyramids_of_giza.render();
  886. glPopMatrix();
  887. }
  888.  
  889. void renderRedSquare()
  890. {
  891. ModelShader.Use();
  892.  
  893. glActiveTexture(GL_TEXTURE0);
  894. glBindTexture(GL_TEXTURE_2D, red_square_texture);
  895. ModelShader.SendUniform("materialDiffuse", 0.5f, 0.5f, 0.5f);
  896. ModelShader.SendUniform("materialAmbient", 0.255f, 0.204f, 0.153f);
  897. glPushMatrix();
  898. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  899. ModelShader.SendUniform("matrixModelView", matrix);
  900. red_square.render();
  901. glPopMatrix();
  902. }
  903.  
  904. void renderGreatWallOfChina()
  905. {
  906. ModelShader.Use();
  907.  
  908. glActiveTexture(GL_TEXTURE0);
  909. glBindTexture(GL_TEXTURE_2D, great_wall_of_china_texture);
  910. ModelShader.SendUniform("materialDiffuse", 0.5f, 0.5f, 0.5f);
  911. ModelShader.SendUniform("materialAmbient", 0.255f, 0.204f, 0.153f);
  912. glPushMatrix();
  913. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  914. ModelShader.SendUniform("matrixModelView", matrix);
  915. great_wall_of_china.render();
  916. glPopMatrix();
  917. }
  918.  
  919. void renderEmpireStateBuilding()
  920. {
  921. ModelShader.Use();
  922.  
  923. glActiveTexture(GL_TEXTURE0);
  924. glBindTexture(GL_TEXTURE_2D, empire_state_building_texture);
  925. ModelShader.SendUniform("materialDiffuse", 0.5f, 0.5f, 0.5f);
  926. ModelShader.SendUniform("materialAmbient", 0.255f, 0.204f, 0.153f);
  927. glPushMatrix();
  928. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  929. ModelShader.SendUniform("matrixModelView", matrix);
  930. empire_state_building.render();
  931. glPopMatrix();
  932. }
  933.  
  934. void renderCube()
  935. {
  936. ModelShader.Use();
  937.  
  938. ModelShader.SendUniform("materialDiffuse", 10.0f, 10.0f, 10.0f);
  939. ModelShader.SendUniform("materialAmbient", 100.5f, 100.5f, 100.5f);
  940. glPushMatrix();
  941. glTranslatef(sunOrigin + cos(sunTime) * 600, 0.0f, sunOrigin + sin(sunTime) * 600);
  942. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  943. ModelShader.SendUniform("matrixModelView", matrix);
  944. glutSolidCube(50);
  945. glPopMatrix();
  946. }
  947.  
  948. // particle shading
  949. void renderParticleSystem()
  950. {
  951. ParticleShader.Use();
  952. glDepthMask(GL_FALSE);
  953. glActiveTexture(GL_TEXTURE0);
  954. glBindTexture(GL_TEXTURE_2D, snow_texture);
  955.  
  956. ParticleShader.SendUniform("time", glutGet(GLUT_ELAPSED_TIME) / 1000.f - 2);
  957. if (isNorth == true) ParticleShader.SendUniform("initialPos", cameraVector[0], cameraVector[1] + 10, cameraVector[2]);
  958. if (isSouth == true) ParticleShader.SendUniform("initialPos", cameraVector[0], cameraVector[1] - 10, cameraVector[2]);
  959.  
  960. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  961. ParticleShader.SendUniform("matrixModelView", matrix);
  962.  
  963. glEnableVertexAttribArray(0);
  964. glEnableVertexAttribArray(1);
  965. glBindBuffer(GL_ARRAY_BUFFER, idBufferVelocity);
  966. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  967. glBindBuffer(GL_ARRAY_BUFFER, idBufferStartTime);
  968. glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, 0);
  969. glDrawArrays(GL_POINTS, 0, NPARTICLES);
  970. glDisableVertexAttribArray(0);
  971. glDisableVertexAttribArray(1);
  972.  
  973. glDepthMask(GL_TRUE);
  974. }
  975.  
  976. // misc (non-shading)
  977. // text rendering
  978. void renderText()
  979. {
  980. // unfortunately the text must be rendered after the first switch statement and object rendering as it must clear the matrix stack
  981. // so this switch must be placed here
  982. switch (viewIndex)
  983. {
  984. case 0:
  985. charIndex = 0; renderBitmapString(1.0f, 1.0f, 1.0f, 25, 40, (void*)font);
  986. if (lock[0])
  987. {
  988. charIndex = 9; renderBitmapString(1.0f, 0.0f, 0.0f, 25.0f, 65.0f, (void*)font);
  989. charIndex = 10; renderBitmapString(1.0f, 0.0f, 0.0f, 25.0f, 90.0f, (void*)font);
  990. charIndex = 11; renderBitmapString(1.0f, 0.0f, 0.0f, 25.0f, 115.0f, (void*)font);
  991. }
  992. break;
  993. case 1:
  994. charIndex = 1; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 40.0f, (void*)font);
  995. if (lock[1])
  996. {
  997. charIndex = 9; renderBitmapString(1.0f, 0.0f, 0.0f, 25.0f, 65.0f, (void*)font);
  998. charIndex = 10; renderBitmapString(1.0f, 0.0f, 0.0f, 25.0f, 90.0f, (void*)font);
  999. charIndex = 11; renderBitmapString(1.0f, 0.0f, 0.0f, 25.0f, 115.0f, (void*)font);
  1000. }
  1001. break;
  1002. case 2:
  1003. charIndex = 2; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 40.0f, (void*)font);
  1004. charIndex = 16; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 135.0f, (void*)font2);
  1005. charIndex = 17; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 147.0f, (void*)font2);
  1006. charIndex = 18; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 159.0f, (void*)font2);
  1007. charIndex = 19; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 171.0f, (void*)font2);
  1008. charIndex = 20; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 195.0f, (void*)font2);
  1009. charIndex = 21; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 207.0f, (void*)font2);
  1010. charIndex = 22; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 219.0f, (void*)font2);
  1011. charIndex = 23; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 231.0f, (void*)font2);
  1012. charIndex = 24; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 243.0f, (void*)font2);
  1013. charIndex = 25; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 255.0f, (void*)font2);
  1014. break;
  1015. case 3:
  1016. charIndex = 3; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 40.0f, (void*)font);
  1017. charIndex = 26; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 135.0f, (void*)font2);
  1018. charIndex = 27; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 147.0f, (void*)font2);
  1019. charIndex = 28; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 159.0f, (void*)font2);
  1020. charIndex = 29; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 171.0f, (void*)font2);
  1021. charIndex = 30; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 195.0f, (void*)font2);
  1022. charIndex = 31; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 207.0f, (void*)font2);
  1023. charIndex = 32; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 219.0f, (void*)font2);
  1024. charIndex = 33; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 231.0f, (void*)font2);
  1025. charIndex = 34; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 243.0f, (void*)font2);
  1026. charIndex = 35; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 255.0f, (void*)font2);
  1027. break;
  1028. case 4:
  1029. charIndex = 4; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 40.0f, (void*)font);
  1030. charIndex = 36; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 135.0f, (void*)font2);
  1031. charIndex = 37; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 147.0f, (void*)font2);
  1032. charIndex = 38; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 159.0f, (void*)font2);
  1033. charIndex = 39; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 171.0f, (void*)font2);
  1034. charIndex = 40; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 195.0f, (void*)font2);
  1035. charIndex = 41; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 207.0f, (void*)font2);
  1036. charIndex = 42; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 219.0f, (void*)font2);
  1037. charIndex = 43; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 231.0f, (void*)font2);
  1038. charIndex = 44; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 243.0f, (void*)font2);
  1039. charIndex = 45; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 255.0f, (void*)font2);
  1040. break;
  1041. case 5:
  1042. charIndex = 5; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 40.0f, (void*)font);
  1043. charIndex = 46; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 135.0f, (void*)font2);
  1044. charIndex = 47; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 147.0f, (void*)font2);
  1045. charIndex = 48; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 159.0f, (void*)font2);
  1046. charIndex = 49; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 171.0f, (void*)font2);
  1047. charIndex = 50; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 195.0f, (void*)font2);
  1048. charIndex = 51; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 207.0f, (void*)font2);
  1049. charIndex = 52; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 219.0f, (void*)font2);
  1050. charIndex = 53; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 231.0f, (void*)font2);
  1051. charIndex = 54; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 243.0f, (void*)font2);
  1052. charIndex = 55; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 255.0f, (void*)font2);
  1053. break;
  1054. case 6:
  1055. charIndex = 6; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 40.0f, (void*)font);
  1056. charIndex = 56; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 135.0f, (void*)font2);
  1057. charIndex = 57; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 147.0f, (void*)font2);
  1058. charIndex = 58; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 159.0f, (void*)font2);
  1059. charIndex = 59; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 171.0f, (void*)font2);
  1060. charIndex = 60; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 195.0f, (void*)font2);
  1061. charIndex = 61; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 207.0f, (void*)font2);
  1062. charIndex = 62; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 219.0f, (void*)font2);
  1063. charIndex = 63; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 231.0f, (void*)font2);
  1064. charIndex = 64; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 243.0f, (void*)font2);
  1065. charIndex = 65; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 255.0f, (void*)font2);
  1066. break;
  1067. case 7:
  1068. charIndex = 7; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 40.0f, (void*)font);
  1069. charIndex = 66; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 135.0f, (void*)font2);
  1070. charIndex = 67; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 147.0f, (void*)font2);
  1071. charIndex = 68; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 159.0f, (void*)font2);
  1072. charIndex = 69; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 171.0f, (void*)font2);
  1073. charIndex = 70; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 195.0f, (void*)font2);
  1074. charIndex = 71; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 207.0f, (void*)font2);
  1075. charIndex = 72; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 219.0f, (void*)font2);
  1076. charIndex = 73; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 231.0f, (void*)font2);
  1077. charIndex = 74; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 243.0f, (void*)font2);
  1078. charIndex = 75; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 255.0f, (void*)font2);
  1079. break;
  1080. case 8:
  1081. charIndex = 8; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 40.0f, (void*)font);
  1082. charIndex = 76; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 135.0f, (void*)font2);
  1083. charIndex = 77; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 147.0f, (void*)font2);
  1084. charIndex = 78; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 159.0f, (void*)font2);
  1085. charIndex = 79; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 171.0f, (void*)font2);
  1086. charIndex = 80; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 195.0f, (void*)font2);
  1087. charIndex = 81; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 207.0f, (void*)font2);
  1088. charIndex = 82; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 219.0f, (void*)font2);
  1089. charIndex = 83; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 231.0f, (void*)font2);
  1090. charIndex = 84; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 243.0f, (void*)font2);
  1091. charIndex = 85; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 255.0f, (void*)font2);
  1092. break;
  1093. }
  1094. if (viewIndex == 2 || viewIndex == 3 || viewIndex == 4 || viewIndex == 5 || viewIndex == 6 || viewIndex == 7 || viewIndex == 8 || !lock[1] && viewIndex == 1 || !lock[0] && viewIndex == 0)
  1095. {
  1096. charIndex = 12; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 70.0f, (void*)font2);
  1097. charIndex = 13; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 82.0f, (void*)font2);
  1098. charIndex = 14; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 94.0f, (void*)font2);
  1099. charIndex = 15; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 106.0f, (void*)font2);
  1100. }
  1101. else
  1102. {
  1103. charIndex = 12; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 135.0f, (void*)font2);
  1104. charIndex = 13; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 147.0f, (void*)font2);
  1105. charIndex = 14; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 159.0f, (void*)font2);
  1106. charIndex = 15; renderBitmapString(1.0f, 1.0f, 1.0f, 25.0f, 171.0f, (void*)font2);
  1107. }
  1108. if (viewIndex > 8) viewIndex = 0;
  1109. }
  1110.  
  1111. // set camera view
  1112. void setCameraView(float x, float y, float z, float centerx, float centery, float centerz, float upx, float upy, float upz, int angleTilt)
  1113. {
  1114. glMatrixMode(GL_MODELVIEW);
  1115. glLoadIdentity();
  1116. glRotatef(angleTilt, 1, 0, 0);
  1117. gluLookAt(x, y, z, centerx, centery, centerz, upx, upy, upz);
  1118. glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
  1119. }
  1120.  
  1121. // render post-processing screen quad
  1122. void renderScreenQuad(int a)
  1123. {
  1124. glMatrixMode(GL_PROJECTION);
  1125. glLoadIdentity();
  1126. glOrtho(0, 1, 0, 1, -1, 1);
  1127. glGetFloatv(GL_PROJECTION_MATRIX, matrix);
  1128. if (a == 1) BlurShader.SendUniform("matrixProjection", matrix);
  1129. else BloomShader.SendUniform("matrixProjection", matrix);
  1130.  
  1131. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  1132. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1133.  
  1134. glMatrixMode(GL_MODELVIEW);
  1135. glLoadIdentity();
  1136. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  1137. if (a == 1) BlurShader.SendUniform("matrixModelView", matrix);
  1138. else BloomShader.SendUniform("matrixModelView", matrix);
  1139.  
  1140. if (a == 1)
  1141. {
  1142. GLuint attribVertex = BlurShader.GetAttribLocation("aVertex");
  1143. GLuint attribTextCoord = BlurShader.GetAttribLocation("aTexCoord");
  1144. glEnableVertexAttribArray(attribVertex);
  1145. glEnableVertexAttribArray(attribTextCoord);
  1146. glBindBuffer(GL_ARRAY_BUFFER, finalRenderBuffer);
  1147. glVertexAttribPointer(attribVertex, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
  1148. glVertexAttribPointer(attribTextCoord, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
  1149. glDrawArrays(GL_QUADS, 0, 4);
  1150. glDisableVertexAttribArray(attribVertex);
  1151. glDisableVertexAttribArray(attribTextCoord);
  1152. }
  1153. else
  1154. {
  1155. GLuint attribVertex = BloomShader.GetAttribLocation("aVertex");
  1156. GLuint attribTextCoord = BloomShader.GetAttribLocation("aTexCoord");
  1157. glEnableVertexAttribArray(attribVertex);
  1158. glEnableVertexAttribArray(attribTextCoord);
  1159. glBindBuffer(GL_ARRAY_BUFFER, finalRenderBuffer);
  1160. glVertexAttribPointer(attribVertex, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
  1161. glVertexAttribPointer(attribTextCoord, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
  1162. glDrawArrays(GL_QUADS, 0, 4);
  1163. glDisableVertexAttribArray(attribVertex);
  1164. glDisableVertexAttribArray(attribTextCoord);
  1165. }
  1166. }
  1167.  
  1168. void prepareShadowMatrix()
  1169. {
  1170. // matrix to transfrom coordinates x,y,z
  1171. // x = x* 0.5 + 0.5
  1172. // y = y* 0.5 + 0.5
  1173. // z = z* 0.5 + 0.5
  1174. // Moving from unit cube [-1,1] to [0,1]
  1175. const float bias[16] = {
  1176. 0.5, 0.0, 0.0, 0.0,
  1177. 0.0, 0.5, 0.0, 0.0,
  1178. 0.0, 0.0, 0.5, 0.0,
  1179. 0.5, 0.5, 0.5, 1.0 };
  1180.  
  1181. // grab modelview and transformation matrices
  1182. static float modelView[16];
  1183. static float projection[16];
  1184. glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
  1185. glGetFloatv(GL_PROJECTION_MATRIX, projection);
  1186.  
  1187. // concatenating all matrices into one
  1188. glPushMatrix();
  1189. glLoadIdentity();
  1190. glLoadMatrixf(bias);
  1191. glMultMatrixf(projection);
  1192. glMultMatrixf(modelView);
  1193.  
  1194. // store and send as uniform variable
  1195. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  1196. EarthShader.SendUniform("matrixShadow", matrix);
  1197. ModelShader.SendUniform("matrixShadow", matrix);
  1198. glPopMatrix();
  1199. }
  1200.  
  1201. void createShadowMap(float x, float y, float z,
  1202. float centerx, float centery, float centerz,
  1203. float upx, float upy, float upz, GLuint FBOid)
  1204. {
  1205. glEnable(GL_CULL_FACE);
  1206. glCullFace(GL_BACK);
  1207.  
  1208. // enable alpha test to discard transparency
  1209. glAlphaFunc(GL_GREATER, 0.9);
  1210. glEnable(GL_ALPHA_TEST);
  1211.  
  1212. // Store the current viewport in a safe place
  1213. GLint viewport[4];
  1214. glGetIntegerv(GL_VIEWPORT, viewport);
  1215. int w = viewport[2];
  1216. int h = viewport[3];
  1217.  
  1218. // setup the viewport
  1219. glViewport(0, 0, w * 2, h * 2);
  1220.  
  1221. // change to orthographic camera (directional light)
  1222. set2DMode();
  1223.  
  1224. // send projection matrix
  1225. glGetFloatv(GL_PROJECTION_MATRIX, matrix);
  1226. AtmosphereShader.SendUniform("matrixProjection", matrix);
  1227. EarthShader.SendUniform("matrixProjection", matrix);
  1228. ModelShader.SendUniform("matrixProjection", matrix);
  1229.  
  1230. // prepare the camera
  1231. glMatrixMode(GL_MODELVIEW);
  1232. glLoadIdentity();
  1233. gluLookAt(x, y, z, centerx, centery, centerz, upx, upy, upz);
  1234.  
  1235. // then send view matrix
  1236. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  1237. AtmosphereShader.SendUniform("matrixView", matrix);
  1238. EarthShader.SendUniform("matrixView", matrix);
  1239. ModelShader.SendUniform("matrixView", matrix);
  1240.  
  1241. // bind FBO
  1242. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, shadowFBO);
  1243. // -- off screen rendering
  1244.  
  1245. // clear previous frame depth values
  1246. glClear(GL_DEPTH_BUFFER_BIT);
  1247.  
  1248. // only enable alpha channel
  1249. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  1250.  
  1251. // call prepare shadow matrix function
  1252. prepareShadowMatrix();
  1253.  
  1254. // render objects that cast a shadow
  1255. renderEarth();
  1256. renderStStephensTower();
  1257. renderEiffelTower();
  1258. renderColosseum();
  1259. renderSagradaFamilia();
  1260. renderPyramidsOfGiza();
  1261. renderRedSquare();
  1262. renderGreatWallOfChina();
  1263. renderEmpireStateBuilding();
  1264. if (uppercloudlayer) renderUpperCloudLayer();
  1265. if (middlecloudlayer) renderMiddleCloudLayer();
  1266.  
  1267. // revert (back to currently bound FBO)
  1268. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBOid);
  1269. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  1270. glDisable(GL_CULL_FACE);
  1271. glDisable(GL_ALPHA_TEST);
  1272. reshape(w, h);
  1273. }
  1274.  
  1275. void render()
  1276. {
  1277. // get window height for text rendering
  1278. abs_screenHeight = glutGet(GLUT_WINDOW_HEIGHT);
  1279.  
  1280. // view position
  1281. //getCamPos();
  1282. //getCamPosDisplay();
  1283.  
  1284. // time (returns in milliseconds)
  1285. time = glutGet(GLUT_ELAPSED_TIME);
  1286. EarthShader.SendUniform("time", time);
  1287. AtmosphereShader.SendUniform("time", time);
  1288.  
  1289. // sun motion
  1290. // x = Xorigin + cos(angle)*radius;
  1291. // y = Yorigin + sin(angle)*radius;
  1292. sunTime = time / 10000;
  1293. sunDir[0] = sunOrigin + cos(sunTime) * sunRadius;
  1294. sunDir[1] = sunOrigin + sin(sunTime) * sunRadius;
  1295. // send uniforms
  1296. EarthShader.SendUniform("lightPoint.position", sunDir[0], 0.0f, sunDir[1]);
  1297. AtmosphereShader.SendUniform("lightPoint.position", sunDir[0], 0.0f, sunDir[1]);
  1298. ModelShader.SendUniform("lightPoint.position", sunDir[0], 0.0f, sunDir[1]);
  1299.  
  1300. // ---- first and second render passes: render everything to be post processed or have their brightness extracted
  1301. for (int i = 0; i < 2; i++)
  1302. {
  1303. glBindFramebufferEXT(GL_FRAMEBUFFER, ppFBO[i]);
  1304.  
  1305. // create shadow map
  1306. createShadowMap(sunDir[0], 0.0f, sunDir[1], 0.0f, 0.0f, 0.0f, 0.0, 1.0, 0.0, ppFBO[i]);
  1307.  
  1308. // clear screen and buffers
  1309. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  1310. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1311.  
  1312. // view settings for locations
  1313. switch (viewIndex)
  1314. {
  1315. case 0:
  1316. if (lock[0]) setCameraView(-1008.5f, 279.973f, 63.586f, 0.0f, 279.973f, 63.586f, 0.0, 1.0, 0.0, 15);
  1317. break;
  1318. case 1:
  1319. if (lock[1]) setCameraView(0.0f, 545.0f, 0.0f, 10.0f, 545.0f, 0.0f, 0.0, 1.0, 0.0, 15);
  1320. break;
  1321. case 2: setCameraView(-480.885f, 499.004f, 179.741f, -307.241f, 378.114f, 107.816f, 0.0, 1.0, 0.0, 0);
  1322. break;
  1323. case 3: setCameraView(-816.507f, 161.529f, 389.861f, -442.657f, 60.8567f, 217.035f, 0.0, 1.0, 0.0, 0);
  1324. break;
  1325. case 4: setCameraView(64.7304f, 466.521f, 792.111f, 64.7301f, 266.844f, 416.57f, 0.0, 1.0, 0.0, 0);
  1326. break;
  1327. case 5: setCameraView(637.171f, -288.431f, 416.492f, 384.7f, -197.045f, 247.794f, 0.0, 1.0, 0.0, 0);
  1328. break;
  1329. case 6: setCameraView(126.285f, 464.575f, -692.372f, 72.6584f, 305.521f, -388.231f, 0.0, 1.0, 0.0, 0);
  1330. break;
  1331. case 7: setCameraView(-443.415f, -257.073f, -739.343f, -249.001f, -160.872f, -396.794f, 0.0, 1.0, 0.0, 0);
  1332. break;
  1333. case 8: setCameraView(0.7432f, -917.147f, 0.0556f, 0.0f, 0.0f, 0.0f, 0.0, 1.0, 0.0, 0);
  1334. break;
  1335. }
  1336.  
  1337. // setup the View Matrix (camera)
  1338. glMatrixMode(GL_MODELVIEW);
  1339. glLoadIdentity();
  1340. if (viewIndex != 0) glRotatef(angleTilt, 1, 0, 0); // switch tilt off --------
  1341. glTranslatef(deltaX, deltaY, deltaZ); // animate camera motion (controlled by WASD keys) |
  1342. if (viewIndex == 1 && !lock[1]) glRotatef(-deltaX / 9.40, 0, 0, 1); // control Z rotation with W/S keys |--- turn these both off for 'noclip' movement (outterearth free movement)
  1343. if (viewIndex == 1 && !lock[1]) glRotatef(deltaZ / 9.40, 1, 0, 0); // control X rotation with A/D keys |
  1344. if (viewIndex != 0) glRotatef(-angleTilt, 1, 0, 0); // switch tilt on --------
  1345. glMultMatrixf(matrixView);
  1346. glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
  1347. if (viewIndex == 2 || viewIndex == 3 || viewIndex == 4 || viewIndex == 5 || viewIndex == 6 || viewIndex == 7 || viewIndex == 8) // set camera lock on any viewIndex that isn't inside the atmosphere
  1348. {
  1349. lock[0] = true;
  1350. lock[1] = true;
  1351. }
  1352.  
  1353. // setup View Matrix
  1354. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  1355. EarthShader.SendUniform("matrixView", matrix);
  1356. AtmosphereShader.SendUniform("matrixView", matrix);
  1357. ModelShader.SendUniform("matrixView", matrix);
  1358.  
  1359. // send shadowmap and shadow strength
  1360. EarthShader.SendUniform("shadowMap", 7);
  1361. ModelShader.SendUniform("shadowMap", 7);
  1362. glActiveTexture(GL_TEXTURE7);
  1363. glBindTexture(GL_TEXTURE_2D, shadowmap_texture);
  1364.  
  1365. // render objects
  1366. // reverse rendering order needed to see through transparency textures
  1367. renderSkybox();
  1368. // for inner earth view
  1369. // extract only the brightness from the scene in second render pass
  1370. if (i == 1)
  1371. {
  1372. EarthShader.SendUniform("brightnessTest", true);
  1373. AtmosphereShader.SendUniform("brightnessTest", true);
  1374. ModelShader.SendUniform("brightnessTest", true);
  1375. // change the alpha of the atmosphere's bloom post-processing depending on the view
  1376. if (viewIndex == 1) AtmosphereShader.SendUniform("bloomView", true);
  1377. else AtmosphereShader.SendUniform("bloomView", false);
  1378. }
  1379. else
  1380. {
  1381. EarthShader.SendUniform("brightnessTest", false);
  1382. AtmosphereShader.SendUniform("brightnessTest", false);
  1383. ModelShader.SendUniform("brightnessTest", false);
  1384. }
  1385. if (viewIndex == 1)
  1386. {
  1387. renderEarth();
  1388. if (atmospherelayer) renderAtmosphereLayer();
  1389. if (uppercloudlayer) renderUpperCloudLayer();
  1390. if (middlecloudlayer) renderMiddleCloudLayer();
  1391. renderStStephensTower();
  1392. renderEiffelTower();
  1393. renderColosseum();
  1394. renderSagradaFamilia();
  1395. renderPyramidsOfGiza();
  1396. renderRedSquare();
  1397. renderGreatWallOfChina();
  1398. renderEmpireStateBuilding();
  1399. //renderCube();
  1400. //renderEarthMap();
  1401. }
  1402. // for outter earth view
  1403. else
  1404. {
  1405. renderEarth();
  1406. renderStStephensTower();
  1407. renderEiffelTower();
  1408. renderColosseum();
  1409. renderSagradaFamilia();
  1410. renderPyramidsOfGiza();
  1411. renderRedSquare();
  1412. renderGreatWallOfChina();
  1413. renderEmpireStateBuilding();
  1414. if (middlecloudlayer) renderMiddleCloudLayer();
  1415. if (uppercloudlayer) renderUpperCloudLayer();
  1416. if (atmospherelayer) renderAtmosphereLayer();
  1417. //renderCube();
  1418. //renderEarthMap();
  1419. }
  1420.  
  1421. // particle system rendering / conditions
  1422. // based upon camera position -- variables needed to calculate camera position must be placed after any clears to the matrix stack
  1423. getCamPos();
  1424.  
  1425. if (particleAlpha > 0) renderParticleSystem();
  1426. if (particleAlpha < 0) particleAlpha = 0;
  1427. if (particleAlpha > 1) particleAlpha = 1;
  1428. if (isNorth == true) ParticleShader.SendUniform("gravity", 0.0f, -0.5f, 0.0f);
  1429. else if (isSouth == true) ParticleShader.SendUniform("gravity", 0.0f, 0.5f, 0.0f);
  1430. if (cameraVector[1] > 500.0f && cameraVector[1] < 550.0f || cameraVector[1] < 500.0f && cameraVector[1] > 400.0f) { isNorth = true; isSouth = false; }
  1431. else if (cameraVector[1] < -500.0f && cameraVector[1] > -550.0f || cameraVector[1] > -500.0f && cameraVector[1] < -400.0f) { isSouth = true; isNorth = false; }
  1432. if (cameraVector[1] > 500.0f && cameraVector[1] < 550.0f || cameraVector[1] < -500.0f && cameraVector[1] > -550.0f) particleAlpha += 0.005f;
  1433. else if (cameraVector[1] < 500.0f && cameraVector[1] > 400.0f || cameraVector[1] > -500.0f && cameraVector[1] < -400.0f || cameraVector[1] > 550.0f || cameraVector[1] < -550.0f) particleAlpha -= 0.005f;
  1434. else particleAlpha -= 0.005f;
  1435. ParticleShader.SendUniform("particleAlpha", particleAlpha);
  1436. }
  1437.  
  1438. // ---- third and fourth render pass of pre-post-processing to blur the final output texture in verticle and horizontal
  1439. // change the sample rate based on viewindex - if innerearth view, turn down bloom samples, if outterearth view switch them up for better effect
  1440. if (viewIndex == 1) BlurShader.SendUniform("samples", 1);
  1441. else BlurShader.SendUniform("samples", 5);
  1442. glBindFramebufferEXT(GL_FRAMEBUFFER, ppFBO[2]);
  1443. BlurShader.Use();
  1444. BlurShader.SendUniform("horizontal", true);
  1445. glActiveTexture(GL_TEXTURE0);
  1446. glBindTexture(GL_TEXTURE_2D, pp_texture[1]);
  1447. renderScreenQuad(1);
  1448.  
  1449. glBindFramebufferEXT(GL_FRAMEBUFFER, ppFBO[3]);
  1450. BlurShader.Use();
  1451. BlurShader.SendUniform("horizontal", false);
  1452. glBindTexture(GL_TEXTURE_2D, pp_texture[2]);
  1453. renderScreenQuad(1);
  1454. // ---- fifth render pass: finally render the quad to screen with additive blending of textures bound to the ppFBO array [0] and [3]
  1455. // ppFBO[0] = colour
  1456. // ppFBO[1] = brightness
  1457. // ppFBO[2] = horizontal blur
  1458. // ppFBO[3] = horizontal + verticle blur
  1459.  
  1460. glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
  1461. BloomShader.Use();
  1462. glActiveTexture(GL_TEXTURE4);
  1463. glBindTexture(GL_TEXTURE_2D, pp_texture[3]);
  1464. glActiveTexture(GL_TEXTURE5);
  1465. glBindTexture(GL_TEXTURE_2D, pp_texture[0]);
  1466. renderScreenQuad(0);
  1467.  
  1468. // -- render text on top of any post processing effects
  1469. renderText();
  1470.  
  1471. //reshape(w, h);
  1472.  
  1473. // essential for double-buffering technique
  1474. glutSwapBuffers();
  1475.  
  1476. // proceed the animation
  1477. glutPostRedisplay();
  1478. }
  1479.  
  1480. void reshape(int w, int h)
  1481. {
  1482. // find screen aspect ratio
  1483. float ratio = w * 1.0f / h;
  1484.  
  1485. // setup the projection matrix
  1486. glMatrixMode(GL_PROJECTION);
  1487. glLoadIdentity();
  1488. glViewport(0, 0, w, h);
  1489. gluPerspective(60.0f, ratio, 0.02f, 1000.0f);
  1490.  
  1491. float matrixProjection[16];
  1492. glGetFloatv(GL_PROJECTION_MATRIX, matrixProjection);
  1493. EarthShader.SendUniform("matrixProjection", matrixProjection);
  1494. AtmosphereShader.SendUniform("matrixProjection", matrixProjection);
  1495. ModelShader.SendUniform("matrixProjection", matrixProjection);
  1496. ParticleShader.SendUniform("matrixProjection", matrixProjection);
  1497. BlurShader.SendUniform("matrixProjection", matrixProjection);
  1498. BloomShader.SendUniform("matrixProjection", matrixProjection);
  1499.  
  1500. ParticleShader.SendUniform("scaleFactor", (float)h / 720.f);
  1501.  
  1502. // set model/view matrix for rendering
  1503. glMatrixMode(GL_MODELVIEW);
  1504.  
  1505. // adjust shadow map size based on window size * 2
  1506. glBindTexture(GL_TEXTURE_2D, shadowmap_texture);
  1507. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, w * 2, h * 2, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
  1508.  
  1509. // adjust post processing texture size based on window size
  1510. for (int i = 0; i < 4; i++)
  1511. {
  1512. glBindTexture(GL_TEXTURE_2D, pp_texture[i]);
  1513. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  1514. }
  1515. }
  1516.  
  1517. // Handle WASDQE keys
  1518. void onKeyDown(unsigned char key, int x, int y)
  1519. {
  1520. switch (tolower(key))
  1521. {
  1522. case 'w': deltaZ = max(deltaZ * 1.05f, 0.01f); break;
  1523. case 's': deltaZ = min(deltaZ * 1.05f, -0.01f); break;
  1524. case 'a': deltaX = max(deltaX * 1.05f, 0.01f); break;
  1525. case 'd': deltaX = min(deltaX * 1.05f, -0.01f); break;
  1526. case 'e': deltaY = max(deltaY * 1.05f, 0.01f); break;
  1527. case 'q': deltaY = min(deltaY * 1.05f, -0.01f); break;
  1528. case '1': middlecloudlayer = !middlecloudlayer; break;
  1529. case '2': uppercloudlayer = !uppercloudlayer; break;
  1530. case '3': atmospherelayer = !atmospherelayer; break;
  1531. }
  1532. // speed limit
  1533. deltaX = max(-0.90f, min(0.90f, deltaX));
  1534. deltaY = max(-0.90f, min(0.90f, deltaY));
  1535. deltaZ = max(-0.90f, min(0.90f, deltaZ));
  1536. }
  1537.  
  1538. // Handle WASDQE keys (key up)
  1539. void onKeyUp(unsigned char key, int x, int y)
  1540. {
  1541. switch (tolower(key))
  1542. {
  1543. case 'w': deltaZ = 0; getCamPos(); getCamPosDisplay(); break;
  1544. case 's': deltaZ = 0; break;
  1545. case 'a':
  1546. case 'd': deltaX = 0; break;
  1547. case 'q':
  1548. case 'e': deltaY = 0; break;
  1549. case ' ':
  1550. if (viewIndex == 0) lock[0] = false;
  1551. if (viewIndex == 1) lock[1] = false;
  1552. break;
  1553. case '4': viewIndex++; break;
  1554. }
  1555. }
  1556.  
  1557. // Handle arrow keys and Alt+F4
  1558. void onSpecDown(int key, int x, int y)
  1559. {
  1560. switch (key)
  1561. {
  1562. case GLUT_KEY_F4: if ((glutGetModifiers() & GLUT_ACTIVE_ALT) != 0) exit(0); break;
  1563. case GLUT_KEY_UP: onKeyDown('w', x, y); break;
  1564. case GLUT_KEY_DOWN: onKeyDown('s', x, y); break;
  1565. case GLUT_KEY_LEFT: onKeyDown('a', x, y); break;
  1566. case GLUT_KEY_RIGHT: onKeyDown('d', x, y); break;
  1567. case GLUT_KEY_PAGE_UP: onKeyDown('q', x, y); break;
  1568. case GLUT_KEY_PAGE_DOWN:onKeyDown('e', x, y); break;
  1569. case GLUT_KEY_F11: glutFullScreenToggle();
  1570. }
  1571. }
  1572.  
  1573. // Handle arrow keys (key up)
  1574. void onSpecUp(int key, int x, int y)
  1575. {
  1576. switch (key)
  1577. {
  1578. case GLUT_KEY_UP: onKeyUp('w', x, y); break;
  1579. case GLUT_KEY_DOWN: onKeyUp('s', x, y); break;
  1580. case GLUT_KEY_LEFT: onKeyUp('a', x, y); break;
  1581. case GLUT_KEY_RIGHT: onKeyUp('d', x, y); break;
  1582. case GLUT_KEY_PAGE_UP: onKeyUp('q', x, y); break;
  1583. case GLUT_KEY_PAGE_DOWN:onKeyUp('e', x, y); break;
  1584. }
  1585. }
  1586.  
  1587. // Handle mouse click
  1588. void onMouse(int button, int state, int x, int y)
  1589. {
  1590. int cx = glutGet(GLUT_WINDOW_WIDTH) / 2;
  1591. int cy = glutGet(GLUT_WINDOW_HEIGHT) / 2;
  1592.  
  1593. if (state == GLUT_DOWN)
  1594. {
  1595. //glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  1596. glutWarpPointer(cx, cy);
  1597. }
  1598. else
  1599. glutSetCursor(GLUT_CURSOR_INHERIT);
  1600. }
  1601.  
  1602. // handle mouse move
  1603. void onMotion(int x, int y)
  1604. {
  1605. int cx = glutGet(GLUT_WINDOW_WIDTH) / 2;
  1606. int cy = glutGet(GLUT_WINDOW_HEIGHT) / 2;
  1607. if (x == cx && y == cy)
  1608. return; // caused by glutWarpPointer
  1609.  
  1610. float amp = 0.25;
  1611. float deltaTilt = amp * (y - cy);
  1612. float deltaPan = amp * (x - cx);
  1613.  
  1614. glutWarpPointer(cx, cy);
  1615.  
  1616. // handle camera tilt (mouse move up & down)
  1617. glMatrixMode(GL_MODELVIEW);
  1618. glLoadIdentity();
  1619. glRotatef(deltaTilt, 1, 0, 0);
  1620. glMultMatrixf(matrixView);
  1621. glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
  1622.  
  1623. angleTilt += deltaTilt;
  1624.  
  1625. // handle camera pan (mouse move left & right)
  1626. glMatrixMode(GL_MODELVIEW);
  1627. glLoadIdentity();
  1628. glRotatef(angleTilt, 1, 0, 0);
  1629. glRotatef(deltaPan, 0, 1, 0);
  1630. glRotatef(-angleTilt, 1, 0, 0);
  1631. glMultMatrixf(matrixView);
  1632. glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
  1633. }
  1634.  
  1635. int main(int argc, char **argv)
  1636. {
  1637. // init GLUT and create Window
  1638. glutInit(&argc, argv);
  1639. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  1640. glutInitWindowPosition(100, 100);
  1641. glutInitWindowSize(800, 600);
  1642. glutCreateWindow("Earth-2018 (Hero Demo, Phoenix Mee (K1652267)");
  1643.  
  1644. // init glew
  1645. GLenum err = glewInit();
  1646. if (GLEW_OK != err)
  1647. {
  1648. cerr << "GLEW Error: " << glewGetErrorString(err) << endl;
  1649. return 0;
  1650. }
  1651. cout << "Using GLEW " << glewGetString(GLEW_VERSION) << endl;
  1652.  
  1653. // register callbacks
  1654. glutDisplayFunc(render);
  1655. glutReshapeFunc(reshape);
  1656. glutKeyboardFunc(onKeyDown);
  1657. glutSpecialFunc(onSpecDown);
  1658. glutKeyboardUpFunc(onKeyUp);
  1659. glutSpecialUpFunc(onSpecUp);
  1660. glutMouseFunc(onMouse);
  1661. glutMotionFunc(onMotion);
  1662.  
  1663. cout << "Vendor: " << glGetString(GL_VENDOR) << endl;
  1664. cout << "Renderer: " << glGetString(GL_RENDERER) << endl;
  1665. cout << "Version: " << glGetString(GL_VERSION) << endl;
  1666.  
  1667. // init light and everything – not a GLUT or callback function!
  1668. if (!init())
  1669. {
  1670. cerr << "Application failed to initialise" << endl;
  1671. return 0;
  1672. }
  1673.  
  1674. // enter GLUT event processing cycle
  1675. glutMainLoop();
  1676.  
  1677. done();
  1678.  
  1679. return 1;
  1680. }
RAW Paste Data