Advertisement
Ladies_Man

norm robit no bez massiva

Mar 25th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.60 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. #define ANGLE 2
  16.  
  17.  
  18.  
  19. void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
  20. {
  21.     if (action == GLFW_PRESS)
  22.         if (key == GLFW_KEY_ESCAPE)
  23.             glfwSetWindowShouldClose(window, GL_TRUE);
  24. }
  25.  
  26. static void error_callback(int error, const char* description)
  27. {
  28.     fputs(description, stderr);
  29. }
  30.  
  31. void draw_cube(int type, int size, int angle_x, int angle_y, int angle_z)
  32. {
  33.     glRotatef(angle_x, 1, 0, 0);
  34.     glRotatef(angle_y, 0, 1, 0);
  35.     glRotatef(angle_z, 0, 0, 1);
  36.  
  37.     if (type == 1) glBegin(GL_QUADS);
  38.     if (type == -1) glBegin(GL_LINE_LOOP);
  39.     //BOTTOM
  40.         glColor3f(0, 0, 0);
  41.         glVertex3f(-size, -size, size);
  42.         glVertex3f(size, -size, size);
  43.         glVertex3f(size, -size, -size);
  44.         glVertex3f(-size, -size, -size);
  45.         glEnd();
  46.     if (type == 1) glBegin(GL_QUADS);
  47.     if (type == -1) glBegin(GL_LINE_LOOP);
  48.     //TOP
  49.         glColor3f(1, 0, 0);
  50.         glVertex3f(-size, size, size);
  51.         glVertex3f(size, size, size);
  52.         glVertex3f(size, size, -size);
  53.         glVertex3f(-size, size, -size);
  54.         glEnd();
  55.     if (type == 1) glBegin(GL_QUADS);
  56.     if (type == -1) glBegin(GL_LINE_LOOP);
  57.     //BACK
  58.         glColor3f(1, 0, 1);
  59.         glVertex3f(-size, -size, -size);
  60.         glVertex3f(size, -size, -size);
  61.         glVertex3f(size, size, -size);
  62.         glVertex3f(-size, size, -size);
  63.         glEnd();
  64.     if (type == 1) glBegin(GL_QUADS);
  65.     if (type == -1) glBegin(GL_LINE_LOOP);
  66.     //FRONT
  67.         glColor3f(0, 0, 1);
  68.         glVertex3f(-size, -size, size);
  69.         glVertex3f(size, -size, size);
  70.         glVertex3f(size, size, size);
  71.         glVertex3f(-size, size, size);
  72.         glEnd();
  73.     if (type == 1) glBegin(GL_QUADS);
  74.     if (type == -1) glBegin(GL_LINE_LOOP);
  75.         //LEFT
  76.         glColor3f(1, 1, 0);
  77.         glVertex3f(-size, -size, size);
  78.         glVertex3f(-size, -size, -size);
  79.         glVertex3f(-size, size, -size);
  80.         glVertex3f(-size, size, size);
  81.         glEnd();
  82.     if (type == 1) glBegin(GL_QUADS);
  83.     if (type == -1) glBegin(GL_LINE_LOOP);
  84.         //RIGHT
  85.         glColor3f(1, 1, 1);
  86.         glVertex3f(size, -size, size);
  87.         glVertex3f(size, -size, -size);
  88.         glVertex3f(size, size, -size);
  89.         glVertex3f(size, size, size);
  90.         glEnd();
  91. }
  92.  
  93. void draw_cone(int type, int top_rad, int bot_rad, int height, int smoothness, int angle_x, int angle_y, int angle_z)
  94. {
  95.     glRotatef(angle_x, 1, 0, 0);
  96.     glRotatef(angle_y, 0, 1, 0);
  97.     glRotatef(angle_z, 0, 0, 1);
  98.  
  99.     if (smoothness <= 3)
  100.         smoothness = 3;
  101.  
  102.     float angle;
  103.     //top
  104.     if (type == 1)
  105.         glBegin(GL_POLYGON);
  106.     if (type == -1)
  107.         glBegin(GL_LINE_LOOP);
  108.        
  109.         glColor3f(0.8, 0.8, 0.8);
  110.         for (angle = 0; angle <= 2 * M_PI; angle += 2 * M_PI / smoothness) {
  111.             glVertex3f(top_rad*sin(angle), height / 2, top_rad*cos(angle));
  112.         }
  113.     glEnd();
  114.  
  115.     //bot
  116.     if (type == 1)
  117.         glBegin(GL_POLYGON);
  118.     if (type == -1)
  119.         glBegin(GL_LINE_LOOP);
  120.  
  121.         glColor3f(0, 0, 0);
  122.         for (angle = 0; angle <= 2 * M_PI; angle += 2 * M_PI / smoothness) {
  123.             glVertex3f(bot_rad*sin(angle), -height / 2, bot_rad*cos(angle));
  124.         }
  125.     glEnd();
  126.  
  127.     //sidelines
  128.     if (type == 1)
  129.         glBegin(GL_QUAD_STRIP);
  130.     if (type == -1)
  131.         glBegin(GL_LINES);
  132.  
  133.         for (angle = 0; angle <= 2 * M_PI + 2*M_PI/smoothness; angle += 2 * M_PI / smoothness) {
  134.             glColor3f(0.7, 0.7, 0.7);
  135.             glVertex3f(top_rad*sin(angle), height / 2, top_rad*cos(angle));
  136.             glColor3f(0.2, 0.2, 0.2);
  137.             glVertex3f(bot_rad*sin(angle), -height/2, bot_rad*cos(angle));
  138.         }
  139.     glEnd();
  140. }
  141.  
  142. void draw_axis()
  143. {
  144.     glBegin(GL_LINES);
  145.         //X
  146.         glColor3f(1, 0, 0);
  147.         glVertex3f(-100, 0, 0);
  148.         glVertex3f(100, 0, 0);
  149.         //Y
  150.         glColor3f(0, 1, 0);
  151.         glVertex3f(0, -3, 0);
  152.         glVertex3f(0, 100, 0);
  153.         //Z
  154.         glColor3f(0, 0, 1);
  155.         glVertex3f(0, 0, -1);
  156.         glVertex3f(0, 0, 100);
  157.     glEnd();
  158. }
  159.  
  160. void draw_net()
  161. {
  162.     int i;
  163.     float j;
  164.  
  165.     for (i = 0; i < 20; i++) {
  166.         glColor3f(0, 0.7, 0);
  167.         for (j = -10; j <= 10; j ++) {
  168.             glBegin(GL_LINES);
  169.             glVertex3f(j, -3, 10);
  170.             glVertex3f(j, -3, -10);
  171.             glEnd();
  172.         }
  173.         for (j = -10; j <= 10; j+=2) {
  174.             glBegin(GL_LINES);
  175.             glVertex3f(10, -3, j);
  176.             glVertex3f(-10, -3, j);
  177.             glEnd();
  178.         }
  179.     }
  180. }
  181.  
  182. void display(GLFWwindow* window)
  183. {
  184.     int smoothness, type;
  185.     static float alpha, beta, gamma, a, b, c, h, r;
  186.  
  187.     alpha = 0;
  188.     beta = 0;
  189.     gamma = 0;
  190.     smoothness = 3;
  191.     type = -1;
  192.     h = 2;
  193.     r = 1;
  194.  
  195.     GLfloat cabinet_view_matrix[] = {
  196.         1, 0, 0, 0,
  197.         0, 1, 0, 0,
  198.         -0.5*cos(M_PI/6), -0.5*sin(M_PI/6), -1, 0,
  199.         0, 0, 0, 1
  200.     };  //0.5*cos(M_PI/6), 0.5*sin(M_PI/6), 0, 0,
  201.  
  202.  
  203.     while (!glfwWindowShouldClose(window))
  204.     {
  205.         glClearColor(0.4, 0.4, 0.4, 1.0);
  206.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  207.        
  208.         if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
  209.             alpha = 0;
  210.             beta = 0;
  211.             gamma = 0;
  212.             h = 2;
  213.             r = 1;
  214.         }
  215.         if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
  216.             alpha -= ANGLE;
  217.         }
  218.         if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
  219.             alpha += ANGLE;
  220.         }
  221.         if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
  222.             beta -= ANGLE;
  223.         }
  224.         if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
  225.             beta += ANGLE;
  226.         }
  227.         if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
  228.             gamma -= ANGLE;
  229.         }
  230.         if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
  231.             gamma += ANGLE;
  232.         }
  233.         if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
  234.             if (h <= 18) h++;
  235.         }
  236.         if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS) {
  237.             if (h > 2) h--;
  238.         }
  239.         if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
  240.             if (r < 10) r++;
  241.         }
  242.         if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
  243.             if (r > 1) r--;
  244.         }
  245.         if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
  246.             if (smoothness <= 50) smoothness++;
  247.         }
  248.         if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
  249.             if (smoothness > 3) smoothness--;
  250.         }
  251.         if (glfwGetKey(window, GLFW_KEY_V) == GLFW_PRESS) {
  252.             type = -type;
  253.         }
  254.  
  255.         glMatrixMode(GL_PROJECTION);
  256.         glLoadIdentity();
  257.         glLoadMatrixf(cabinet_view_matrix);
  258.         //gluPerspective(60, SCREEN_WIDTH / SCREEN_HEIGHT, 0, 100);
  259.         glOrtho(-10, 10, -10, 10, 10, -10);
  260.  
  261.         //gluLookAt(5, 5, 5, 0, 0, 0, 0, 1, 0);
  262.  
  263.         //DO I REALLY NEED THIS???????????????
  264.         //glMatrixMode(GL_MODELVIEW);
  265.         //glLoadIdentity();
  266.         //DO I REALLY NEED THIS???????????????
  267.         draw_net();
  268.         draw_axis();
  269.  
  270.         glTranslatef(-4, 0, 0);
  271.  
  272.         draw_cube(type, 2, 0, 0, 0);
  273.  
  274.         glTranslatef(8, 0, 0);
  275.  
  276.         //draw_cube(type, 2, alpha, beta, gamma);
  277.         draw_cone(type, r, r*2, h, smoothness, alpha, beta, gamma);
  278.  
  279.         glfwSwapBuffers(window);
  280.         glfwWaitEvents();
  281.     }
  282. }
  283.  
  284. int main(int argc, char** argv)
  285. {
  286.     // initialise GLFW
  287.     if (!glfwInit())
  288.     {
  289.         printf("glfwInit failed\n");
  290.         return -1;
  291.     }
  292.     glfwWindowHint(GLFW_SAMPLES, PC_MODE);
  293.     glfwSetErrorCallback(error_callback);
  294.  
  295.     //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  296.     //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  297.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  298.     GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test app", NULL, NULL);
  299.  
  300.     if (window == NULL)
  301.     {
  302.         printf("glfwOpenWindow failed.\n");
  303.         glfwTerminate();
  304.         return -2;
  305.     }
  306.  
  307.     int attrib;
  308.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  309.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  310.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  311.  
  312.     glfwMakeContextCurrent(window);
  313.     glfwSetKeyCallback(window, controls);
  314.  
  315.     //glDepthFunc(GL_LEQUAL);
  316.     glEnable(GL_DEPTH_TEST);
  317.  
  318.     if (NULL != window)
  319.     {
  320.         display(window);
  321.     }
  322.     glfwDestroyWindow(window);
  323.     glfwTerminate();
  324.     return 0;
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement