Advertisement
Ladies_Man

cg lab3 alomst (array rdy)

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