Advertisement
Guest User

Untitled

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