Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. /*
  2. Konovalov Anton BPI132
  3. USING FREEGLUT!
  4. There is no render scene
  5. */
  6.  
  7. #include <cstdlib>
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. #include <GL/glew.h>
  13. #include "GL/freeglut.h"
  14. #include "shaders.h"
  15.  
  16. float firBottom[9];
  17. float firUp[9];
  18. float firColor[9];
  19.  
  20. float firTrunk[12];
  21. float firTrunkColor[12];
  22.  
  23. float starA[9];
  24. float starB[9];
  25. float starColor[9];
  26.  
  27. const int figures_count = 5;
  28.  
  29. UINT uiVBO[figures_count * 2];
  30. UINT uiVAO[figures_count];
  31.  
  32. CShader shVertex, shFragment;
  33. CShaderProgram spMain;
  34.  
  35. int initCounter = 0;
  36.  
  37. void initTriangle(float vertexAr[], float colorAr[]) {
  38.  
  39. int index = initCounter++;
  40.  
  41. glBindVertexArray(uiVAO[index]);
  42.  
  43. glBindBuffer(GL_ARRAY_BUFFER, uiVBO[index * 2]);
  44. glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), vertexAr, GL_STATIC_DRAW);
  45. glEnableVertexAttribArray(0);
  46. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  47.  
  48. glBindBuffer(GL_ARRAY_BUFFER, uiVBO[index * 2 + 1]);
  49. glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), colorAr, GL_STATIC_DRAW);
  50. glEnableVertexAttribArray(1);
  51. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
  52. }
  53.  
  54. void initSquare(float vertexAr[], float colorAr[]) {
  55.  
  56. int index = initCounter++;
  57.  
  58. glBindVertexArray(uiVAO[index]);
  59.  
  60. glBindBuffer(GL_ARRAY_BUFFER, uiVBO[index * 2]);
  61. glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), vertexAr, GL_STATIC_DRAW);
  62. glEnableVertexAttribArray(0);
  63. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  64.  
  65. glBindBuffer(GL_ARRAY_BUFFER, uiVBO[index * 2 + 1]);
  66. glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), colorAr, GL_STATIC_DRAW);
  67. glEnableVertexAttribArray(1);
  68. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
  69. }
  70.  
  71. bool init_resources(void) {
  72. glClearColor(0.1f, 0.0f, 0.2f, 1.0f);
  73.  
  74. // Setup firbot vertices
  75. firBottom[0] = -0.4f; firBottom[1] = -0.5f; firBottom[2] = 0.0f;
  76. firBottom[3] = 0.4f; firBottom[4] = -0.5f; firBottom[5] = 0.0f;
  77. firBottom[6] = 0.0f; firBottom[7] = 0.0f; firBottom[8] = 0.0f;
  78.  
  79. // Setup firup vertices
  80. firUp[0] = -0.4f; firUp[1] = -0.2f; firUp[2] = 0.0f;
  81. firUp[3] = 0.4f; firUp[4] = -0.2f; firUp[5] = 0.0f;
  82. firUp[6] = 0.0f; firUp[7] = 0.3f; firUp[8] = 0.0f;
  83.  
  84. // Setup fir color
  85. firColor[0] = 0.0f; firColor[1] = 1.0f; firColor[2] = 0.2f;
  86. firColor[3] = 0.0f; firColor[4] = 1.0f; firColor[5] = 0.2f;
  87. firColor[6] = 0.0f; firColor[7] = 1.0f; firColor[8] = 0.2f;
  88.  
  89. // Setup trunk vertices
  90. firTrunk[0] = -0.1f; firTrunk[1] = -0.5f; firTrunk[2] = 0.0f;
  91. firTrunk[3] = -0.1f; firTrunk[4] = -0.7f; firTrunk[5] = 0.0f;
  92. firTrunk[6] = 0.1f; firTrunk[7] = -0.5f; firTrunk[8] = 0.0f;
  93. firTrunk[9] = 0.1f; firTrunk[10] = -0.7f; firTrunk[11] = 0.0f;
  94.  
  95. // Setup trunk color
  96. firTrunkColor[0] = 0.4f; firTrunkColor[1] = 0.2f; firTrunkColor[2] = 0.1f;
  97. firTrunkColor[3] = 0.4f; firTrunkColor[4] = 0.2f; firTrunkColor[8] = 0.1f;
  98. firTrunkColor[6] = 0.4f; firTrunkColor[7] = 0.2f; firTrunkColor[5] = 0.1f;
  99. firTrunkColor[9] = 0.4f; firTrunkColor[10] = 0.2f; firTrunkColor[11] = 0.1f;
  100.  
  101. // Setup starA vertices
  102. starA[0] = -0.1f; starA[1] = 0.25f; starA[2] = 0.0f;
  103. starA[3] = 0.1f; starA[4] = 0.25f; starA[5] = 0.0f;
  104. starA[6] = 0.0f; starA[7] = 0.45f; starA[8] = 0.0f;
  105.  
  106. // Setup starB vertices
  107. starB[0] = -0.1f; starB[1] = 0.4f; starB[2] = 0.0f;
  108. starB[3] = 0.1f; starB[4] = 0.4f; starB[5] = 0.0f;
  109. starB[6] = 0.0f; starB[7] = 0.2f; starB[8] = 0.0f;
  110.  
  111. // Setup star color
  112. starColor[0] = 1.0f; starColor[1] = 1.0f; starColor[2] = 0.0f;
  113. starColor[3] = 1.0f; starColor[4] = 1.0f; starColor[5] = 0.0f;
  114. starColor[6] = 1.0f; starColor[7] = 1.0f; starColor[8] = 0.0f;
  115.  
  116.  
  117. glGenVertexArrays(figures_count, uiVAO); // Generate two VAOs, one for triangle and one for quad
  118. glGenBuffers(figures_count * 2, uiVBO); // And four VBOs
  119.  
  120. // Setup fir trunk
  121. initSquare(firTrunk, firTrunkColor);
  122.  
  123. // Setup fir bot
  124. initTriangle(firBottom, firColor);
  125.  
  126. // Setup firUp
  127. initTriangle(firUp, firColor);
  128.  
  129. // Setup starA
  130. initTriangle(starA, starColor);
  131.  
  132. // Setup starB
  133. initTriangle(starB, starColor);
  134.  
  135.  
  136. // Load shaders and create shader program
  137. shVertex.LoadShader("data\\shaders\\shader.vert", GL_VERTEX_SHADER);
  138. shFragment.LoadShader("data\\shaders\\shader.frag", GL_FRAGMENT_SHADER);
  139.  
  140. spMain.CreateProgram();
  141. spMain.AddShaderToProgram(&shVertex);
  142. spMain.AddShaderToProgram(&shFragment);
  143.  
  144. spMain.LinkProgram();
  145. spMain.UseProgram();
  146. return true;
  147. }
  148.  
  149. void free_resources() {
  150. spMain.DeleteProgram();
  151.  
  152. shVertex.DeleteShader();
  153. shFragment.DeleteShader();
  154. }
  155.  
  156. void draw()
  157. {
  158. glClear(GL_COLOR_BUFFER_BIT);
  159.  
  160. //Rectangle
  161. glBindVertexArray(uiVAO[0]);
  162. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  163.  
  164. //Triangles
  165. for (int i = 1; i < figures_count; i++)
  166. {
  167. glBindVertexArray(uiVAO[i]);
  168. glDrawArrays(GL_TRIANGLES, 0, 3);
  169. }
  170.  
  171. glutSwapBuffers();
  172. }
  173.  
  174. int main(int argc, char **argv)
  175. {
  176. glutInit(&argc, argv);
  177. glutInitDisplayMode(GLUT_SINGLE);
  178. glutInitWindowSize(500, 500);
  179. glutInitWindowPosition(100, 100);
  180. glutCreateWindow("OpenGL HW1 - Konovalov Anton");
  181.  
  182. GLenum glew_status = glewInit();
  183. if (glew_status != GLEW_OK) {
  184. cerr << "Error: glewInit: " << glewGetErrorString(glew_status) << endl;
  185. return EXIT_FAILURE;
  186. }
  187.  
  188. if (!init_resources())
  189. return EXIT_FAILURE;
  190.  
  191. glutDisplayFunc(draw);
  192. glutMainLoop();
  193.  
  194. free_resources();
  195.  
  196. return EXIT_SUCCESS;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement