Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.03 KB | None | 0 0
  1. #include "imgui.h"
  2. #include "imgui_impl_glfw.h"
  3. #include "imgui_impl_opengl3.h"
  4. #include <glm/glm.hpp>
  5. #include <glm/gtc/matrix_transform.hpp>
  6. #include <glm/gtc/type_ptr.hpp>
  7. #include <iostream>
  8. #include <stdio.h>
  9. #include <vector>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <string>
  14. #include <filesystem>
  15. #include "stb_image.h"
  16. #include "model.h"
  17. #include "GraphNode.h"
  18. #include "shader.h"
  19. #include "camera.h"
  20.  
  21. // About OpenGL function loaders: modern OpenGL doesn't have a standard header file and requires individual function pointers to be loaded manually.
  22. // Helper libraries are often used for this purpose! Here we are supporting a few common ones: gl3w, glew, glad.
  23. // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
  24. #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
  25. #include <GL/gl3w.h> // Initialize with gl3wInit()
  26. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
  27. #include <GL/glew.h> // Initialize with glewInit()
  28. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
  29. #include <glad/glad.h> // Initialize with gladLoadGL()
  30. #else
  31. #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM
  32. #endif
  33.  
  34. #include <GLFW/glfw3.h> // Include glfw3.h after our OpenGL definitions
  35.  
  36. static void glfw_error_callback(int error, const char* description)
  37. {
  38. fprintf(stderr, "Glfw Error %d: %s\n", error, description);
  39. }
  40. const int SCREEN_WIDTH = 1280;
  41. const int SCREEN_HEIGHT = 1024;
  42. double seconds;
  43. double timeUntilNextP = 0;
  44. using namespace std;
  45.  
  46. std::vector<Model*> models;
  47. std::vector<Model*> steamEnginge;
  48. glm::vec3 steamEngingePosition = glm::vec3(0.0, 40.0f, -0.5f);
  49. static float directColor[3] = { 1.0f,1.0f,1.0f };
  50. static float speedSteamEngine;
  51. static float rotationSteamEngine;
  52.  
  53. unsigned int loadTexture(char const * path);
  54.  
  55.  
  56. #pragma region Shaders
  57. const GLchar* vertexShaderSource =
  58. "#version 330 core\n"
  59. "layout(location = 0) in vec3 aPos; \n"
  60. "layout(location = 1) in vec3 aNormal; \n"
  61. "layout(location = 2) in vec2 aTexCoords; \n"
  62. "out vec2 TexCoords; \n"
  63. "uniform mat4 model; \n"
  64. "uniform mat4 view; \n"
  65. "uniform mat4 projection; \n"
  66. "void main()\n"
  67. "{\n"
  68. " TexCoords = aTexCoords; \n"
  69. " gl_Position = projection * view * model * vec4(aPos, 1.0); \n"
  70. "}\n";
  71.  
  72. const GLchar* fragmentShaderSource =
  73. "#version 330 core\n"
  74. "out vec4 FragColor; \n"
  75. "in vec2 TexCoords; \n"
  76. "uniform sampler2D texture_diffuse1; \n"
  77. "void main()\n"
  78. "{\n"
  79. " FragColor = texture(texture_diffuse1, TexCoords); \n"
  80. "}\n";
  81.  
  82.  
  83. #pragma endregion
  84. #pragma region Camera
  85. // camera
  86. Camera camera(glm::vec3(24.25f, 7.68f, 15.06f));
  87. float lastX = (float)SCREEN_WIDTH / 2.0;
  88. float lastY = (float)SCREEN_HEIGHT / 2.0;
  89. bool firstMouse = true;
  90. bool canMoveByMouse = true;
  91. // timing
  92. float deltaTime = 0.0f; // time between current frame and last frame
  93. float lastFrame = 0.0f;
  94. #pragma endregion
  95. #pragma region Methods
  96. int createShaderProgram(const GLchar* vsSource, const GLchar* fsSource, string vsName, string fsName);
  97. void processInput(GLFWwindow *window);
  98. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  99. void mouse_callback(GLFWwindow* window, double xpos, double ypos);
  100. unsigned int loadCubemap(vector<std::string> faces);
  101. #pragma endregion
  102.  
  103. static float FIRSTWHEELPOSITION = -90.0f;
  104. static float GAPBETWEENTHEWHEELS = 25.0f;
  105. static float HEIGHTPOSITIONWHEEL = -30.0f;
  106. static float GAPBETWEENBEAMS = 20.0f;
  107. float MoveW;
  108. float MoveA;
  109. string smoke = "smoke";
  110. string cameraName = "camera";
  111. bool canMakeSmoke = true;
  112. static float MS_PER_UPDATE = 1 / 60.0f;
  113. bool isCameraAdded;
  114. glm::vec3 theLastPositionSteamEngine;
  115. glm::vec3 cameraFront, cameraUp, cameraWorldUp, cameraRight;
  116. float cameraPitch, cameraYaw;
  117. int main(int, char**)
  118. {
  119.  
  120. cameraFront = camera.Front;
  121. cameraUp = camera.Up;
  122. cameraWorldUp = camera.WorldUp;
  123. cameraRight = camera.Right;
  124. cameraPitch = camera.Pitch;
  125. cameraYaw = camera.Yaw;
  126. float b = -3.0f;
  127. float a = 2.0f;
  128. float h;
  129. float xodleglosc;
  130. theLastPositionSteamEngine = steamEngingePosition;
  131. glfwInit();
  132. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  133. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  134. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  135. GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Pagi zadanie 4", nullptr, nullptr);
  136.  
  137. if (window == nullptr)
  138. {
  139. std::cout << "Failed to create GLFW window" << std::endl;
  140. glfwTerminate();
  141. return -1;
  142. }
  143.  
  144. glfwMakeContextCurrent(window);
  145. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  146. glfwSetCursorPosCallback(window, mouse_callback);
  147. // tell GLFW to capture our mouse
  148. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  149.  
  150. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  151. {
  152. std::cout << "Failed to initialize GLAD" << std::endl;
  153. return -1;
  154. }
  155. glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  156.  
  157. //wczytywanie shaderow
  158. int shaderProgram = createShaderProgram(vertexShaderSource, fragmentShaderSource, "vertexShaderSource", "fragmentShaderSource");
  159. Shader enviromentShader("res/shaders/enviromentShader.vs", "res/shaders/enviromentShader.fs");
  160. Shader simpleDepthShader("res/shaders/simpleDepthShader.vs", "res/shaders/simpleDepthShader.fs");
  161. Shader debugDepthQuad("res/shaders/debugDepthQuad.vs", "res/shaders/debugDepthQuad.fs");
  162. Shader skyboxShader("res/shaders/skyBoxShader.vs", "res/shaders/skyBoxShader.fs");
  163. Shader reflectShader("res/shaders/reflectShader.vs", "res/shaders/reflectShader.fs");
  164. Shader refractShader("res/shaders/reflectShader.vs", "res/shaders/refractShader.fs");
  165.  
  166. float skyboxVertices[] = {
  167. // positions
  168. -1.0f, 1.0f, -1.0f,
  169. -1.0f, -1.0f, -1.0f,
  170. 1.0f, -1.0f, -1.0f,
  171. 1.0f, -1.0f, -1.0f,
  172. 1.0f, 1.0f, -1.0f,
  173. -1.0f, 1.0f, -1.0f,
  174.  
  175. -1.0f, -1.0f, 1.0f,
  176. -1.0f, -1.0f, -1.0f,
  177. -1.0f, 1.0f, -1.0f,
  178. -1.0f, 1.0f, -1.0f,
  179. -1.0f, 1.0f, 1.0f,
  180. -1.0f, -1.0f, 1.0f,
  181.  
  182. 1.0f, -1.0f, -1.0f,
  183. 1.0f, -1.0f, 1.0f,
  184. 1.0f, 1.0f, 1.0f,
  185. 1.0f, 1.0f, 1.0f,
  186. 1.0f, 1.0f, -1.0f,
  187. 1.0f, -1.0f, -1.0f,
  188.  
  189. -1.0f, -1.0f, 1.0f,
  190. -1.0f, 1.0f, 1.0f,
  191. 1.0f, 1.0f, 1.0f,
  192. 1.0f, 1.0f, 1.0f,
  193. 1.0f, -1.0f, 1.0f,
  194. -1.0f, -1.0f, 1.0f,
  195.  
  196. -1.0f, 1.0f, -1.0f,
  197. 1.0f, 1.0f, -1.0f,
  198. 1.0f, 1.0f, 1.0f,
  199. 1.0f, 1.0f, 1.0f,
  200. -1.0f, 1.0f, 1.0f,
  201. -1.0f, 1.0f, -1.0f,
  202.  
  203. -1.0f, -1.0f, -1.0f,
  204. -1.0f, -1.0f, 1.0f,
  205. 1.0f, -1.0f, -1.0f,
  206. 1.0f, -1.0f, -1.0f,
  207. -1.0f, -1.0f, 1.0f,
  208. 1.0f, -1.0f, 1.0f
  209. };
  210. // load textures
  211.  
  212. // configure depth map FBO
  213. // -----------------------
  214. const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
  215. unsigned int depthMapFBO;
  216. glGenFramebuffers(1, &depthMapFBO);
  217. // create depth texture
  218. unsigned int depthMap;
  219. glGenTextures(1, &depthMap);
  220. glBindTexture(GL_TEXTURE_2D, depthMap);
  221.  
  222. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  223. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  224. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  225. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  226. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  227. // attach depth texture as FBO's depth buffer
  228. glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
  229. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
  230. glDrawBuffer(GL_NONE);
  231. glReadBuffer(GL_NONE);
  232. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  233.  
  234. // skybox VAO
  235. unsigned int skyboxVAO, skyboxVBO;
  236. glGenVertexArrays(1, &skyboxVAO);
  237. glGenBuffers(1, &skyboxVBO);
  238. glBindVertexArray(skyboxVAO);
  239. glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
  240. glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
  241. glEnableVertexAttribArray(0);
  242. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  243.  
  244. debugDepthQuad.use();
  245. debugDepthQuad.setInt("depthMap", 0);
  246.  
  247. // lighting info
  248. // -------------
  249. glm::vec3 lightPos(1.5f, 1.0f, -0.5f);
  250. glm::mat4 model(1);
  251. glm::mat4 view(1);
  252. glm::mat4 projection(1);
  253. glm::vec4 clear_color(0.0f, 0.06f, 0.29f, 1.0f);
  254. glClearColor(0.0f, 0.06f, 0.29f, 1.0f);
  255.  
  256. glEnable(GL_DEPTH_TEST);
  257.  
  258. //IMGUI
  259. IMGUI_CHECKVERSION();
  260. ImGui::CreateContext();
  261. ImGuiIO& io = ImGui::GetIO(); (void)io;
  262.  
  263. ImGui_ImplGlfw_InitForOpenGL(window, true);
  264. ImGui_ImplOpenGL3_Init("#version 330");
  265.  
  266.  
  267. for (int i = 0; i < 2; i++)
  268. {
  269. models.push_back(new Model("res/models/DTH/Death_Star.obj"));
  270. }
  271. steamEnginge.push_back(new Model("res/models/Lokomotywa/belka1.obj"));
  272. steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
  273. steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
  274. steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
  275. steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
  276. steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
  277. steamEnginge.push_back(new Model("res/models/Lokomotywa/lokomotywa.obj"));
  278. steamEnginge.push_back(new Model("res/models/Lokomotywa/dym.obj"));
  279. glm::mat4 localTransform(1);
  280. GLint modelLoc = glGetUniformLocation(enviromentShader.ID, "model");
  281. GraphNode mainGraphNode(localTransform, steamEnginge[6], modelLoc, enviromentShader.ID);
  282. GraphNode kolaGraphNode(localTransform, steamEnginge[1], modelLoc, enviromentShader.ID);
  283. GraphNode kolaGraphNode2(localTransform, steamEnginge[2], modelLoc, enviromentShader.ID);
  284. GraphNode kolaGraphNode3(localTransform, steamEnginge[3], modelLoc, enviromentShader.ID);
  285. GraphNode kolaGraphNode4(localTransform, steamEnginge[4], modelLoc, enviromentShader.ID);
  286. GraphNode kolaGraphNode5(localTransform, steamEnginge[5], modelLoc, enviromentShader.ID);
  287. GraphNode belkaGraphNode(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
  288. GraphNode belkaGraphNode2(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
  289. GraphNode belkaGraphNode3(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
  290. GraphNode belkaGraphNode4(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
  291. GraphNode belkaGraphNode5(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
  292. GraphNode belkaGraphNode6(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
  293. GraphNode dymGraphNode(localTransform, steamEnginge[7], modelLoc, enviromentShader.ID, smoke);
  294. GraphNode dymGraphNode2(localTransform, steamEnginge[7], modelLoc, enviromentShader.ID, smoke);
  295. GraphNode dymGraphNode3(localTransform, steamEnginge[7], modelLoc, enviromentShader.ID, smoke);
  296. GraphNode dymGraphNode4(localTransform, steamEnginge[7], modelLoc, enviromentShader.ID, smoke);
  297. localTransform = glm::translate(localTransform, glm::vec3(30.0f, 30.0f, 0.0f));
  298. GraphNode cameraNode(localTransform, &camera, modelLoc, enviromentShader.ID, cameraName);
  299.  
  300. mainGraphNode.addChildren(&kolaGraphNode);
  301. mainGraphNode.addChildren(&kolaGraphNode2);
  302. mainGraphNode.addChildren(&kolaGraphNode3);
  303. mainGraphNode.addChildren(&kolaGraphNode4);
  304. mainGraphNode.addChildren(&kolaGraphNode5);
  305. mainGraphNode.addChildren(&belkaGraphNode);
  306. mainGraphNode.addChildren(&belkaGraphNode2);
  307. mainGraphNode.addChildren(&belkaGraphNode3);
  308. mainGraphNode.addChildren(&belkaGraphNode4);
  309. mainGraphNode.addChildren(&belkaGraphNode5);
  310. mainGraphNode.addChildren(&belkaGraphNode6);
  311. camera.mainGraphNode = &cameraNode;
  312.  
  313.  
  314.  
  315. bool showGrid = false;
  316. int directRotationAngleX = -0.2f, directRotationAngleY = -1.0f, directRotationAngleZ = -0.3f;
  317.  
  318.  
  319. // load textures (we now use a utility function to keep the code more organized)
  320. // -----------------------------------------------------------------------------
  321. unsigned int diffuseMap = loadTexture("res/models/DTH/Death_Star_D.jpg");
  322. unsigned int specularMap = loadTexture("res/models/DTH/Death_Star_S.jpg");
  323. unsigned int surfaceTexture = loadTexture("res/textures/stone.jpg");
  324.  
  325. // shader configuration
  326. // --------------------
  327. enviromentShader.use();
  328. enviromentShader.setInt("material.diffuse", 0);
  329. enviromentShader.setInt("material.specular", 1);
  330.  
  331. int intensityDirect = 1;
  332.  
  333. bool direct = false;
  334. vector<std::string> faces
  335. {
  336. "res/textures/darkgloom_rt.tga",
  337. "res/textures/darkgloom_lf.tga",
  338. "res/textures/darkgloom_up.tga",
  339. "res/textures/darkgloom_dn.tga",
  340. "res/textures/darkgloom_ft.tga",
  341. "res/textures/darkgloom_bk.tga"
  342. };
  343. unsigned int cubemapTexture = loadCubemap(faces);
  344. reflectShader.use();
  345. reflectShader.setInt("skybox", 0);
  346. skyboxShader.use();
  347. skyboxShader.setInt("skybox", 0);
  348. refractShader.use();
  349. refractShader.setInt("skybox", 0);
  350.  
  351. float previous = glfwGetTime();
  352. float lag = 0.0f;
  353. while (!glfwWindowShouldClose(window))
  354. {
  355. seconds = glfwGetTime();//moje
  356. // per-frame time logic
  357. // --------------------
  358. float currentFrame = glfwGetTime();
  359. float elapsed = currentFrame - previous;
  360. previous = currentFrame;
  361. lag += elapsed;
  362.  
  363. deltaTime = currentFrame - lastFrame;//kamera
  364. lastFrame = currentFrame;//kamera
  365. processInput(window);
  366. #pragma region ImGui
  367. // Setup style
  368. ImGui::StyleColorsDark();
  369. ImGui_ImplOpenGL3_NewFrame();
  370. ImGui_ImplGlfw_NewFrame();
  371. ImGui::NewFrame();
  372. //ImGui::SetNextWindowSize(ImVec2(360, 185), 0);
  373. ImGui::SetNextWindowPos(ImVec2(0, 0));
  374. ImGui::Begin("Ustawienia");
  375. ImGui::ColorEdit3("directional", directColor);
  376. if (ImGui::Checkbox("Wylacz directionalLight", &direct)) {
  377. if (direct) {
  378. intensityDirect = 0;
  379. }
  380. else {
  381. intensityDirect = 1;
  382. }
  383. }
  384.  
  385. ImGui::Text("Zmien kierunek directional Light");
  386. ImGui::PushStyleColor(ImGuiCol_SliderGrab, ImVec4(clear_color.x, clear_color.y, clear_color.z, clear_color.w));
  387. ImGui::PushStyleColor(ImGuiCol_SliderGrabActive, ImVec4(clear_color.x, clear_color.y, clear_color.z, clear_color.w));
  388. ImGui::SliderInt("D: X", &directRotationAngleX, -360, 360);
  389. ImGui::SliderInt("D: Y", &directRotationAngleY, -360, 360);
  390. ImGui::SliderInt("D: Z", &directRotationAngleZ, -360, 360);
  391. ImGui::PopStyleColor(2);
  392. if (ImGui::Checkbox("Pokaz siatke", &showGrid)) {
  393. if (showGrid) {
  394. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  395. }
  396. else {
  397. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  398. }
  399. }
  400. ImGui::End();
  401. #pragma endregion
  402.  
  403. glClearColor(0.0f, 0.06f, 0.29f, 1.0f);
  404. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  405.  
  406. // be sure to activate shader when setting uniforms/drawing objects
  407. enviromentShader.use();
  408. enviromentShader.setVec3("viewPos", camera.Position);
  409. enviromentShader.setFloat("material.shininess", 64.0f);
  410. // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
  411. glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 100.0f);
  412. glm::mat4 view = camera.GetViewMatrix();
  413. glm::mat4 model(1.0f);
  414. enviromentShader.setMat4("projection", projection);
  415. enviromentShader.setMat4("view", view);
  416. // world transformation
  417. enviromentShader.setMat4("model", model);
  418. /*
  419. Here we set all the uniforms for the 5/6 types of lights we have. We have to set them manually and index
  420. the proper PointLight struct in the array to set each uniform variable. This can be done more code-friendly
  421. by defining light types as classes and set their values in there, or by using a more efficient uniform approach
  422. by using 'Uniform buffer objects', but that is something we'll discuss in the 'Advanced GLSL' tutorial.
  423. */
  424.  
  425. // directional light
  426. enviromentShader.setVec3("dirLight.direction", glm::vec3(glm::radians((float)directRotationAngleX), glm::radians((float)directRotationAngleY), glm::radians((float)directRotationAngleZ)));
  427. enviromentShader.setVec3("dirLight.ambient", 0.05f, 0.05f, 0.05f);
  428. enviromentShader.setVec3("dirLight.diffuse", 0.4f, 0.4f, 0.4f);
  429. enviromentShader.setVec3("dirLight.specular", 0.5f, 0.5f, 0.5f);
  430. enviromentShader.setVec3("dirLight.color", glm::vec3(directColor[0] * intensityDirect, directColor[1] * intensityDirect, directColor[2] * intensityDirect));
  431.  
  432. // bind diffuse map
  433. glActiveTexture(GL_TEXTURE0);
  434. glBindTexture(GL_TEXTURE_2D, diffuseMap);
  435. // bind specular map
  436. glActiveTexture(GL_TEXTURE1);
  437. glBindTexture(GL_TEXTURE_2D, specularMap);
  438. reflectShader.use();
  439. reflectShader.setMat4("view", view);
  440. reflectShader.setMat4("projection", projection);
  441. reflectShader.setVec3("cameraPos", camera.Position);
  442. // R E N D E R S C E N E - enviromentShader
  443. int j = 0;
  444. for (int i = 0; i < 2; i++)
  445. {
  446. model = glm::mat4(1.0f);
  447. if (i < 1)
  448. {
  449. model = glm::translate(model, glm::vec3((4 * 5.5f), 1.0f, 10.0f));
  450. model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
  451. //model = glm::rotate(model, 40.0f, glm::vec3(1.0f, 0.0f, 0.0f));
  452. }
  453. else
  454. {
  455. model = glm::translate(model, glm::vec3((4 * 5.5f), 1.0f, 20.0f));
  456. model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
  457. //model = glm::rotate(model, 40.0f, glm::vec3(1.0f, 0.0f, 0.0f));
  458. j++;
  459. }
  460. if (i < 1)
  461. {
  462. reflectShader.setMat4("model", model);
  463. models[i]->Draw(reflectShader.ID);
  464. }
  465. else
  466. {
  467. refractShader.use();
  468. refractShader.setMat4("view", view);
  469. refractShader.setMat4("projection", projection);
  470. refractShader.setVec3("cameraPos", camera.Position);
  471. refractShader.setMat4("model", model);
  472. models[i]->Draw(refractShader.ID);
  473. }
  474.  
  475. }
  476. enviromentShader.use();
  477. while (lag >= MS_PER_UPDATE)
  478. {
  479. steamEngingePosition += glm::vec3(-1.0f, 0.0f, 0.0f) * speedSteamEngine;
  480. model = glm::mat4(1.0f);
  481. model = glm::scale(model, glm::vec3(0.1f, 0.1f, 0.1f));
  482. model = glm::rotate(model, MoveA, glm::vec3(0.0f, 1.0f, 0.0f));
  483. model = glm::translate(model, steamEngingePosition);
  484. mainGraphNode.setLocalTransform(model);
  485.  
  486. if (!canMoveByMouse)
  487. {
  488. if (!isCameraAdded)
  489. {
  490. mainGraphNode.addChildren(&cameraNode);
  491. camera.Front = cameraFront;
  492. camera.Up = cameraUp;
  493. camera.WorldUp = cameraWorldUp;
  494. camera.Pitch = cameraPitch;
  495. camera.Yaw = cameraYaw;
  496. camera.Right = cameraRight;
  497. isCameraAdded = !isCameraAdded; //true
  498. mainGraphNode.hasCamera = true;
  499. }
  500.  
  501. camera.IsFollowingTrain = true;
  502. theLastPositionSteamEngine = steamEngingePosition;
  503.  
  504. }
  505. else if (mainGraphNode.hasCamera)
  506. {
  507. glm::vec3 cameraPosition = mainGraphNode.getCamera()->getTransform()[3];
  508. camera.IsFollowingTrain = false;
  509. if (isCameraAdded)
  510. {
  511. mainGraphNode.removeChildrenByName(cameraName);
  512. isCameraAdded = !isCameraAdded;
  513. camera.Position = cameraPosition;
  514. }
  515. }
  516.  
  517. model = glm::mat4(1.0f);
  518. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 0 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
  519. model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
  520. kolaGraphNode.setLocalTransform(model);
  521.  
  522. model = glm::mat4(1.0f);
  523. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
  524. model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
  525. kolaGraphNode2.setLocalTransform(model);
  526.  
  527. model = glm::mat4(1.0f);
  528. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 2 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
  529. model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
  530. kolaGraphNode3.setLocalTransform(model);
  531.  
  532. model = glm::mat4(1.0f);
  533. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 3 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
  534. model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
  535. kolaGraphNode4.setLocalTransform(model);
  536.  
  537. model = glm::mat4(1.0f);
  538. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 4 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
  539. model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
  540. kolaGraphNode5.setLocalTransform(model);
  541.  
  542. //belka
  543. model = glm::mat4(1.0f);
  544. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, GAPBETWEENBEAMS));
  545. model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime() * MoveW), b * sin((float)glfwGetTime() * MoveW), 0.0f));
  546. belkaGraphNode.setLocalTransform(model);
  547. //jak się rus\zamy no to musimy dodac do kata zmiane ruchu belek
  548. if (MoveW != 0)
  549. {
  550. h = (HEIGHTPOSITIONWHEEL + 10.0f) - (HEIGHTPOSITIONWHEEL + b * sin((float)glfwGetTime() * MoveW));
  551. xodleglosc = (FIRSTWHEELPOSITION + 3 * GAPBETWEENTHEWHEELS) + a * cos((float)glfwGetTime() * MoveW)
  552. - (FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS) + a * cos((float)glfwGetTime() * MoveW);
  553. }
  554. //ustawienie początkowe
  555. else
  556. {
  557. h = (HEIGHTPOSITIONWHEEL + 10.0f) - (HEIGHTPOSITIONWHEEL);
  558. xodleglosc = (FIRSTWHEELPOSITION + 3 * GAPBETWEENTHEWHEELS)
  559. - (FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS);
  560. }
  561. //to cos laczace belki
  562. model = glm::mat4(1.0f);
  563. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS + 3.0f, HEIGHTPOSITIONWHEEL + 10.0f, GAPBETWEENBEAMS));
  564. model = glm::rotate(model, -tan(h / xodleglosc), glm::vec3(0.0f, 0.0f, 1.0f));
  565. model = glm::scale(model, glm::vec3(0.6f, 1.0f, 1.0f));
  566. belkaGraphNode2.setLocalTransform(model);
  567.  
  568. //tlok
  569. model = glm::mat4(1.0f);
  570. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL + 10.0f, GAPBETWEENBEAMS));
  571. model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime() * MoveW), 0.0f, 0.0f));
  572. //model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime()), b * sin((float)glfwGetTime()), 0.0f));
  573. model = glm::scale(model, glm::vec3(0.05f, 1.0f, 1.0f));
  574. belkaGraphNode3.setLocalTransform(model);
  575.  
  576. //belka
  577. model = glm::mat4(1.0f);
  578. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, -GAPBETWEENBEAMS));
  579. model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime() * MoveW), b * sin((float)glfwGetTime() * MoveW), 0.0f));
  580. belkaGraphNode4.setLocalTransform(model);
  581.  
  582. //to cos laczace belki
  583. model = glm::mat4(1.0f);
  584. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS + 3.0f, HEIGHTPOSITIONWHEEL + 10.0f, -GAPBETWEENBEAMS));
  585. model = glm::rotate(model, -tan(h / xodleglosc), glm::vec3(0.0f, 0.0f, 1.0f));
  586. model = glm::scale(model, glm::vec3(0.6f, 1.0f, 1.0f));
  587. belkaGraphNode5.setLocalTransform(model);
  588.  
  589. //tlok
  590. model = glm::mat4(1.0f);
  591. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL + 10.0f, -GAPBETWEENBEAMS));
  592. model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime() * MoveW), 0.0f, 0.0f));
  593. //model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime()), b * sin((float)glfwGetTime()), 0.0f));
  594. model = glm::scale(model, glm::vec3(0.05f, 1.0f, 1.0f));
  595. belkaGraphNode6.setLocalTransform(model);
  596.  
  597. //dym
  598. if (MoveW != 0.0f && canMakeSmoke)
  599. {
  600. mainGraphNode.addChildren(&dymGraphNode);
  601. mainGraphNode.addChildren(&dymGraphNode2);
  602. mainGraphNode.addChildren(&dymGraphNode3);
  603. mainGraphNode.addChildren(&dymGraphNode4);
  604. model = glm::mat4(1.0f);
  605. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 0.5f * GAPBETWEENTHEWHEELS, 30.0f, 0.0f));
  606. dymGraphNode.setLocalTransform(model);
  607. model = glm::mat4(1.0f);
  608. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, 35.0f, 0.0f));
  609. dymGraphNode2.setLocalTransform(model);
  610. model = glm::mat4(1.0f);
  611. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1.5f * GAPBETWEENTHEWHEELS, 40.0f, 0.0f));
  612. dymGraphNode3.setLocalTransform(model);
  613. model = glm::mat4(1.0f);
  614. model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 2 * GAPBETWEENTHEWHEELS, 45.0f, 0.0f));
  615. dymGraphNode4.setLocalTransform(model);
  616. canMakeSmoke = !canMakeSmoke;//false
  617. }
  618.  
  619. else if (MoveW == 0.0f && !canMakeSmoke)
  620. {
  621. mainGraphNode.removeChildrenByName(smoke);//coś nie pyka :/
  622. canMakeSmoke = !canMakeSmoke;//true
  623. }
  624.  
  625. lag -= MS_PER_UPDATE;
  626. }
  627. //cout << canMoveByMouse << endl;
  628.  
  629.  
  630. mainGraphNode.draw();
  631.  
  632. glBindTexture(GL_TEXTURE_2D, 0);
  633.  
  634. // draw skybox as last
  635. glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
  636. skyboxShader.use();
  637. view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
  638. skyboxShader.setMat4("view", view);
  639. skyboxShader.setMat4("projection", projection);
  640. // skybox cube
  641. glBindVertexArray(skyboxVAO);
  642. glActiveTexture(GL_TEXTURE0);
  643. glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
  644. glDrawArrays(GL_TRIANGLES, 0, 36);
  645. glBindVertexArray(0);
  646. glDepthFunc(GL_LESS); // set
  647. //surface.draw();
  648. ImGui::Render();
  649. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  650. glfwSwapBuffers(window);
  651. glfwPollEvents();
  652. }
  653.  
  654. ImGui_ImplOpenGL3_Shutdown();
  655. ImGui_ImplGlfw_Shutdown();
  656. ImGui::DestroyContext();
  657. glDeleteVertexArrays(1, &skyboxVAO);
  658. glDeleteBuffers(1, &skyboxVAO);
  659. glfwTerminate();
  660. return 0;
  661. }
  662.  
  663.  
  664.  
  665. // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  666. // ---------------------------------------------------------------------------------------------------------
  667. void processInput(GLFWwindow *window)
  668. {
  669. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  670. glfwSetWindowShouldClose(window, true);
  671. if (canMoveByMouse)
  672. {
  673. if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
  674. camera.ProcessKeyboard(FORWARD, deltaTime);
  675. if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
  676. camera.ProcessKeyboard(BACKWARD, deltaTime);
  677. if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
  678. camera.ProcessKeyboard(LEFT, deltaTime);
  679. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
  680. camera.ProcessKeyboard(RIGHT, deltaTime);
  681. }
  682. else
  683. {
  684. if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
  685. {
  686. MoveW += 0.7f;
  687. speedSteamEngine += 0.05f;
  688. camera.ProcessKeyboard(FORWARD, deltaTime);
  689. }
  690. if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
  691. {
  692. speedSteamEngine -= 0.05f;
  693. MoveW -= 0.7f;
  694. camera.ProcessKeyboard(BACKWARD, deltaTime);
  695. }
  696. if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
  697. {
  698. rotationSteamEngine += 0.1f;
  699. //steamEngingePosition += glm::vec3(0.0f, 0.0f, 1.0f) * rotationSteamEngine;
  700. MoveA += 3.14f / 270.0f;
  701. camera.Yaw += 10.0f;
  702. }
  703. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
  704. {
  705. rotationSteamEngine -= 0.1f;
  706. //steamEngingePosition += glm::vec3(0.0f, 0.0f, 1.0f) * rotationSteamEngine;
  707. MoveA -= 3.14f / 270.0f;
  708. camera.Yaw -= 10.0f;
  709. }
  710. }
  711. if (seconds > timeUntilNextP)
  712. {
  713. if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS)
  714. {
  715. canMoveByMouse = !canMoveByMouse;
  716. if (canMoveByMouse) {
  717. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  718. firstMouse = true;
  719. camera.Front = cameraFront;
  720. camera.Up = cameraUp;
  721. camera.WorldUp = cameraWorldUp;
  722. camera.Pitch = cameraPitch;
  723. camera.Yaw = cameraYaw;
  724. camera.Right = cameraRight;
  725. camera.Position.x = theLastPositionSteamEngine.x;
  726. camera.Position.y = theLastPositionSteamEngine.y;
  727. camera.Position.z = theLastPositionSteamEngine.z;
  728. }
  729. else
  730. {
  731. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  732. firstMouse = false;
  733. }
  734.  
  735. }
  736. timeUntilNextP = seconds + 0.1;
  737. }
  738. }
  739.  
  740. // glfw: whenever the window size changed (by OS or user resize) this callback function executes
  741. // ---------------------------------------------------------------------------------------------
  742. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  743. {
  744. // make sure the viewport matches the new window dimensions; note that width and
  745. // height will be significantly larger than specified on retina displays.
  746. glViewport(0, 0, width, height);
  747. }
  748.  
  749. // glfw: whenever the mouse moves, this callback is called
  750. // -------------------------------------------------------
  751. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
  752. {
  753. if (canMoveByMouse)
  754. {
  755. if (firstMouse)
  756. {
  757. lastX = xpos;
  758. lastY = ypos;
  759. firstMouse = false;
  760. }
  761.  
  762. float xoffset = xpos - lastX;
  763. float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
  764.  
  765. lastX = xpos;
  766. lastY = ypos;
  767.  
  768. camera.ProcessMouseMovement(xoffset, yoffset);
  769. }
  770. }
  771.  
  772.  
  773.  
  774. int createShaderProgram(const GLchar* vsSource, const GLchar* fsSource, string vsName, string fsName)
  775. {
  776. int success;
  777. char infoLog[512];
  778.  
  779. int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  780. glShaderSource(vertexShader, 1, &vsSource, NULL);
  781. glCompileShader(vertexShader);
  782.  
  783. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  784. if (!success) {
  785. glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  786. std::cout << "Error: " << vsName << " compilation failed:\n" << infoLog << std::endl;
  787. }
  788.  
  789. int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  790. glShaderSource(fragmentShader, 1, &fsSource, NULL);
  791. glCompileShader(fragmentShader);
  792.  
  793. glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  794. if (!success) {
  795. glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
  796. std::cout << "Error: " << fsName << " compilation failed:\n" << infoLog << std::endl;
  797. }
  798.  
  799. //link shaders
  800. int shaderProgram = glCreateProgram();
  801. glAttachShader(shaderProgram, vertexShader);
  802. glAttachShader(shaderProgram, fragmentShader);
  803. glLinkProgram(shaderProgram);
  804.  
  805. glGetProgramiv(shaderProgram, GL_COMPILE_STATUS, &success);
  806. if (!success) {
  807. glGetShaderInfoLog(shaderProgram, 512, NULL, infoLog);
  808. std::cout << "Error: Shader Program linking failed:\n" << infoLog << std::endl;
  809. }
  810.  
  811. glDeleteShader(vertexShader);
  812. glDeleteShader(fragmentShader);
  813. return shaderProgram;
  814. }
  815.  
  816. unsigned int loadTexture(char const * path)
  817. {
  818. unsigned int textureID;
  819. glGenTextures(1, &textureID);
  820.  
  821. int width, height, nrComponents;
  822. unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
  823. if (data)
  824. {
  825. GLenum format;
  826. if (nrComponents == 1)
  827. format = GL_RED;
  828. else if (nrComponents == 3)
  829. format = GL_RGB;
  830. else if (nrComponents == 4)
  831. format = GL_RGBA;
  832.  
  833. glBindTexture(GL_TEXTURE_2D, textureID);
  834. glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
  835. glGenerateMipmap(GL_TEXTURE_2D);
  836.  
  837. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT); // for this tutorial: use GL_CLAMP_TO_EDGE to prevent semi-transparent borders. Due to interpolation it takes texels from next repeat
  838. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT);
  839. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  840. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  841.  
  842. stbi_image_free(data);
  843. }
  844. else
  845. {
  846. std::cout << "Texture failed to load at path: " << path << std::endl;
  847. stbi_image_free(data);
  848. }
  849.  
  850. return textureID;
  851. }
  852.  
  853. unsigned int loadCubemap(vector<std::string> faces)
  854. {
  855. unsigned int textureID;
  856. glGenTextures(1, &textureID);
  857. glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
  858.  
  859. int width, height, nrChannels;
  860. for (unsigned int i = 0; i < faces.size(); i++)
  861. {
  862. unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
  863. if (data)
  864. {
  865. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
  866. 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data
  867. );
  868. stbi_image_free(data);
  869. }
  870. else
  871. {
  872. std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
  873. stbi_image_free(data);
  874. }
  875. }
  876. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  877. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  878. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  879. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  880. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  881.  
  882. return textureID;
  883. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement