Advertisement
Ladies_Man

cg lab6 supposed to be animated

May 18th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.19 KB | None | 0 0
  1. // glfw_30.cpp : Defines the entry point for the console application.
  2. //  http://www.glfw.org/docs/latest/quick.html
  3.  
  4. #include "stdafx.h"
  5. #include <time.h>
  6. #define _USE_MATH_DEFINES
  7. #include <cmath>
  8. #include <GL/glew.h>
  9. #include <GLFW/glfw3.h>
  10. #include <vector>
  11.  
  12. #define SCREEN_WIDTH 1000
  13. #define SCREEN_HEIGHT 1000
  14. #define PC_MODE 16
  15. #define CONSOLE_MODE 0
  16.  
  17. #define MAXRAD 4
  18. #define MINRAD 1
  19. #define MAXHEIGHT 7
  20. #define MINHEIGHT 1
  21. #define MAXFREQ_X 40
  22. #define MINFREQ_X 3
  23. #define ANGLE 2
  24. #define TWINKIE_STEPS 1000
  25.  
  26. typedef struct {
  27.     float x, y, z;
  28. } coord;
  29.  
  30. typedef struct cone{
  31.     int type;
  32.     int top_rad, bot_rad, height;
  33.     int freq_x, freq_y;
  34.     int angle_x, angle_y, angle_z;
  35.     std::vector<std::vector<coord>> cone_top;
  36.     std::vector<std::vector<coord>> cone_bot;
  37.     std::vector<std::vector<coord>> cone_side;
  38. }prepare_ur_conus;
  39.  
  40.  
  41.  
  42. std::vector<std::vector<coord>> cone_top;
  43. std::vector<std::vector<coord>> cone_bot;
  44. std::vector<std::vector<coord>> cone_side;
  45.  
  46. std::vector<std::vector<coord>> cyl_top;
  47. std::vector<std::vector<coord>> cyl_bot;
  48. std::vector<std::vector<coord>> cyl_side;
  49.  
  50. std::vector<std::vector<coord>> final_top;
  51. std::vector<std::vector<coord>> final_bot;
  52. std::vector<std::vector<coord>> final_side;
  53.  
  54. int k = 1;
  55. bool twinkie_flag;
  56. int type, stop_i, stop_j, freq_x, freq_y;
  57. float t = 0, dt = 1 / TWINKIE_STEPS;
  58.  
  59. static float alpha, beta, gamma, a, b, c, h, r;
  60.  
  61. static void error_callback(int error, const char* description)
  62. {
  63.     fputs(description, stderr);
  64. }
  65.  
  66. void free_vect(std::vector<std::vector<coord>>& vect)
  67. {
  68.     for (int i = 0; i < freq_y + 1; i++) {
  69.         vect[i].clear();
  70.         vect[i].shrink_to_fit();
  71.     }
  72.     vect.clear();
  73.     vect.shrink_to_fit();
  74. }
  75.  
  76. void copy_regular_vertices_to_final_vector()
  77. {
  78.    
  79. }
  80.  
  81. void count_cylinder()
  82. {
  83.     int i = 0, j = 0, rad = r;
  84.     float angle = 0, dangle, height, dheight, drad;
  85.    
  86.  
  87.  
  88.     height = h;
  89.     dangle = 2 * M_PI / freq_x;
  90.     dheight = height / freq_y;
  91.     drad = rad / (float)freq_y;
  92.  
  93.     //count size of array
  94.     j = 0;
  95.     while (angle <= 2 * M_PI + dangle) {
  96.         angle += dangle;
  97.         j++;
  98.     }
  99.     stop_j = j;
  100.  
  101.  
  102.  
  103.  
  104.     cyl_top.resize(freq_y + 1);
  105.     for (i = 0; i < freq_y + 1; i++) cyl_top[i].resize(stop_j + 1);
  106.  
  107.     cyl_bot.resize(freq_y + 1);
  108.     for (i = 0; i < freq_y + 1; i++) cyl_bot[i].resize(stop_j + 1);
  109.  
  110.     cyl_side.resize(freq_y + 1);
  111.     for (i = 0; i < freq_y + 1; i++) cyl_side[i].resize(stop_j + 1);
  112.  
  113.  
  114.  
  115.  
  116.     for (i = 0; i < freq_y; i += 1) {
  117.         for (j = 0, angle = 0; angle <= 2 * M_PI + dangle; j += 1, angle += dangle) {
  118.             cyl_top[i][j].x = (0 + drad*i)*cos(angle);
  119.             cyl_top[i][j].y = height;
  120.             cyl_top[i][j].z = (0 + drad*i)*sin(angle);
  121.  
  122.             cyl_top[i][j + 1].x = (0 + drad*i)*cos(angle + dangle);
  123.             cyl_top[i][j + 1].y = height;
  124.             cyl_top[i][j + 1].z = (0 + drad*i)*sin(angle + dangle);
  125.  
  126.             cyl_top[i + 1][j + 1].x = (0 + drad*(i + 1))*cos(angle + dangle);
  127.             cyl_top[i + 1][j + 1].y = height;
  128.             cyl_top[i + 1][j + 1].z = (0 + drad*(i + 1))*sin(angle + dangle);
  129.  
  130.             cyl_top[i + 1][j].x = (0 + drad*(i + 1))*cos(angle);
  131.             cyl_top[i + 1][j].y = height;
  132.             cyl_top[i + 1][j].z = (0 + drad*(i + 1))*sin(angle);
  133.  
  134.  
  135.             cyl_bot[i][j].x = (0 + drad*i)*cos(angle);
  136.             cyl_bot[i][j].y = 0;
  137.             cyl_bot[i][j].z = (0 + drad*i)*sin(angle);
  138.  
  139.             cyl_bot[i][j + 1].x = (0 + drad*i)*cos(angle + dangle);
  140.             cyl_bot[i][j + 1].y = 0;
  141.             cyl_bot[i][j + 1].z = (0 + drad*i)*sin(angle + dangle);
  142.  
  143.             cyl_bot[i + 1][j + 1].x = (0 + drad*(i + 1))*cos(angle + dangle);
  144.             cyl_bot[i + 1][j + 1].y = 0;
  145.             cyl_bot[i + 1][j + 1].z = (0 + drad*(i + 1))*sin(angle + dangle);
  146.  
  147.             cyl_bot[i + 1][j].x = (0 + drad*(i + 1))*cos(angle);
  148.             cyl_bot[i + 1][j].y = 0;
  149.             cyl_bot[i + 1][j].z = (0 + drad*(i + 1))*sin(angle);
  150.  
  151.  
  152.             cyl_side[i][j].x = rad*cos(angle);
  153.             cyl_side[i][j].y = height - dheight*i;
  154.             cyl_side[i][j].z = rad*sin(angle);
  155.  
  156.             cyl_side[i][j + 1].x = rad*cos(angle + dangle);
  157.             cyl_side[i][j + 1].y = height - dheight*i;
  158.             cyl_side[i][j + 1].z = rad*sin(angle + dangle);
  159.  
  160.             cyl_side[i + 1][j + 1].x = rad*cos(angle + dangle);
  161.             cyl_side[i + 1][j + 1].y = height - dheight*(i + 1);
  162.             cyl_side[i + 1][j + 1].z = rad*sin(angle + dangle);
  163.  
  164.             cyl_side[i + 1][j].x = rad*cos(angle);
  165.             cyl_side[i + 1][j].y = height - dheight*(i + 1);
  166.             cyl_side[i + 1][j].z = rad*sin(angle);
  167.         }
  168.     }
  169. }
  170.  
  171. void count_cone()
  172. {
  173.     int i = 0, j = 0, top_rad = r, bot_rad = r * 2;
  174.     float angle = 0, dangle, height, dheight, drad, drad2;
  175.  
  176.     height = h;
  177.     dangle = 2 * M_PI / freq_x;
  178.     dheight = height / freq_y;
  179.     drad = (bot_rad - top_rad) / (float)freq_y;
  180.     drad2 = bot_rad / (float)freq_y;
  181.  
  182.  
  183.  
  184.     //count size of array
  185.     j = 0;
  186.     while (angle <= 2 * M_PI + dangle) {
  187.         angle += dangle;
  188.         j++;
  189.     }
  190.     stop_j = j;
  191.  
  192.  
  193.  
  194.     //allocate memory for top_ side_ vertices
  195.     cone_top.resize(freq_y + 1);
  196.     for (i = 0; i < freq_y + 1; i++) cone_top[i].resize(stop_j + 1);
  197.  
  198.     cone_bot.resize(freq_y + 1);
  199.     for (i = 0; i < freq_y + 1; i++) cone_bot[i].resize(stop_j + 1);
  200.  
  201.     cone_side.resize(freq_y + 1);
  202.     for (i = 0; i < freq_y + 1; i++) cone_side[i].resize(stop_j + 1);
  203.  
  204.  
  205.  
  206.     //count vert
  207.     for (i = 0; i < freq_y; i += 1) {
  208.         for (j = 0, angle = 0; angle <= 2 * M_PI + dangle; j += 1, angle += dangle) {
  209.             cone_top[i][j].x = (0 + drad*i)*cos(angle);
  210.             cone_top[i][j].y = height;
  211.             cone_top[i][j].z = (0 + drad*i)*sin(angle);
  212.  
  213.             cone_top[i][j + 1].x = (0 + drad*i)*cos(angle + dangle);
  214.             cone_top[i][j + 1].y = height;
  215.             cone_top[i][j + 1].z = (0 + drad*i)*sin(angle + dangle);
  216.  
  217.             cone_top[i + 1][j + 1].x = (0 + drad*(i + 1))*cos(angle + dangle);
  218.             cone_top[i + 1][j + 1].y = height;
  219.             cone_top[i + 1][j + 1].z = (0 + drad*(i + 1))*sin(angle + dangle);
  220.  
  221.             cone_top[i + 1][j].x = (0 + drad*(i + 1))*cos(angle);
  222.             cone_top[i + 1][j].y = height;
  223.             cone_top[i + 1][j].z = (0 + drad*(i + 1))*sin(angle);
  224.  
  225.  
  226.  
  227.             cone_bot[i][j].x = (0 + drad2*i)*cos(angle);
  228.             cone_bot[i][j].y = 0;
  229.             cone_bot[i][j].z = (0 + drad2*i)*sin(angle);
  230.  
  231.             cone_bot[i][j + 1].x = (0 + drad2*i)*cos(angle + dangle);
  232.             cone_bot[i][j + 1].y = 0;
  233.             cone_bot[i][j + 1].z = (0 + drad2*i)*sin(angle + dangle);
  234.  
  235.             cone_bot[i + 1][j + 1].x = (0 + drad2*(i + 1))*cos(angle + dangle);
  236.             cone_bot[i + 1][j + 1].y = 0;
  237.             cone_bot[i + 1][j + 1].z = (0 + drad2*(i + 1))*sin(angle + dangle);
  238.  
  239.             cone_bot[i + 1][j].x = (0 + drad2*(i + 1))*cos(angle);
  240.             cone_bot[i + 1][j].y = 0;
  241.             cone_bot[i + 1][j].z = (0 + drad2*(i + 1))*sin(angle);
  242.  
  243.  
  244.  
  245.             cone_side[i][j].x = (top_rad + drad*i)*cos(angle);
  246.             cone_side[i][j].y = height - dheight*i;
  247.             cone_side[i][j].z = (top_rad + drad*i)*sin(angle);
  248.  
  249.             cone_side[i][j + 1].x = (top_rad + drad*i)*cos(angle + dangle);
  250.             cone_side[i][j + 1].y = height - dheight*i;
  251.             cone_side[i][j + 1].z = (top_rad + drad*i)*sin(angle + dangle);
  252.  
  253.             cone_side[i + 1][j + 1].x = (top_rad + drad*(i + 1))*cos(angle + dangle);
  254.             cone_side[i + 1][j + 1].y = height - dheight*(i + 1);
  255.             cone_side[i + 1][j + 1].z = (top_rad + drad*(i + 1))*sin(angle + dangle);
  256.  
  257.             cone_side[i + 1][j].x = (top_rad + drad*(i + 1))*cos(angle);
  258.             cone_side[i + 1][j].y = height - dheight*(i + 1);
  259.             cone_side[i + 1][j].z = (top_rad + drad*(i + 1))*sin(angle);
  260.         }
  261.     }
  262.     stop_i = i;
  263.     stop_j = j;
  264.  
  265.     printf("coordinates renewed: h=%.2f, top_r=%d, fr_x=%d, fr_y=%d\n", height, top_rad, freq_x, freq_y);
  266. }
  267.  
  268. void draw_figure(
  269.     std::vector<std::vector<coord>>& top,
  270.     std::vector<std::vector<coord>>& bot,
  271.     std::vector<std::vector<coord>>& side
  272.     )
  273. {
  274.     int i, j;
  275.  
  276.     glRotatef(alpha, 1, 0, 0);
  277.     glRotatef(beta, 0, 1, 0);
  278.     glRotatef(gamma, 0, 0, 1);
  279.  
  280.     for (i = 0; i < stop_i; i += 1) {
  281.         for (j = 0; j < stop_j; j += 1) {
  282.             //top
  283.             glBegin(GL_POLYGON);
  284.             glColor3f(0.5, 0.5, 0.5);
  285.             glVertex3f(top[i][j].x, top[i][j].y, top[i][j].z);
  286.             glColor3f(0, 0, 0);
  287.             glVertex3f(top[i][j + 1].x, top[i][j + 1].y, top[i][j + 1].z);
  288.             glVertex3f(top[i + 1][j + 1].x, top[i + 1][j + 1].y, top[i + 1][j + 1].z);
  289.             glVertex3f(top[i + 1][j].x, top[i + 1][j].y, top[i + 1][j].z);
  290.             glEnd();
  291.  
  292.  
  293.             //bot
  294.             glBegin(GL_POLYGON);
  295.             glColor3f(0.3, 0.3, 0.3);
  296.             glVertex3f(bot[i][j].x, bot[i][j].y, bot[i][j].z);
  297.             glColor3f(0, 0, 0);
  298.             glVertex3f(bot[i][j + 1].x, bot[i][j + 1].y, bot[i][j + 1].z);
  299.             glVertex3f(bot[i + 1][j + 1].x, bot[i + 1][j + 1].y, bot[i + 1][j + 1].z);
  300.             glVertex3f(bot[i + 1][j].x, bot[i + 1][j].y, bot[i + 1][j].z);
  301.             glEnd();
  302.  
  303.  
  304.             //sidelines
  305.             glBegin(GL_POLYGON);
  306.             glColor3f(0, 0, 0);
  307.             glVertex3f(side[i][j].x, side[i][j].y, side[i][j].z);
  308.             glColor3f(0.8, 0, 0);
  309.             glVertex3f(side[i][j + 1].x, side[i][j + 1].y, side[i][j + 1].z);
  310.             glColor3f(0, 0.8, 0);
  311.             glVertex3f(side[i + 1][j + 1].x, side[i + 1][j + 1].y, side[i + 1][j + 1].z);
  312.             glColor3f(0, 0, 0.8);
  313.             glVertex3f(side[i + 1][j].x, side[i + 1][j].y, side[i + 1][j].z);
  314.             glEnd();
  315.         }
  316.     }
  317. }
  318.  
  319. void func_1()
  320. {
  321.     int i, j;
  322.  
  323.     if (t > 1) t = 1;
  324.  
  325.  
  326.  
  327.     std::vector<std::vector<coord>> gap_top;
  328.     std::vector<std::vector<coord>> gap_bot;
  329.     std::vector<std::vector<coord>> gap_side;
  330.  
  331.  
  332.  
  333.     gap_top.resize(stop_i);
  334.     for (i = 0; i < stop_j; i++) gap_top[i].resize(stop_j);
  335.  
  336.     gap_bot.resize(stop_i);
  337.     for (i = 0; i < stop_j; i++) gap_bot[i].resize(stop_j);
  338.  
  339.     gap_side.resize(stop_i);
  340.     for (i = 0; i < stop_j; i++) gap_side[i].resize(stop_j);
  341.  
  342.  
  343.  
  344.     for (i = 0; i < stop_i; i += 1) {
  345.         for (j = 0; j < stop_j; j += 1) {
  346.             gap_top[i][j].x = 3 * 0.5*(cyl_top[i][j].x - cone_top[i][j].x) + cone_top[i][j].x;
  347.             gap_top[i][j].y = 0.5*(cyl_top[i][j].y - cone_top[i][j].y) + cone_top[i][j].y;
  348.             gap_top[i][j].z = cone_top[i][j].z / 2;
  349.  
  350.             gap_bot[i][j].x = 3 * 0.5*(cyl_top[i][j].x - cone_top[i][j].x) + cone_top[i][j].x;
  351.             gap_bot[i][j].y = 0.5*(cyl_top[i][j].y - cone_top[i][j].y) + cone_top[i][j].y;
  352.             gap_bot[i][j].z = cone_top[i][j].z / 2;
  353.  
  354.             gap_side[i][j].x = 3 * 0.5*(cyl_top[i][j].x - cone_top[i][j].x) + cone_top[i][j].x;
  355.             gap_side[i][j].y = 0.5*(cyl_top[i][j].y - cone_top[i][j].y) + cone_top[i][j].y;
  356.             gap_side[i][j].z = cone_top[i][j].z / 2;
  357.         }
  358.     }
  359.  
  360.     //Bezier cube:
  361.     //(1-t)^3*p0 + 3t(1-t)^2*p1 + 3*t^2*(1-t)*p2 + t^3*p3
  362.  
  363.     for (i = 0; i < stop_i; i += 1) {
  364.         for (j = 0; j < stop_j; j += 1) {
  365.             final_top[i][j].x = cone_top[i][j].x*(t - 1)*(t - 1) + gap_top[i][j].x*t*(t - 1) + cyl_top[i][j].x*t*t;
  366.             final_top[i][j].y = cone_top[i][j].y*(t - 1)*(t - 1) + gap_top[i][j].y*t*(t - 1) + cyl_top[i][j].y*t*t;
  367.             final_top[i][j].z = cone_top[i][j].z*(t - 1)*(t - 1) + gap_top[i][j].z*t*(t - 1) + cyl_top[i][j].z*t*t;
  368.  
  369.             final_bot[i][j].x = cone_bot[i][j].x*(t - 1)*(t - 1) + gap_bot[i][j].x*t*(t - 1) + cyl_bot[i][j].x*t*t;
  370.             final_bot[i][j].y = cone_bot[i][j].y*(t - 1)*(t - 1) + gap_bot[i][j].y*t*(t - 1) + cyl_bot[i][j].y*t*t;
  371.             final_bot[i][j].z = cone_bot[i][j].z*(t - 1)*(t - 1) + gap_bot[i][j].z*t*(t - 1) + cyl_bot[i][j].z*t*t;
  372.  
  373.             final_side[i][j].x = cone_side[i][j].x*(t - 1)*(t - 1) + gap_side[i][j].x*t*(t - 1) + cyl_side[i][j].x*t*t;
  374.             final_side[i][j].y = cone_side[i][j].y*(t - 1)*(t - 1) + gap_side[i][j].y*t*(t - 1) + cyl_side[i][j].y*t*t;
  375.             final_side[i][j].z = cone_side[i][j].z*(t - 1)*(t - 1) + gap_side[i][j].z*t*(t - 1) + cyl_side[i][j].z*t*t;
  376.         }
  377.     }
  378.  
  379.  
  380.     if (t == 1) {
  381.         k = 0;
  382.         twinkie_flag = false;
  383.     }
  384.     t += dt;
  385.  
  386.     free_vect(gap_top);
  387.     free_vect(gap_bot);
  388.     free_vect(gap_side);
  389. }
  390.  
  391. void func_2()
  392. {
  393.     int i, j;
  394.  
  395.     if (t < 0) t = 0;
  396.  
  397.  
  398.  
  399.     std::vector<std::vector<coord>> gap_top;
  400.     std::vector<std::vector<coord>> gap_bot;
  401.     std::vector<std::vector<coord>> gap_side;
  402.  
  403.  
  404.  
  405.     gap_top.resize(stop_i);
  406.     for (i = 0; i < stop_j; i++) gap_top[i].resize(stop_j);
  407.  
  408.     gap_bot.resize(stop_i);
  409.     for (i = 0; i < stop_j; i++) gap_bot[i].resize(stop_j);
  410.  
  411.     gap_side.resize(stop_i);
  412.     for (i = 0; i < stop_j; i++) gap_side[i].resize(stop_j);
  413.  
  414.  
  415.  
  416.     for (i = 0; i < stop_i; i += 1) {
  417.         for (j = 0; j < stop_j; j += 1) {
  418.             gap_top[i][j].x = 0.5*(cyl_top[i][j].x - cone_top[i][j].x) + cone_top[i][j].x;
  419.             gap_top[i][j].y = 0.5*(cyl_top[i][j].y - cone_top[i][j].y) + cone_top[i][j].y;
  420.             gap_top[i][j].z = cone_top[i][j].z;
  421.  
  422.             gap_bot[i][j].x = 0.5*(cyl_top[i][j].x - cone_top[i][j].x) + cone_top[i][j].x;
  423.             gap_bot[i][j].y = 0.5*(cyl_top[i][j].y - cone_top[i][j].y) + cone_top[i][j].y;
  424.             gap_bot[i][j].z = cone_top[i][j].z;
  425.  
  426.             gap_side[i][j].x = 0.5*(cyl_top[i][j].x - cone_top[i][j].x) + cone_top[i][j].x;
  427.             gap_side[i][j].y = 0.5*(cyl_top[i][j].y - cone_top[i][j].y) + cone_top[i][j].y;
  428.             gap_side[i][j].z = cone_top[i][j].z;
  429.         }
  430.     }
  431.  
  432.     //Bezier cube:
  433.     //(1-t)^3*p0 + 3t(1-t)^2*p1 + 3*t^2*(1-t)*p2 + t^3*p3
  434.  
  435.     for (i = 0; i < stop_i; i += 1) {
  436.         for (j = 0; j < stop_j; j += 1) {
  437.             final_top[i][j].x = cone_top[i][j].x*(t - 1)*(t - 1) + gap_top[i][j].x*t*(t - 1) + cyl_top[i][j].x*t*t;
  438.             final_top[i][j].y = cone_top[i][j].y*(t - 1)*(t - 1) + gap_top[i][j].y*t*(t - 1) + cyl_top[i][j].y*t*t;
  439.             final_top[i][j].z = cone_top[i][j].z*(t - 1)*(t - 1) + gap_top[i][j].z*t*(t - 1) + cyl_top[i][j].z*t*t;
  440.  
  441.             final_bot[i][j].x = cone_bot[i][j].x*(t - 1)*(t - 1) + gap_bot[i][j].x*t*(t - 1) + cyl_bot[i][j].x*t*t;
  442.             final_bot[i][j].y = cone_bot[i][j].y*(t - 1)*(t - 1) + gap_bot[i][j].y*t*(t - 1) + cyl_bot[i][j].y*t*t;
  443.             final_bot[i][j].z = cone_bot[i][j].z*(t - 1)*(t - 1) + gap_bot[i][j].z*t*(t - 1) + cyl_bot[i][j].z*t*t;
  444.  
  445.             final_side[i][j].x = cone_side[i][j].x*(t - 1)*(t - 1) + gap_side[i][j].x*t*(t - 1) + cyl_side[i][j].x*t*t;
  446.             final_side[i][j].y = cone_side[i][j].y*(t - 1)*(t - 1) + gap_side[i][j].y*t*(t - 1) + cyl_side[i][j].y*t*t;
  447.             final_side[i][j].z = cone_side[i][j].z*(t - 1)*(t - 1) + gap_side[i][j].z*t*(t - 1) + cyl_side[i][j].z*t*t;
  448.         }
  449.     }
  450.  
  451.  
  452.     if (t == 0) {
  453.         k = 1;
  454.         twinkie_flag = false;
  455.     }
  456.  
  457.     t -= dt;
  458.  
  459.     free_vect(gap_top);
  460.     free_vect(gap_bot);
  461.     free_vect(gap_side);
  462. }
  463.  
  464. void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
  465. {
  466.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  467.         glfwSetWindowShouldClose(window, GL_TRUE);
  468.  
  469.     switch (key) {
  470.     case (GLFW_KEY_SPACE) :
  471.         if (action == GLFW_PRESS) {
  472.         alpha = 0;
  473.         beta = 0;
  474.         gamma = 0;
  475.         h = 2;
  476.         r = 1;
  477.         freq_x = 3;
  478.         freq_y = 3;
  479.         count_cone();
  480.         count_cylinder();
  481.         }
  482.                           break;
  483.     case (GLFW_KEY_UP) :
  484.         if (action == GLFW_PRESS) {
  485.         if (h <= MAXHEIGHT) {
  486.             h += 2;
  487.             freq_y++;
  488.             count_cone();
  489.             count_cylinder();
  490.         }
  491.         }
  492.                        break;
  493.     case (GLFW_KEY_DOWN) :
  494.         if (action == GLFW_PRESS) {
  495.         if (h > MINHEIGHT) {
  496.             h -= 2;
  497.             freq_y--;
  498.             count_cone();
  499.             count_cylinder();
  500.         }
  501.         }
  502.                          break;
  503.     case (GLFW_KEY_RIGHT) :
  504.         if (action == GLFW_PRESS) {
  505.         if (r < MAXRAD) {
  506.             r++;
  507.             count_cone();
  508.             count_cylinder();
  509.         }
  510.         }
  511.                           break;
  512.     case (GLFW_KEY_LEFT) :
  513.         if (action == GLFW_PRESS) {
  514.         if (r > MINRAD) {
  515.             r--;
  516.             count_cone();
  517.             count_cylinder();
  518.         }
  519.         }
  520.                          break;
  521.     case (GLFW_KEY_R) :
  522.         if (action == GLFW_PRESS) {
  523.         if (freq_x <= MAXFREQ_X) {
  524.             freq_x++;
  525.             count_cone();
  526.             count_cylinder();
  527.         }
  528.         }
  529.                       break;
  530.     case (GLFW_KEY_F) :
  531.         if (action == GLFW_PRESS) {
  532.         if (freq_x > MINFREQ_X) {
  533.             freq_x--;
  534.             count_cone();
  535.             count_cylinder();
  536.         }
  537.         }
  538.                       break;
  539.     case (GLFW_KEY_V) :
  540.         if (action == GLFW_PRESS) {
  541.             type = -type;
  542.             if (type == 1) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  543.             if (type == -1) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  544.         }
  545.         break;
  546.  
  547.     case (GLFW_KEY_T) :
  548.         if (action == GLFW_PRESS) {
  549.             twinkie_flag = !twinkie_flag;
  550.         }
  551.         break;
  552.  
  553.     }
  554.  
  555. }
  556.  
  557. GLfloat cabinet_view_matrix[] = {
  558.     1, 0, 0, 0,
  559.     0, 1, 0, 0,
  560.     -0.5*cos(M_PI / 6), -0.5*sin(M_PI / 6), -1, 0,
  561.     0, 0, 0, 1
  562. };
  563.  
  564. void display(GLFWwindow* window)
  565. {
  566.     twinkie_flag = false;
  567.  
  568.     t = 0;
  569.     freq_x = 3;
  570.     freq_y = 3;
  571.     type = -1;
  572.     h = 3;
  573.     r = 1;
  574.     alpha = 0;
  575.     beta = 0;
  576.     gamma = 0;
  577.  
  578.     count_cone();
  579.     count_cylinder();
  580.  
  581.     copy_regular_vertices_to_final_vector();
  582.  
  583.     while (!glfwWindowShouldClose(window))
  584.     {
  585.         glClearColor(0.4, 0.4, 0.4, 1.0);
  586.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  587.  
  588.         if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) alpha -= ANGLE;
  589.         if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) alpha += ANGLE;
  590.         if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) beta -= ANGLE;
  591.         if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) beta += ANGLE;
  592.         if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) gamma -= ANGLE;
  593.         if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) gamma += ANGLE;
  594.  
  595.         glMatrixMode(GL_PROJECTION);
  596.         glLoadIdentity();
  597.         glLoadMatrixf(cabinet_view_matrix);
  598.  
  599.         glOrtho(-10, 10, -10, 10, 10, -10);
  600.  
  601.         glMatrixMode(GL_MODELVIEW);
  602.         glLoadIdentity();
  603.  
  604.         //draw_net();
  605.         //draw_axis(-100, 100, -3, 100, -1, 100);
  606.  
  607.         if (twinkie_flag) {
  608.             if (k) func_1();
  609.             else func_2();
  610.  
  611.             draw_figure(final_top, final_bot, final_side);
  612.         }
  613.         else {
  614.             draw_figure(cone_top, cone_bot, cone_side);
  615.         }
  616.         //copy_regular_vertices_to_final_vector();
  617.        
  618.  
  619.         //draw_figure(cyl_top, cyl_bot, cyl_side);
  620.  
  621.  
  622.  
  623.         glfwSwapBuffers(window);
  624.         //glfwWaitEvents();
  625.         glfwPollEvents();
  626.     }
  627.  
  628.     free_vect(cone_top);
  629.     free_vect(cone_bot);
  630.     free_vect(cone_side);
  631.  
  632. }
  633.  
  634. int main(int argc, char** argv)
  635. {
  636.     // initialise GLFW
  637.     if (!glfwInit())
  638.     {
  639.         printf("glfwInit failed\n");
  640.         return -1;
  641.     }
  642.     glfwWindowHint(GLFW_SAMPLES, PC_MODE);
  643.     glfwSetErrorCallback(error_callback);
  644.  
  645.     //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  646.     //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  647.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  648.     GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test app", NULL, NULL);
  649.  
  650.     if (window == NULL)
  651.     {
  652.         printf("glfwOpenWindow failed.\n");
  653.         glfwTerminate();
  654.         return -2;
  655.     }
  656.  
  657.     int attrib;
  658.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  659.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  660.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  661.  
  662.     glfwMakeContextCurrent(window);
  663.     glfwSetKeyCallback(window, controls);
  664.  
  665.     //glDepthFunc(GL_LEQUAL);
  666.     glEnable(GL_DEPTH_TEST);
  667.  
  668.     GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  669.     GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 0.7f };
  670.  
  671.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
  672.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
  673.  
  674.     glEnable(GL_LIGHT0);
  675.  
  676.     GLfloat lightPos[] = { -50.0f, 50.0f, 100.0f, 1.0f };
  677.     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  678.     //glLightModelf(GL_LIGHT_MODEL_AMBIENT, ambientLight);
  679.    
  680.     if (NULL != window)
  681.     {
  682.         display(window);
  683.     }
  684.     glfwDestroyWindow(window);
  685.     glfwTerminate();
  686.     return 0;
  687. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement