Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.19 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <malloc.h>
  6. #include <fstream>
  7. #include <glm/glm.hpp>
  8. #include <glm/gtc/type_ptr.hpp>
  9. #include <glm/gtc/matrix_transform.hpp>
  10. #include <vector>
  11. #include <cmath>
  12. #include "cube.hh"
  13. #include "stb_image.h"
  14. #include <map>
  15. #include <unordered_map>
  16. #include <algorithm>
  17.  
  18. #define UNIF(shader_name, uniform_name) glGetUniformLocation(shader_name, uniform_name)
  19. #define GL_N GL_NEAREST
  20. #define GL_L GL_LINEAR
  21.  
  22. #define TINYOBJLOADER_IMPLEMENTATION
  23. #include "tiny_obj_loader.h"
  24.  
  25. //Units in the world are in decimeters.
  26.  
  27. // ---------------------------------------------------------------------------------------------------------------------- NAMESPACES ETC.
  28.  
  29. using namespace glm;
  30. using std::string;
  31. using std::cout;
  32. using std::cin;
  33. using std::endl;
  34.  
  35. typedef unsigned int uint;
  36.  
  37. // ---------------------------------------------------------------------------------------------------------------------- GLOBAL VARIABLES
  38.  
  39. float dt = 0;
  40. float mouseWheel = 0;
  41. int windowHeight = 900, windowWidth = 1500;
  42. float aspectRatio = windowWidth / windowHeight;
  43. float fov = 60.0f * 180.0f / 3.14159265f;
  44. float g = 98.1;
  45. bool isNight = true, drawWall = true;
  46.  
  47. static uint createShader(const string& vertexShader, const string& fragmentShader);
  48. string readFile(string filename);
  49. string toString(uint number);
  50.  
  51. // ---------------------------------------------------------------------------------------------------------------------- CAMERA
  52.  
  53. struct Camera
  54. {
  55. vec3 pos;
  56. vec3 up;
  57. vec3 front;
  58. vec3 flat;
  59. float speed;
  60. };
  61.  
  62. // ---------------------------------------------------------------------------------------------------------------------- MOUSE
  63.  
  64. struct Mouse
  65. {
  66. float x;
  67. float y;
  68. float xPrev;
  69. float yPrev;
  70. float yaw;
  71. float pitch;
  72.  
  73. bool firstMove;
  74. };
  75.  
  76. // ---------------------------------------------------------------------------------------------------------------------- VERTEX
  77.  
  78. struct Vertex
  79. {
  80. vec3 position;
  81. vec3 color;
  82. vec2 texcoord;
  83. vec3 normal;
  84.  
  85. Vertex(vec3 position, vec3 color, vec2 texcoord, vec3 normal)
  86. : position(position), color(color), texcoord(texcoord), normal(normal) {}
  87. };
  88.  
  89. // ---------------------------------------------------------------------------------------------------------------------- COLLIDER
  90.  
  91. struct RectCollider //p1 and p3 are supposed to bo on the opposite sides!
  92. {
  93. float width, height;
  94. vec3 center;
  95. vec3 normal;
  96.  
  97. RectCollider(float width, float height, vec3 p1, vec3 p2, vec3 p3)
  98. : width(width), height(height)
  99. {
  100. center = (p1 + p3);
  101. center.x /= 2;
  102. center.y /= 2;
  103. center.z /= 2;
  104. vec3 u = p2 - p1;
  105. vec3 v = p3 - p1;
  106. normal.x = u.y*v.z - u.z*v.y;
  107. normal.y = u.z*v.x - u.x*v.z;
  108. normal.z = u.x*v.y - u.y*v.x;
  109. }
  110. };
  111.  
  112. struct Collision
  113. {
  114. vec3 position, normal;
  115. float penetration;
  116. };
  117.  
  118. // ---------------------------------------------------------------------------------------------------------------------- GLOBAL STRUCTS
  119.  
  120. RectCollider colliderRampTop(20.0f, sqrt(15.0f*15.0f + 30.0f*30.0f), vec3(10.0f, 0.0f, 40.0f), vec3(-10.0f, 0.0f, 40.0f), vec3(-10.0f, 15.0f, 70.0f));
  121.  
  122. Camera camera;
  123.  
  124. Mouse mouse;
  125.  
  126. // ---------------------------------------------------------------------------------------------------------------------- SHADER CLASS AND GLOBAL DECLARATION
  127.  
  128. class Shader
  129. {
  130. private:
  131. uint shaderProgram;
  132.  
  133. public:
  134. Shader()
  135. {
  136.  
  137. }
  138.  
  139. void LoadShader(string vertexShaderPath, string fragmentShaderPath)
  140. {
  141. //shader = createShader(readFile("model_vertex_shader.vert"), readFile("model_fragment_shader.vert"));
  142. shaderProgram = createShader(readFile(vertexShaderPath), readFile(fragmentShaderPath));
  143. }
  144.  
  145. void UseShader()
  146. {
  147. glUseProgram(shaderProgram);
  148. }
  149.  
  150. uint& GetShader()
  151. {
  152. return shaderProgram;
  153. }
  154. };
  155.  
  156. Shader modelShader;
  157. Shader simpleShader;
  158. Shader lightShader;
  159. Shader daylightShader;
  160.  
  161. // ---------------------------------------------------------------------------------------------------------------------- MODEL
  162.  
  163. class Model
  164. {
  165. private:
  166. std::vector<Vertex> vertices;
  167. std::vector<uint32_t> indices;
  168.  
  169. uint vao;
  170. uint* vbo;
  171. //uint shader;
  172. uint texture;
  173. int width, height, nrChannels;
  174. unsigned char* data;
  175. float modelScale;
  176.  
  177. public:
  178. Model()
  179. {
  180. vbo = new uint[5];
  181. }
  182.  
  183. Model(string modelPath, float modelScale = 1.0f, float texScale = 1.0f)
  184. {
  185. vbo = new uint[5];
  186. LoadModel(modelPath, modelScale, texScale);
  187. }
  188.  
  189. ~Model()
  190. {
  191. delete [] vbo;
  192. }
  193.  
  194. void LoadModel(string modelPath, float modelScale = 1.0f, float texScale = 1.0f)
  195. {
  196. this->modelScale = modelScale;
  197.  
  198. tinyobj::attrib_t attributes;
  199. std::vector<tinyobj::shape_t> shapes;
  200. std::vector<tinyobj::material_t> materials;
  201. string warning, error;
  202. //string modelPath = "basketball/NBA BASKETBALL.obj";
  203.  
  204. if (!tinyobj::LoadObj(&attributes, &shapes, &materials, &warning, &error, modelPath.c_str()))
  205. {
  206. throw std::runtime_error(warning + error);
  207. }
  208.  
  209. for (const tinyobj::shape_t& shape : shapes)
  210. {
  211. for (const tinyobj::index_t& index : shape.mesh.indices)
  212. {
  213. const vec3 position{
  214. attributes.vertices[3 * index.vertex_index + 0], // x
  215. attributes.vertices[3 * index.vertex_index + 1], // y
  216. attributes.vertices[3 * index.vertex_index + 2], // z
  217. };
  218.  
  219. const vec3 color{ 1.f, 1.f, 1.f };
  220.  
  221. const vec2 texcoord{
  222. attributes.texcoords[2 * index.texcoord_index + 0],
  223. attributes.texcoords[2 * index.texcoord_index + 1],
  224. };
  225.  
  226. const vec3 normal{
  227. attributes.normals[3 * index.normal_index + 0], // x
  228. attributes.normals[3 * index.normal_index + 1], // y
  229. attributes.normals[3 * index.normal_index + 2], // z
  230. };
  231.  
  232. this->vertices.push_back(Vertex(position, color, texcoord, normal));
  233. this->indices.push_back(vertices.size() - 1);
  234. }
  235. }
  236.  
  237. if (modelScale != 1.0f && modelScale > 0.0f)
  238. {
  239. uint vertSize = vertices.size();
  240. for (uint i = 0; i < vertSize; i++)
  241. {
  242. vertices[i].position.x *= modelScale;
  243. vertices[i].position.y *= modelScale;
  244. vertices[i].position.z *= modelScale;
  245. }
  246. }
  247.  
  248. if (texScale != 1.0f && texScale > 0.0f)
  249. {
  250. uint vertSize = vertices.size();
  251. for (uint i = 0; i < vertSize; i++)
  252. {
  253. vertices[i].texcoord.x *= texScale;
  254. vertices[i].texcoord.y *= texScale;
  255. }
  256. }
  257.  
  258. /*
  259. float minY = 0.0f, maxY = 0.0f;
  260. float minX = 0.0f, maxX = 0.0f;
  261. float minZ = 0.0f, maxZ = 0.0f;
  262. for (int i = 0; i < vertices.size(); i++)
  263. {
  264. if (vertices[i].position.y > maxY) maxY = vertices[i].position.y;
  265. if (vertices[i].position.y < minY) minY = vertices[i].position.y;
  266.  
  267. if (vertices[i].position.x > maxX) maxX = vertices[i].position.x;
  268. if (vertices[i].position.x < minX) minX = vertices[i].position.x;
  269.  
  270. if (vertices[i].position.z > maxZ) maxZ = vertices[i].position.z;
  271. if (vertices[i].position.z < minZ) minZ = vertices[i].position.z;
  272. }
  273.  
  274. cout << "\nMAX Y: " << maxY << ", MIN Y: " << minY << "\n";
  275. cout << "\nMAX X: " << maxX << ", MIN X: " << minX << "\n";
  276. cout << "\nMAX Z: " << maxZ << ", MIN Z: " << minZ << "\n";
  277. */
  278.  
  279. /*
  280. cout << "vertices size: " << this->vertices.size() << "\n";
  281. cout << "first index:\n position: " << this->vertices[0].position.x << ", " <<
  282. this->vertices[0].position.y << ", " <<
  283. this->vertices[0].position.z << "\n";
  284. cout << "first index:\n color: " << this->vertices[0].color.x << ", " <<
  285. this->vertices[0].color.y << ", " <<
  286. this->vertices[0].color.z << "\n";
  287. cout << "first index:\n texcoord: " << this->vertices[0].texcoord.x << ", " <<
  288. this->vertices[0].texcoord.y << "\n";
  289. */
  290. }
  291.  
  292. void InitModel(string texturePath)
  293. {
  294. //SHADER:
  295. //shader = createShader(readFile("model_vertex_shader.vert"), readFile("model_fragment_shader.vert"));
  296.  
  297. //BUFORY:
  298. glGenVertexArrays(1, &vao);
  299. glGenBuffers(5, vbo);
  300.  
  301. glBindVertexArray(vao);
  302.  
  303. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  304. glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), this->vertices.data(), GL_STATIC_DRAW);
  305. glVertexAttribPointer(0, 3, GL_FLOAT, false, 11 * sizeof(float), 0);
  306. glEnableVertexAttribArray(0);
  307.  
  308. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  309. glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), this->vertices.data(), GL_STATIC_DRAW);
  310. glVertexAttribPointer(1, 3, GL_FLOAT, false, 11 * sizeof(float), (void*) (3 * sizeof(float)));
  311. glEnableVertexAttribArray(1);
  312.  
  313. glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
  314. glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), this->vertices.data(), GL_STATIC_DRAW);
  315. glVertexAttribPointer(2, 2, GL_FLOAT, false, 11 * sizeof(float), (void*) (6 * sizeof(float)));
  316. glEnableVertexAttribArray(2);
  317.  
  318. glBindBuffer(GL_ARRAY_BUFFER, vbo[3]);
  319. glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), this->vertices.data(), GL_STATIC_DRAW);
  320. glVertexAttribPointer(3, 3, GL_FLOAT, false, 11 * sizeof(float), (void*) (8 * sizeof(float)));
  321. glEnableVertexAttribArray(3);
  322.  
  323. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]);
  324. glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(indices[0]), indices.data(), GL_STATIC_DRAW);
  325.  
  326. glBindVertexArray(0);
  327.  
  328. //TEKSTURA:
  329. stbi_set_flip_vertically_on_load(1);
  330. glGenTextures(1, &texture);
  331. glBindTexture(GL_TEXTURE_2D, texture);
  332. //glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture);
  333. // set the texture wrapping/filtering options (on the currently bound texture object)
  334. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  335. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  336. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_L);
  337. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_L);
  338. // load and generate the texture
  339. //data = stbi_load("basketball/maps/NBA BASKETBALL DIFFUSE.tga", &width, &height, &nrChannels, 0); //"basketball/maps/ballTex.jpg"
  340. data = stbi_load(texturePath.c_str(), &width, &height, &nrChannels, 0);
  341. if (data)
  342. {
  343. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  344. //glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGB, width, height, GL_TRUE);
  345. glGenerateMipmap(GL_TEXTURE_2D);
  346. }
  347. else
  348. {
  349. cout << "Failed to load texture" << endl;
  350. }
  351. glBindTexture(GL_TEXTURE_2D, 0);
  352.  
  353. stbi_image_free(data);
  354. }
  355.  
  356. void Bind()
  357. {
  358. glBindTexture(GL_TEXTURE_2D, texture);
  359. glBindVertexArray(vao);
  360. //glUseProgram(shader);
  361. }
  362.  
  363. void Unbind()
  364. {
  365. glBindVertexArray(0);
  366. }
  367.  
  368. void Draw(GLFWwindow* window, Shader& shader, const mat4& P, const mat4& V, const mat4& M)
  369. {
  370. Bind();
  371.  
  372. shader.UseShader();
  373. const uint& shaderName = shader.GetShader();
  374.  
  375. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //FILL OR WIREFRAME?
  376.  
  377. glUniformMatrix4fv(UNIF(shaderName, "M"), 1, false, value_ptr(M));
  378. glUniformMatrix4fv(UNIF(shaderName, "P"), 1, false, value_ptr(P));
  379. glUniformMatrix4fv(UNIF(shaderName, "V"), 1, false, value_ptr(V));
  380. //glUniform3fv(UNIF(shader, "lightColor"), 1, value_ptr(lightColor));
  381.  
  382. glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
  383. //glDrawArrays(GL_TRIANGLES, 0, this->vertices.size());
  384.  
  385. Unbind();
  386. }
  387.  
  388. void Draw(GLFWwindow* window, Shader& shader, const mat4& P, const mat4& V)
  389. {
  390. Bind();
  391.  
  392. shader.UseShader();
  393. const uint& shaderName = shader.GetShader();
  394.  
  395. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //FILL OR WIREFRAME?
  396.  
  397. mat4 M(1.0f);
  398.  
  399. glUniformMatrix4fv(UNIF(shaderName, "M"), 1, false, value_ptr(M));
  400. glUniformMatrix4fv(UNIF(shaderName, "P"), 1, false, value_ptr(P));
  401. glUniformMatrix4fv(UNIF(shaderName, "V"), 1, false, value_ptr(V));
  402. //glUniform3fv(UNIF(shader, "lightColor"), 1, value_ptr(lightColor));
  403.  
  404. glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
  405. //glDrawArrays(GL_TRIANGLES, 0, this->vertices.size());
  406.  
  407. Unbind();
  408. }
  409.  
  410. float GetScale()
  411. {
  412. return modelScale;
  413. }
  414. };
  415.  
  416. // ---------------------------------------------------------------------------------------------------------------------- SKYBOX
  417.  
  418. class Skybox
  419. {
  420. private:
  421. uint vao;
  422. uint* vbo;
  423. std::vector<float> positions;
  424. std::vector<float> texs;
  425. //uint shader;;
  426. uint texture;
  427. int width, height, nrChannels;
  428. unsigned char* data;
  429.  
  430. public:
  431. Skybox()
  432. {
  433. positions = {
  434. -0.5f, -0.5f, -0.5f, //back
  435. 0.5f, -0.5f, -0.5f,
  436. 0.5f, 0.5f, -0.5f,
  437. 0.5f, 0.5f, -0.5f,
  438. -0.5f, 0.5f, -0.5f,
  439. -0.5f, -0.5f, -0.5f,
  440.  
  441. -0.5f, -0.5f, 0.5f, //front
  442. 0.5f, -0.5f, 0.5f,
  443. 0.5f, 0.5f, 0.5f,
  444. 0.5f, 0.5f, 0.5f,
  445. -0.5f, 0.5f, 0.5f,
  446. -0.5f, -0.5f, 0.5f,
  447.  
  448. -0.5f, 0.5f, 0.5f, //left
  449. -0.5f, 0.5f, -0.5f,
  450. -0.5f, -0.5f, -0.5f,
  451. -0.5f, -0.5f, -0.5f,
  452. -0.5f, -0.5f, 0.5f,
  453. -0.5f, 0.5f, 0.5f,
  454.  
  455. 0.5f, 0.5f, 0.5f, //right
  456. 0.5f, 0.5f, -0.5f,
  457. 0.5f, -0.5f, -0.5f,
  458. 0.5f, -0.5f, -0.5f,
  459. 0.5f, -0.5f, 0.5f,
  460. 0.5f, 0.5f, 0.5f,
  461.  
  462. -0.5f, -0.5f, -0.5f, //bottom
  463. 0.5f, -0.5f, -0.5f,
  464. 0.5f, -0.5f, 0.5f,
  465. 0.5f, -0.5f, 0.5f,
  466. -0.5f, -0.5f, 0.5f,
  467. -0.5f, -0.5f, -0.5f,
  468.  
  469. -0.5f, 0.5f, -0.5f, //top
  470. 0.5f, 0.5f, -0.5f,
  471. 0.5f, 0.5f, 0.5f,
  472. 0.5f, 0.5f, 0.5f,
  473. -0.5f, 0.5f, 0.5f,
  474. -0.5f, 0.5f, -0.5f };
  475.  
  476. for (int i = 0; i < positions.size(); i++) positions[i] *= 3000;
  477.  
  478. texs = {
  479. 0.0f, 0.25f,
  480. 0.25f, 0.25f,
  481. 0.25f, 0.5f,
  482. 0.25f, 0.5f,
  483. 0.0f, 0.5f,
  484. 0.0f, 0.25f,
  485.  
  486. 0.75f, 0.25f,
  487. 0.5f, 0.25f,
  488. 0.5f, 0.5f,
  489. 0.5f, 0.5f,
  490. 0.75f, 0.5f,
  491. 0.75f, 0.25f,
  492.  
  493. 0.75f, 0.5f,
  494. 1.0f, 0.5f,
  495. 1.0f, 0.25f,
  496. 1.0f, 0.25f,
  497. 0.75f, 0.25f,
  498. 0.75f, 0.5f,
  499.  
  500. 0.5f, 0.5f,
  501. 0.25f, 0.5f,
  502. 0.25f, 0.25f,
  503. 0.25f, 0.25f,
  504. 0.5f, 0.25f,
  505. 0.5f, 0.5f,
  506.  
  507. 1.0f, 0.25f,
  508. 1.0f, 0.0f,
  509. 0.75f, 0.0f,
  510. 0.75f, 0.0f,
  511. 0.75f, 0.25f,
  512. 1.0f, 0.25f,
  513.  
  514. 1.0f, 0.5f,
  515. 1.0f, 0.75f,
  516. 0.75f, 0.75f,
  517. 0.75f, 0.75f,
  518. 0.75f, 0.5f,
  519. 1.0f, 0.5f, };
  520.  
  521. vbo = new uint[2];
  522. }
  523.  
  524. ~Skybox()
  525. {
  526. delete [] vbo;
  527. }
  528.  
  529. void InitSkybox(string texPath)
  530. {
  531. //SHADER:
  532. //shader = createShader(readFile("sky_vertex_shader.vert"), readFile("sky_fragment_shader.vert"));
  533.  
  534. //BUFORY:
  535. glGenVertexArrays(1, &vao);
  536. glGenBuffers(2, vbo);
  537.  
  538. glBindVertexArray(vao);
  539.  
  540. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  541. glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(positions[0]), positions.data(), GL_STATIC_DRAW);
  542. glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
  543. glEnableVertexAttribArray(0);
  544.  
  545. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  546. glBufferData(GL_ARRAY_BUFFER, texs.size() * sizeof(texs[0]), texs.data(), GL_STATIC_DRAW);
  547. glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
  548. glEnableVertexAttribArray(1);
  549.  
  550. glBindVertexArray(0);
  551.  
  552. //TEKSTURA:
  553.  
  554. stbi_set_flip_vertically_on_load(1);
  555. glGenTextures(1, &texture);
  556. glBindTexture(GL_TEXTURE_2D, texture);
  557. //glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture);
  558. // set the texture wrapping/filtering options (on the currently bound texture object)
  559. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  560. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  561. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_L);
  562. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_L);
  563. // load and generate the texture
  564. data = stbi_load(texPath.c_str(), &width, &height, &nrChannels, 0);
  565. if (data)
  566. {
  567. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  568. //glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGB, width, height, GL_TRUE);
  569. glGenerateMipmap(GL_TEXTURE_2D);
  570. }
  571. else
  572. {
  573. cout << "Failed to load texture" << endl;
  574. }
  575. glBindTexture(GL_TEXTURE_2D, 0);
  576.  
  577. stbi_image_free(data);
  578. }
  579.  
  580. void Bind()
  581. {
  582. glBindTexture(GL_TEXTURE_2D, texture);
  583. glBindVertexArray(vao);
  584. //glUseProgram(shader);
  585. }
  586.  
  587. void Unbind()
  588. {
  589. glBindVertexArray(0);
  590. }
  591.  
  592. void Draw(GLFWwindow* window, const mat4& P, const mat4& V, const vec3& camPos)
  593. {
  594. Bind();
  595.  
  596. simpleShader.UseShader();
  597. const uint& shaderName = simpleShader.GetShader();
  598.  
  599. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //FILL OR WIREFRAME?
  600.  
  601. mat4 M(1.0f);
  602. M = glm::translate(M, camPos);
  603.  
  604. glUniformMatrix4fv(UNIF(shaderName, "M"), 1, false, value_ptr(M));
  605. glUniformMatrix4fv(UNIF(shaderName, "P"), 1, false, value_ptr(P));
  606. glUniformMatrix4fv(UNIF(shaderName, "V"), 1, false, value_ptr(V));
  607.  
  608. glDrawArrays(GL_TRIANGLES, 0, 36);
  609. Unbind();
  610. }
  611. };
  612.  
  613. // ---------------------------------------------------------------------------------------------------------------------- LIGHT
  614.  
  615. class Light
  616. {
  617. public:
  618. static Model model;
  619.  
  620. private:
  621. vec3 lightPos;
  622. vec3 lightColor;
  623. mat4 M;
  624. static uint nr;
  625. string lightPosStr, lightColorStr;
  626.  
  627. public:
  628. Light(vec3 lightPos = vec3(0.0f, 0.0f, 0.0f), vec3 lightColor = vec3(1.0f, 1.0f, 1.0f))
  629. : lightPos(lightPos), lightColor(lightColor), M(1.0f)
  630. {
  631. nr++;
  632. M = glm::translate(M, lightPos);
  633. string nrStr = toString(nr);
  634. lightPosStr = "lightPos" + nrStr;
  635. lightColorStr = "lightColor" + nrStr;
  636. }
  637.  
  638. ~Light()
  639. {
  640. nr--;
  641. }
  642.  
  643. void Draw(GLFWwindow* window, const mat4& P, const mat4& V)
  644. {
  645. if (isNight)
  646. {
  647. modelShader.UseShader();
  648. const uint& shaderName = modelShader.GetShader();
  649. glUniform3fv(UNIF(shaderName, lightPosStr.c_str()), 1, value_ptr(lightPos));
  650. glUniform3fv(UNIF(shaderName, lightColorStr.c_str()), 1, value_ptr(lightColor));
  651. //glUniform3fv(UNIF(shaderName, "lightPos1"), 1, value_ptr(lightPos));
  652. //glUniform3fv(UNIF(shaderName, "lightColor1"), 1, value_ptr(lightColor));
  653. glUniform3fv(UNIF(shaderName, "cameraPos"), 1, value_ptr(camera.pos));
  654. }
  655. else
  656. {
  657. daylightShader.UseShader();
  658. const uint& shaderName = daylightShader.GetShader();
  659. glUniform3fv(UNIF(shaderName, lightPosStr.c_str()), 1, value_ptr(lightPos));
  660. glUniform3fv(UNIF(shaderName, lightColorStr.c_str()), 1, value_ptr(lightColor));
  661. //glUniform3fv(UNIF(shaderName, "lightPos1"), 1, value_ptr(lightPos));
  662. //glUniform3fv(UNIF(shaderName, "lightColor1"), 1, value_ptr(lightColor));
  663. glUniform3fv(UNIF(shaderName, "cameraPos"), 1, value_ptr(camera.pos));
  664. }
  665.  
  666. model.Draw(window, lightShader, P, V, M);
  667. }
  668.  
  669. void Move(vec3 ds)
  670. {
  671. lightPos += ds;
  672. M = glm::translate(M, ds);
  673. }
  674. };
  675. Model Light::model;
  676. uint Light::nr = 0;
  677.  
  678. // ---------------------------------------------------------------------------------------------------------------------- BALL
  679.  
  680. class Ball
  681. {
  682. public:
  683. static Model model;
  684.  
  685. private:
  686. float r;
  687. float scale;
  688. mat4 M;
  689. vec3 center;
  690. vec3 v;
  691. vec3 ds;
  692. float angle;
  693. int id;
  694. vec3 pivot;
  695. float weaker;
  696.  
  697. public:
  698. Ball(float startV = 0, float centerOffset = 3.0f, int id = -1)
  699. : v(camera.front * startV), r(18.9 * model.GetScale()), scale(/*0.0635f*/1.0f), M(1.0f), center(camera.pos + centerOffset * camera.front), id(id), pivot(vec3(0.0f, 1.0f, 0.0f)), weaker(6.0f)
  700. {
  701. M = glm::translate(M, center);
  702. M = glm::scale(M, vec3(scale, scale, scale));
  703. r *= scale;
  704. }
  705.  
  706. Draw(GLFWwindow* window, const mat4& P, const mat4& V)
  707. {
  708. if (isNight) model.Draw(window, modelShader, P, V, M);
  709. else model.Draw(window, daylightShader, P, V, M);
  710. }
  711.  
  712. vec3 GetCenter()
  713. {
  714. return center;
  715. }
  716.  
  717. void MoveBall(std::vector<Ball>& balls)
  718. {
  719. //flight:
  720. ds.x = v.x * dt;
  721. ds.z = v.z * dt;
  722.  
  723. v.y = v.y - g * dt * 1.0f;
  724. if (v.y < -1000.0f) v.y = -1000.0f;
  725. ds.y = v.y * dt;
  726.  
  727. center += ds;
  728.  
  729. //Bouncing:
  730. if (center.y - r < 0.0f && (center.x - r < 100.0f && center.x + r > -100.0f) && (center.z - r < 100.0f && center.z + r > -100.0f))
  731. {
  732. //vec3 t = camera.pos + 2.0f * camera.front;
  733. //cout << center.y << " ... " << center.y - r << " ... " << camera.pos.y << " ... " << t.y << '\n';
  734. float temp = (center.y - r) * (-1.0f);
  735. center.y += temp;
  736. ds.y += temp;
  737. v.y = v.y * (-1.0f) * 0.7f;
  738. if (abs(ds.y) > 0.1f)
  739. {
  740. v.x = v.x * 0.7f;
  741. v.z = v.z * 0.7f;
  742. if (weaker > 1.0f) weaker--;
  743. }
  744. else
  745. {
  746. //v.x = v.x * 0.999f;
  747. //v.z = v.z * 0.999f;
  748. v.x = v.x * 0.999f;
  749. v.z = v.z * 0.999f;
  750. weaker = 1.0f;
  751. }
  752. if (abs(v.x) < 0.1f && abs(v.z) < 0.1f)
  753. {
  754. v.x = 0.0f;
  755. v.z = 0.0f;
  756. }
  757. }
  758.  
  759. //Wall bouncing:
  760. if (center.x - r < -100.0f)
  761. {
  762. float temp = (center.x - r + 100.0f) * (-1.0f);
  763. center.x += temp;
  764. ds.x += temp;
  765. v.x *= (-0.8f);
  766. }
  767. if (center.z - r < -100.0f)
  768. {
  769. float temp = (center.z - r + 100.0f) * (-1.0f);
  770. center.z += temp;
  771. ds.z += temp;
  772. v.z *= (-0.8f);
  773. }
  774. if (center.x + r > 100.0f)
  775. {
  776. float temp = (center.x + r - 100.0f) * (-1.0f);
  777. center.x += temp;
  778. ds.x += temp;
  779. v.x *= (-0.8f);
  780. }
  781. if (center.z + r > 100.0f)
  782. {
  783. float temp = (center.z + r - 100.0f) * (-1.0f);
  784. center.z += temp;
  785. ds.z += temp;
  786. v.z *= (-0.8f);
  787. }
  788.  
  789. //Collide with other balls:
  790. for (int i = 0; i < balls.size(); i++)
  791. {
  792. if (balls[i].GetID() == id) continue;
  793. if (glm::distance(balls[i].GetCenter(), center) < r*2)
  794. {
  795.  
  796. }
  797. }
  798.  
  799. //Rotate:
  800.  
  801. vec3 dsFlat = glm::normalize(vec3(ds.x, 0.0f, ds.z));
  802. vec3 up = vec3(0.0f, 1.0f, 0.0f);
  803. if (ds.x != 0.0f || ds.z != 0.0f) pivot = glm::cross(dsFlat, up);
  804. angle -= sqrt((ds.x*ds.x) + (ds.z*ds.z)) / weaker;
  805.  
  806. //Update M matrix:
  807.  
  808. M = mat4(1.0f);
  809. M = glm::translate(M, center);
  810. M = glm::rotate(M, angle, pivot);
  811. }
  812.  
  813. int GetID()
  814. {
  815. return id;
  816. }
  817. };
  818. Model Ball::model;
  819.  
  820. // ---------------------------------------------------------------------------------------------------------------------- BALLS SET
  821.  
  822. class BallsSet
  823. {
  824. private:
  825. uint ballsCount = 0;
  826. std::vector<Ball> balls;
  827.  
  828. public:
  829. BallsSet()
  830. {
  831.  
  832. }
  833.  
  834. ~BallsSet()
  835. {
  836.  
  837. }
  838.  
  839. void CreateBall()
  840. {
  841. ballsCount++;
  842. Ball newBall(70.0f, 3.0f, ballsCount);
  843. balls.push_back(newBall);
  844. }
  845.  
  846. void CreateStaticBall()
  847. {
  848. ballsCount++;
  849. Ball newBall(0.0f, 10.0f, ballsCount);
  850. balls.push_back(newBall);
  851. }
  852.  
  853. void RemoveLastBall()
  854. {
  855. if (!ballsCount) return;
  856. balls.pop_back();
  857. ballsCount--;
  858. }
  859.  
  860. void UpdateBalls()
  861. {
  862. for (int i = 0; i < ballsCount; i++)
  863. {
  864. balls[i].MoveBall(balls);
  865. }
  866. }
  867.  
  868. void DrawBalls(GLFWwindow* window, const mat4& P, const mat4& V)
  869. {
  870. for (int i = 0; i < ballsCount; i++)
  871. {
  872. balls[i].Draw(window, P, V);
  873. }
  874. }
  875. };
  876.  
  877. // ---------------------------------------------------------------------------------------------------------------------- WALL
  878.  
  879. class SquareWall
  880. {
  881. public:
  882. static Model model;
  883.  
  884. private:
  885. mat4 M;
  886. float distanceFromOrigin;
  887. float dip;
  888.  
  889. public:
  890. SquareWall(float distanceFromOrigin = 109.0f, float dip = -50.0f)
  891. : M(1.0f), distanceFromOrigin(distanceFromOrigin), dip(dip) {}
  892.  
  893. void Draw(GLFWwindow* window, const mat4& P, const mat4& V)
  894. {
  895. M = mat4(1.0f);
  896. M = glm::translate(M, vec3(0.0f, dip, distanceFromOrigin));
  897. if (isNight) model.Draw(window, modelShader, P, V, M);
  898. else model.Draw(window, daylightShader, P, V, M);
  899.  
  900. M = mat4(1.0f);
  901. M = glm::translate(M, vec3(0.0f, dip, -distanceFromOrigin));
  902. M = glm::rotate(M, glm::radians(180.0f), vec3(0.0f, 1.0f, 0.0f));
  903. if (isNight) model.Draw(window, modelShader, P, V, M);
  904. else model.Draw(window, daylightShader, P, V, M);
  905.  
  906. M = mat4(1.0f);
  907. M = glm::translate(M, vec3(distanceFromOrigin, dip, 0.0f));
  908. M = glm::rotate(M, glm::radians(90.0f), vec3(0.0f, 1.0f, 0.0f));
  909. if (isNight) model.Draw(window, modelShader, P, V, M);
  910. else model.Draw(window, daylightShader, P, V, M);
  911.  
  912. M = mat4(1.0f);
  913. M = glm::translate(M, vec3(-distanceFromOrigin, dip, 0.0f));
  914. M = glm::rotate(M, glm::radians(270.0f), vec3(0.0f, 1.0f, 0.0f));
  915. if (isNight) model.Draw(window, modelShader, P, V, M);
  916. else model.Draw(window, daylightShader, P, V, M);
  917. }
  918. };
  919. Model SquareWall::model;
  920.  
  921. // ---------------------------------------------------------------------------------------------------------------------- GLOBAL MODELS DECLARATION
  922.  
  923. Model basketballPost("basketball/Basketball_Post_No_Ball.obj");
  924.  
  925. Model ground("surface/ground.obj", 100.0f, 10.0f);
  926.  
  927. Model house1("houses/house1/House01_OBJ.obj", 10.0f);
  928.  
  929. Model ramp1("surface/ramp1.obj");
  930. Model ramp2("surface/ramp2.obj");
  931.  
  932. BallsSet ballsSet;
  933.  
  934. Skybox skyboxNight;
  935. Skybox skyboxDay;
  936.  
  937. Light light1(vec3(150.0f, 100.0f, 50.0f), vec3(0.8f, 0.8f, 0.8f));
  938. Light light2(vec3(-100.0f, 100.0f, -50.0f), vec3(0.5f, 0.5f, 0.5f));
  939. Light light3(vec3(1000000.0f, 1000000.0f, 0.0f), vec3(0.8f, 0.7f, 0.6f));
  940. //Light light3(vec3(1000000.0f, 1000000.0f, 0.0f), vec3(1.0f, 0.6f, 0.25f));
  941.  
  942. SquareWall wall;
  943.  
  944. // ---------------------------------------------------------------------------------------------------------------------- SHADERS BUILDING
  945.  
  946. static uint compileShader(uint type, const string& source)
  947. {
  948. uint id = glCreateShader(type);
  949. const char* src = source.c_str();
  950. glShaderSource(id, 1, &src, nullptr);
  951. glCompileShader(id);
  952.  
  953. int result;
  954. glGetShaderiv(id, GL_COMPILE_STATUS, &result);
  955. if (result == GL_FALSE) // !result
  956. {
  957. int length;
  958. glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
  959. char* message = (char*) alloca(length * sizeof(char));
  960. glGetShaderInfoLog(id, length, &length, message);
  961. cout << "Failed to compile " <<
  962. (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader:\n";
  963. cout << message << endl;
  964. glDeleteShader(id);
  965. return 0;
  966. }
  967.  
  968. return id;
  969. }
  970.  
  971. static uint createShader(const string& vertexShader, const string& fragmentShader)
  972. {
  973. uint program = glCreateProgram();
  974. uint vs = compileShader(GL_VERTEX_SHADER, vertexShader);
  975. uint fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
  976.  
  977. glAttachShader(program, vs);
  978. glAttachShader(program, fs);
  979. glLinkProgram(program);
  980. glValidateProgram(program);
  981.  
  982. glDeleteShader(vs); //glDetachShader(vs);
  983. glDeleteShader(fs); //glDetachShader(fs);
  984.  
  985. return program;
  986. }
  987.  
  988. // ---------------------------------------------------------------------------------------------------------------------- READ FILE
  989.  
  990. string readFile(string filename)
  991. {
  992. std::ifstream file(filename);
  993. std::string content((std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>()));
  994. return content;
  995. }
  996.  
  997. // ---------------------------------------------------------------------------------------------------------------------- TO STRING
  998.  
  999. string toString(uint number)
  1000. {
  1001. string result = "";
  1002. while (number)
  1003. {
  1004. uint digit = number % 10;
  1005. number = number / 10;
  1006. char chDigit = digit + 48;
  1007. result += chDigit;
  1008. }
  1009. reverse(result.begin(), result.end());
  1010. return result;
  1011. }
  1012.  
  1013. // ---------------------------------------------------------------------------------------------------------------------- EVENTS
  1014.  
  1015. void onMouseScroll(GLFWwindow *window, double xOffset, double yOffset)
  1016. {
  1017. mouseWheel += (float) yOffset;
  1018. }
  1019.  
  1020. void onMouseClick(GLFWwindow *window, int button, int action, int mods)
  1021. {
  1022. if (action == GLFW_PRESS)
  1023. {
  1024. double x, y;
  1025. glfwGetCursorPos(window, &x, &y);
  1026. if (button == GLFW_MOUSE_BUTTON_LEFT)
  1027. {
  1028. ballsSet.CreateBall();
  1029. }
  1030. if (button == GLFW_MOUSE_BUTTON_RIGHT)
  1031. {
  1032. ballsSet.RemoveLastBall();
  1033. }
  1034. if (button == GLFW_MOUSE_BUTTON_MIDDLE)
  1035. {
  1036. ballsSet.CreateStaticBall();
  1037. }
  1038. }
  1039. }
  1040.  
  1041. void handleCamera(GLFWwindow* window)
  1042. {
  1043. if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
  1044. camera.pos += dt * camera.speed * camera.flat;
  1045.  
  1046. if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
  1047. camera.pos -= dt * camera.speed * camera.flat;
  1048.  
  1049. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
  1050. camera.pos += dt * glm::normalize(glm::cross(camera.front, camera.up)) * camera.speed;
  1051.  
  1052. if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
  1053. camera.pos -= dt * glm::normalize(glm::cross(camera.front, camera.up)) * camera.speed;
  1054.  
  1055. if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
  1056. camera.pos.y += dt * camera.speed;
  1057.  
  1058. if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) camera.pos.y -= dt * camera.speed;
  1059.  
  1060. if (camera.pos.y < 3.0f) camera.pos.y = 3.0f;
  1061. if (camera.pos.y > 47.0f) camera.pos.y = 47.0f;
  1062. if (camera.pos.x < -98.0f) camera.pos.x = -98.0f;
  1063. if (camera.pos.x > 98.0f) camera.pos.x = 98.0f;
  1064. if (camera.pos.z < -98.0f) camera.pos.z = -98.0f;
  1065. if (camera.pos.z > 98.0f) camera.pos.z = 98.0f;
  1066. }
  1067.  
  1068. void onKeyboard(GLFWwindow *window, int key, int code, int action, int mods)
  1069. {
  1070. static int turn = 0;
  1071.  
  1072. if (action == GLFW_PRESS)
  1073. {
  1074. if (key == GLFW_KEY_ESCAPE) glfwSetWindowShouldClose(window, true);
  1075. if (key == GLFW_KEY_Z)
  1076. {
  1077. if ((turn++ % 2)) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  1078. else glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  1079. }
  1080. if (key == GLFW_KEY_X)
  1081. {
  1082. if (isNight) isNight = 0;
  1083. else isNight = 1;
  1084. }
  1085. if (key == GLFW_KEY_C)
  1086. {
  1087. if (drawWall) drawWall = 0;
  1088. else drawWall = 1;
  1089. }
  1090. }
  1091. }
  1092.  
  1093. void onResize(GLFWwindow *window, int width, int height)
  1094. {
  1095. windowWidth = width, windowHeight = height;
  1096. if (height == 0) return;
  1097. glViewport(0, 0, width, height);
  1098. aspectRatio = (float) width / (float) height;
  1099. cout << windowHeight << ", " << windowWidth << "\n";
  1100. }
  1101.  
  1102. void mouse_callback(GLFWwindow* window, double x, double y) //MOUSE CALLBACK
  1103. {
  1104. mouse.x = x;
  1105. mouse.y = y;
  1106.  
  1107. if(mouse.firstMove) // this bool variable is initially set to true
  1108. {
  1109. mouse.xPrev = mouse.x;
  1110. mouse.yPrev = mouse.y;
  1111. mouse.firstMove = false;
  1112. }
  1113.  
  1114. float xOffset = mouse.x - mouse.xPrev;
  1115. float yOffset = mouse.yPrev - mouse.y; // reversed since y-coordinates range from bottom to top
  1116. mouse.xPrev = mouse.x;
  1117. mouse.yPrev = mouse.y;
  1118.  
  1119. float sensitivity = 0.1f;
  1120. xOffset *= sensitivity;
  1121. yOffset *= sensitivity;
  1122.  
  1123. mouse.yaw += xOffset;
  1124. mouse.pitch += yOffset;
  1125. if(mouse.pitch > 89.0f) mouse.pitch = 89.0f;
  1126. if(mouse.pitch < -89.0f) mouse.pitch = -89.0f;
  1127.  
  1128. camera.front.x = cos(glm::radians(mouse.pitch)) * cos(glm::radians(mouse.yaw));
  1129. camera.front.y = sin(glm::radians(mouse.pitch));
  1130. camera.front.z = cos(glm::radians(mouse.pitch)) * sin(glm::radians(mouse.yaw));
  1131. camera.front = glm::normalize(camera.front);
  1132.  
  1133. camera.flat.x = cos(glm::radians(mouse.yaw));
  1134. camera.flat.z = sin(glm::radians(mouse.yaw));
  1135. camera.flat = glm::normalize(camera.flat);
  1136. }
  1137.  
  1138. // ---------------------------------------------------------------------------------------------------------------------- INITIALIZATIONS OF MOUSE AND CAMERA
  1139.  
  1140. void initMouse()
  1141. {
  1142. mouse.yaw = 0.0f;Camera camera;
  1143.  
  1144. Mouse mouse;
  1145. mouse.pitch = 0.0f;
  1146. mouse.firstMove = true;
  1147. }
  1148.  
  1149. void initCamera()
  1150. {
  1151. camera.pos = vec3(0.0f, 0.0f, 10.0f);
  1152. camera.up = vec3(0.0f, 1.0f, 0.0f);
  1153. camera.front = vec3(0.0f, 0.0f, -1.0f);
  1154. camera.flat = vec3(0.0f, 0.0f, -1.0f);
  1155. camera.speed = 30.0f;
  1156. }
  1157.  
  1158. // ---------------------------------------------------------------------------------------------------------------------- INITIALIZATION OF SCENE, MODELS AND OTHER OBJECTS
  1159.  
  1160. void initScene()
  1161. {
  1162. glClearColor(0.08f, 0.08f, 0.12f, 0.0f);
  1163. glEnable(GL_DEPTH_TEST);
  1164.  
  1165. modelShader.LoadShader("model_vertex_shader.vert", "model_fragment_shader.vert");
  1166. simpleShader.LoadShader("sky_vertex_shader.vert", "sky_fragment_shader.vert");
  1167. lightShader.LoadShader("light_vertex_shader.vert", "light_fragment_shader.vert");
  1168. daylightShader.LoadShader("daylight_vertex_shader.vert", "daylight_fragment_shader.vert");
  1169.  
  1170. Light::model.LoadModel("light/lantern/Gamelantern_updated.obj", 20.0f);
  1171. Light::model.InitModel("light/lantern/Old_lantern_UV_Diffuse.png");
  1172. //Light::model.LoadModel("light/modernLamp/export.obj", 0.2f);
  1173. //Light::model.InitModel("light/modernLamp/texture/vasa otlivom biruzy.jpg");
  1174.  
  1175. Ball::model.LoadModel("basketball/NBA BASKETBALL.obj", 0.0635f);
  1176. Ball::model.InitModel("basketball/maps/ballTex.png");
  1177.  
  1178. SquareWall::model.LoadModel("wall/stone wall/stonewall.obj", 17.0f);
  1179. SquareWall::model.InitModel("wall/stone wall/Textures/red4.jpg");
  1180.  
  1181. initMouse();
  1182. initCamera();
  1183.  
  1184. skyboxNight.InitSkybox("skybox/nightSkybox.png");
  1185. //skyboxDay.InitSkybox("skybox/skyboxSunset.png");
  1186. skyboxDay.InitSkybox("skybox/skybox4.png");
  1187.  
  1188.  
  1189. basketballPost.InitModel("basketball/maps/Basketball_Post.jpg");
  1190.  
  1191. house1.InitModel("houses/house1/Building.Texture2.jpg");
  1192. //house1.InitModel("basketball/maps/Basketball_Post.jpg");
  1193.  
  1194. ground.InitModel("surface/tiles2.jpg");
  1195. //ground.InitModel("brick.png");
  1196.  
  1197. ramp1.InitModel("surface/concrete.jpg");
  1198. ramp2.InitModel("surface/concrete.jpg");
  1199. }
  1200.  
  1201. // ---------------------------------------------------------------------------------------------------------------------- DRAWING SCENE
  1202.  
  1203. void drawScene(GLFWwindow* window)
  1204. {
  1205. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1206.  
  1207. handleCamera(window);
  1208.  
  1209. mat4 P = glm::perspective(fov, aspectRatio, 0.5f, 3000.0f); //500.0f
  1210. mat4 V = glm::lookAt(camera.pos, camera.pos + camera.front, camera.up);
  1211. mat4 M_basketballPost(1.0f);
  1212.  
  1213. M_basketballPost = glm::translate(M_basketballPost, vec3(0.0f, 0.0f, -70.0f));
  1214. M_basketballPost = glm::scale(M_basketballPost, vec3(0.2f, 0.2f, 0.2f));
  1215.  
  1216. float dist = 250.0f;
  1217. vec3 Trans[12] = {
  1218. vec3(dist, 15.0f, dist),
  1219. vec3(dist/2, 15.0f, dist),
  1220. vec3(0.0f, 15.0f, dist),
  1221. vec3(-dist/2, 15.0f, dist),
  1222. vec3(-dist, 15.0f, dist),
  1223. vec3(dist, 15.0f, 0.0f),
  1224. vec3(-dist, 15.0f, 0.0f),
  1225. vec3(dist, 15.0f, -dist),
  1226. vec3(dist/2, 15.0f, -dist),
  1227. vec3(0.0f, 15.0f, -dist),
  1228. vec3(-dist/2, 15.0f, -dist),
  1229. vec3(-dist, 15.0f, -dist),
  1230. };
  1231. if (isNight)
  1232. {
  1233. light1.Draw(window, P, V);
  1234. light2.Draw(window, P, V);
  1235. skyboxNight.Draw(window, glm::perspective(fov, aspectRatio, 0.001f, 5000.0f), V, camera.pos); ////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1236. basketballPost.Draw(window, modelShader, P, V, M_basketballPost);
  1237. ground.Draw(window, modelShader, P, V);
  1238.  
  1239. ramp1.Draw(window, modelShader, P, V);
  1240. ramp2.Draw(window, modelShader, P, V);
  1241.  
  1242. for (int i = 0; i < 12; i++)
  1243. {
  1244. mat4 M(1.0f);
  1245. M = glm::translate(M, Trans[i]);
  1246. house1.Draw(window, modelShader, P, V, M);
  1247. }
  1248. }
  1249. else
  1250. {
  1251. light3.Draw(window, P, V);
  1252. skyboxDay.Draw(window, glm::perspective(fov, aspectRatio, 0.001f, 5000.0f), V, camera.pos);
  1253. basketballPost.Draw(window, daylightShader, P, V, M_basketballPost);
  1254. ground.Draw(window, daylightShader, P, V);
  1255. ramp1.Draw(window, daylightShader, P, V);
  1256. ramp2.Draw(window, daylightShader, P, V);
  1257.  
  1258. for (int i = 0; i < 12; i++)
  1259. {
  1260. mat4 M(1.0f);
  1261. M = glm::translate(M, Trans[i]);
  1262. house1.Draw(window, daylightShader, P, V, M);
  1263. }
  1264. }
  1265.  
  1266. if (drawWall) wall.Draw(window, P, V);
  1267.  
  1268. ballsSet.UpdateBalls();
  1269. ballsSet.DrawBalls(window, P, V);
  1270.  
  1271. glfwSwapBuffers(window);
  1272. }
  1273.  
  1274. // ---------------------------------------------------------------------------------------------------------------------- MAIN()
  1275.  
  1276. int main()
  1277. {
  1278. glfwInit();
  1279. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  1280. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  1281. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  1282. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  1283. glfwWindowHint(GLFW_SAMPLES, 8);
  1284. glEnable(GL_MULTISAMPLE);
  1285.  
  1286. GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "My Balls", NULL, NULL);
  1287. if (!window)
  1288. {
  1289. cout << "Failed to create GLFW window\n";
  1290. glfwTerminate();
  1291. return -1;
  1292. }
  1293. glfwMakeContextCurrent(window); //PRZYPISANIE KONTEKSTU DO OKNA window
  1294.  
  1295. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  1296.  
  1297. if (GLEW_OK != glewInit()) cout << "Failed to initialize GLEW\n";
  1298. cout << glGetString(GL_VERSION) << endl;
  1299.  
  1300. glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); //REAGOWNIE NA INPUTY
  1301. glfwSetKeyCallback(window, onKeyboard);
  1302. glfwSetScrollCallback(window, onMouseScroll);
  1303. glfwSetMouseButtonCallback(window, onMouseClick);
  1304. glfwSetWindowSizeCallback(window, onResize);
  1305. glfwSetCursorPosCallback(window, mouse_callback);
  1306. glfwPollEvents();
  1307.  
  1308. //glEnable(GL_CULL_FACE);
  1309. //glCullFace(GL_BACK);
  1310. //glFrontFace(GL_CCW);
  1311.  
  1312. initScene();
  1313. glfwSetTime(0);
  1314.  
  1315. onResize(window, windowWidth, windowHeight);
  1316.  
  1317. while(!glfwWindowShouldClose(window))
  1318. {
  1319. dt = glfwGetTime();
  1320. glfwSetTime(0);
  1321. drawScene(window);
  1322. glfwPollEvents();
  1323. }
  1324.  
  1325. glfwTerminate();
  1326. return 0;
  1327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement