Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1.  
  2. #include <windows.h>
  3. #include <GLFW/glfw3.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <iostream>
  7. #include <math.h>
  8. #include <GL/glu.h>
  9. #include <vector>
  10. void print(float[]);
  11. using namespace std;
  12. class level{
  13. public:
  14. vector<double> point;
  15. vector<vector<double>> points;
  16. level(double h){
  17. point.reserve(3);
  18. points.reserve(100);
  19. double x, y,phi,r1 = 4*sqrt(h),r2 = 2 * sqrt(h);
  20. phi = 0;
  21. while (phi < 6.2){
  22. x = r1*cos(phi);
  23. y = r2*sin(phi);
  24. point.emplace_back(x);
  25. point.emplace_back(y);
  26. point.emplace_back(h);
  27. points.emplace_back(point);
  28. }
  29.  
  30.  
  31. }
  32. };
  33. double xpos, ypos;
  34. // 24 вариант
  35.  
  36. static void error_callback(int error, const char* description)
  37. {
  38. fputs(description, stderr);
  39. }
  40. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  41. {
  42. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  43. glfwSetWindowShouldClose(window, GL_TRUE);
  44. }
  45. void make3per() {
  46. glMatrixMode(GL_PROJECTION);
  47. glLoadIdentity();
  48. double M[] = {
  49. 1 , 0 , 0 , 0 ,
  50. 0 , 1 , 0 , 0 ,
  51. 0 , 0 , -1.5 , 0 ,
  52. 0 , 0 , -0.9, 3
  53. };
  54. double phi = -0.2;
  55. double M1[] = {
  56. cos(-phi) , 0 , sin(-phi) , 0,
  57. 0 , 1 , 0 , 0,
  58. -sin(-phi), 0 , cos(-phi) , 0,
  59. 0 , 0 , 0, 1
  60. };
  61.  
  62. double M3[] = {
  63. 1 , 0 , 0 , 0,
  64. 0 , cos(phi) , sin(phi) , 0,
  65. 0 , -sin(phi) , cos(phi) , 0,
  66. 0 , 0 , 0, 1
  67. };
  68. glLoadMatrixd(M);
  69. glMultMatrixd(M1);
  70. glMultMatrixd(M3);
  71. }
  72.  
  73.  
  74.  
  75. int main(void)
  76. {
  77. double xCenter = 0.f, yCenter = 0.f, zCenter = 0.0f, scale = 1.f;
  78. int rAngle = 0, dAngle = 0;
  79. double h = 0;
  80.  
  81. //грани
  82. float front[] = { 0.f ,1.f ,1.f,
  83. 0.f,0.f,0.f,
  84. 0.5f,0.f,0.f,
  85. 0.5f,0.5f,0.f,
  86. 0.f,0.5f,0.f
  87. };
  88. float back[] = { 0.f ,0.5f ,1.f,
  89. 0.f,0.f,0.5f,
  90. 0.5f,0.f,0.5f,
  91. 0.5f,0.5f,0.5f,
  92. 0.f,0.5f,0.5f
  93. };
  94. float down[] = { 1.f ,0.5f, 0.f,
  95. 0.f,0.f,0.f,
  96. 0.5f,0.f,0.f,
  97. 0.5f,0.f,0.5f,
  98. 0.f,0.f,0.5f
  99. };
  100. float top[] = { 1.f ,1.f ,0.f,
  101. 0.f,0.5f,0.f,
  102. 0.5f,0.5f,0.f,
  103. 0.5f,0.5f,0.5f,
  104. 0.f,0.5f,0.5f
  105. };
  106. float lleft[] = { 0.f,1.f,0.5f,
  107. 0.f,0.f,0.f,
  108. 0.f,0.f,0.5f,
  109. 0.f,0.5f,0.5f,
  110. 0.f,0.5f,0.f
  111. };
  112. float rright[] = { 0.5f,1.f,0.f,
  113. 0.5f,0.f,0.f,
  114. 0.5f,0.f,0.5f,
  115. 0.5f,0.5f,0.5f,
  116. 0.5f,0.5f,0.f
  117. };
  118. vector<level> l;
  119. for (int i = 0; i < 30; i++) {
  120. level a(h);
  121. l[i] = malloc(sizeof(level));
  122. l[i] = a;
  123. h += 0.1;
  124. }
  125. GLFWwindow* window;
  126. glfwSetErrorCallback(error_callback);
  127. if (!glfwInit()) {
  128. exit(EXIT_FAILURE);
  129. }
  130. window = glfwCreateWindow(800, 800, "Laba2", NULL, NULL);
  131. if (!window) {
  132. glfwTerminate();
  133. }
  134. glfwMakeContextCurrent(window);
  135. glfwSetKeyCallback(window, key_callback);
  136.  
  137. int width, height;
  138. double ratio;
  139. make3per();
  140. glfwGetFramebufferSize(window, &width, &height);
  141. ratio = width / height;
  142. glViewport(0, 0, width, height);
  143. glMatrixMode(GL_MODELVIEW_MATRIX);
  144. glEnable(GL_DEPTH_TEST);
  145. while (!glfwWindowShouldClose(window))
  146. {
  147. glfwGetCursorPos(window, &xpos, &ypos);
  148. glClearColor(0, 0, 0, 0);
  149. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  150. /*for (int i = 0; i < 30; i++) {
  151. level a = l[i];
  152. for(int j = 0 ; j < l[i].points.size() - 1 ; j++){
  153. glBegin(GL_LINES);
  154. vector<double> p1 = l[i].points[j];
  155. vector<double> p2 = l[i].points[j + 1];
  156. p1.reserve(3);
  157. p2.reserve(3);
  158. glColor3d(1, 0, 0);
  159. glVertex3d(p1[0], p1[1], p1[2]);
  160. glVertex3d(p2[0], p2[1], p2[2]);
  161. glEnd();
  162. }
  163.  
  164. }*/
  165. //отрисовка граней
  166. /*print(back);
  167. print(front);
  168. print(lleft);
  169. print(rright);
  170. print(down);
  171. print(top);*/
  172. glfwSwapBuffers(window);
  173. glfwPollEvents();
  174. }
  175. glfwTerminate();
  176. exit(EXIT_SUCCESS);
  177. }
  178.  
  179. void print(float plane[]) {
  180. glBegin(GL_QUADS);
  181. glVertex3d(plane[3], plane[4], plane[5]);
  182. glColor3f(plane[0] + xpos / 300, plane[1] - xpos / 200, plane[2] + ypos / 900);
  183. glVertex3d(plane[6], plane[7], plane[8]);
  184. glColor3f(plane[0] - xpos / 900, plane[1] - xpos / 700, plane[2] - ypos / 900);
  185. glVertex3d(plane[9], plane[10], plane[11]);
  186. glColor3f(plane[0] + xpos / 900, plane[1] + xpos / 300, plane[2] - ypos / 600);
  187. glVertex3d(plane[12], plane[13], plane[14]);
  188. glColor3f(plane[0] - xpos / 800, plane[1] + xpos / 900, plane[2] + ypos / 900);
  189. glEnd();
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement