Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <stdio.h>
  6.  
  7. #include <GL/glew.h>
  8. #include <GL/freeglut.h>
  9.  
  10. #include <glm/mat4x4.hpp>
  11. #include <glm/gtx/transform.hpp>
  12. #include <glm/gtc/type_ptr.hpp>
  13. #include <glm/gtc/constants.hpp>
  14.  
  15. #define PI glm::pi<float>()
  16.  
  17. GLuint shader_programme, vao;
  18. glm::mat4 projectionMatrix, viewMatrix;
  19. std::vector<float> points;
  20.  
  21.  
  22. std::string textFileRead(char *fn)
  23. {
  24. std::ifstream ifile(fn);
  25. std::string filetext;
  26. while (ifile.good()) {
  27. std::string line;
  28. std::getline(ifile, line);
  29. filetext.append(line + "\n");
  30. }
  31. return filetext;
  32. }
  33.  
  34. void display()
  35. {
  36. glClear(GL_COLOR_BUFFER_BIT);
  37. glUseProgram(shader_programme);
  38.  
  39. glm::mat4 modelMatrix;
  40.  
  41. glBindVertexArray(vao);
  42. GLuint matrixID = glGetUniformLocation(shader_programme, "modelViewProjectionMatrix");
  43.  
  44. glUniformMatrix4fv(matrixID, 1, GL_FALSE, glm::value_ptr(projectionMatrix * viewMatrix * modelMatrix));
  45. glDrawArrays(GL_LINE_STRIP, 0, points.size() / 3);
  46.  
  47. glFlush();
  48. }
  49.  
  50. void createPointsVector()
  51. {
  52. /*
  53. //primul punct
  54. points.push_back(0.0f);
  55. points.push_back(0.0f);
  56. points.push_back(0.0f);
  57.  
  58. //al doilea punct
  59. points.push_back(1.0f);
  60. points.push_back(0.0f);
  61. points.push_back(0.0f);
  62.  
  63. //al treilea punct
  64. points.push_back(2.0f);
  65. points.push_back(1.0f);
  66. points.push_back(0.0f);
  67. */
  68. //conditiile geometrice ale curbei
  69. float Geometry[4][3] = {
  70. { 10,10,0 }, // P0
  71. { -10,5,-2 }, // P1
  72. { 5,-5,0 }, // Tangenta in P0
  73. { 5,10,0 } // Tangenta in P1
  74. };
  75. unsigned int LOD = 20;
  76. for (int i = 0;i<LOD;i++) {
  77. float u = (float)i / (LOD - 1);
  78. double u2 = u*u;
  79. double u3 = u2*u;
  80. // calcularea functiilor de amestec
  81. float f0 = 2*u3-3*u2+1;
  82. float f1 = -2*u3+3*u2;
  83. float f2 = u3-2*u2+u;
  84. float f3 = u3-u2;
  85. // calcularea coordonatelor x,y,z ale punctului curent de pe curba
  86. float x = f0 * Geometry[0][0] + f1 * Geometry[1][0] + f2 * Geometry[2][0] + f3 * Geometry[3][0];
  87. float y = f0 * Geometry[0][1] + f1 * Geometry[1][1] + f2 * Geometry[2][1]+ f3 * Geometry[3][1];
  88. float z = f0 * Geometry[0][2] + f1 * Geometry[1][2] + f2 * Geometry[2][2]+ f3 * Geometry[3][2];
  89. // specificarea punctului pe pozitia i in VBO
  90. points.push_back(x);
  91. points.push_back(y);
  92. points.push_back(z);
  93. }
  94. }
  95.  
  96. void init()
  97. {
  98. // get version info
  99. const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
  100. const GLubyte* version = glGetString(GL_VERSION); // version as a string
  101. printf("Renderer: %s\n", renderer);
  102. printf("OpenGL version supported %s\n", version);
  103.  
  104. glClearColor(1, 1, 1, 0);
  105.  
  106. glewInit();
  107.  
  108. GLuint vbo = 1;
  109. glGenBuffers(1, &vbo);
  110. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  111. glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(float), &points[0], GL_STATIC_DRAW);
  112.  
  113. vao = 0;
  114. glGenVertexArrays(1, &vao);
  115. glBindVertexArray(vao);
  116. glEnableVertexAttribArray(0);
  117. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  118. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  119.  
  120. std::string vstext = textFileRead("vertex.vert");
  121. std::string fstext = textFileRead("fragment.frag");
  122. const char* vertex_shader = vstext.c_str();
  123. const char* fragment_shader = fstext.c_str();
  124.  
  125. GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  126. glShaderSource(vs, 1, &vertex_shader, NULL);
  127. glCompileShader(vs);
  128. GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  129. glShaderSource(fs, 1, &fragment_shader, NULL);
  130. glCompileShader(fs);
  131.  
  132. shader_programme = glCreateProgram();
  133. glAttachShader(shader_programme, fs);
  134. glAttachShader(shader_programme, vs);
  135. glLinkProgram(shader_programme);
  136. }
  137.  
  138.  
  139. void reshape(int w, int h)
  140. {
  141. glViewport(0, 0, w, h);
  142. projectionMatrix = glm::perspective(PI / 3, (float)w / h, 0.1f, 100.0f);
  143. /*
  144. viewMatrix este matricea transformarii de observare. Parametrii functiei
  145. lookAt sunt trei vectori ce reprezinta, in ordine:
  146. - pozitia observatorului
  147. - punctul catre care priveste observatorul
  148. - directia dupa care este orientat observatorul
  149. */
  150. viewMatrix = glm::lookAt(glm::vec3(0, 0, 20), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
  151. }
  152.  
  153. int main(int argc, char** argv)
  154. {
  155.  
  156. glutInit(&argc, argv);
  157. glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  158. glutInitWindowPosition(200, 200);
  159. glutInitWindowSize(700, 700);
  160. glutCreateWindow("SPG");
  161.  
  162. createPointsVector();
  163. init();
  164.  
  165. glutDisplayFunc(display);
  166. glutReshapeFunc(reshape);
  167.  
  168. glutMainLoop();
  169.  
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement