Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "imgui.h"
- #include "imgui_impl_glfw.h"
- #include "imgui_impl_opengl3.h"
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <iostream>
- #include <stdio.h>
- #include <vector>
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <filesystem>
- #include "stb_image.h"
- #include "model.h"
- #include "GraphNode.h"
- #include "shader.h"
- #include "camera.h"
- // About OpenGL function loaders: modern OpenGL doesn't have a standard header file and requires individual function pointers to be loaded manually.
- // Helper libraries are often used for this purpose! Here we are supporting a few common ones: gl3w, glew, glad.
- // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
- #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
- #include <GL/gl3w.h> // Initialize with gl3wInit()
- #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
- #include <GL/glew.h> // Initialize with glewInit()
- #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
- #include <glad/glad.h> // Initialize with gladLoadGL()
- #else
- #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM
- #endif
- #include <GLFW/glfw3.h> // Include glfw3.h after our OpenGL definitions
- static void glfw_error_callback(int error, const char* description)
- {
- fprintf(stderr, "Glfw Error %d: %s\n", error, description);
- }
- const int SCREEN_WIDTH = 1280;
- const int SCREEN_HEIGHT = 1024;
- double seconds;
- double timeUntilNextP = 0;
- using namespace std;
- std::vector<Model*> models;
- std::vector<Model*> steamEnginge;
- glm::vec3 steamEngingePosition = glm::vec3(0.0, 40.0f, -0.5f);
- static float directColor[3] = { 1.0f,1.0f,1.0f };
- static float speedSteamEngine;
- static float rotationSteamEngine;
- unsigned int loadTexture(char const * path);
- #pragma region Shaders
- const GLchar* vertexShaderSource =
- "#version 330 core\n"
- "layout(location = 0) in vec3 aPos; \n"
- "layout(location = 1) in vec3 aNormal; \n"
- "layout(location = 2) in vec2 aTexCoords; \n"
- "out vec2 TexCoords; \n"
- "uniform mat4 model; \n"
- "uniform mat4 view; \n"
- "uniform mat4 projection; \n"
- "void main()\n"
- "{\n"
- " TexCoords = aTexCoords; \n"
- " gl_Position = projection * view * model * vec4(aPos, 1.0); \n"
- "}\n";
- const GLchar* fragmentShaderSource =
- "#version 330 core\n"
- "out vec4 FragColor; \n"
- "in vec2 TexCoords; \n"
- "uniform sampler2D texture_diffuse1; \n"
- "void main()\n"
- "{\n"
- " FragColor = texture(texture_diffuse1, TexCoords); \n"
- "}\n";
- #pragma endregion
- #pragma region Camera
- // camera
- Camera camera(glm::vec3(24.25f, 7.68f, 15.06f));
- float lastX = (float)SCREEN_WIDTH / 2.0;
- float lastY = (float)SCREEN_HEIGHT / 2.0;
- bool firstMouse = true;
- bool canMoveByMouse = true;
- // timing
- float deltaTime = 0.0f; // time between current frame and last frame
- float lastFrame = 0.0f;
- #pragma endregion
- #pragma region Methods
- int createShaderProgram(const GLchar* vsSource, const GLchar* fsSource, string vsName, string fsName);
- void processInput(GLFWwindow *window);
- void framebuffer_size_callback(GLFWwindow* window, int width, int height);
- void mouse_callback(GLFWwindow* window, double xpos, double ypos);
- unsigned int loadCubemap(vector<std::string> faces);
- #pragma endregion
- static float FIRSTWHEELPOSITION = -90.0f;
- static float GAPBETWEENTHEWHEELS = 25.0f;
- static float HEIGHTPOSITIONWHEEL = -30.0f;
- static float GAPBETWEENBEAMS = 20.0f;
- float MoveW;
- float MoveA;
- string smoke = "smoke";
- string cameraName = "camera";
- bool canMakeSmoke = true;
- static float MS_PER_UPDATE = 1 / 60.0f;
- bool isCameraAdded;
- glm::vec3 theLastPositionSteamEngine;
- glm::vec3 cameraFront, cameraUp, cameraWorldUp, cameraRight;
- float cameraPitch, cameraYaw;
- int main(int, char**)
- {
- cameraFront = camera.Front;
- cameraUp = camera.Up;
- cameraWorldUp = camera.WorldUp;
- cameraRight = camera.Right;
- cameraPitch = camera.Pitch;
- cameraYaw = camera.Yaw;
- float b = -3.0f;
- float a = 2.0f;
- float h;
- float xodleglosc;
- theLastPositionSteamEngine = steamEngingePosition;
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Pagi zadanie 4", nullptr, nullptr);
- if (window == nullptr)
- {
- std::cout << "Failed to create GLFW window" << std::endl;
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- glfwSetCursorPosCallback(window, mouse_callback);
- // tell GLFW to capture our mouse
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
- {
- std::cout << "Failed to initialize GLAD" << std::endl;
- return -1;
- }
- glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
- //wczytywanie shaderow
- int shaderProgram = createShaderProgram(vertexShaderSource, fragmentShaderSource, "vertexShaderSource", "fragmentShaderSource");
- Shader enviromentShader("res/shaders/enviromentShader.vs", "res/shaders/enviromentShader.fs");
- Shader simpleDepthShader("res/shaders/simpleDepthShader.vs", "res/shaders/simpleDepthShader.fs");
- Shader debugDepthQuad("res/shaders/debugDepthQuad.vs", "res/shaders/debugDepthQuad.fs");
- Shader skyboxShader("res/shaders/skyBoxShader.vs", "res/shaders/skyBoxShader.fs");
- Shader reflectShader("res/shaders/reflectShader.vs", "res/shaders/reflectShader.fs");
- Shader refractShader("res/shaders/reflectShader.vs", "res/shaders/refractShader.fs");
- float skyboxVertices[] = {
- // positions
- -1.0f, 1.0f, -1.0f,
- -1.0f, -1.0f, -1.0f,
- 1.0f, -1.0f, -1.0f,
- 1.0f, -1.0f, -1.0f,
- 1.0f, 1.0f, -1.0f,
- -1.0f, 1.0f, -1.0f,
- -1.0f, -1.0f, 1.0f,
- -1.0f, -1.0f, -1.0f,
- -1.0f, 1.0f, -1.0f,
- -1.0f, 1.0f, -1.0f,
- -1.0f, 1.0f, 1.0f,
- -1.0f, -1.0f, 1.0f,
- 1.0f, -1.0f, -1.0f,
- 1.0f, -1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, -1.0f,
- 1.0f, -1.0f, -1.0f,
- -1.0f, -1.0f, 1.0f,
- -1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, -1.0f, 1.0f,
- -1.0f, -1.0f, 1.0f,
- -1.0f, 1.0f, -1.0f,
- 1.0f, 1.0f, -1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- -1.0f, 1.0f, 1.0f,
- -1.0f, 1.0f, -1.0f,
- -1.0f, -1.0f, -1.0f,
- -1.0f, -1.0f, 1.0f,
- 1.0f, -1.0f, -1.0f,
- 1.0f, -1.0f, -1.0f,
- -1.0f, -1.0f, 1.0f,
- 1.0f, -1.0f, 1.0f
- };
- // load textures
- // configure depth map FBO
- // -----------------------
- const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
- unsigned int depthMapFBO;
- glGenFramebuffers(1, &depthMapFBO);
- // create depth texture
- unsigned int depthMap;
- glGenTextures(1, &depthMap);
- glBindTexture(GL_TEXTURE_2D, depthMap);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- // attach depth texture as FBO's depth buffer
- glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
- glDrawBuffer(GL_NONE);
- glReadBuffer(GL_NONE);
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- // skybox VAO
- unsigned int skyboxVAO, skyboxVBO;
- glGenVertexArrays(1, &skyboxVAO);
- glGenBuffers(1, &skyboxVBO);
- glBindVertexArray(skyboxVAO);
- glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
- debugDepthQuad.use();
- debugDepthQuad.setInt("depthMap", 0);
- // lighting info
- // -------------
- glm::vec3 lightPos(1.5f, 1.0f, -0.5f);
- glm::mat4 model(1);
- glm::mat4 view(1);
- glm::mat4 projection(1);
- glm::vec4 clear_color(0.0f, 0.06f, 0.29f, 1.0f);
- glClearColor(0.0f, 0.06f, 0.29f, 1.0f);
- glEnable(GL_DEPTH_TEST);
- //IMGUI
- IMGUI_CHECKVERSION();
- ImGui::CreateContext();
- ImGuiIO& io = ImGui::GetIO(); (void)io;
- ImGui_ImplGlfw_InitForOpenGL(window, true);
- ImGui_ImplOpenGL3_Init("#version 330");
- for (int i = 0; i < 2; i++)
- {
- models.push_back(new Model("res/models/DTH/Death_Star.obj"));
- }
- steamEnginge.push_back(new Model("res/models/Lokomotywa/belka1.obj"));
- steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
- steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
- steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
- steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
- steamEnginge.push_back(new Model("res/models/Lokomotywa/koła.obj"));
- steamEnginge.push_back(new Model("res/models/Lokomotywa/lokomotywa.obj"));
- steamEnginge.push_back(new Model("res/models/Lokomotywa/dym.obj"));
- glm::mat4 localTransform(1);
- GLint modelLoc = glGetUniformLocation(enviromentShader.ID, "model");
- GraphNode mainGraphNode(localTransform, steamEnginge[6], modelLoc, enviromentShader.ID);
- GraphNode kolaGraphNode(localTransform, steamEnginge[1], modelLoc, enviromentShader.ID);
- GraphNode kolaGraphNode2(localTransform, steamEnginge[2], modelLoc, enviromentShader.ID);
- GraphNode kolaGraphNode3(localTransform, steamEnginge[3], modelLoc, enviromentShader.ID);
- GraphNode kolaGraphNode4(localTransform, steamEnginge[4], modelLoc, enviromentShader.ID);
- GraphNode kolaGraphNode5(localTransform, steamEnginge[5], modelLoc, enviromentShader.ID);
- GraphNode belkaGraphNode(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
- GraphNode belkaGraphNode2(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
- GraphNode belkaGraphNode3(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
- GraphNode belkaGraphNode4(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
- GraphNode belkaGraphNode5(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
- GraphNode belkaGraphNode6(localTransform, steamEnginge[0], modelLoc, enviromentShader.ID);
- GraphNode dymGraphNode(localTransform, steamEnginge[7], modelLoc, enviromentShader.ID, smoke);
- GraphNode dymGraphNode2(localTransform, steamEnginge[7], modelLoc, enviromentShader.ID, smoke);
- GraphNode dymGraphNode3(localTransform, steamEnginge[7], modelLoc, enviromentShader.ID, smoke);
- GraphNode dymGraphNode4(localTransform, steamEnginge[7], modelLoc, enviromentShader.ID, smoke);
- localTransform = glm::translate(localTransform, glm::vec3(30.0f, 30.0f, 0.0f));
- GraphNode cameraNode(localTransform, &camera, modelLoc, enviromentShader.ID, cameraName);
- mainGraphNode.addChildren(&kolaGraphNode);
- mainGraphNode.addChildren(&kolaGraphNode2);
- mainGraphNode.addChildren(&kolaGraphNode3);
- mainGraphNode.addChildren(&kolaGraphNode4);
- mainGraphNode.addChildren(&kolaGraphNode5);
- mainGraphNode.addChildren(&belkaGraphNode);
- mainGraphNode.addChildren(&belkaGraphNode2);
- mainGraphNode.addChildren(&belkaGraphNode3);
- mainGraphNode.addChildren(&belkaGraphNode4);
- mainGraphNode.addChildren(&belkaGraphNode5);
- mainGraphNode.addChildren(&belkaGraphNode6);
- camera.mainGraphNode = &cameraNode;
- bool showGrid = false;
- int directRotationAngleX = -0.2f, directRotationAngleY = -1.0f, directRotationAngleZ = -0.3f;
- // load textures (we now use a utility function to keep the code more organized)
- // -----------------------------------------------------------------------------
- unsigned int diffuseMap = loadTexture("res/models/DTH/Death_Star_D.jpg");
- unsigned int specularMap = loadTexture("res/models/DTH/Death_Star_S.jpg");
- unsigned int surfaceTexture = loadTexture("res/textures/stone.jpg");
- // shader configuration
- // --------------------
- enviromentShader.use();
- enviromentShader.setInt("material.diffuse", 0);
- enviromentShader.setInt("material.specular", 1);
- int intensityDirect = 1;
- bool direct = false;
- vector<std::string> faces
- {
- "res/textures/darkgloom_rt.tga",
- "res/textures/darkgloom_lf.tga",
- "res/textures/darkgloom_up.tga",
- "res/textures/darkgloom_dn.tga",
- "res/textures/darkgloom_ft.tga",
- "res/textures/darkgloom_bk.tga"
- };
- unsigned int cubemapTexture = loadCubemap(faces);
- reflectShader.use();
- reflectShader.setInt("skybox", 0);
- skyboxShader.use();
- skyboxShader.setInt("skybox", 0);
- refractShader.use();
- refractShader.setInt("skybox", 0);
- float previous = glfwGetTime();
- float lag = 0.0f;
- while (!glfwWindowShouldClose(window))
- {
- seconds = glfwGetTime();//moje
- // per-frame time logic
- // --------------------
- float currentFrame = glfwGetTime();
- float elapsed = currentFrame - previous;
- previous = currentFrame;
- lag += elapsed;
- deltaTime = currentFrame - lastFrame;//kamera
- lastFrame = currentFrame;//kamera
- processInput(window);
- #pragma region ImGui
- // Setup style
- ImGui::StyleColorsDark();
- ImGui_ImplOpenGL3_NewFrame();
- ImGui_ImplGlfw_NewFrame();
- ImGui::NewFrame();
- //ImGui::SetNextWindowSize(ImVec2(360, 185), 0);
- ImGui::SetNextWindowPos(ImVec2(0, 0));
- ImGui::Begin("Ustawienia");
- ImGui::ColorEdit3("directional", directColor);
- if (ImGui::Checkbox("Wylacz directionalLight", &direct)) {
- if (direct) {
- intensityDirect = 0;
- }
- else {
- intensityDirect = 1;
- }
- }
- ImGui::Text("Zmien kierunek directional Light");
- ImGui::PushStyleColor(ImGuiCol_SliderGrab, ImVec4(clear_color.x, clear_color.y, clear_color.z, clear_color.w));
- ImGui::PushStyleColor(ImGuiCol_SliderGrabActive, ImVec4(clear_color.x, clear_color.y, clear_color.z, clear_color.w));
- ImGui::SliderInt("D: X", &directRotationAngleX, -360, 360);
- ImGui::SliderInt("D: Y", &directRotationAngleY, -360, 360);
- ImGui::SliderInt("D: Z", &directRotationAngleZ, -360, 360);
- ImGui::PopStyleColor(2);
- if (ImGui::Checkbox("Pokaz siatke", &showGrid)) {
- if (showGrid) {
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- }
- else {
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- }
- }
- ImGui::End();
- #pragma endregion
- glClearColor(0.0f, 0.06f, 0.29f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // be sure to activate shader when setting uniforms/drawing objects
- enviromentShader.use();
- enviromentShader.setVec3("viewPos", camera.Position);
- enviromentShader.setFloat("material.shininess", 64.0f);
- // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
- glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 100.0f);
- glm::mat4 view = camera.GetViewMatrix();
- glm::mat4 model(1.0f);
- enviromentShader.setMat4("projection", projection);
- enviromentShader.setMat4("view", view);
- // world transformation
- enviromentShader.setMat4("model", model);
- /*
- Here we set all the uniforms for the 5/6 types of lights we have. We have to set them manually and index
- the proper PointLight struct in the array to set each uniform variable. This can be done more code-friendly
- by defining light types as classes and set their values in there, or by using a more efficient uniform approach
- by using 'Uniform buffer objects', but that is something we'll discuss in the 'Advanced GLSL' tutorial.
- */
- // directional light
- enviromentShader.setVec3("dirLight.direction", glm::vec3(glm::radians((float)directRotationAngleX), glm::radians((float)directRotationAngleY), glm::radians((float)directRotationAngleZ)));
- enviromentShader.setVec3("dirLight.ambient", 0.05f, 0.05f, 0.05f);
- enviromentShader.setVec3("dirLight.diffuse", 0.4f, 0.4f, 0.4f);
- enviromentShader.setVec3("dirLight.specular", 0.5f, 0.5f, 0.5f);
- enviromentShader.setVec3("dirLight.color", glm::vec3(directColor[0] * intensityDirect, directColor[1] * intensityDirect, directColor[2] * intensityDirect));
- // bind diffuse map
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, diffuseMap);
- // bind specular map
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, specularMap);
- reflectShader.use();
- reflectShader.setMat4("view", view);
- reflectShader.setMat4("projection", projection);
- reflectShader.setVec3("cameraPos", camera.Position);
- // R E N D E R S C E N E - enviromentShader
- int j = 0;
- for (int i = 0; i < 2; i++)
- {
- model = glm::mat4(1.0f);
- if (i < 1)
- {
- model = glm::translate(model, glm::vec3((4 * 5.5f), 1.0f, 10.0f));
- model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
- //model = glm::rotate(model, 40.0f, glm::vec3(1.0f, 0.0f, 0.0f));
- }
- else
- {
- model = glm::translate(model, glm::vec3((4 * 5.5f), 1.0f, 20.0f));
- model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
- //model = glm::rotate(model, 40.0f, glm::vec3(1.0f, 0.0f, 0.0f));
- j++;
- }
- if (i < 1)
- {
- reflectShader.setMat4("model", model);
- models[i]->Draw(reflectShader.ID);
- }
- else
- {
- refractShader.use();
- refractShader.setMat4("view", view);
- refractShader.setMat4("projection", projection);
- refractShader.setVec3("cameraPos", camera.Position);
- refractShader.setMat4("model", model);
- models[i]->Draw(refractShader.ID);
- }
- }
- enviromentShader.use();
- while (lag >= MS_PER_UPDATE)
- {
- steamEngingePosition += glm::vec3(-1.0f, 0.0f, 0.0f) * speedSteamEngine;
- model = glm::mat4(1.0f);
- model = glm::scale(model, glm::vec3(0.1f, 0.1f, 0.1f));
- model = glm::rotate(model, MoveA, glm::vec3(0.0f, 1.0f, 0.0f));
- model = glm::translate(model, steamEngingePosition);
- mainGraphNode.setLocalTransform(model);
- if (!canMoveByMouse)
- {
- if (!isCameraAdded)
- {
- mainGraphNode.addChildren(&cameraNode);
- camera.Front = cameraFront;
- camera.Up = cameraUp;
- camera.WorldUp = cameraWorldUp;
- camera.Pitch = cameraPitch;
- camera.Yaw = cameraYaw;
- camera.Right = cameraRight;
- isCameraAdded = !isCameraAdded; //true
- mainGraphNode.hasCamera = true;
- }
- camera.IsFollowingTrain = true;
- theLastPositionSteamEngine = steamEngingePosition;
- }
- else if (mainGraphNode.hasCamera)
- {
- glm::vec3 cameraPosition = mainGraphNode.getCamera()->getTransform()[3];
- camera.IsFollowingTrain = false;
- if (isCameraAdded)
- {
- mainGraphNode.removeChildrenByName(cameraName);
- isCameraAdded = !isCameraAdded;
- camera.Position = cameraPosition;
- }
- }
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 0 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
- model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
- kolaGraphNode.setLocalTransform(model);
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
- model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
- kolaGraphNode2.setLocalTransform(model);
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 2 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
- model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
- kolaGraphNode3.setLocalTransform(model);
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 3 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
- model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
- kolaGraphNode4.setLocalTransform(model);
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 4 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, 0.0f));
- model = glm::rotate(model, MoveW * (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
- kolaGraphNode5.setLocalTransform(model);
- //belka
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, GAPBETWEENBEAMS));
- model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime() * MoveW), b * sin((float)glfwGetTime() * MoveW), 0.0f));
- belkaGraphNode.setLocalTransform(model);
- //jak się rus\zamy no to musimy dodac do kata zmiane ruchu belek
- if (MoveW != 0)
- {
- h = (HEIGHTPOSITIONWHEEL + 10.0f) - (HEIGHTPOSITIONWHEEL + b * sin((float)glfwGetTime() * MoveW));
- xodleglosc = (FIRSTWHEELPOSITION + 3 * GAPBETWEENTHEWHEELS) + a * cos((float)glfwGetTime() * MoveW)
- - (FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS) + a * cos((float)glfwGetTime() * MoveW);
- }
- //ustawienie początkowe
- else
- {
- h = (HEIGHTPOSITIONWHEEL + 10.0f) - (HEIGHTPOSITIONWHEEL);
- xodleglosc = (FIRSTWHEELPOSITION + 3 * GAPBETWEENTHEWHEELS)
- - (FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS);
- }
- //to cos laczace belki
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS + 3.0f, HEIGHTPOSITIONWHEEL + 10.0f, GAPBETWEENBEAMS));
- model = glm::rotate(model, -tan(h / xodleglosc), glm::vec3(0.0f, 0.0f, 1.0f));
- model = glm::scale(model, glm::vec3(0.6f, 1.0f, 1.0f));
- belkaGraphNode2.setLocalTransform(model);
- //tlok
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL + 10.0f, GAPBETWEENBEAMS));
- model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime() * MoveW), 0.0f, 0.0f));
- //model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime()), b * sin((float)glfwGetTime()), 0.0f));
- model = glm::scale(model, glm::vec3(0.05f, 1.0f, 1.0f));
- belkaGraphNode3.setLocalTransform(model);
- //belka
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL, -GAPBETWEENBEAMS));
- model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime() * MoveW), b * sin((float)glfwGetTime() * MoveW), 0.0f));
- belkaGraphNode4.setLocalTransform(model);
- //to cos laczace belki
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS + 3.0f, HEIGHTPOSITIONWHEEL + 10.0f, -GAPBETWEENBEAMS));
- model = glm::rotate(model, -tan(h / xodleglosc), glm::vec3(0.0f, 0.0f, 1.0f));
- model = glm::scale(model, glm::vec3(0.6f, 1.0f, 1.0f));
- belkaGraphNode5.setLocalTransform(model);
- //tlok
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, HEIGHTPOSITIONWHEEL + 10.0f, -GAPBETWEENBEAMS));
- model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime() * MoveW), 0.0f, 0.0f));
- //model = glm::translate(model, glm::vec3(a * cos((float)glfwGetTime()), b * sin((float)glfwGetTime()), 0.0f));
- model = glm::scale(model, glm::vec3(0.05f, 1.0f, 1.0f));
- belkaGraphNode6.setLocalTransform(model);
- //dym
- if (MoveW != 0.0f && canMakeSmoke)
- {
- mainGraphNode.addChildren(&dymGraphNode);
- mainGraphNode.addChildren(&dymGraphNode2);
- mainGraphNode.addChildren(&dymGraphNode3);
- mainGraphNode.addChildren(&dymGraphNode4);
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 0.5f * GAPBETWEENTHEWHEELS, 30.0f, 0.0f));
- dymGraphNode.setLocalTransform(model);
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1 * GAPBETWEENTHEWHEELS, 35.0f, 0.0f));
- dymGraphNode2.setLocalTransform(model);
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 1.5f * GAPBETWEENTHEWHEELS, 40.0f, 0.0f));
- dymGraphNode3.setLocalTransform(model);
- model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(FIRSTWHEELPOSITION + 2 * GAPBETWEENTHEWHEELS, 45.0f, 0.0f));
- dymGraphNode4.setLocalTransform(model);
- canMakeSmoke = !canMakeSmoke;//false
- }
- else if (MoveW == 0.0f && !canMakeSmoke)
- {
- mainGraphNode.removeChildrenByName(smoke);//coś nie pyka :/
- canMakeSmoke = !canMakeSmoke;//true
- }
- lag -= MS_PER_UPDATE;
- }
- //cout << canMoveByMouse << endl;
- mainGraphNode.draw();
- glBindTexture(GL_TEXTURE_2D, 0);
- // draw skybox as last
- glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
- skyboxShader.use();
- view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
- skyboxShader.setMat4("view", view);
- skyboxShader.setMat4("projection", projection);
- // skybox cube
- glBindVertexArray(skyboxVAO);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
- glDrawArrays(GL_TRIANGLES, 0, 36);
- glBindVertexArray(0);
- glDepthFunc(GL_LESS); // set
- //surface.draw();
- ImGui::Render();
- ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- ImGui_ImplOpenGL3_Shutdown();
- ImGui_ImplGlfw_Shutdown();
- ImGui::DestroyContext();
- glDeleteVertexArrays(1, &skyboxVAO);
- glDeleteBuffers(1, &skyboxVAO);
- glfwTerminate();
- return 0;
- }
- // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
- // ---------------------------------------------------------------------------------------------------------
- void processInput(GLFWwindow *window)
- {
- if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window, true);
- if (canMoveByMouse)
- {
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
- camera.ProcessKeyboard(FORWARD, deltaTime);
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
- camera.ProcessKeyboard(BACKWARD, deltaTime);
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
- camera.ProcessKeyboard(LEFT, deltaTime);
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
- camera.ProcessKeyboard(RIGHT, deltaTime);
- }
- else
- {
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
- {
- MoveW += 0.7f;
- speedSteamEngine += 0.05f;
- camera.ProcessKeyboard(FORWARD, deltaTime);
- }
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
- {
- speedSteamEngine -= 0.05f;
- MoveW -= 0.7f;
- camera.ProcessKeyboard(BACKWARD, deltaTime);
- }
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
- {
- rotationSteamEngine += 0.1f;
- //steamEngingePosition += glm::vec3(0.0f, 0.0f, 1.0f) * rotationSteamEngine;
- MoveA += 3.14f / 270.0f;
- camera.Yaw += 10.0f;
- }
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
- {
- rotationSteamEngine -= 0.1f;
- //steamEngingePosition += glm::vec3(0.0f, 0.0f, 1.0f) * rotationSteamEngine;
- MoveA -= 3.14f / 270.0f;
- camera.Yaw -= 10.0f;
- }
- }
- if (seconds > timeUntilNextP)
- {
- if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS)
- {
- canMoveByMouse = !canMoveByMouse;
- if (canMoveByMouse) {
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- firstMouse = true;
- camera.Front = cameraFront;
- camera.Up = cameraUp;
- camera.WorldUp = cameraWorldUp;
- camera.Pitch = cameraPitch;
- camera.Yaw = cameraYaw;
- camera.Right = cameraRight;
- camera.Position.x = theLastPositionSteamEngine.x;
- camera.Position.y = theLastPositionSteamEngine.y;
- camera.Position.z = theLastPositionSteamEngine.z;
- }
- else
- {
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
- firstMouse = false;
- }
- }
- timeUntilNextP = seconds + 0.1;
- }
- }
- // glfw: whenever the window size changed (by OS or user resize) this callback function executes
- // ---------------------------------------------------------------------------------------------
- void framebuffer_size_callback(GLFWwindow* window, int width, int height)
- {
- // make sure the viewport matches the new window dimensions; note that width and
- // height will be significantly larger than specified on retina displays.
- glViewport(0, 0, width, height);
- }
- // glfw: whenever the mouse moves, this callback is called
- // -------------------------------------------------------
- void mouse_callback(GLFWwindow* window, double xpos, double ypos)
- {
- if (canMoveByMouse)
- {
- if (firstMouse)
- {
- lastX = xpos;
- lastY = ypos;
- firstMouse = false;
- }
- float xoffset = xpos - lastX;
- float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
- lastX = xpos;
- lastY = ypos;
- camera.ProcessMouseMovement(xoffset, yoffset);
- }
- }
- int createShaderProgram(const GLchar* vsSource, const GLchar* fsSource, string vsName, string fsName)
- {
- int success;
- char infoLog[512];
- int vertexShader = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertexShader, 1, &vsSource, NULL);
- glCompileShader(vertexShader);
- glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
- if (!success) {
- glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
- std::cout << "Error: " << vsName << " compilation failed:\n" << infoLog << std::endl;
- }
- int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentShader, 1, &fsSource, NULL);
- glCompileShader(fragmentShader);
- glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
- if (!success) {
- glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
- std::cout << "Error: " << fsName << " compilation failed:\n" << infoLog << std::endl;
- }
- //link shaders
- int shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
- glGetProgramiv(shaderProgram, GL_COMPILE_STATUS, &success);
- if (!success) {
- glGetShaderInfoLog(shaderProgram, 512, NULL, infoLog);
- std::cout << "Error: Shader Program linking failed:\n" << infoLog << std::endl;
- }
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
- return shaderProgram;
- }
- unsigned int loadTexture(char const * path)
- {
- unsigned int textureID;
- glGenTextures(1, &textureID);
- int width, height, nrComponents;
- unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
- if (data)
- {
- GLenum format;
- if (nrComponents == 1)
- format = GL_RED;
- else if (nrComponents == 3)
- format = GL_RGB;
- else if (nrComponents == 4)
- format = GL_RGBA;
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
- glGenerateMipmap(GL_TEXTURE_2D);
- 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
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- stbi_image_free(data);
- }
- else
- {
- std::cout << "Texture failed to load at path: " << path << std::endl;
- stbi_image_free(data);
- }
- return textureID;
- }
- unsigned int loadCubemap(vector<std::string> faces)
- {
- unsigned int textureID;
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
- int width, height, nrChannels;
- for (unsigned int i = 0; i < faces.size(); i++)
- {
- unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
- if (data)
- {
- glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
- 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data
- );
- stbi_image_free(data);
- }
- else
- {
- std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
- stbi_image_free(data);
- }
- }
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
- return textureID;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement