Advertisement
Ladies_Man

#CG LAB2-3 (projections) COMPLETE

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