Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NOMINMAX
- #define RADPERDEG 0.0174533
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <ctime>
- #include <cmath>
- #include <vector>
- #include <sstream>
- #include <algorithm>
- // GLEW
- //#define GLEW_STATIC
- #include <glew.h>
- // GLFW
- #include <glfw3.h>
- // GLUT
- #include <glut.h>
- //VTK
- #include <vtkSmartPointer.h>
- #include <vtkPolyData.h>
- #include <vtkXMLPolyDataReader.h>
- #include <vtkCell.h>
- #include <vtkCellData.h>
- #include <vtkPoints.h>
- #include <vtkPointData.h>
- #include <vtkIdList.h>
- #include <vtkDoubleArray.h>
- // GL includes
- #include "Shader.h"
- #include "camera.h"
- #include "Model.h"
- #include "ModelPathlines.h"
- // GLM Mathemtics
- #include <glm.hpp>
- #include <gtc/matrix_transform.hpp>
- #include <gtc/type_ptr.hpp>
- // Other Libs
- #include <SOIL.h>
- #include <ft2build.h>
- #include FT_FREETYPE_H
- #include <random> // necessary for generation of random floats (for sample kernel and noise texture)
- // Properties
- const GLuint SCR_WIDTH = 1800, SCR_HEIGHT = 1000;
- // Function prototypes
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
- void mouse_callback(GLFWwindow* window, double xpos, double ypos);
- void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
- void Do_Movement();
- void RenderQuad();
- void RenderText(Shader &shader, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color);
- // Camera
- Camera camera(glm::vec3(0.0f, 0.0f, 2.5f));
- // Delta
- bool _useAnimation = true;
- GLfloat _timeRange = 0.0f;
- GLfloat _lineLength = 0.5f;
- GLfloat _minVelocity = 0.0f;
- GLfloat _maxVelocity = 0.0f;
- GLfloat _currentTime = 0.0f;
- std::time_t _anim_t_start;
- std::time_t _anim_t_end;
- GLfloat onesec = 0.0f;
- const float _anim_update_interval = 0.01f; // ms
- //const GLfloat _anim_update_delta = 0.05;
- const GLfloat _anim_update_delta = 0.1;
- GLfloat deltaTime = 0.0f;
- GLfloat lastFrame = 0.0f;
- clock_t deltaTimeFPS = 0;
- unsigned int frames = 0;
- double frameRate = 30;
- double averageFrameTimeMilliseconds = 33.333;
- // Options
- GLuint draw_mode = 1;
- //Camera Arcball
- GLfloat offsetX = 0.0f;
- GLfloat offsetY = 0.0f;
- GLboolean leftMousebuttonPressed = false;
- //Order Independent Transparency
- GLuint _buffer_atomic_counter;
- GLuint _buffer_fragments;
- GLuint _buffer_linkedlist_startIDs;
- const unsigned int max_fragments_per_pixel = 20;
- const GLuint _zero = 0;
- //Debug
- GLfloat debug = 1.0;
- GLuint VAO, VBO;
- GLfloat lerp(GLfloat a, GLfloat b, GLfloat f)
- {
- return a + f * (b - a);
- }
- // The MAIN function, from here we start our application and run our Game loop
- int main()
- {
- // Init GLFW
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", nullptr, nullptr); // Windowed
- glfwMakeContextCurrent(window);
- // Set the required callback functions
- glfwSetKeyCallback(window, key_callback);
- glfwSetCursorPosCallback(window, mouse_callback);
- glfwSetMouseButtonCallback(window, mouse_button_callback);
- //glfwSetScrollCallback(window, scroll_callback);
- // Options
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- // Initialize GLEW to setup the OpenGL Function pointers
- glewExperimental = GL_TRUE;
- glewInit();
- glGetError();
- // Define the viewport dimensions
- glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
- // Setup some OpenGL options
- glEnable(GL_DEPTH_TEST);
- // Setup and compile our shaders
- Shader oitFillShader("OITFillLinkedList.vs", "OITFillLinkedList.frag", "OITFillLinkedList.geom");
- // Set samplers
- shaderLightingPassPathlines.Use();
- glUniform1i(glGetUniformLocation(shaderLightingPassPathlines.Program, "ndmap"), 0);
- glUniform1i(glGetUniformLocation(shaderLightingPassPathlines.Program, "colormap"), 1);
- glUniform1i(glGetUniformLocation(shaderLightingPassPathlines.Program, "zoommap"), 2);
- glUniform1i(glGetUniformLocation(shaderLightingPassPathlines.Program, "tangentmap"), 3);
- // Objects
- Model aorta("C:/Users/Ba/UNI_OVGU/wip-hannes-lineao/data/aorta_mesh.vtp", false);
- ModelPathlines pathlines("C:/Users/Ba/UNI_OVGU/wip-hannes-lineao/data/aorta_pathlines.vtp");
- // Lights
- glm::vec3 lightPos = glm::vec3(2.0, 4.0, -2.0);
- glm::vec3 lightColor = glm::vec3(0.2, 0.2, 0.7);
- GLuint rboDepth;
- glGenRenderbuffers(1, &rboDepth);
- glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, SCR_WIDTH, SCR_HEIGHT);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);
- // - Finally check if framebuffer is complete
- if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
- std::cout << "GBuffer Framebuffer not complete!" << std::endl;
- glBindFramebuffer(GL_FRAMEBUFFER,
- // Configure VAO/VBO for texture quads
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- _anim_t_start = std::clock();
- _anim_t_end = _anim_t_start;
- glm::vec3 velocityAndTimeRange = pathlines.getMinMaxVelocity();
- _timeRange = velocityAndTimeRange[2];
- // Game loop
- while (!glfwWindowShouldClose(window))
- {
- clock_t beginFrame = clock();
- glDisable(GL_BLEND);
- // Set frame time
- GLfloat currentFrame = glfwGetTime();
- deltaTime = currentFrame - lastFrame;
- lastFrame = currentFrame;
- // Check and call events
- glfwPollEvents();
- Do_Movement();
- // pathlines model scale and translate
- glm::vec3 meshCenter = aorta.getMeshcenter();
- GLfloat meshCenterX = meshCenter.x;
- GLfloat meshCenterY = meshCenter.y;
- GLfloat meshCenterZ = meshCenter.z;
- glm::mat4 model = glm::mat4();
- model = glm::rotate(model, glm::radians(offsetX), glm::vec3(0.0f, 1.0f, 0.0f));
- model = glm::rotate(model, glm::radians(offsetY), glm::vec3(1.0f, 0.0f, 0.0f));
- model = glm::translate(model, glm::vec3(-meshCenterX * 0.02f, -meshCenterY * 0.02f + 0.5, -meshCenterZ * 0.02f));
- model = glm::scale(model, glm::vec3(0.02f));
- glm::mat4 projection = glm::perspective(camera.Zoom, (GLfloat)SCR_WIDTH / (GLfloat)SCR_HEIGHT, 0.1f, 50.0f);
- glm::mat4 view = camera.GetViewMatrix();
- glDepthFunc(GL_LESS);
- debug = 2.0;
- glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- oitFillShader.Use();
- glUniform1f(glGetUniformLocation(oitFillShader.Program, "debug"), debug);
- glUniformMatrix4fv(glGetUniformLocation(oitFillShader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
- glUniformMatrix4fv(glGetUniformLocation(oitFillShader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
- glUniformMatrix4fv(glGetUniformLocation(oitFillShader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
- pathlines.Draw(oitFillShader);
- // Swap the buffers
- glfwSwapBuffers(window);
- }
- glfwTerminate();
- return 0;
- }
- // RenderQuad() Renders a 1x1 quad in NDC, best used for framebuffer color targets
- // and post-processing effects.
- GLuint quadVAO = 0;
- GLuint quadVBO;
- void RenderQuad()
- {
- if (quadVAO == 0)
- {
- GLfloat quadVertices[] = {
- // Positions // Texture Coords
- -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
- -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
- 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
- 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
- };
- // Setup plane VAO
- glGenVertexArrays(1, &quadVAO);
- glGenBuffers(1, &quadVBO);
- glBindVertexArray(quadVAO);
- glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
- }
- glBindVertexArray(quadVAO);
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- glBindVertexArray(0);
- }
- bool keys[1024];
- bool keysPressed[1024];
- // Moves/alters the camera positions based on user input
- void Do_Movement()
- {
- // Camera controls
- if (keys[GLFW_KEY_W])
- camera.ProcessKeyboard(FORWARD, deltaTime);
- if (keys[GLFW_KEY_S])
- camera.ProcessKeyboard(BACKWARD, deltaTime);
- if (keys[GLFW_KEY_A])
- camera.ProcessKeyboard(LEFT, deltaTime);
- if (keys[GLFW_KEY_D])
- camera.ProcessKeyboard(RIGHT, deltaTime);
- if (keys[GLFW_KEY_1])
- draw_mode = 1;
- if (keys[GLFW_KEY_2])
- draw_mode = 2;
- if (keys[GLFW_KEY_3])
- draw_mode = 3;
- if (keys[GLFW_KEY_4])
- draw_mode = 4;
- if (keys[GLFW_KEY_5])
- draw_mode = 5;
- }
- GLfloat lastX = 400, lastY = 300;
- GLboolean rotateB = false;
- GLfloat theta = 0.0f, phi = 0.0f;
- bool firstMouse = true;
- // Is called whenever a key is pressed/released via GLFW
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- if (key >= 0 && key <= 1024)
- {
- if (action == GLFW_PRESS)
- keys[key] = true;
- else if (action == GLFW_RELEASE)
- {
- keys[key] = false;
- keysPressed[key] = false;
- }
- }
- }
- void mouse_callback(GLFWwindow* window, double xpos, double ypos)
- {
- rotateB = false;
- int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
- //if (state == GLFW_PRESS)
- if (firstMouse)//&& state == GLFW_PRESS)
- {
- lastX = xpos;
- lastY = ypos;
- firstMouse = false;
- rotateB = true;
- }
- GLfloat xoffset = xpos - lastX;
- GLfloat yoffset = lastY - ypos;
- if (leftMousebuttonPressed) {
- offsetX += xoffset;
- offsetY -= yoffset;
- }
- lastX = xpos;
- lastY = ypos;
- camera.ProcessMouseMovement(0.0f, 0.0f);
- //new implementation
- //camera.ProcessMouseMovementNew(theta, phi);
- }
- void mouse_button_callback(GLFWwindow * window, int button, int action, int mods)
- {
- if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
- camera.LeftMouseButtonPressed = true;
- leftMousebuttonPressed = true;
- }
- if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
- camera.LeftMouseButtonPressed = false;
- leftMousebuttonPressed = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement