Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.87 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. #include <glm/vec3.hpp>
  11. #include <glm/gtc/matrix_transform.hpp>
  12. #include <glm/gtc/type_ptr.hpp>
  13. using namespace std;
  14.  
  15. //глобальные переменные
  16. double xpos, ypos;
  17. double step = 0.5;
  18. double step2 = 0.05;
  19. class level{
  20. public:
  21. vector<double> point;
  22. vector<vector<double>> points;
  23. level(double h){
  24. //point.reserve(3);
  25. //points.reserve(100);
  26. double height = h, d = 0.1;
  27. double x, y,phi,r1 = 0.7*sqrt(height),r2 = 0.9* sqrt(height);
  28. phi = 0;
  29. while (phi < 6.2){
  30. point.clear();
  31. x = r1*cos(phi);
  32. y = r2*sin(phi);
  33. point.push_back(x);
  34. point.push_back(y);
  35. point.push_back(height);
  36. points.emplace_back(point);
  37. phi += step;
  38. }
  39. }
  40. };
  41.  
  42.  
  43. vector<level> l;
  44.  
  45.  
  46. void print(float[]);
  47. void makeLattice();
  48. void printParaboloid();
  49. void make3per();
  50. void makePoly();
  51.  
  52.  
  53. static void error_callback(int error, const char* description)
  54. {
  55. fputs(description, stderr);
  56. }
  57. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  58. {
  59. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
  60. glfwSetWindowShouldClose(window, GL_TRUE);
  61. }
  62. if (key == GLFW_KEY_Q && action == GLFW_PRESS) {
  63. step += 0.01;
  64. makeLattice();
  65. }
  66. if (key == GLFW_KEY_E && action == GLFW_PRESS) {
  67. step -= 0.01;
  68. makeLattice();
  69. }
  70. if (key == GLFW_KEY_Z && action == GLFW_PRESS) {
  71. step2 += 0.01;
  72. makeLattice();
  73. }
  74. if (key == GLFW_KEY_X && action == GLFW_PRESS) {
  75. step2 -= 0.01;
  76. makeLattice();
  77. }
  78. }
  79.  
  80.  
  81. int main(void)
  82. {
  83. double xCenter = 0.f, yCenter = 0.f, zCenter = 0.0f, scale = 1.f;
  84. int rAngle = 0, dAngle = 0;
  85.  
  86. //грани
  87. float front[] = { 0.f ,1.f ,1.f,
  88. -1.5f,-1.5f,-1.5f,
  89. -1.f,-1.5f,-1.5f,
  90. -1.f,-1.f,-1.5f,
  91. -1.5f,-1.f,-1.5f
  92. };
  93. float back[] = { 0.f ,0.5f ,1.f,
  94. -1.5f,-1.5f,-1.f,
  95. -1.f,-1.5f,-1.f,
  96. -1.f,-1.f,-1.f,
  97. -1.5f,-1.f,-1.f
  98. };
  99. float down[] = { 1.f ,0.5f, 0.f,
  100. -1.5f,-1.5f,-1.5f,
  101. -1.f,-1.5f,-1.5f,
  102. -1.f,-1.5f,-1.f,
  103. -1.5f,-1.5f,-1.f
  104. };
  105. float top[] = { 1.f ,1.f ,0.f,
  106. -1.5f,-1.f,-1.5f,
  107. -1.f,-1.f,-1.5f,
  108. -1.f,-1.f,-1.f,
  109. -1.5f,-1.f,-1.f
  110. };
  111. float lleft[] = { 0.f,1.f,0.5f,
  112. -1.5f,-1.5f,-1.5f,
  113. -1.5f,-1.5f,-1.f,
  114. -1.5f,-1.f,-1.f,
  115. -1.5f,-1.f,-1.5f
  116. };
  117. float rright[] = { 0.5f,1.f,0.f,
  118. -1.f,-1.5f,-1.5f,
  119. -1.f,-1.5f,-1.f,
  120. -1.f,-1.f,-1.f,
  121. -1.f,-1.f,-1.5f
  122. };
  123.  
  124. GLFWwindow* window;
  125. glfwSetErrorCallback(error_callback);
  126. if (!glfwInit()) {
  127. exit(EXIT_FAILURE);
  128. }
  129. window = glfwCreateWindow(800, 800, "Laba2", NULL, NULL);
  130. if (!window) {
  131. glfwTerminate();
  132. }
  133. glfwMakeContextCurrent(window);
  134. glfwSetKeyCallback(window, key_callback);
  135. makeLattice();
  136. int width, height;
  137. glfwGetFramebufferSize(window, &width, &height);
  138. glViewport(0, 0, width, height);
  139. glEnable(GL_DEPTH_TEST);
  140. while (!glfwWindowShouldClose(window))
  141. {
  142. glfwGetCursorPos(window, &xpos, &ypos);
  143. glMatrixMode(GL_MODELVIEW_MATRIX);
  144. //управление кубом
  145. if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
  146. rAngle -= 1;
  147. }
  148. if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
  149. dAngle -= 1;
  150. }
  151. if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
  152. dAngle += 1;
  153. }
  154. if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
  155. rAngle += 1;
  156. }
  157. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
  158. xCenter += 0.05f;
  159. }
  160.  
  161. if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
  162. xCenter -= 0.05f;
  163. }
  164. if (glfwGetKey(window, GLFW_KEY_KP_ADD) == GLFW_PRESS) {
  165. scale += 0.05f;
  166. }
  167. if (glfwGetKey(window, GLFW_KEY_KP_SUBTRACT) == GLFW_PRESS) {
  168. scale -= 0.05f;
  169. }
  170.  
  171. if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
  172. zCenter = zCenter - 0.02f;
  173. }
  174. if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
  175. zCenter = zCenter + 0.02;
  176. }
  177.  
  178. glClearColor(0, 0, 0, 0);
  179. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  180.  
  181. //отрисовка граней
  182. print(back);
  183. print(front);
  184. print(lleft);
  185. print(rright);
  186. print(down);
  187. print(top);
  188.  
  189.  
  190. glPushMatrix();
  191. glTranslated(xCenter, yCenter, zCenter);
  192. glRotated(dAngle, -1.f, 0.f, 0.f);
  193. glRotated(rAngle, 0.f, 1.f, 0.f);
  194. makePoly();
  195. printParaboloid();
  196. glPopMatrix();
  197.  
  198. glLoadIdentity();
  199.  
  200. glMatrixMode(GL_PROJECTION_MATRIX);
  201. make3per();
  202.  
  203. glfwPollEvents();
  204. glfwSwapBuffers(window);
  205. }
  206. glfwTerminate();
  207. exit(EXIT_SUCCESS);
  208. }
  209.  
  210.  
  211.  
  212. //расчет точек фигуры;
  213. void makeLattice() {
  214. l.clear();
  215. double h = 0;
  216. while (h <= 2.0) {
  217. level a(h);
  218. l.push_back(a);
  219. h += step2;
  220. }
  221. cout << 1 << endl;
  222. }
  223.  
  224. //отрисовка граней куба
  225. void print(float plane[]) {
  226. glBegin(GL_QUADS);
  227. glVertex3d(plane[3], plane[4], plane[5]);
  228. glColor3f(plane[0] + xpos / 300, plane[1] - xpos / 200, plane[2] + ypos / 900);
  229. glVertex3d(plane[6], plane[7], plane[8]);
  230. glColor3f(plane[0] - xpos / 900, plane[1] - xpos / 700, plane[2] - ypos / 900);
  231. glVertex3d(plane[9], plane[10], plane[11]);
  232. glColor3f(plane[0] + xpos / 900, plane[1] + xpos / 300, plane[2] - ypos / 600);
  233. glVertex3d(plane[12], plane[13], plane[14]);
  234. glColor3f(plane[0] - xpos / 800, plane[1] + xpos / 900, plane[2] + ypos / 900);
  235. glEnd();
  236. }
  237.  
  238. //отрисовка параболоида
  239. void printParaboloid() {
  240.  
  241. for (int i = 0; i < 30; i++) {
  242. level a = l[i];
  243. vector<vector<double>> v = l[i].points;
  244. for (int j = 0; j < v.size(); j++) {
  245. glLineWidth(1);
  246. int k = j + 1;
  247. glBegin(GL_LINES);
  248. if (k == v.size()) k = 0; ;
  249. vector<double> p1 = v[j];
  250. vector<double> p2 = v[k];
  251. glColor3d(1, 0, 0);
  252. //printf("%lf %lf %lf \n", p2[0], p2[1], p2[2]);
  253. glVertex3d(p1[0], p1[1], p1[2]);
  254. glColor3d(1, 0, 0);
  255. glVertex3d(p2[0], p2[1], p2[2]);
  256. glEnd();
  257. }
  258. }
  259. }
  260.  
  261. //создание полигональной сетки
  262. void makePoly() {
  263. int i, j;
  264. vector<vector<double>> pz = l[0].points;
  265. vector<vector<double>> p = l[1].points;
  266. for (j = 0; j < p.size(); j++) {
  267. int k = j + 1;
  268. if (k == p.size()) k = 0;
  269. glBegin(GL_TRIANGLES);
  270. glColor3f(1.0, 1.0, 1.0);
  271. glVertex3d(p[j][0], p[j][1], p[j][2]);
  272. glVertex3d(p[k][0], p[k][1], p[k][2]);
  273. glVertex3d(pz[0][0], pz[0][1], pz[0][2]);
  274. glEnd();
  275. }
  276. for (i = 0; i < l.size() - 1; i++) {
  277. vector<vector<double>> p1;
  278. vector<vector<double>> p2;
  279. p1 = l[i].points;
  280. p2 = l[i + 1].points;
  281. for (j = 0; j < p1.size(); j++) {
  282. int k = j + 1;
  283. if (k == p1.size()) k = 0;
  284. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  285. glBegin(GL_TRIANGLES);
  286. glColor3f(1.0, 1.0, 1.0);
  287. glVertex3d(p1[j][0], p1[j][1], p1[j][2]);
  288. glVertex3d(p1[k][0], p1[k][1], p1[k][2]);
  289. glVertex3d(p2[j][0], p2[j][1], p2[j][2]);
  290. glEnd();
  291. glBegin(GL_TRIANGLES);
  292. glColor3f(1.0, 0.0, 1.0);
  293. glVertex3d(p2[j][0], p2[j][1], p2[j][2]);
  294. glVertex3d(p2[k][0], p2[k][1], p2[k][2]);
  295. glVertex3d(p1[k][0], p1[k][1], p1[k][2]);
  296. glEnd();
  297. }
  298. }
  299. /*vector<vector<double>> p1 = l[l.size() - 1].points;
  300. for(i = 0; i < p1.size();i++){
  301. glBegin(GL_TRIANGLES);
  302. glColor3f(1.0, 0.0, 1.0);
  303. glVertex3d(p1[j][0], p1[j][1], p1[j][2]);
  304. glVertex3d(p1[i + 4][0], p1[i + 4][1], p2[i + 4][2]);
  305. glVertex3d(p1[i + 4][0], p1[i + 4][1], p1[i + 4k][2]);
  306. glEnd();
  307. }*/
  308. }
  309.  
  310.  
  311. // установка трехточесной перспективы
  312. void make3per() {
  313. glMatrixMode(GL_PROJECTION);
  314. double M[] = {
  315. 1 , 0 , 0 , 0 ,
  316. 0 , 1 , 0 , 0 ,
  317. 0 , 0 , -1 , 0 ,
  318. 0 , 0 , -0.6, 3.5
  319. };
  320. double phi = -0.2;
  321. double phi1 = -1.2;
  322. double M1[] = {
  323. cos(phi) , 0 , sin(phi) , 0,
  324. 0 , 1 , 0 , 0,
  325. -sin(phi), 0 , cos(phi) , 0,
  326. 0 , 0 , 0, 1
  327. };
  328.  
  329. double M3[] = {
  330. 1 , 0 , 0 , 0,
  331. 0 , cos(phi1) , sin(phi1) , 0,
  332. 0 , -sin(phi1) , cos(phi1) , 0,
  333. 0 , 0 , 0, 1
  334. };
  335. glLoadMatrixd(M);
  336. glMultMatrixd(M1);
  337. glMultMatrixd(M3);
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement