Advertisement
Ladies_Man

cg lab3 hope its final

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