Advertisement
Guest User

main

a guest
Dec 8th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define NOMINMAX
  2. #define RADPERDEG 0.0174533
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8. #include <cmath>
  9. #include <vector>
  10. #include <sstream>
  11. #include <algorithm>
  12.  
  13. // GLEW
  14. //#define GLEW_STATIC
  15. #include <glew.h>
  16.  
  17. // GLFW
  18. #include <glfw3.h>
  19.  
  20. // GLUT
  21. #include <glut.h>
  22.  
  23. //VTK
  24. #include <vtkSmartPointer.h>
  25. #include <vtkPolyData.h>
  26. #include <vtkXMLPolyDataReader.h>
  27. #include <vtkCell.h>
  28. #include <vtkCellData.h>
  29. #include <vtkPoints.h>
  30. #include <vtkPointData.h>
  31. #include <vtkIdList.h>
  32. #include <vtkDoubleArray.h>
  33.  
  34. // GL includes
  35. #include "Shader.h"
  36. #include "camera.h"
  37. #include "Model.h"
  38. #include "ModelPathlines.h"
  39.  
  40. // GLM Mathemtics
  41. #include <glm.hpp>
  42. #include <gtc/matrix_transform.hpp>
  43. #include <gtc/type_ptr.hpp>
  44.  
  45. // Other Libs
  46. #include <SOIL.h>
  47. #include <ft2build.h>
  48. #include FT_FREETYPE_H  
  49.  
  50. #include <random> // necessary for generation of random floats (for sample kernel and noise texture)
  51.  
  52. // Properties
  53. const GLuint SCR_WIDTH = 1800, SCR_HEIGHT = 1000;
  54.  
  55. // Function prototypes
  56. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
  57. void mouse_callback(GLFWwindow* window, double xpos, double ypos);
  58. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
  59. void Do_Movement();
  60. void RenderQuad();
  61.  
  62. void RenderText(Shader &shader, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color);
  63.  
  64.  
  65. // Camera
  66. Camera camera(glm::vec3(0.0f, 0.0f, 2.5f));
  67.  
  68. // Delta
  69. bool _useAnimation = true;
  70. GLfloat _timeRange = 0.0f;
  71. GLfloat _lineLength = 0.5f;
  72. GLfloat _minVelocity = 0.0f;
  73. GLfloat _maxVelocity = 0.0f;
  74. GLfloat _currentTime = 0.0f;
  75. std::time_t _anim_t_start;
  76. std::time_t _anim_t_end;
  77. GLfloat onesec = 0.0f;
  78. const float _anim_update_interval = 0.01f; // ms
  79.                                            //const GLfloat _anim_update_delta = 0.05;
  80. const GLfloat _anim_update_delta = 0.1;
  81.  
  82. GLfloat deltaTime = 0.0f;
  83. GLfloat lastFrame = 0.0f;
  84.  
  85. clock_t deltaTimeFPS = 0;
  86. unsigned int frames = 0;
  87. double  frameRate = 30;
  88. double  averageFrameTimeMilliseconds = 33.333;
  89.  
  90. // Options
  91. GLuint draw_mode = 1;
  92.  
  93. //Camera Arcball
  94. GLfloat offsetX = 0.0f;
  95. GLfloat offsetY = 0.0f;
  96. GLboolean leftMousebuttonPressed = false;
  97.  
  98. //Order Independent Transparency
  99. GLuint _buffer_atomic_counter;
  100. GLuint _buffer_fragments;
  101. GLuint _buffer_linkedlist_startIDs;
  102.  
  103. const unsigned int max_fragments_per_pixel = 20;
  104. const GLuint _zero = 0;
  105.  
  106. //Debug
  107. GLfloat debug = 1.0;
  108.  
  109. GLuint VAO, VBO;
  110.  
  111. GLfloat lerp(GLfloat a, GLfloat b, GLfloat f)
  112. {
  113.     return a + f * (b - a);
  114. }
  115.  
  116. // The MAIN function, from here we start our application and run our Game loop
  117. int main()
  118. {
  119.     // Init GLFW
  120.     glfwInit();
  121.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  122.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  123.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  124.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  125.  
  126.     GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", nullptr, nullptr); // Windowed
  127.     glfwMakeContextCurrent(window);
  128.  
  129.     // Set the required callback functions
  130.     glfwSetKeyCallback(window, key_callback);
  131.     glfwSetCursorPosCallback(window, mouse_callback);
  132.     glfwSetMouseButtonCallback(window, mouse_button_callback);
  133.     //glfwSetScrollCallback(window, scroll_callback);
  134.  
  135.     // Options
  136.     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  137.  
  138.     // Initialize GLEW to setup the OpenGL Function pointers
  139.     glewExperimental = GL_TRUE;
  140.     glewInit();
  141.     glGetError();
  142.  
  143.     // Define the viewport dimensions
  144.     glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
  145.  
  146.     // Setup some OpenGL options
  147.     glEnable(GL_DEPTH_TEST);
  148.  
  149.     // Setup and compile our shaders
  150.     Shader oitFillShader("OITFillLinkedList.vs", "OITFillLinkedList.frag", "OITFillLinkedList.geom");
  151.  
  152.     // Set samplers
  153.     shaderLightingPassPathlines.Use();
  154.     glUniform1i(glGetUniformLocation(shaderLightingPassPathlines.Program, "ndmap"), 0);
  155.     glUniform1i(glGetUniformLocation(shaderLightingPassPathlines.Program, "colormap"), 1);
  156.     glUniform1i(glGetUniformLocation(shaderLightingPassPathlines.Program, "zoommap"), 2);
  157.     glUniform1i(glGetUniformLocation(shaderLightingPassPathlines.Program, "tangentmap"), 3);
  158.  
  159.     // Objects
  160.     Model aorta("C:/Users/Ba/UNI_OVGU/wip-hannes-lineao/data/aorta_mesh.vtp", false);
  161.     ModelPathlines pathlines("C:/Users/Ba/UNI_OVGU/wip-hannes-lineao/data/aorta_pathlines.vtp");
  162.  
  163.     // Lights
  164.     glm::vec3 lightPos = glm::vec3(2.0, 4.0, -2.0);
  165.     glm::vec3 lightColor = glm::vec3(0.2, 0.2, 0.7);
  166.    
  167.     GLuint rboDepth;
  168.     glGenRenderbuffers(1, &rboDepth);
  169.     glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
  170.     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, SCR_WIDTH, SCR_HEIGHT);
  171.     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);
  172.     // - Finally check if framebuffer is complete
  173.     if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  174.         std::cout << "GBuffer Framebuffer not complete!" << std::endl;
  175.     glBindFramebuffer(GL_FRAMEBUFFER,
  176.  
  177.     // Configure VAO/VBO for texture quads
  178.     glGenVertexArrays(1, &VAO);
  179.     glGenBuffers(1, &VBO);
  180.     glBindVertexArray(VAO);
  181.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  182.     glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
  183.     glEnableVertexAttribArray(0);
  184.     glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
  185.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  186.     glBindVertexArray(0);
  187.  
  188.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  189.  
  190.     _anim_t_start = std::clock();
  191.     _anim_t_end = _anim_t_start;
  192.     glm::vec3 velocityAndTimeRange = pathlines.getMinMaxVelocity();
  193.     _timeRange = velocityAndTimeRange[2];
  194.  
  195.     // Game loop
  196.     while (!glfwWindowShouldClose(window))
  197.     {
  198.         clock_t beginFrame = clock();
  199.  
  200.         glDisable(GL_BLEND);
  201.  
  202.         // Set frame time
  203.         GLfloat currentFrame = glfwGetTime();
  204.         deltaTime = currentFrame - lastFrame;
  205.         lastFrame = currentFrame;
  206.  
  207.         // Check and call events
  208.         glfwPollEvents();
  209.         Do_Movement();
  210.  
  211.         // pathlines model scale and translate
  212.         glm::vec3 meshCenter = aorta.getMeshcenter();
  213.         GLfloat meshCenterX = meshCenter.x;
  214.         GLfloat meshCenterY = meshCenter.y;
  215.         GLfloat meshCenterZ = meshCenter.z;
  216.  
  217.         glm::mat4 model = glm::mat4();
  218.         model = glm::rotate(model, glm::radians(offsetX), glm::vec3(0.0f, 1.0f, 0.0f));
  219.         model = glm::rotate(model, glm::radians(offsetY), glm::vec3(1.0f, 0.0f, 0.0f));
  220.         model = glm::translate(model, glm::vec3(-meshCenterX * 0.02f, -meshCenterY * 0.02f + 0.5, -meshCenterZ * 0.02f));
  221.         model = glm::scale(model, glm::vec3(0.02f));
  222.         glm::mat4 projection = glm::perspective(camera.Zoom, (GLfloat)SCR_WIDTH / (GLfloat)SCR_HEIGHT, 0.1f, 50.0f);
  223.         glm::mat4 view = camera.GetViewMatrix();
  224.  
  225.         glDepthFunc(GL_LESS);
  226.  
  227.         debug = 2.0;
  228.         glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  229.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  230.  
  231.         oitFillShader.Use();
  232.  
  233.         glUniform1f(glGetUniformLocation(oitFillShader.Program, "debug"), debug);
  234.         glUniformMatrix4fv(glGetUniformLocation(oitFillShader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
  235.         glUniformMatrix4fv(glGetUniformLocation(oitFillShader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
  236.         glUniformMatrix4fv(glGetUniformLocation(oitFillShader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
  237.  
  238.         pathlines.Draw(oitFillShader);
  239.        
  240.         // Swap the buffers
  241.         glfwSwapBuffers(window);
  242.     }
  243.  
  244.     glfwTerminate();
  245.     return 0;
  246. }
  247.  
  248.  
  249. // RenderQuad() Renders a 1x1 quad in NDC, best used for framebuffer color targets
  250. // and post-processing effects.
  251. GLuint quadVAO = 0;
  252. GLuint quadVBO;
  253. void RenderQuad()
  254. {
  255.     if (quadVAO == 0)
  256.     {
  257.         GLfloat quadVertices[] = {
  258.             // Positions        // Texture Coords
  259.             -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  260.             -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
  261.             1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
  262.             1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
  263.         };
  264.         // Setup plane VAO
  265.         glGenVertexArrays(1, &quadVAO);
  266.         glGenBuffers(1, &quadVBO);
  267.         glBindVertexArray(quadVAO);
  268.         glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
  269.         glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
  270.         glEnableVertexAttribArray(0);
  271.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
  272.         glEnableVertexAttribArray(1);
  273.         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
  274.     }
  275.     glBindVertexArray(quadVAO);
  276.     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  277.     glBindVertexArray(0);
  278. }
  279.  
  280. bool keys[1024];
  281. bool keysPressed[1024];
  282. // Moves/alters the camera positions based on user input
  283. void Do_Movement()
  284. {
  285.     // Camera controls
  286.     if (keys[GLFW_KEY_W])
  287.         camera.ProcessKeyboard(FORWARD, deltaTime);
  288.     if (keys[GLFW_KEY_S])
  289.         camera.ProcessKeyboard(BACKWARD, deltaTime);
  290.     if (keys[GLFW_KEY_A])
  291.         camera.ProcessKeyboard(LEFT, deltaTime);
  292.     if (keys[GLFW_KEY_D])
  293.         camera.ProcessKeyboard(RIGHT, deltaTime);
  294.  
  295.     if (keys[GLFW_KEY_1])
  296.         draw_mode = 1;
  297.     if (keys[GLFW_KEY_2])
  298.         draw_mode = 2;
  299.     if (keys[GLFW_KEY_3])
  300.         draw_mode = 3;
  301.     if (keys[GLFW_KEY_4])
  302.         draw_mode = 4;
  303.     if (keys[GLFW_KEY_5])
  304.         draw_mode = 5;
  305. }
  306.  
  307. GLfloat lastX = 400, lastY = 300;
  308. GLboolean rotateB = false;
  309. GLfloat theta = 0.0f, phi = 0.0f;
  310. bool firstMouse = true;
  311. // Is called whenever a key is pressed/released via GLFW
  312. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  313. {
  314.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  315.         glfwSetWindowShouldClose(window, GL_TRUE);
  316.  
  317.     if (key >= 0 && key <= 1024)
  318.     {
  319.         if (action == GLFW_PRESS)
  320.             keys[key] = true;
  321.         else if (action == GLFW_RELEASE)
  322.         {
  323.             keys[key] = false;
  324.             keysPressed[key] = false;
  325.         }
  326.     }
  327. }
  328.  
  329. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
  330. {
  331.     rotateB = false;
  332.     int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
  333.     //if (state == GLFW_PRESS)
  334.     if (firstMouse)//&& state == GLFW_PRESS)
  335.     {
  336.         lastX = xpos;
  337.         lastY = ypos;
  338.         firstMouse = false;
  339.         rotateB = true;
  340.  
  341.     }
  342.  
  343.     GLfloat xoffset = xpos - lastX;
  344.     GLfloat yoffset = lastY - ypos;
  345.     if (leftMousebuttonPressed) {
  346.         offsetX += xoffset;
  347.         offsetY -= yoffset;
  348.     }
  349.     lastX = xpos;
  350.     lastY = ypos;
  351.  
  352.     camera.ProcessMouseMovement(0.0f, 0.0f);
  353.     //new implementation
  354.     //camera.ProcessMouseMovementNew(theta, phi);
  355. }
  356.  
  357. void mouse_button_callback(GLFWwindow * window, int button, int action, int mods)
  358. {
  359.     if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  360.         camera.LeftMouseButtonPressed = true;
  361.         leftMousebuttonPressed = true;
  362.     }
  363.     if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
  364.         camera.LeftMouseButtonPressed = false;
  365.         leftMousebuttonPressed = false;
  366.     }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement