Advertisement
Guest User

Untitled

a guest
Mar 6th, 2021
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.98 KB | None | 0 0
  1. #define GLEW_STATIC
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4.  
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include "functions.h"
  8. #include "window.h"
  9.  
  10.  
  11. // Shader sources
  12. const GLchar* vertexSource = R"glsl(
  13.    #version 150 core
  14.    in vec2 position;
  15.    in float color;
  16.    out float Color;
  17.    void main() {
  18.        Color = color;
  19.        gl_Position = vec4(position, 0.0, 1.0);
  20.    }
  21. )glsl";
  22. const GLchar* fragmentSource = R"glsl(
  23.    #version 150 core
  24.    in float Color;
  25.    out vec4 outColor;
  26.    void main() {
  27.        const vec4 kRedVec4 = vec4(0.13572138, 4.61539260, -42.66032258, 132.13108234);
  28.        const vec4 kGreenVec4 = vec4(0.09140261, 2.19418839, 4.84296658, -14.18503333);
  29.        const vec4 kBlueVec4 = vec4(0.10667330, 12.64194608, -60.58204836, 110.36276771);
  30.        const vec2 kRedVec2 = vec2(-152.94239396, 59.28637943);
  31.        const vec2 kGreenVec2 = vec2(4.27729857, 2.82956604);
  32.        const vec2 kBlueVec2 = vec2(-89.90310912, 27.34824973);
  33.  
  34.        vec4 v4 = vec4( 1.0, Color, Color * Color, Color * Color * Color);
  35.        vec2 v2 = v4.zw * v4.z;
  36.  
  37.        outColor = vec4(
  38.            dot(v4, kRedVec4)   + dot(v2, kRedVec2),
  39.            dot(v4, kGreenVec4) + dot(v2, kGreenVec2),
  40.            dot(v4, kBlueVec4)  + dot(v2, kBlueVec2),
  41.            1.0
  42.        );
  43.    }
  44. )glsl";
  45.  
  46.  
  47. int main(int argc, char* argv[]) {
  48.  
  49.     // Initialise window
  50.     GLFWwindow *window = init_window();
  51.  
  52.     // Create Vertex Array Object
  53.     GLuint vao;
  54.     glGenVertexArrays(1, &vao);
  55.     glBindVertexArray(vao);
  56.  
  57.     // Create and compile the vertex shader
  58.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  59.     glShaderSource(vertexShader, 1, &vertexSource, NULL);
  60.     glCompileShader(vertexShader);
  61.  
  62.     // Create and compile the fragment shader
  63.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  64.     glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  65.     glCompileShader(fragmentShader);
  66.  
  67.     // Link the vertex and fragment shader into a shader program
  68.     GLuint shaderProgram = glCreateProgram();
  69.     glAttachShader(shaderProgram, vertexShader);
  70.     glAttachShader(shaderProgram, fragmentShader);
  71.     glBindFragDataLocation(shaderProgram, 0, "outColor");
  72.     glLinkProgram(shaderProgram);
  73.     glUseProgram(shaderProgram);
  74.  
  75.     // Create a Vertex Buffer Object for positions
  76.     GLuint vbo_pos;
  77.     glGenBuffers(1, &vbo_pos);
  78.  
  79.     GLfloat positions[2*N*N];
  80.     for (int i = 0; i < N; i++) {
  81.         for (int j = 0; j < N; j++) {
  82.             int ind = i*N+j;
  83.             positions[2*ind  ] = (float)(-1.0 + 2.0*i/(N-1));
  84.             positions[2*ind+1] = (float)(-1.0 + 2.0*j/(N-1));
  85.         }
  86.     }
  87.  
  88.     glBindBuffer(GL_ARRAY_BUFFER, vbo_pos);
  89.     glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
  90.  
  91.     // Specify vbo_pos' layout
  92.     GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
  93.     glEnableVertexAttribArray(posAttrib);
  94.     glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
  95.  
  96.     // Create an Element Buffer Object and copy the element data to it
  97.     GLuint ebo;
  98.     glGenBuffers(1, &ebo);
  99.  
  100.     GLuint elements[6*(N-1)*(N-1)];
  101.     for (int i = 0; i < N-1; i++) {
  102.         for (int j = 0; j < N-1; j++) {
  103.             int ind  = i*N+j;
  104.             int ind_ = i*(N-1)+j;
  105.             // Lower triangle
  106.             elements[6*ind_  ] = ind;
  107.             elements[6*ind_+1] = ind+N;
  108.             elements[6*ind_+2] = ind+N+1;
  109.             // Upper triangle
  110.             elements[6*ind_+3] = ind;
  111.             elements[6*ind_+4] = ind+1;
  112.             elements[6*ind_+5] = ind+N+1;
  113.         }
  114.     }
  115.  
  116.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  117.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
  118.  
  119.     // Simulation parameters
  120.     int n = N;
  121.     int t = 0;
  122.     double dt = 1e-6/4;
  123.     double skip = 10;
  124.  
  125.     // Initialise Cahn-Hilliard solver
  126.     init_functions();
  127.     double *c = (double*) malloc(n*n*sizeof(double));
  128.     for (int i = 0; i < n*n; i++) {
  129.         c[i] = 2.0*((double)rand() / (double)RAND_MAX ) - 1.0;
  130.     }
  131.  
  132.     // Create a Vertex Buffer Object for colors
  133.     GLuint vbo_colors;
  134.     glGenBuffers(1, &vbo_colors);
  135.  
  136.     GLfloat colors[N*N];
  137.     for (int i = 0; i < N; i++) {
  138.         for (int j = 0; j < N; j++) {
  139.             int ind = i*N+j;
  140.             colors[ind] = (float) ((c[ind] + 1.0)/2.0);
  141.         }
  142.     }
  143.  
  144.     glBindBuffer(GL_ARRAY_BUFFER, vbo_colors);
  145.     glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STREAM_DRAW);
  146.  
  147.     // Specify vbo_color's layout
  148.     GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
  149.     glEnableVertexAttribArray(colAttrib);
  150.     glVertexAttribPointer(colAttrib, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);
  151.  
  152.  
  153.     clock_t begin, end;
  154.     while(!glfwWindowShouldClose(window)) {
  155.         glfwSwapBuffers(window);
  156.         glfwPollEvents();
  157.  
  158.         // Clear the screen to black
  159.         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  160.         glClear(GL_COLOR_BUFFER_BIT);
  161.  
  162.         // Timestepping
  163.         for (int i = 0; i < skip; i++) {
  164.             RungeKutta4(c, dt);
  165.             t++;
  166.         }
  167.  
  168.         // Update plot
  169.         for (int i = 0; i < N*N; i++) {
  170.             colors[i] = (float) ((c[i] + 1.0)/2.0);
  171.         }
  172.  
  173.         glBindBuffer(GL_ARRAY_BUFFER, vbo_colors);
  174.         glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STREAM_DRAW);
  175.  
  176.  
  177.         // Draw elements
  178.         glDrawElements(GL_TRIANGLES, 6*(N-1)*(N-1), GL_UNSIGNED_INT, 0);
  179.  
  180.         if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  181.             glfwSetWindowShouldClose(window, GL_TRUE);
  182.     }
  183.  
  184.  
  185.     glDeleteProgram(shaderProgram);
  186.     glDeleteShader(fragmentShader);
  187.     glDeleteShader(vertexShader);
  188.  
  189.     glDeleteBuffers(1, &ebo);
  190.     glDeleteBuffers(1, &vbo_pos);
  191.     glDeleteBuffers(1, &vbo_colors);
  192.  
  193.     glDeleteVertexArrays(1, &vao);
  194.  
  195.     glfwDestroyWindow(window);
  196.     glfwTerminate();
  197.     free_functions();
  198.     free(c);
  199.  
  200.     return EXIT_SUCCESS;
  201. }
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement