Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. #include <glew.h>
  2. #include <glfw3.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <math.h>
  6.  
  7. #include "shaders.h"
  8.  
  9. #define radius 1.0f
  10. #define zmin -1.0f
  11. #define zmax 1.0f
  12. #define theta_max 360.0f
  13.  
  14. constexpr int WIDTH = 600; // szerokosc okna
  15. constexpr int HEIGHT = 600; // wysokosc okna
  16.  
  17. //******************************************************************************************
  18. GLuint shaderProgram; // identyfikator programu cieniowania
  19.  
  20. GLuint vertexLoc; // lokalizacja atrybutu wierzcholka - wspolrzedne wierzcholkow
  21. GLuint colorLoc; // lokalizacja zmiennej jednorodnej - kolor rysowania prymitywu
  22.  
  23. GLuint vao; // identyfikatory VAO
  24. GLuint buffers[2]; // identyfikatory VBO
  25.  
  26. GLint indicesNumber = 0; // liczba indeksow definiujacych obiekt
  27.  
  28. float color[] = { 0.0f, 1.0f, 0.0f, 1.0f }; // kolor jakim rysowac siatke
  29. float lineWidth = 3.0f; // grubosc linii
  30. //******************************************************************************************
  31.  
  32. void errorCallback( int error, const char* description );
  33. void keyCallback( GLFWwindow* window, int key, int scancode, int action, int mods );
  34. void onShutdown();
  35. void initGL();
  36. void setupShaders();
  37. void setupBuffers();
  38. void renderScene();
  39. void renderSphere(float zMin, float zMax, float rad, float tetaMax);
  40.  
  41. int main(int argc, char* argv[])
  42. {
  43. atexit( onShutdown );
  44.  
  45. GLFWwindow* window;
  46.  
  47. glfwSetErrorCallback( errorCallback );
  48.  
  49. if( !glfwInit() )
  50. exit( EXIT_FAILURE );
  51.  
  52. glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
  53. glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
  54.  
  55. glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
  56.  
  57. window = glfwCreateWindow( WIDTH, HEIGHT, "OpenGL (Core Profile) - primitive restart", nullptr, nullptr );
  58. if( !window )
  59. {
  60. glfwTerminate();
  61. exit( EXIT_FAILURE );
  62. }
  63.  
  64. glfwSetKeyCallback( window, keyCallback );
  65.  
  66. glfwMakeContextCurrent( window );
  67.  
  68. glewExperimental = GL_TRUE;
  69. GLenum err = glewInit();
  70. if( err != GLEW_OK )
  71. {
  72. std::cerr << "Blad: " << glewGetErrorString( err ) << std::endl;
  73. exit( 1 );
  74. }
  75.  
  76. if( !GLEW_VERSION_3_3 )
  77. {
  78. std::cerr << "Brak obslugi OpenGL 3.3\n";
  79. exit( 2 );
  80. }
  81.  
  82. glfwSwapInterval( 1 ); // v-sync on
  83.  
  84. initGL();
  85.  
  86. while( !glfwWindowShouldClose( window ) )
  87. {
  88. renderScene();
  89.  
  90. glfwSwapBuffers( window );
  91. glfwPollEvents();
  92. }
  93.  
  94. glfwDestroyWindow( window );
  95. glfwTerminate();
  96. exit( EXIT_SUCCESS );
  97.  
  98. return 0;
  99. }
  100.  
  101. /*------------------------------------------------------------------------------------------
  102. ** funkcja zwrotna do obslugi bledow biblioteki GLFW
  103. ** error - kod bledu
  104. ** description - opis bledu
  105. **------------------------------------------------------------------------------------------*/
  106. void errorCallback( int error, const char* description )
  107. {
  108. std::cerr << "Error: " << description << std::endl;
  109. }
  110.  
  111. /*------------------------------------------------------------------------------------------
  112. ** funkcja zwrotna do obslugi klawiatury
  113. ** window - okno, które otrzymalo zdarzenie
  114. ** key - klawisz jaki zostal nacisniety lub zwolniony
  115. ** scancode - scancode klawisza specyficzny dla systemu
  116. ** action - zachowanie klawisza (GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT)
  117. ** mods - pole bitowe zawierajace informacje o nacisnietych modyfikatorach (GLFW_MOD_SHIFT, GLFW_MOD_CONTROL, GLFW_MOD_ALT, GLFW_MOD_SUPER)
  118. **------------------------------------------------------------------------------------------*/
  119. void keyCallback( GLFWwindow* window, int key, int scancode, int action, int mods )
  120. {
  121. if( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS )
  122. glfwSetWindowShouldClose( window, GLFW_TRUE );
  123. }
  124.  
  125. /*------------------------------------------------------------------------------------------
  126. ** funkcja wykonywana przed zamknieciem programu
  127. **------------------------------------------------------------------------------------------*/
  128. void onShutdown()
  129. {
  130. glDeleteBuffers( 2, buffers );
  131. glDeleteVertexArrays( 1, &vao );
  132. glDeleteProgram( shaderProgram );
  133. }
  134.  
  135. /*------------------------------------------------------------------------------------------
  136. ** funkcja inicjujaca ustawienia OpenGL
  137. **------------------------------------------------------------------------------------------*/
  138. void initGL()
  139. {
  140. std::cout << "GLEW = " << glewGetString( GLEW_VERSION ) << std::endl;
  141. std::cout << "GL_VENDOR = " << glGetString( GL_VENDOR ) << std::endl;
  142. std::cout << "GL_RENDERER = " << glGetString( GL_RENDERER ) << std::endl;
  143. std::cout << "GL_VERSION = " << glGetString( GL_VERSION ) << std::endl;
  144. std::cout << "GLSL = " << glGetString( GL_SHADING_LANGUAGE_VERSION ) << std::endl;
  145.  
  146. glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
  147.  
  148. setupShaders();
  149.  
  150. setupBuffers();
  151. }
  152.  
  153. /*------------------------------------------------------------------------------------------
  154. ** funkcja tworzaca program cieniowania skladajacy sie z shadera wierzcholkow i fragmentow
  155. **------------------------------------------------------------------------------------------*/
  156. void setupShaders()
  157. {
  158. if( !setupShaders( "shaders/vertex.vert", "shaders/fragment.frag", shaderProgram ) )
  159. exit( 3 );
  160.  
  161. vertexLoc = glGetAttribLocation( shaderProgram, "vPosition" );
  162.  
  163. colorLoc = glGetUniformLocation( shaderProgram, "color" );
  164. }
  165.  
  166. /*------------------------------------------------------------------------------------------
  167. ** funkcja inicjujaca VAO oraz zawarte w nim VBO z danymi o modelu
  168. **------------------------------------------------------------------------------------------*/
  169. void setupBuffers()
  170. {
  171. renderSphere(radius, zmin, zmax, theta_max);
  172. }
  173.  
  174. void renderSphere(float zMin, float zMax, float rad, float thetaMax)
  175. {
  176. glGenVertexArrays(1, &vao);
  177. glGenBuffers(2, buffers);
  178.  
  179. glBindVertexArray(vao); //powiazanie vao, reprezentuje sfere
  180.  
  181. const float PI = 3.14159265359;
  182. int horizontalVertices = 16;
  183. int verticalVertices = 8;
  184.  
  185.  
  186. float phi_min = -90.0f, phi_max = 90.0f;
  187.  
  188. /*if (zMin > (-rad))
  189. phi_min = asin(zMin / rad);
  190.  
  191. if (zMax < rad)
  192. phi_max = asin(zMax / rad);*/
  193.  
  194. float phi, theta;
  195. float x, y, z, u, v;
  196.  
  197. std::vector<GLfloat> vertices;
  198. std::vector<GLuint> indices;
  199.  
  200. for (int i = 0; i < verticalVertices; i++)
  201. {
  202. for (int j = 0; j < horizontalVertices; j++)
  203. {
  204. u = (float)j / (horizontalVertices - 1);
  205. v = (float) i / (verticalVertices - 1);
  206. phi = (phi_min + v * (phi_max - phi_min)) * PI / 180;
  207. theta = (u * thetaMax) * PI / 180;
  208.  
  209. x = rad * cos(theta) * cos(phi);
  210. y = rad * sin(theta) * cos(phi);
  211. z = rad * sin(phi);
  212.  
  213. vertices.push_back(x);
  214. vertices.push_back(y);
  215. vertices.push_back(z);
  216.  
  217. if (i != (verticalVertices - 1) && j != (horizontalVertices - 1)) //rysowanie
  218. { //bez uwzglednienia ostatniego wierzcholka, bez sensu rysowac go dwa razy
  219. indices.push_back(i * horizontalVertices + horizontalVertices + j);
  220. indices.push_back(i * horizontalVertices + j + 1);
  221. indices.push_back(i * horizontalVertices + j);
  222. //wrzucenie kolejnych wierzcholkow do wektora z trojkatami w odpowiedniej
  223. //kolejnosci zeby je dobrze polaczyc
  224. indices.push_back(i * horizontalVertices + j + 1);
  225. indices.push_back(i * horizontalVertices + horizontalVertices + j);
  226. indices.push_back(i * horizontalVertices + horizontalVertices + j + 1);
  227. }
  228. }
  229. }
  230.  
  231.  
  232. indicesNumber = 3 * indices.size();
  233.  
  234. //wrzucenie wierzcholkow do pierwszego bufora przy vao
  235. glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
  236. glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
  237. glEnableVertexAttribArray(vertexLoc);
  238. glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
  239.  
  240. //wrzucenie trojkatow do drugiego bufora przy vao
  241. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
  242. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
  243.  
  244. glBindVertexArray(0);
  245. }
  246.  
  247.  
  248. /*------------------------------------------------------------------------------------------
  249. ** funkcja rysujaca scene
  250. **------------------------------------------------------------------------------------------*/
  251. void renderScene()
  252. {
  253. glClear( GL_COLOR_BUFFER_BIT );
  254.  
  255. glUseProgram( shaderProgram );
  256.  
  257. glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
  258. glLineWidth( lineWidth );
  259.  
  260. glBindVertexArray( vao );
  261. glUniform4fv( colorLoc, 1, color );
  262. glDrawElements( GL_TRIANGLES, indicesNumber, GL_UNSIGNED_INT, 0 );
  263. glBindVertexArray( 0 );
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement