Advertisement
Guest User

Untitled

a guest
May 27th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.89 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <glaux.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #define _USE_MATH_DEFINES
  7. #include <math.h>
  8. #include <iostream>
  9. #include <sstream>
  10. #include <fstream>
  11.  
  12. #define SCREEN_WIDTH 640
  13. #define SCREEN_HEIGHT 480
  14.  
  15. float x_0 = 0.f, y_0 = 0.f, z_0 = 0.f;
  16. float nOC = 2.f, nOP = 2.f;
  17. float kX = 0.f, kY = 0.f, kZ = 0.f;
  18. float mX = 0.f, mY = 0.f, mZ = 0.f;
  19. float k = 1;
  20. float t = 0;
  21. GLuint texture;
  22. GLenum mode = GL_LINE;
  23. bool animation = false;
  24. bool texturing = false;
  25. bool increase = true;
  26.  
  27.  
  28. static struct vert
  29. {
  30. float x, y, z;
  31. float r, g, b;
  32. vert() {
  33. x = y = z = 0;
  34. r = 0.56f;
  35. g = 0.23f;
  36. b = 0.63f;
  37. }
  38. vert(float x, float y, float z) {
  39. this->x = x;
  40. this->y = y;
  41. this->z = z;
  42. r = 0.56f;
  43. g = 0.23f;
  44. b = 0.63f;
  45. }
  46. };
  47.  
  48. static struct face
  49. {
  50. int c1, p1, c2, p2, c3, p3;
  51. face() {
  52. c1 = p1 = c2 = p2 = c3 = p3 = 0;
  53. }
  54. };
  55.  
  56. struct Cube {
  57. struct vert * vertices;
  58. Cube() {
  59. vertices = new struct vert[8];
  60. vertices[7] = vert(0.7f, 0.f, 0.5f);
  61. vertices[6] = vert(1.f, 0.f, 0.5f);
  62. vertices[3] = vert(0.7f, -0.25f, 0.5f);
  63. vertices[2] = vert(1.f, -0.25f, 0.5f);
  64. vertices[4] = vert(0.7f, 0.f, 0.8f);
  65. vertices[5] = vert(1.f, 0.f, 0.8f);
  66. vertices[0] = vert(0.7f, -0.25f, 0.8f);
  67. vertices[1] = vert(1.f, -0.25f, 0.8f);
  68. }
  69. ~Cube() {
  70. delete[]vertices;
  71. }
  72.  
  73. };
  74.  
  75.  
  76. class Sphere {
  77. public:
  78. ~Sphere() {
  79. for (auto i = 0; i < numberOfCircles; i++) {
  80. delete[] vertices[i];
  81. }
  82. delete[]vertices;
  83. delete[] faces;
  84.  
  85. }
  86. float numberOfCircles;
  87. float numberOfPoints;
  88. float numberOfEdges;
  89. float radius = 1.f;
  90. struct vert ** vertices;
  91. struct face * faces;
  92. Sphere() {
  93. numberOfCircles = nOC + 1;
  94. numberOfPoints = nOP;
  95. numberOfEdges = 2 * numberOfPoints*numberOfCircles;
  96. vertices = new struct vert *[numberOfCircles];
  97. for (auto i = 0; i < numberOfCircles; i++) {
  98. vertices[i] = new struct vert[numberOfPoints];
  99. for (auto j = 0; j < numberOfPoints; j++) {
  100. vertices[i][j] = vert();
  101. }
  102. }
  103. faces = new struct face[numberOfEdges];
  104. for (auto i = 0; i < numberOfEdges; i++) {
  105. faces[i] = face();
  106. }
  107. createVertices();
  108. }
  109.  
  110.  
  111. void createVertices() {
  112. for (auto i = 0; i < this->numberOfCircles; i++) {
  113. for (auto j = 0; j < this->numberOfPoints; j++) {
  114. this->vertices[i][j].x = x_0 + this->radius*sin(M_PI*i / (this->numberOfCircles - 1))*cos(2 * M_PI*j / (this->numberOfPoints));
  115. this->vertices[i][j].y = y_0 + this->radius*sin(M_PI*i / (this->numberOfCircles - 1))*sin(2 * M_PI*j / (this->numberOfPoints));
  116. this->vertices[i][j].z = z_0 + this->radius*cos(M_PI*i / (this->numberOfCircles - 1));
  117. }
  118. }
  119.  
  120. }
  121.  
  122.  
  123. void createFaces() {
  124. int k = 0;
  125. for (auto i = 1; i < this->numberOfCircles; i++) {
  126. for (auto j = 0; j < this->numberOfPoints; j++) {
  127. int z = j + 1;
  128. if (j == this->numberOfPoints - 1) z = 0;
  129. this->faces[k].c1 = i; //first face
  130. this->faces[k].p1 = z;
  131. this->faces[k].c2 = i;
  132. this->faces[k].p2 = j;
  133. this->faces[k].c3 = i - 1;
  134. this->faces[k].p3 = j;
  135. k++;
  136.  
  137. this->faces[k].c1 = i - 1; //second face
  138. this->faces[k].p1 = j;
  139. this->faces[k].c2 = i - 1;
  140. this->faces[k].p2 = z;
  141. this->faces[k].c3 = i;
  142. this->faces[k].p3 = z;
  143. k++;
  144.  
  145. }
  146. }
  147. this->numberOfEdges = k;
  148. }
  149.  
  150. float getValue(float first, float last) {
  151. float middle = last + first / 2;
  152. float second = first + (2 * (middle - first)) / 3;
  153. float third = middle + (last - middle) / 3;
  154. return (1 - t)*(1 - t)*(1 - t)*first + 3 * t*(1 - t)*(1 - t)*second + 3 * t*t*(1 - t)*third + t * t*t*last;
  155. }
  156.  
  157. void twinningVertices() {
  158.  
  159. Cube cube = Cube();
  160. //std::cout << numberOfPoints << std::endl;
  161. for (auto i = 0; i <this->numberOfCircles ; i++) {
  162. for (auto j = 0; j< this->numberOfPoints; j++) {
  163. float delta_y = (0.25f / numberOfCircles)*i;
  164. int l;
  165. if ((int)numberOfPoints % 4== 0) {
  166. l = numberOfPoints / 4;
  167. }
  168. else l = numberOfPoints / 4 + 1;
  169. int sector = j / l;
  170. if (i > numberOfCircles / 2) {
  171. sector += 4;
  172. delta_y = -(delta_y * (numberOfCircles-i)/i);
  173. }
  174. int next = sector + 1;
  175. if (next == 8) next = 5;
  176. if (next == 4) next = 0;
  177.  
  178. float delta_x = (cube.vertices[next].x - cube.vertices[sector].x) / l;
  179. float delta_z = (cube.vertices[next].z - cube.vertices[sector].z) / l;
  180. this->vertices[i][j].x = getValue(this->vertices[i][j].x, cube.vertices[sector].x + (j% l)*delta_x);
  181. this->vertices[i][j].y = getValue(this->vertices[i][j].y, cube.vertices[sector].y + delta_y);
  182. this->vertices[i][j].z = getValue(this->vertices[i][j].z, cube.vertices[sector].z + (j % l)*delta_z);
  183. //std::cout << delta_x << " " << delta_y << " " << delta_z << std::endl;
  184. /*this->vertices[i][j].x = getValue(this->vertices[i][j].x, cube.vertices[sector].x );
  185. this->vertices[i][j].y = getValue(this->vertices[i][j].y, cube.vertices[sector].y );
  186. this->vertices[i][j].z = getValue(this->vertices[i][j].z, cube.vertices[sector].z );*/
  187. }
  188. }
  189. }
  190.  
  191. void texCoord() {
  192. for (auto i = 0; i < this->numberOfCircles; i++) {
  193. glBegin(GL_POLYGON);
  194. for (auto j = 0; j < this->numberOfPoints; j++) {
  195. glColor3f(this->vertices[i][j].r, this->vertices[i][j].g, this->vertices[i][j].b);
  196. glVertex3f(this->vertices[i][j].x, this->vertices[i][j].y, this->vertices[i][j].z);
  197. glNormal3f(-this->vertices[i][j].x, -this->vertices[i][j].y, -this->vertices[i][j].z);
  198. glTexCoord2f(-this->vertices[i][j].x, -this->vertices[i][j].y);
  199. }
  200. glEnd();
  201. }
  202.  
  203. }
  204.  
  205. void drawFaces() {
  206. glBegin(GL_TRIANGLES);
  207. for (auto k = 0; k < this->numberOfEdges; k++) {
  208. glColor3f(this->vertices[this->faces[k].c1][this->faces[k].p1].r, this->vertices[this->faces[k].c1][this->faces[k].p1].g, this->vertices[this->faces[k].c1][this->faces[k].p1].b);
  209. glNormal3f(-this->vertices[this->faces[k].c1][this->faces[k].p1].x, -this->vertices[this->faces[k].c1][this->faces[k].p1].y, -this->vertices[this->faces[k].c1][this->faces[k].p1].z);
  210. glTexCoord2f(this->vertices[this->faces[k].c1][this->faces[k].p1].x, this->vertices[this->faces[k].c1][this->faces[k].p1].y);
  211. glVertex3f(this->vertices[this->faces[k].c1][this->faces[k].p1].x, this->vertices[this->faces[k].c1][this->faces[k].p1].y, this->vertices[this->faces[k].c1][this->faces[k].p1].z);
  212.  
  213. glColor3f(this->vertices[this->faces[k].c2][this->faces[k].p2].r, this->vertices[this->faces[k].c2][this->faces[k].p2].g, this->vertices[this->faces[k].c2][this->faces[k].p2].b);
  214. glNormal3f(-this->vertices[this->faces[k].c2][this->faces[k].p2].x, -this->vertices[this->faces[k].c2][this->faces[k].p2].y, -this->vertices[this->faces[k].c2][this->faces[k].p2].z);
  215. glTexCoord2f(this->vertices[this->faces[k].c2][this->faces[k].p2].x, this->vertices[this->faces[k].c2][this->faces[k].p2].y);
  216. glVertex3f(this->vertices[this->faces[k].c2][this->faces[k].p2].x, this->vertices[this->faces[k].c2][this->faces[k].p2].y, this->vertices[this->faces[k].c2][this->faces[k].p2].z);
  217.  
  218. glColor3f(this->vertices[this->faces[k].c3][this->faces[k].p3].r, this->vertices[this->faces[k].c3][this->faces[k].p3].g, this->vertices[this->faces[k].c3][this->faces[k].p3].b);
  219. glNormal3f(-this->vertices[this->faces[k].c3][this->faces[k].p3].x, -this->vertices[this->faces[k].c3][this->faces[k].p3].y, -this->vertices[this->faces[k].c3][this->faces[k].p3].z);
  220. glTexCoord2f(this->vertices[this->faces[k].c3][this->faces[k].p3].x, this->vertices[this->faces[k].c3][this->faces[k].p3].y);
  221. glVertex3f(this->vertices[this->faces[k].c3][this->faces[k].p3].x, this->vertices[this->faces[k].c3][this->faces[k].p3].y, this->vertices[this->faces[k].c3][this->faces[k].p3].z);
  222. }
  223. glEnd();
  224. }
  225.  
  226.  
  227. void drawSphere() {
  228. createFaces();
  229. drawFaces();
  230. }
  231.  
  232. void animateSphere() {
  233. double coef = 0.009;
  234. if (animation) {
  235. if (increase)
  236. t += coef;
  237. else t -= coef;
  238. if (t >= 1 || t <= 0)
  239. increase = !increase;
  240. twinningVertices();
  241. }
  242. }
  243.  
  244.  
  245. };
  246.  
  247.  
  248.  
  249.  
  250. void display(GLFWwindow * window)
  251. {
  252.  
  253. int screenWidth, screenHeight;
  254. glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
  255.  
  256. float ratio = screenWidth / (float)screenHeight;
  257. glClearColor(0.3, 0.3, 0.3, 0.0);
  258.  
  259. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  260. glEnable(GL_DEPTH_TEST); // depth testing
  261. glDepthFunc(GL_LEQUAL);
  262.  
  263.  
  264. glViewport(0.0f, 0.0f, screenWidth, screenHeight);
  265.  
  266. glMatrixMode(GL_PROJECTION);
  267. glLoadIdentity();
  268. glOrtho(-ratio, ratio, -1.f, 1.f, 1.f * 3, -1.f * 3);// essentially set coordinate system
  269.  
  270. glMatrixMode(GL_MODELVIEW);
  271. glLoadIdentity();
  272.  
  273.  
  274. glScalef(1, 1, -1); //isometria
  275. glRotatef(35, 1, 0, 0);
  276. glRotatef(45, 0, 1, 0);
  277.  
  278.  
  279. glTranslatef(mX, mY, mZ);
  280. glRotatef(kX, 1, 0, 0);
  281. glRotatef(kY, 0, 1, 0);
  282. glRotatef(kZ, 0, 0, 1);
  283.  
  284. glScalef(k, k, k);
  285. Sphere newSphere = Sphere();
  286. newSphere.animateSphere();
  287. newSphere.drawSphere();
  288. glShadeModel(GL_SMOOTH);
  289. if (texturing) {
  290.  
  291. glEnable(GL_TEXTURE_2D);
  292. }
  293. else
  294. glDisable(GL_TEXTURE_2D);
  295. glPolygonMode(GL_FRONT_AND_BACK, mode);
  296.  
  297.  
  298. glfwSwapBuffers(window);
  299. glfwPollEvents();
  300. }
  301.  
  302. void draw(GLFWwindow * window) {
  303.  
  304. glEnable(GL_NORMALIZE);
  305. glEnable(GL_LIGHTING);
  306. GLfloat material_diffuse[] = { 0.0, 1.0, 0.0, 1.0 }; //цвет объекта
  307. glMaterialfv(GL_FRONT, GL_DIFFUSE, material_diffuse);
  308.  
  309. glEnable(GL_LIGHT0);
  310.  
  311. GLfloat specular[] = { 0.0, 0.0, 0.0, 1.0 };
  312. GLfloat diffuse[] = { 0.4, 0.7, 0.2 };
  313. GLfloat position[] = { -1.0, 0.0, 1.0, 1.0 };
  314. GLfloat direction[] = { 1.0, 0.0, -1.0 };
  315. glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  316. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  317. glLightfv(GL_LIGHT0, GL_POSITION, position);
  318. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction);
  319. glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.0); //убывание интенсивности с расстоянием
  320. glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.2);
  321. glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.4);
  322. glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45);
  323. glLightf(GL_LIGHT4, GL_SPOT_EXPONENT, 15.0); //интенсивность для прожектора
  324.  
  325. AUX_RGBImageRec *texture1;
  326. texture1 = auxDIBImageLoad("C:/Users/Alexis/Downloads/camomreal.bmp");
  327.  
  328. glGenTextures(1, &texture);
  329. glBindTexture(GL_TEXTURE_2D, texture);
  330. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  331. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  332. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  333. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  334. glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
  335. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  336.  
  337.  
  338. while (!glfwWindowShouldClose(window))
  339. {
  340. display(window);
  341.  
  342. }
  343. }
  344.  
  345. void error(int error, const char * description)
  346. {
  347. fputs(description, stderr);
  348. }
  349. void key(GLFWwindow * window, int key, int scancode, int action, int mods)
  350. {
  351. const GLfloat rotation = 5;
  352. const GLfloat movement = 0.05f;
  353. if (action == GLFW_PRESS || action == GLFW_REPEAT)
  354. {
  355. switch (key)
  356. {
  357. case GLFW_KEY_COMMA:
  358. if (animation) animation=false;
  359. else animation = true;
  360. break;
  361. case GLFW_KEY_LEFT_BRACKET:
  362. if (texturing) texturing = false;
  363. else texturing = true;
  364. break;
  365. case GLFW_KEY_Z:
  366. kZ -= rotation;
  367. break;
  368. case GLFW_KEY_X:
  369. kZ += rotation;
  370. break;
  371. case GLFW_KEY_UP:
  372. kX -= rotation;
  373. break;
  374. case GLFW_KEY_DOWN:
  375. kX += rotation;
  376. break;
  377. case GLFW_KEY_RIGHT:
  378. kY += rotation;
  379. break;
  380. case GLFW_KEY_LEFT:
  381. kY -= rotation;
  382. break;
  383. case GLFW_KEY_A:
  384. mX -= movement;
  385. break;
  386. case GLFW_KEY_D:
  387. mX += movement;
  388. break;
  389. case GLFW_KEY_W:
  390. mY += movement;
  391. break;
  392. case GLFW_KEY_S:
  393. mY -= movement;
  394. break;
  395. case GLFW_KEY_Q:
  396. mZ += movement;
  397. break;
  398. case GLFW_KEY_E:
  399. mZ -= movement;
  400. break;
  401. case GLFW_KEY_SPACE:
  402. k += 0.05;
  403. break;
  404. case GLFW_KEY_BACKSPACE:
  405. k -= 0.05;
  406. break;
  407. case GLFW_KEY_J:
  408. if (nOP != 1.f) {
  409. nOP--;
  410. //newSphere = Sphere();
  411. }
  412. break;
  413. case GLFW_KEY_K:
  414. nOP++;
  415. //newSphere = Sphere();
  416. break;
  417. case GLFW_KEY_U:
  418. if (nOC != 1.f) {
  419. nOC--;
  420. //newSphere = Sphere();
  421. }
  422. break;
  423. case GLFW_KEY_I:
  424. nOC++;
  425. //newSphere = Sphere();
  426. break;
  427. case GLFW_KEY_TAB:
  428. if (mode == GL_FILL) {
  429. mode = GL_LINE;
  430. }
  431. else mode = GL_FILL;
  432. break;
  433. }
  434. }
  435.  
  436. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  437. glfwSetWindowShouldClose(window, GL_TRUE);
  438. }
  439.  
  440. int main()
  441. {
  442. glfwSetErrorCallback(error);
  443.  
  444. if (!glfwInit()) return -1;
  445.  
  446. GLFWwindow * window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
  447. glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
  448.  
  449.  
  450. if (!window)
  451. {
  452. glfwTerminate();
  453. return -1;
  454. }
  455.  
  456. glfwMakeContextCurrent(window);
  457. glfwSetKeyCallback(window, key);
  458.  
  459.  
  460. draw(window);
  461.  
  462. glfwDestroyWindow(window);
  463. glfwTerminate();
  464.  
  465. return 0;
  466. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement