SHARE
TWEET

Untitled

a guest Jan 24th, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #include <GL/gl3w.h>
  5. #include <GLFW/glfw3.h>
  6.  
  7. #include <glm/vec3.hpp>
  8. #include <glm/mat4x4.hpp>
  9. #include <glm/gtc/matrix_transform.hpp>
  10. #include <glm/gtc/type_ptr.hpp>
  11. #include <iostream>
  12.  
  13.  
  14. #include <cmath>
  15.  
  16.  
  17. GLuint rendering_program;
  18. GLuint vertex_array_object;
  19. GLuint buffer;
  20. GLuint index_buffer;
  21. static double posx;
  22. static double posy;
  23. static double scroll_x;
  24. static double scroll_y;
  25. static bool gorna_cwiartka = true;
  26. static float rotate = 1.0;
  27. static double R = 10;
  28.  
  29. GLint mv_location;
  30. GLint proj_location;
  31. GLint v_location;
  32.  
  33. float aspect;
  34. glm::mat4 model_matrix;
  35. glm::mat4 proj_matrix;
  36. glm::mat4 view_matrix;
  37.  
  38. static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
  39. {
  40.     posx = xpos;
  41.     posy = ypos;
  42.     std::cout<<xpos<<", "<<ypos<<"\n";
  43. }
  44.  
  45. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
  46. {
  47.     std::cout<<"Mouse clicked\n";
  48. }
  49.  
  50. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
  51. {
  52.     std::cout<<"Scroll:"<<xoffset<<", "<<yoffset<<"\n";
  53.     scroll_x = xoffset;
  54.     scroll_y = yoffset;
  55.     R += yoffset;
  56. }
  57.  
  58. void error_callback(int error, const char* description) {
  59.     fprintf(stderr, "Error: %s\n", description);
  60. }
  61.  
  62. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  63.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  64.         glfwSetWindowShouldClose(window, GLFW_TRUE);
  65. }
  66.  
  67. static void resize_callback(GLFWwindow* window, int width, int height) {
  68.     glViewport(0, 0, width, height);
  69.  
  70.     aspect = (float) width / (float) height;
  71.     proj_matrix = glm::perspective(glm::radians(50.0f), aspect, 0.1f, 1000.0f);
  72. }
  73.  
  74. GLuint compile_shaders(void) {
  75.     GLuint vertex_shader;
  76.     GLuint fragment_shader;
  77.     GLuint program;
  78.  
  79.     static const GLchar * vertex_shader_source[] = {
  80.                 "#version 330 core \n"
  81.  
  82.         "in vec4 position;"
  83.         "in vec3 col;"
  84.         "out vec3 incolor;"
  85.         "uniform mat4 model_matrix;"
  86.         "uniform mat4 proj_matrix;"
  87.         "uniform mat4 view_matrix;"
  88.  
  89.     "mat4 wynik = proj_matrix *  view_matrix *  model_matrix;"
  90.  
  91.         "void main(void) {"
  92.         "gl_Position = wynik * position;"
  93.         "}"
  94.     };
  95.  
  96.     static const GLchar * fragment_shader_source[] = {
  97.         "#version 330 core \n"
  98.  
  99.         "out vec4 color;"
  100.         "in vec3 incolor;"
  101.  
  102.         "void main(void) {"
  103.         "    color = vec4(0,0,0,0);"
  104.         "}"
  105.  
  106.     };
  107.  
  108.     vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  109.     glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
  110.     glCompileShader(vertex_shader);
  111.  
  112.     fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  113.     glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
  114.     glCompileShader(fragment_shader);
  115.  
  116.     program = glCreateProgram();
  117.     glAttachShader(program, vertex_shader);
  118.     glAttachShader(program, fragment_shader);
  119.     glLinkProgram(program);
  120.  
  121.     glDeleteShader(vertex_shader);
  122.     glDeleteShader(fragment_shader);
  123.  
  124.     return program;
  125. }
  126.  
  127. void startup() {
  128.     rendering_program = compile_shaders();
  129.     glCreateVertexArrays(1, &vertex_array_object);
  130.     glBindVertexArray(vertex_array_object);
  131.  
  132.     static const GLfloat vertex_positions[] = {
  133.         0,0,0,
  134.         0,0,1,
  135.         0,1,0,
  136.         0,1,1,
  137.         1,0,0,
  138.         1,1,0,
  139.         1,0,1,
  140.         1,1,1
  141.     };
  142.  
  143.     // static const GLfloat vertex_positions[] = {
  144.     //     -0.25f,  0.25f, -0.25f, 1.0f, 0.0f, 0.0f,
  145.     //     -0.25f, -0.25f, -0.25f, 0.0f, 1.0f, 0.0f,
  146.     //      0.25f, -0.25f, -0.25f, 0.0f, 0.0f, 1.0f,
  147.  
  148.     //      0.25f, -0.25f, -0.25f,0.0f, 1.0f, 0.0f,
  149.     //      0.25f,  0.25f, -0.25f,0.0f, 1.0f, 0.0f,
  150.     //     -0.25f,  0.25f, -0.25f, 0.0f, 0.0f, 1.0f,
  151.  
  152.     //      0.25f, -0.25f, -0.25f,1.0f, 0.0f, 0.0f,
  153.     //      0.25f, -0.25f,  0.25f,0.0f, 1.0f, 0.0f,
  154.     //      0.25f,  0.25f, -0.25f, 0.0f, 0.0f, 1.0f,
  155.  
  156.     //      0.25f, -0.25f,  0.25f,1.0f, 0.0f, 0.0f,
  157.     //      0.25f,  0.25f,  0.25f,0.0f, 1.0f, 0.0f,
  158.     //      0.25f,  0.25f, -0.25f, 0.0f, 0.0f, 1.0f,
  159.  
  160.     //      0.25f, -0.25f,  0.25f,1.0f, 0.0f, 0.0f,
  161.     //     -0.25f, -0.25f,  0.25f,0.0f, 1.0f, 0.0f,
  162.     //      0.25f,  0.25f,  0.25f, 0.0f, 0.0f, 1.0f,
  163.  
  164.     //     -0.25f, -0.25f,  0.25f,1.0f, 0.0f, 0.0f,
  165.     //     -0.25f,  0.25f,  0.25f,0.0f, 1.0f, 0.0f,
  166.     //      0.25f,  0.25f,  0.25f, 0.0f, 0.0f, 1.0f,
  167.  
  168.     //     -0.25f, -0.25f,  0.25f,1.0f, 0.0f, 0.0f,
  169.     //     -0.25f, -0.25f, -0.25f,0.0f, 1.0f, 0.0f,
  170.     //     -0.25f,  0.25f,  0.25f, 0.0f, 0.0f, 1.0f,
  171.  
  172.     //     -0.25f, -0.25f, -0.25f,1.0f, 0.0f, 0.0f,
  173.     //     -0.25f,  0.25f, -0.25f,0.0f, 1.0f, 0.0f,
  174.     //     -0.25f,  0.25f,  0.25f, 0.0f, 0.0f, 1.0f,
  175.  
  176.     //     -0.25f, -0.25f,  0.25f,1.0f, 0.0f, 0.0f,
  177.     //      0.25f, -0.25f,  0.25f,0.0f, 1.0f, 0.0f,
  178.     //      0.25f, -0.25f, -0.25f, 0.0f, 0.0f, 1.0f,
  179.  
  180.     //      0.25f, -0.25f, -0.25f,1.0f, 0.0f, 0.0f,
  181.     //     -0.25f, -0.25f, -0.25f,0.0f, 1.0f, 0.0f,
  182.     //     -0.25f, -0.25f,  0.25f, 0.0f, 0.0f, 1.0f,
  183.  
  184.     //     -0.25f,  0.25f, -0.25f,1.0f, 0.0f, 0.0f,
  185.     //      0.25f,  0.25f, -0.25f,0.0f, 1.0f, 0.0f,
  186.     //      0.25f,  0.25f,  0.25f, 0.0f, 0.0f, 1.0f,
  187.  
  188.     //      0.25f,  0.25f,  0.25f,1.0f, 0.0f, 0.0f,
  189.     //     -0.25f,  0.25f,  0.25f,0.0f, 1.0f, 0.0f,
  190.     //     -0.25f,  0.25f, -0.25f, 0.0f, 0.0f, 1.0f
  191.     // };
  192.  
  193.     glGenBuffers(1, &buffer);
  194.     glBindBuffer(GL_ARRAY_BUFFER, buffer);
  195.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions), vertex_positions, GL_STATIC_DRAW);
  196.  
  197.     static const GLint vertex_indexes[] = {
  198. 0,1,2,
  199. 1,2,3,
  200.  
  201. 0,4,2,
  202. 2,4,5,
  203.  
  204. 4,5,6,
  205. 5,6,7,
  206.  
  207. 2,3,5,
  208. 3,5,7,
  209.  
  210. 0,2,4,
  211. 2,5,4,
  212.  
  213. 0,1,4,
  214. 1,4,6,
  215.  
  216.  
  217. };
  218.  
  219.     glGenBuffers(1, &index_buffer);
  220.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
  221.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_indexes), vertex_indexes, GL_STATIC_DRAW);
  222.  
  223.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  224. glEnableVertexAttribArray(0);
  225. // color attribute
  226. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float)));
  227. glEnableVertexAttribArray(1);
  228.     //glEnable(GL_CULL_FACE);
  229.     glFrontFace(GL_CW);
  230. }
  231.  
  232. void shutdown() {
  233.     glDeleteVertexArrays(1, &vertex_array_object);
  234.     glDeleteProgram(rendering_program);
  235.     glDeleteBuffers(1, &buffer);
  236. }
  237.  
  238. void render(double currentTime) {
  239.     float f = (float) currentTime * (float) M_PI * 0.1f;
  240.     glm::mat4 I = glm::mat4(1.0f);
  241.     model_matrix = (
  242.         glm::translate(I, glm::vec3(-3,-3,-8))
  243.         *
  244.         glm::rotate(I, 0.0f, glm::vec3(1.0f, 0.0f, 0.0f))
  245.     );
  246.  
  247.     const GLfloat color[] = {
  248.         (float) sin(0) * 0.5f + 0.5f,
  249.         (float) cos(0) * 0.5f + 0.5f,
  250.         0.0f,
  251.         1.0f
  252.     };
  253.     glClearBufferfv(GL_COLOR, 0, color);
  254.  
  255.     glUseProgram(rendering_program);
  256.  
  257.     double scaled_x = posx / 500.0;
  258.     double scaled_y = posy / 500.0;
  259.  
  260.     view_matrix = glm::lookAt(
  261.     glm::vec3(R*sin(scaled_x)*cos(scaled_y), R*sin(scaled_x), R*cos(scaled_x)*sin(scaled_y)),
  262.     glm::vec3(0.0f, 0.0f, 0.0f),
  263.     glm::vec3(0.0f, rotate, 0.0f)
  264.     );
  265.    
  266.     mv_location = glGetUniformLocation(rendering_program, "model_matrix");
  267.     proj_location = glGetUniformLocation(rendering_program, "proj_matrix");
  268.     v_location = glGetUniformLocation(rendering_program, "view_matrix");
  269.     glUniformMatrix4fv(mv_location, 1, GL_FALSE, glm::value_ptr(model_matrix));
  270.     glUniformMatrix4fv(proj_location, 1, GL_FALSE, glm::value_ptr(proj_matrix));
  271.     glUniformMatrix4fv(v_location, 1, GL_FALSE, glm::value_ptr(view_matrix));
  272.  
  273.     //glDrawArrays(GL_TRIANGLES, 0, 36);
  274.  
  275.     //glDrawArraysInstanced(GL_TRIANGLES, 0, 7,100);
  276.  
  277.     // for(int i=0; i<5; i++){
  278.     //     mv_matrix = (
  279.     //         glm::translate(mv_matrix, glm::vec3(1,1,1))
  280.     //     );
  281.     //     glUniformMatrix4fv(mv_location, 1, GL_FALSE, glm::value_ptr(mv_matrix));
  282.     //     glDrawArrays(GL_TRIANGLES, 0, 36);
  283.     // }
  284.  
  285.     glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, NULL);
  286.  
  287.  
  288.    
  289. }
  290.  
  291. int main(void) {
  292.     GLFWwindow* window;
  293.  
  294.     glfwSetErrorCallback(error_callback);
  295.    
  296.  
  297.     if(! glfwInit())
  298.         return -1;
  299.  
  300.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  301.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  302.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  303.  
  304. proj_matrix = glm::perspective(glm::radians(50.0f), 800/600.0f, 0.1f, 1000.0f);
  305.     window = glfwCreateWindow(800, 600, "Hello World", NULL, NULL);
  306.     if(! window) {
  307.         glfwTerminate();
  308.         return -1;
  309.     }
  310.  
  311.     glfwSetCursorPosCallback(window, cursor_position_callback);
  312.     glfwSetMouseButtonCallback(window, mouse_button_callback);
  313.     glfwSetScrollCallback(window, scroll_callback);
  314.  
  315.     glfwMakeContextCurrent(window);
  316.     if(gl3wInit()) {
  317.         fprintf(stderr, "failed to initialize OpenGL\n");
  318.         return -1;
  319.     }
  320.     if(! gl3wIsSupported(3, 3)) {
  321.         fprintf(stderr, "OpenGL 3.3 not supported\n");
  322.         return -1;
  323.     }
  324.     printf("OpenGL %s, GLSL %s\n", glGetString(GL_VERSION),
  325.            glGetString(GL_SHADING_LANGUAGE_VERSION));
  326.  
  327.     glfwSetKeyCallback(window, key_callback);
  328.     glfwSetWindowSizeCallback(window, resize_callback);
  329.     glfwSwapInterval(1);
  330.  
  331.     startup();
  332.  
  333.     while(!glfwWindowShouldClose(window)){
  334.         render(glfwGetTime());
  335.  
  336.         glfwSwapBuffers(window);
  337.         glfwPollEvents();
  338.     }
  339.  
  340.     shutdown();
  341.  
  342.     glfwDestroyWindow(window);
  343.     glfwTerminate();
  344.     return 0;
  345. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top