Advertisement
Ladies_Man

cg lab6 (lab2 + vector for bot vertices)

May 17th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.05 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.  
  24. #define ANGLE 2
  25.  
  26. typedef struct {
  27.     float x, y, z;
  28. } coord;
  29.  
  30. std::vector<std::vector<coord>> cone_coord_top;
  31. std::vector<std::vector<coord>> cone_coord_bot;
  32. std::vector<std::vector<coord>> cone_coord_side;
  33.  
  34. std::vector<std::vector<coord>> cyl_coord_top;
  35. std::vector<std::vector<coord>> cyl_coord_bot;
  36. std::vector<std::vector<coord>> cyl_coord_side;
  37.  
  38. int type, stop_i, stop_j, freq_x, freq_y;
  39. static float alpha, beta, gamma, a, b, c, h, r;
  40.  
  41. static void error_callback(int error, const char* description)
  42. {
  43.     fputs(description, stderr);
  44. }
  45.  
  46. void free_vect(std::vector<std::vector<coord>>& vect)
  47. {
  48.     for (int i = 0; i < freq_y + 1; i++) {
  49.         vect[i].clear();
  50.         vect[i].shrink_to_fit();
  51.     }
  52.     vect.clear();
  53.     vect.shrink_to_fit();
  54. }
  55. /*
  56. void count_cylinder()
  57. {
  58.     int i = 0, j = 0, top_rad = r, bot_rad = r;
  59.     float angle = 0, dangle, height, dheight, drad;
  60.  
  61.     height = h;
  62.     dangle = 2 * M_PI / freq_x;
  63.     dheight = height / freq_y;
  64.  
  65.     //count size of array
  66.     while (angle <= 2 * M_PI + dangle) {
  67.         angle += dangle;
  68.         j++;
  69.     }
  70.     stop_j = j;
  71.  
  72.     cyl_coord_top.resize(freq_y + 1);
  73.     for (i = 0; i < freq_y + 1; i++) cyl_coord_top[i].resize(stop_j + 1);
  74.  
  75.     cyl_coord_side.resize(freq_y + 1);
  76.     for (i = 0; i < freq_y + 1; i++) cyl_coord_side[i].resize(stop_j + 1);
  77.  
  78.     for (i = 0; i < freq_y; i += 1) {
  79.         for (j = 0, angle = 0; angle <= 2 * M_PI + dangle; j += 1, angle += dangle) {
  80.             cyl_coord_top[i][j].x = (0 + drad*i)*cos(angle);
  81.             cyl_coord_top[i][j].z = (0 + drad*i)*sin(angle);
  82.  
  83.             cyl_coord_top[i][j + 1].x = (0 + drad*i)*cos(angle + dangle);
  84.             cyl_coord_top[i][j + 1].z = (0 + drad*i)*sin(angle + dangle);
  85.  
  86.             cyl_coord_top[i + 1][j + 1].x = (0 + drad*(i + 1))*cos(angle + dangle);
  87.             cyl_coord_top[i + 1][j + 1].z = (0 + drad*(i + 1))*sin(angle + dangle);
  88.  
  89.             cyl_coord_top[i + 1][j].x = (0 + drad*(i + 1))*cos(angle);
  90.             cyl_coord_top[i + 1][j].z = (0 + drad*(i + 1))*sin(angle);
  91.  
  92.  
  93.             cyl_coord_bot[i][j].x = (0 + drad*i)*cos(angle);
  94.             cyl_coord_bot[i][j].z = (0 + drad*i)*sin(angle);
  95.  
  96.             cyl_coord_bot[i][j + 1].x = (0 + drad*i)*cos(angle + dangle);
  97.             cyl_coord_bot[i][j + 1].z = (0 + drad*i)*sin(angle + dangle);
  98.  
  99.             cyl_coord_bot[i + 1][j + 1].x = (0 + drad*(i + 1))*cos(angle + dangle);
  100.             cyl_coord_bot[i + 1][j + 1].z = (0 + drad*(i + 1))*sin(angle + dangle);
  101.  
  102.             cyl_coord_bot[i + 1][j].x = (0 + drad*(i + 1))*cos(angle);
  103.             cyl_coord_bot[i + 1][j].z = (0 + drad*(i + 1))*sin(angle);
  104.  
  105.  
  106.             cyl_coord_side[i][j].x = (top_rad + drad*i)*cos(angle);
  107.             cyl_coord_side[i][j].y = height - dheight*i;
  108.             cyl_coord_side[i][j].z = (top_rad + drad*i)*sin(angle);
  109.  
  110.             cyl_coord_side[i][j + 1].x = (top_rad + drad*i)*cos(angle + dangle);
  111.             cyl_coord_side[i][j + 1].y = height - dheight*i;
  112.             cyl_coord_side[i][j + 1].z = (top_rad + drad*i)*sin(angle + dangle);
  113.  
  114.             cyl_coord_side[i + 1][j + 1].x = (top_rad + drad*(i + 1))*cos(angle + dangle);
  115.             cyl_coord_side[i + 1][j + 1].y = height - dheight*(i + 1);
  116.             cyl_coord_side[i + 1][j + 1].z = (top_rad + drad*(i + 1))*sin(angle + dangle);
  117.  
  118.             cyl_coord_side[i + 1][j].x = (top_rad + drad*(i + 1))*cos(angle);
  119.             cyl_coord_side[i + 1][j].y = height - dheight*(i + 1);
  120.             cyl_coord_side[i + 1][j].z = (top_rad + drad*(i + 1))*sin(angle);
  121.         }
  122.     }
  123. }
  124. */
  125. void count_cone()
  126. {
  127.     int i = 0, j = 0, top_rad = r, bot_rad = r * 2;
  128.     float angle = 0, dangle, height, dheight, drad, drad2;
  129.  
  130.     height = h;
  131.     dangle = 2 * M_PI / freq_x;
  132.     dheight = height / freq_y;
  133.     drad = (bot_rad - top_rad) / (float)freq_y;
  134.     drad2 = bot_rad / (float)freq_y;
  135.  
  136.  
  137.  
  138.     //count size of array
  139.     while (angle <= 2 * M_PI + dangle) {
  140.         angle += dangle;
  141.         j++;
  142.     }
  143.     stop_j = j;
  144.  
  145.  
  146.  
  147.     //allocate memory for top_ side_ vertices
  148.     cone_coord_top.resize(freq_y + 1);
  149.     for (i = 0; i < freq_y + 1; i++) cone_coord_top[i].resize(stop_j + 1);
  150.  
  151.     cone_coord_bot.resize(freq_y + 1);
  152.     for (i = 0; i < freq_y + 1; i++) cone_coord_bot[i].resize(stop_j + 1);
  153.  
  154.     cone_coord_side.resize(freq_y + 1);
  155.     for (i = 0; i < freq_y + 1; i++) cone_coord_side[i].resize(stop_j + 1);
  156.  
  157.  
  158.  
  159.     //count vert
  160.     for (i = 0; i < freq_y; i += 1) {
  161.         for (j = 0, angle = 0; angle <= 2 * M_PI + dangle; j += 1, angle += dangle) {
  162.             cone_coord_top[i][j].x = (0 + drad*i)*cos(angle);
  163.             cone_coord_top[i][j].y = height;
  164.             cone_coord_top[i][j].z = (0 + drad*i)*sin(angle);
  165.  
  166.             cone_coord_top[i][j + 1].x = (0 + drad*i)*cos(angle + dangle);
  167.             cone_coord_top[i][j + 1].y = height;
  168.             cone_coord_top[i][j + 1].z = (0 + drad*i)*sin(angle + dangle);
  169.  
  170.             cone_coord_top[i + 1][j + 1].x = (0 + drad*(i + 1))*cos(angle + dangle);
  171.             cone_coord_top[i + 1][j + 1].y = height;
  172.             cone_coord_top[i + 1][j + 1].z = (0 + drad*(i + 1))*sin(angle + dangle);
  173.  
  174.             cone_coord_top[i + 1][j].x = (0 + drad*(i + 1))*cos(angle);
  175.             cone_coord_top[i + 1][j].y = height;
  176.             cone_coord_top[i + 1][j].z = (0 + drad*(i + 1))*sin(angle);
  177.  
  178.  
  179.  
  180.             cone_coord_bot[i][j].x = (0 + drad2*i)*cos(angle);
  181.             cone_coord_bot[i][j].y = 0;
  182.             cone_coord_bot[i][j].z = (0 + drad2*i)*sin(angle);
  183.  
  184.             cone_coord_bot[i][j + 1].x = (0 + drad2*i)*cos(angle + dangle);
  185.             cone_coord_bot[i][j + 1].y = 0;
  186.             cone_coord_bot[i][j + 1].z = (0 + drad2*i)*sin(angle + dangle);
  187.  
  188.             cone_coord_bot[i + 1][j + 1].x = (0 + drad2*(i + 1))*cos(angle + dangle);
  189.             cone_coord_bot[i + 1][j + 1].y = 0;
  190.             cone_coord_bot[i + 1][j + 1].z = (0 + drad2*(i + 1))*sin(angle + dangle);
  191.  
  192.             cone_coord_bot[i + 1][j].x = (0 + drad2*(i + 1))*cos(angle);
  193.             cone_coord_bot[i + 1][j].y = 0;
  194.             cone_coord_bot[i + 1][j].z = (0 + drad2*(i + 1))*sin(angle);
  195.  
  196.  
  197.  
  198.             cone_coord_side[i][j].x = (top_rad + drad*i)*cos(angle);
  199.             cone_coord_side[i][j].y = height - dheight*i;
  200.             cone_coord_side[i][j].z = (top_rad + drad*i)*sin(angle);
  201.  
  202.             cone_coord_side[i][j + 1].x = (top_rad + drad*i)*cos(angle + dangle);
  203.             cone_coord_side[i][j + 1].y = height - dheight*i;
  204.             cone_coord_side[i][j + 1].z = (top_rad + drad*i)*sin(angle + dangle);
  205.  
  206.             cone_coord_side[i + 1][j + 1].x = (top_rad + drad*(i + 1))*cos(angle + dangle);
  207.             cone_coord_side[i + 1][j + 1].y = height - dheight*(i + 1);
  208.             cone_coord_side[i + 1][j + 1].z = (top_rad + drad*(i + 1))*sin(angle + dangle);
  209.  
  210.             cone_coord_side[i + 1][j].x = (top_rad + drad*(i + 1))*cos(angle);
  211.             cone_coord_side[i + 1][j].y = height - dheight*(i + 1);
  212.             cone_coord_side[i + 1][j].z = (top_rad + drad*(i + 1))*sin(angle);
  213.         }
  214.     }
  215.     stop_i = i;
  216.     stop_j = j;
  217.  
  218.     printf("coordinates renewed: h=%.2f, top_r=%d, fr_x=%d, fr_y=%d\n", height, top_rad, freq_x, freq_y);
  219. }
  220.  
  221. void draw_cone(
  222.     int type,
  223.     int top_rad, int bot_rad,
  224.     int height,
  225.     int freq_x, int freq_y,
  226.     int angle_x, int angle_y, int angle_z
  227.     )
  228. {
  229.     int i, j;
  230.  
  231.     glRotatef(angle_x, 1, 0, 0);
  232.     glRotatef(angle_y, 0, 1, 0);
  233.     glRotatef(angle_z, 0, 0, 1);
  234.  
  235.     for (i = 0; i < stop_i; i += 1) {
  236.         for (j = 0; j < stop_j; j += 1) {
  237.             //top
  238.             type > 0 ? glBegin(GL_POLYGON) : glBegin(GL_LINE_LOOP);
  239.             glColor3f(0.5, 0.5, 0.5);
  240.             glVertex3f(cone_coord_top[i][j].x, cone_coord_top[i][j].y, cone_coord_top[i][j].z);
  241.             glColor3f(0, 0, 0);
  242.             glVertex3f(cone_coord_top[i][j + 1].x, cone_coord_top[i][j + 1].y, cone_coord_top[i][j + 1].z);
  243.             glVertex3f(cone_coord_top[i + 1][j + 1].x, cone_coord_top[i + 1][j + 1].y, cone_coord_top[i + 1][j + 1].z);
  244.             glVertex3f(cone_coord_top[i + 1][j].x, cone_coord_top[i + 1][j].y, cone_coord_top[i + 1][j].z);
  245.             glEnd();
  246.  
  247.  
  248.             //bot
  249.             type > 0 ? glBegin(GL_POLYGON) : glBegin(GL_LINE_LOOP);
  250.             glColor3f(0.3, 0.3, 0.3);
  251.             glVertex3f(cone_coord_bot[i][j].x, cone_coord_bot[i][j].y, cone_coord_bot[i][j].z);
  252.             glColor3f(0, 0, 0);
  253.             glVertex3f(cone_coord_bot[i][j + 1].x, cone_coord_bot[i][j + 1].y, cone_coord_bot[i][j + 1].z);
  254.             glVertex3f(cone_coord_bot[i + 1][j + 1].x, cone_coord_bot[i + 1][j + 1].y, cone_coord_bot[i + 1][j + 1].z);
  255.             glVertex3f(cone_coord_bot[i + 1][j].x, cone_coord_bot[i + 1][j].y, cone_coord_bot[i + 1][j].z);
  256.             glEnd();
  257.  
  258.  
  259.             //sidelines
  260.             type > 0 ? glBegin(GL_POLYGON) : glBegin(GL_LINE_LOOP);
  261.             glColor3f(0, 0, 0);
  262.             glVertex3f(cone_coord_side[i][j].x, cone_coord_side[i][j].y, cone_coord_side[i][j].z);
  263.             glColor3f(0.8, 0, 0);
  264.             glVertex3f(cone_coord_side[i][j + 1].x, cone_coord_side[i][j + 1].y, cone_coord_side[i][j + 1].z);
  265.             glColor3f(0, 0.8, 0);
  266.             glVertex3f(cone_coord_side[i + 1][j + 1].x, cone_coord_side[i + 1][j + 1].y, cone_coord_side[i + 1][j + 1].z);
  267.             glColor3f(0, 0, 0.8);
  268.             glVertex3f(cone_coord_side[i + 1][j].x, cone_coord_side[i + 1][j].y, cone_coord_side[i + 1][j].z);
  269.             glEnd();
  270.         }
  271.     }
  272. }
  273.  
  274. void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
  275. {
  276.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  277.         glfwSetWindowShouldClose(window, GL_TRUE);
  278.  
  279.     switch (key) {
  280.     case (GLFW_KEY_SPACE) :
  281.         if (action == GLFW_PRESS) {
  282.         alpha = 0;
  283.         beta = 0;
  284.         gamma = 0;
  285.         h = 2;
  286.         r = 1;
  287.         freq_x = 3;
  288.         freq_y = 3;
  289.         count_cone();
  290.         }
  291.                           break;
  292.     case (GLFW_KEY_UP) :
  293.         if (action == GLFW_PRESS) {
  294.         if (h <= MAXHEIGHT) {
  295.             h += 2;
  296.             freq_y++;
  297.             count_cone();
  298.         }
  299.         }
  300.                        break;
  301.     case (GLFW_KEY_DOWN) :
  302.         if (action == GLFW_PRESS) {
  303.         if (h > MINHEIGHT) {
  304.             h -= 2;
  305.             freq_y--;
  306.             count_cone();
  307.         }
  308.         }
  309.                          break;
  310.     case (GLFW_KEY_RIGHT) :
  311.         if (action == GLFW_PRESS) {
  312.         if (r < MAXRAD) {
  313.             r++;
  314.             count_cone();
  315.         }
  316.         }
  317.                           break;
  318.     case (GLFW_KEY_LEFT) :
  319.         if (action == GLFW_PRESS) {
  320.         if (r > MINRAD) {
  321.             r--;
  322.             count_cone();
  323.         }
  324.         }
  325.                          break;
  326.     case (GLFW_KEY_R) :
  327.         if (action == GLFW_PRESS) {
  328.         if (freq_x <= MAXFREQ_X) {
  329.             freq_x++;
  330.             count_cone();
  331.         }
  332.         }
  333.                       break;
  334.     case (GLFW_KEY_F) :
  335.         if (action == GLFW_PRESS) {
  336.         if (freq_x > MINFREQ_X) {
  337.             freq_x--;
  338.             count_cone();
  339.         }
  340.         }
  341.                       break;
  342.     case (GLFW_KEY_V) :
  343.         if (action == GLFW_PRESS) {
  344.         type = -type;
  345.         if (type == 1) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  346.         if (type == -1) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  347.         }
  348.                       break;
  349.     }
  350.  
  351. }
  352.  
  353. void display(GLFWwindow* window)
  354. {
  355.     int i;
  356.  
  357.     freq_x = 3;
  358.     freq_y = 3;
  359.     type = -1;
  360.     h = 3;
  361.     r = 1;
  362.     alpha = 0;
  363.     beta = 0;
  364.     gamma = 0;
  365.  
  366.     GLfloat cabinet_view_matrix[] = {
  367.         1, 0, 0, 0,
  368.         0, 1, 0, 0,
  369.         -0.5*cos(M_PI / 6), -0.5*sin(M_PI / 6), -1, 0,
  370.         0, 0, 0, 1
  371.     };      //0.5*cos(M_PI/6), 0.5*sin(M_PI/6), 0, 0,
  372.  
  373.     count_cone();
  374.     while (!glfwWindowShouldClose(window))
  375.     {
  376.         glClearColor(0.4, 0.4, 0.4, 1.0);
  377.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  378.  
  379.         if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) alpha -= ANGLE;
  380.         if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) alpha += ANGLE;
  381.         if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) beta -= ANGLE;
  382.         if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) beta += ANGLE;
  383.         if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) gamma -= ANGLE;
  384.         if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) gamma += ANGLE;
  385.  
  386.         glMatrixMode(GL_PROJECTION);
  387.         glLoadIdentity();
  388.         glLoadMatrixf(cabinet_view_matrix);
  389.  
  390.         glOrtho(-10, 10, -10, 10, 10, -10);
  391.  
  392.         glMatrixMode(GL_MODELVIEW);
  393.         glLoadIdentity();
  394.  
  395.         //draw_net();
  396.         //draw_axis(-100, 100, -3, 100, -1, 100);
  397.  
  398.         draw_cone(type, r, r * 2, h, freq_x, freq_y, alpha, beta, gamma);
  399.  
  400.         glfwSwapBuffers(window);
  401.         glfwWaitEvents();
  402.         //glfwPollEvents();
  403.     }
  404.  
  405.     free_vect(cone_coord_top);
  406.     free_vect(cone_coord_bot);
  407.     free_vect(cone_coord_side);
  408.  
  409. }
  410.  
  411. int main(int argc, char** argv)
  412. {
  413.     // initialise GLFW
  414.     if (!glfwInit())
  415.     {
  416.         printf("glfwInit failed\n");
  417.         return -1;
  418.     }
  419.     glfwWindowHint(GLFW_SAMPLES, PC_MODE);
  420.     glfwSetErrorCallback(error_callback);
  421.  
  422.     //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  423.     //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  424.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  425.     GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test app", NULL, NULL);
  426.  
  427.     if (window == NULL)
  428.     {
  429.         printf("glfwOpenWindow failed.\n");
  430.         glfwTerminate();
  431.         return -2;
  432.     }
  433.  
  434.     int attrib;
  435.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  436.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  437.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  438.  
  439.     glfwMakeContextCurrent(window);
  440.     glfwSetKeyCallback(window, controls);
  441.  
  442.     //glDepthFunc(GL_LEQUAL);
  443.     glEnable(GL_DEPTH_TEST);
  444.  
  445.     GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  446.     GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 0.7f };
  447.  
  448.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
  449.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
  450.  
  451.     glEnable(GL_LIGHT0);
  452.  
  453.     GLfloat lightPos[] = { -50.0f, 50.0f, 100.0f, 1.0f };
  454.     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  455.     //glLightModelf(GL_LIGHT_MODEL_AMBIENT, ambientLight);
  456.    
  457.     if (NULL != window)
  458.     {
  459.         display(window);
  460.     }
  461.     glfwDestroyWindow(window);
  462.     glfwTerminate();
  463.     return 0;
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement