Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GL/freeglut.h>
  3. #include <stdio.h>
  4. #include <stack>
  5. #include <iostream>
  6. #include <glm/mat4x4.hpp>
  7. #include <glm/gtx/transform.hpp>
  8. #include <glm/gtc/type_ptr.hpp>
  9. #include <glm/gtc/constants.hpp>
  10. #include <vector>
  11. #include <fstream>
  12. #include <string>
  13. #define PI glm::pi<float>()
  14. glm::mat4 projectionMatrix, viewMatrix, modelMatrix;
  15.  
  16. std::vector<GLfloat> vertices;
  17. std::vector<GLuint> indices;
  18. GLuint vao_sphere;
  19. GLuint vboV_sphere; //VBO - varfurile ce formeaza poligoanele sferei
  20. GLuint vboI_sphere; //IBO – indicii varfurilor din VBO care formeaza poligoanele
  21.  
  22. //varfurile triunghiului
  23.  
  24. GLuint shader_programme, vao;
  25.  
  26. void GenSphere(std::vector<GLfloat> &vertices, std::vector<GLuint> &indices, int slices = 40, int stacks = 40) {
  27. int i, j;
  28. int indicator = 0;
  29. for (i = 0; i <= stacks; i++) {
  30. double lat0 = glm::pi<double>() * (-0.5 + (double)(i - 1) / stacks);
  31. double z0 = sin(lat0);
  32. double zr0 = cos(lat0);
  33. double lat1 = glm::pi<double>() * (-0.5 + (double)i / stacks);
  34. double z1 = sin(lat1);
  35. double zr1 = cos(lat1);
  36. for (j = 0; j <= slices; j++) {
  37. double lng = 2 * glm::pi<double>() * (double)(j - 1) / slices;
  38. double x = cos(lng);
  39. double y = sin(lng);
  40. vertices.push_back(x * zr0);
  41. vertices.push_back(y * zr0);
  42. vertices.push_back(z0);
  43. indices.push_back(indicator);
  44. indicator++;
  45. vertices.push_back(x * zr1);
  46. vertices.push_back(y * zr1);
  47. vertices.push_back(z1);
  48. indices.push_back(indicator);
  49. indicator++;
  50. }
  51. indices.push_back(GL_PRIMITIVE_RESTART_FIXED_INDEX);
  52. }
  53. }
  54.  
  55. void display()
  56. {
  57. glBindVertexArray(vao_sphere);
  58. glEnable(GL_PRIMITIVE_RESTART);
  59. glPrimitiveRestartIndex(GL_PRIMITIVE_RESTART_FIXED_INDEX);
  60. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboI_sphere);
  61. glDrawElements(GL_QUAD_STRIP, indices.size(), GL_UNSIGNED_INT, NULL);
  62.  
  63. }
  64. void init()
  65. {
  66. glGenVertexArrays(1, &vao_sphere);
  67. glBindVertexArray(vao_sphere);
  68. GenSphere(vertices, indices);
  69. glGenBuffers(1, &vboV_sphere);
  70. glBindBuffer(GL_ARRAY_BUFFER, vboV_sphere);
  71. glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0],GL_STATIC_DRAW);
  72. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  73. glEnableVertexAttribArray(0);
  74. glGenBuffers(1, &vboI_sphere);
  75. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboI_sphere);
  76. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0],GL_STATIC_DRAW);
  77.  
  78. GLuint shader_programme;
  79. std::string vstext = textFileRead("vertex.vert");
  80. std::string fstext = textFileRead("fragment.frag");
  81. const char* vertex_shader = vstext.c_str();
  82. const char* fragment_shader = fstext.c_str();
  83. GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  84. glShaderSource(vs, 1, &vertex_shader, NULL);
  85. glCompileShader(vs);
  86. GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  87. glShaderSource(fs, 1, &fragment_shader, NULL);
  88. glCompileShader(fs);
  89. shader_programme = glCreateProgram();
  90. glAttachShader(shader_programme, fs);
  91. glAttachShader(shader_programme, vs);
  92. glLinkProgram(shader_programme);
  93. }
  94. std::string textFileRead(char *fn) {
  95. std::ifstream ifile(fn);
  96. std::string filetext;
  97. while (ifile.good()) {
  98. std::string line;
  99. std::getline(ifile, line);
  100. filetext.append(line + "\n");
  101. }
  102. return filetext;
  103. }
  104.  
  105. int main(int argc, char** argv)
  106. {
  107. glutInit(&argc, argv);
  108. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  109. glutInitWindowPosition(200, 200);
  110. glutInitWindowSize(512, 512);
  111. glutCreateWindow("SPG");
  112. init();
  113. glutDisplayFunc(display);
  114. glutMainLoop();
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement