Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. #include <GLFW/glfw3.h>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include "math.h"
  5.  
  6. GLfloat rotationX = 0, rotationY = 0, rotationZ = 0, n = 20, rx1 = 60;
  7. int count = 3, flag = 1, rx = 120, rz = 70, x, y, z, delta = 30, count2 = 1, h = 200;
  8.  
  9. float colour[6][3] = {
  10. {0.0, 0.0, 1.0},
  11. {0.0, 1.0, 0.0},
  12. {0.0, 1.0, 1.0},
  13. {1.0, 0.0, 0.0},
  14. {1.0, 0.0, 1.0},
  15. {1.0, 1.0, 0.0}
  16. };
  17.  
  18. void drawBase(GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat rX, GLfloat rZ) {
  19.  
  20. float step = 2 * M_PI / count;
  21.  
  22. if (flag == 1) {
  23. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  24. } else {
  25. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  26. }
  27. //glEnable(GL_DEPTH_TEST);
  28.  
  29. glBegin(GL_TRIANGLE_FAN);
  30. glVertex3f(centerX, centerY, centerZ);
  31. for (float angle = -step; angle <= 2 * M_PI; angle += step) {
  32. float a = fabsf(angle - float(2 * M_PI));
  33. float dx = rX * cosf(a);
  34. float dz = rZ * sinf(a);
  35. glVertex3f(centerX + dx, centerY, centerZ + dz);
  36. }
  37. glEnd();
  38. }
  39.  
  40. void drawNoBase(GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat h1, GLfloat rX, GLfloat rZ, GLfloat rX1) {
  41.  
  42. float step = 2 * M_PI / count;
  43.  
  44. if (flag == 1) {
  45. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  46. } else {
  47. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  48. }
  49. //glEnable(GL_DEPTH_TEST);
  50.  
  51. glBegin(GL_TRIANGLE_STRIP);
  52. GLfloat k = rX1/rX;//=rz1/rZ
  53. for (float angle = -step; angle <= 2 * M_PI; angle += step) {
  54. float a = fabsf(angle - float(2 * M_PI));
  55. float dx = rX * cosf(a);
  56. float dz = rZ * sinf(a);
  57. float dx1 = rX1 * cosf(a);
  58. float dz1 = k* rZ * sinf(a);
  59. glVertex3f(centerX + dx, centerY, centerZ + dz);
  60. glVertex3f(centerX + dx1, centerY + h1, centerZ + dz1);
  61. }
  62. glEnd();
  63. }
  64.  
  65. void drawFigure(GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat h1, GLfloat rX, GLfloat rZ, GLfloat rx1) {
  66. drawBase(centerX, centerY, centerZ, rX, rZ);
  67. drawBase(centerX, centerY + h1, centerZ, rx1, rZ * rx1/rX);
  68.  
  69. GLfloat h2 = h1 / count2;
  70. // GLfloat delt2 = delt / count2;
  71. GLfloat k = rx1/rX;
  72. GLfloat deltX = (rX-rx1)/count2;
  73. GLfloat deltZ = rZ*(1-k)/count2;
  74. for (int i = 0; i < count2; i++){
  75.  
  76. // drawNoBase(centerX, centerY + i * h2, centerZ, h2, rX-i*deltX, rZ-i*deltZ, delt2,);
  77. drawNoBase(centerX, centerY + i * h2, centerZ, h2, rX-i*deltX, rZ-i*deltZ, rX);
  78. }
  79.  
  80. }
  81.  
  82. void DrawCube(GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat size) {
  83. GLfloat halfSize = size / 2;
  84.  
  85. GLfloat vertices[] = {
  86. // front
  87. centerX - halfSize, centerY + halfSize, centerZ + halfSize,
  88. centerX + halfSize, centerY + halfSize, centerZ + halfSize,
  89. centerX + halfSize, centerY - halfSize, centerZ + halfSize,
  90. centerX - halfSize, centerY - halfSize, centerZ + halfSize,
  91.  
  92. // back
  93. centerX - halfSize, centerY + halfSize, centerZ - halfSize,
  94. centerX + halfSize, centerY + halfSize, centerZ - halfSize,
  95. centerX + halfSize, centerY - halfSize, centerZ - halfSize,
  96. centerX - halfSize, centerY - halfSize, centerZ - halfSize,
  97.  
  98. // left
  99. centerX - halfSize, centerY + halfSize, centerZ + halfSize,
  100. centerX - halfSize, centerY + halfSize, centerZ - halfSize,
  101. centerX - halfSize, centerY - halfSize, centerZ - halfSize,
  102. centerX - halfSize, centerY - halfSize, centerZ + halfSize,
  103.  
  104. // right
  105. centerX + halfSize, centerY + halfSize, centerZ + halfSize,
  106. centerX + halfSize, centerY + halfSize, centerZ - halfSize,
  107. centerX + halfSize, centerY - halfSize, centerZ - halfSize,
  108. centerX + halfSize, centerY - halfSize, centerZ + halfSize,
  109.  
  110. // top
  111. centerX - halfSize, centerY + halfSize, centerZ + halfSize,
  112. centerX - halfSize, centerY + halfSize, centerZ - halfSize,
  113. centerX + halfSize, centerY + halfSize, centerZ - halfSize,
  114. centerX + halfSize, centerY + halfSize, centerZ + halfSize,
  115.  
  116. //bot
  117. centerX - halfSize, centerY - halfSize, centerZ + halfSize,
  118. centerX - halfSize, centerY - halfSize, centerZ - halfSize,
  119. centerX + halfSize, centerY - halfSize, centerZ - halfSize,
  120. centerX + halfSize, centerY - halfSize, centerZ + halfSize
  121. };
  122.  
  123. if (flag == 1) {
  124. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  125. } else {
  126. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  127. }
  128. glEnable(GL_DEPTH_TEST);
  129. glEnableClientState(GL_VERTEX_ARRAY);
  130. glVertexPointer(3, GL_FLOAT, 0, vertices);
  131. for (int i = 0; i < 6; i++) {
  132. glColor3f(colour[i][0], colour[i][1], colour[i][2]);
  133. glDrawArrays(GL_QUADS, i * 4, 4);
  134. }
  135. glDisableClientState(GL_VERTEX_ARRAY);
  136. }
  137.  
  138. void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  139. GLfloat speed = 10;
  140.  
  141. if (action == GLFW_PRESS || action == GLFW_REPEAT) {
  142. switch (key) {
  143. case GLFW_KEY_W:
  144. rotationX += speed;
  145. break;
  146. case GLFW_KEY_S:
  147. rotationX -= speed;
  148. break;
  149. case GLFW_KEY_A:
  150. rotationY -= speed;
  151. break;
  152. case GLFW_KEY_D:
  153. rotationY += speed;
  154. break;
  155. case GLFW_KEY_Q:
  156. rotationZ -= speed;
  157. break;
  158. case GLFW_KEY_E:
  159. rotationZ += speed;
  160. break;
  161. case GLFW_KEY_ESCAPE:
  162. glfwSetWindowShouldClose(window, GLFW_TRUE);
  163. break;
  164. case GLFW_KEY_EQUAL:
  165. count++;
  166. break;
  167. case GLFW_KEY_MINUS:
  168. if (count > 3) {
  169. count--;
  170. }
  171. break;
  172. case GLFW_KEY_Z:
  173. if (rx > 5) {
  174. rx -= 5;
  175. }
  176. break;
  177. case GLFW_KEY_X:
  178. rx += 5;
  179. break;
  180. case GLFW_KEY_C:
  181. if (rz > 5) {
  182. rz -= 5;
  183. }
  184. break;
  185. case GLFW_KEY_V:
  186. rz += 5;
  187. break;
  188. case GLFW_KEY_UP:
  189. y += 5;
  190. break;
  191. case GLFW_KEY_DOWN:
  192. y -= 5;
  193. break;
  194. case GLFW_KEY_RIGHT:
  195. x += 5;
  196. break;
  197. case GLFW_KEY_LEFT:
  198. x -= 5;
  199. break;
  200. case GLFW_KEY_1:
  201. flag = 1;
  202. break;
  203. case GLFW_KEY_2:
  204. flag = 0;
  205. break;
  206. case GLFW_KEY_KP_8:
  207. count2 += 1;
  208. break;
  209. case GLFW_KEY_KP_2:
  210. if (rz > 1) {
  211. count2 -= 1;
  212. }
  213. break;
  214. }
  215. }
  216. }
  217.  
  218. int main() {
  219.  
  220. GLint scrW = 1280, scrH = 960;
  221.  
  222. if (!glfwInit()) {
  223. return -1;
  224. }
  225.  
  226. GLFWwindow *window = glfwCreateWindow(scrW, scrH, "Lab_3", NULL, NULL);
  227.  
  228. // int screenW, screenH;
  229. // glfwGetFramebufferSize(window, &screenW, &screenH);
  230.  
  231. glfwMakeContextCurrent(window);
  232.  
  233. glfwSetKeyCallback(window, keyCallback);
  234. // glfwSetFramebufferSizeCallback(window, frameCallback);
  235.  
  236. // glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
  237.  
  238.  
  239. // glViewport(0.0f, 0.0f, screenW, screenH);
  240.  
  241. glMatrixMode(GL_PROJECTION);
  242. glLoadIdentity();
  243. glOrtho(0, scrW, 0, scrH, 0, 1000);
  244. glMatrixMode(GL_MODELVIEW);
  245. glLoadIdentity();
  246.  
  247.  
  248. x = scrW / 2;
  249. y = scrH / 2;
  250. z = 500;
  251. while (!glfwWindowShouldClose(window)) {
  252.  
  253. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  254.  
  255. //маленький кубик
  256. glPushMatrix();
  257. glRotatef(10, 1, 0, 0);
  258. glRotatef(10, 0, 1, 0);
  259. DrawCube(100, 100, -100, 100);
  260. glPopMatrix();
  261.  
  262. //фигура
  263. glPushMatrix();
  264. glTranslatef(x, y, -z);
  265. glRotatef(rotationX, 1, 0, 0);
  266. glRotatef(rotationY, 0, 1, 0);
  267. glRotatef(rotationZ, 0, 0, 1);
  268. glTranslatef(-x, -y, z);
  269. //drawBase(x, y, -z, rx, rz, 0);
  270. //drawNoBase(x, y, -z, h, rx, rz, delta, 0);
  271. // drawFigure(x, y, -z, h, rx, rz, delta, n);
  272. drawFigure(x, y, -z, h, rx, rz,rx1);
  273. glPopMatrix();
  274.  
  275. glfwSwapBuffers(window);
  276. glfwPollEvents();
  277. }
  278.  
  279. glfwTerminate();
  280. return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement