Advertisement
Ladies_Man

#CG LAB6 (realistic scene) COMPLETE

May 21st, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.30 KB | None | 0 0
  1. //учет ослабления интенсивности света с расстоянием от источника;
  2. //твининг-анимация кубическая
  3. //использование текстуры для определения интенсивности поверхности;
  4.  
  5. //управление: QEWASD T Y L G H Z space
  6.  
  7. // glfw_30.cpp : Defines the entry point for the console application.
  8. //  http://www.glfw.org/docs/latest/quick.html
  9. #include "stdafx.h"
  10. #define _CRT_SECURE_NO_DEPRECATE
  11. #include <time.h>
  12. #define _USE_MATH_DEFINES
  13. #include <cmath>
  14. #include <GL/glew.h>
  15. #include <GL/glut.h>
  16. #include <GL/GL.h>
  17. #include <GL/glaux.h>
  18. #pragma comment(lib, "glaux.lib")
  19. #include <GLFW/glfw3.h>
  20. #include <vector>
  21. #include <Windows.h>
  22.  
  23. #define SCREEN_WIDTH 1000
  24. #define SCREEN_HEIGHT 1000
  25. #define PC_MODE 16
  26. #define CONSOLE_MODE 0
  27.  
  28. #define MAXRAD 4
  29. #define MINRAD 1
  30. #define MAXHEIGHT 7
  31. #define MINHEIGHT 2
  32. #define MAXFREQ_X 40
  33. #define MINFREQ_X 3
  34. #define ANGLE 2
  35. #define DLIGHT 0.5
  36. #define TWINKIE_SPEED 20
  37.  
  38. typedef struct {
  39.     GLfloat x, y, z;
  40. } point;
  41.  
  42. std::vector<std::vector<point>> cone_top;
  43. std::vector<std::vector<point>> cone_bot;
  44. std::vector<std::vector<point>> cone_side;
  45.  
  46. std::vector<std::vector<point>> cyl_top;
  47. std::vector<std::vector<point>> cyl_bot;
  48. std::vector<std::vector<point>> cyl_side;
  49.  
  50. bool shift, twinkie_flag, tex_flag, light_flag;
  51. int type, stop_i, stop_j, freq_x, freq_y;
  52. float t, twinkie_steps = 100, dt = 5 / twinkie_steps;
  53. GLfloat light_pos_x, light_pos_y, light_pos_z;
  54. static float alpha, beta, gamma, h, r;
  55.  
  56.  
  57. static void error_callback(int error, const char* description)
  58. {
  59.     fputs(description, stderr);
  60. }
  61.  
  62. point count_normal(point p1, point p2, point p3)
  63. {
  64.     point a, b, n;
  65.     GLfloat l;
  66.  
  67.     a.x = p2.x - p1.x;
  68.     a.y = p2.y - p1.y;
  69.     a.z = p2.z - p1.z;
  70.  
  71.     b.x = p3.x - p1.x;
  72.     b.y = p3.y - p1.y;
  73.     b.z = p3.z - p1.z;
  74.  
  75.     n.x = (a.y * b.z) - (a.z * b.y);
  76.     n.y = (a.z * b.x) - (a.x * b.z);
  77.     n.z = (a.x * b.y) - (a.y * b.x);
  78.  
  79.     // Normalize (divide by root of dot product)
  80.     l = sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
  81.     n.x /= l;
  82.     n.y /= l;
  83.     n.z /= l;
  84.  
  85.     return n;
  86. }
  87.  
  88. GLuint texture[1];
  89.  
  90. void load_textures()
  91. {
  92.  
  93.     AUX_RGBImageRec *texture1;
  94.     texture1 = auxDIBImageLoadA("tex_nicolas_cage_you_dont_say.bmp");
  95.     glGenTextures(1, &texture[0]);
  96.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  97.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  98.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  99.     glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
  100.  
  101.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  102. }
  103.  
  104. void light_enable()
  105. {
  106.     //glShadeModel(GL_FLAT);
  107.     glShadeModel(GL_SMOOTH);
  108.  
  109.     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  110.  
  111.     GLfloat material_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  112.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
  113.  
  114.  
  115.     GLfloat light0_diffuse[] = { 0.4, 0.7, 0.2 };
  116.     GLfloat light0_position[] = { light_pos_x, light_pos_y, light_pos_z, 1.0 };
  117.     //GLfloat spot_direction[] = { -1.0, -1.0, 0.0 };
  118.  
  119.  
  120.     //glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
  121.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  122.     glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  123.     //glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
  124.     //glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT, );
  125.  
  126.  
  127.     glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.5);
  128.     glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
  129.     glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.05);
  130. }
  131.  
  132. void count_cone()
  133. {
  134.     int i, j, top_rad = r, bot_rad = r * 2;
  135.     float angle, dangle, height, dheight, drad0, drad1, drad2;
  136.  
  137.  
  138.  
  139.     height = h;
  140.     dheight = height / (float)freq_y;
  141.     dangle = 2 * M_PI / (float)freq_x;
  142.     drad0 = top_rad / (float)freq_y;
  143.     drad1 = (bot_rad - top_rad) / (float)freq_y;
  144.     drad2 = bot_rad / (float)freq_y;
  145.  
  146.  
  147.  
  148.     cone_top.resize(freq_y + 1);
  149.     for (i = 0; i < freq_y + 1; i++) cone_top[i].resize(freq_x + 1);
  150.  
  151.     cone_bot.resize(freq_y + 1);
  152.     for (i = 0; i < freq_y + 1; i++) cone_bot[i].resize(freq_x + 1);
  153.  
  154.     cone_side.resize(freq_y + 1);
  155.     for (i = 0; i < freq_y + 1; i++) cone_side[i].resize(freq_x + 1);
  156.  
  157.  
  158.  
  159.     for (i = 0; i < freq_y + 1; i += 1) {
  160.         for (j = 0, angle = 0; angle <= 2 * M_PI; j += 1, angle += dangle) {
  161.             cone_top[i][j].x = drad0 * i * cos(angle);
  162.             cone_top[i][j].y = height;
  163.             cone_top[i][j].z = drad0 * i * sin(angle);
  164.  
  165.             cone_side[i][j].x = (top_rad + drad1 * i) * cos(angle);
  166.             cone_side[i][j].y = height - dheight * i;
  167.             cone_side[i][j].z = (top_rad + drad1 * i) * sin(angle);
  168.  
  169.             cone_bot[i][j].x = drad2 * i * cos(angle);
  170.             cone_bot[i][j].y = 0;
  171.             cone_bot[i][j].z = drad2 * i * sin(angle);
  172.         }
  173.     }
  174.     stop_i = i;
  175.     stop_j = j;
  176.  
  177.     printf("cone_renewed: h=%.2f, top_r=%d, fr_x=%d, fr_y=%d s_i=%d s_j=%d\n", height, top_rad, freq_x, freq_y, stop_i, stop_j);
  178. }
  179.  
  180. void count_cylinder()
  181. {
  182.     int i, j, rad = r;
  183.     float angle, dangle, height, dheight, drad0;
  184.  
  185.  
  186.     height = h;
  187.     dheight = height / (float)freq_y;
  188.     dangle = 2 * M_PI / (float)freq_x;
  189.     drad0 = rad / (float)freq_y;
  190.  
  191.  
  192.  
  193.     cyl_top.resize(freq_y + 1);
  194.     for (i = 0; i < freq_y + 1; i++) cyl_top[i].resize(freq_x + 1);
  195.  
  196.     cyl_bot.resize(freq_y + 1);
  197.     for (i = 0; i < freq_y + 1; i++) cyl_bot[i].resize(freq_x + 1);
  198.  
  199.     cyl_side.resize(freq_y + 1);
  200.     for (i = 0; i < freq_y + 1; i++) cyl_side[i].resize(freq_x + 1);
  201.  
  202.  
  203.  
  204.     for (i = 0; i < freq_y + 1; i += 1) {
  205.         for (j = 0, angle = 0; angle <= 2 * M_PI; j += 1, angle += dangle) {
  206.             cyl_top[i][j].x = drad0 * i * cos(angle);
  207.             cyl_top[i][j].y = height;
  208.             cyl_top[i][j].z = drad0 * i * sin(angle);
  209.  
  210.             cyl_side[i][j].x = rad * cos(angle);
  211.             cyl_side[i][j].y = height - dheight * i;
  212.             cyl_side[i][j].z = rad * sin(angle);
  213.  
  214.             cyl_bot[i][j].x = drad0 * i * cos(angle);
  215.             cyl_bot[i][j].y = 0;
  216.             cyl_bot[i][j].z = drad0 * i * sin(angle);
  217.         }
  218.     }
  219.     stop_i = i;
  220.     stop_j = j;
  221.  
  222.     printf("cyl_renewed: h=%.2f, r=%d, fr_x=%d, fr_y=%d s_i=%d s_j=%d\n", height, rad, freq_x, freq_y, stop_i, stop_j);
  223. }
  224.  
  225. void draw_figure(
  226.     std::vector<std::vector<point>>& top,
  227.     std::vector<std::vector<point>>& side,
  228.     std::vector<std::vector<point>>& bot
  229.     )
  230. {
  231.     int i, j;
  232.     point normal;
  233.     GLfloat tex_i, tex_j, dtex_i, dtex_j;
  234.  
  235.     dtex_i = 1.0 / (float)(freq_y);
  236.     dtex_j = 1.0 / (float)(freq_x);
  237.  
  238.     glRotatef(alpha, 1, 0, 0);
  239.     glRotatef(beta, 0, 1, 0);
  240.     glRotatef(gamma, 0, 0, 1);
  241.  
  242.     glMatrixMode(GL_MODELVIEW);
  243.     glLoadIdentity();
  244.  
  245.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  246.  
  247.     for (i = 0, tex_i = 0; i < stop_i-1; i += 1, tex_i += dtex_i) {
  248.         for (j = 0, tex_j = 0; j < stop_j; j += 1, tex_j += dtex_j) {
  249.             //top
  250.             glBegin(GL_POLYGON);
  251.                 normal = count_normal(top[i][j], top[i + 1][(j + 1) % stop_j], top[i + 1][j]);
  252.                 glNormal3f(normal.x, normal.y, normal.z);
  253.                
  254.                 glColor3f(1, 1, 1);
  255.                 glVertex3f(top[i][j].x, top[i][j].y, top[i][j].z);
  256.                 glVertex3f(top[i][(j + 1) % stop_j].x, top[i][(j + 1) % stop_j].y, top[i][(j + 1) % stop_j].z);
  257.                 glVertex3f(top[i + 1][(j + 1) % stop_j].x, top[i + 1][(j + 1) % stop_j].y, top[i + 1][(j + 1) % stop_j].z);
  258.                 glVertex3f(top[i + 1][j].x, top[i + 1][j].y, top[i + 1][j].z);
  259.             glEnd();
  260.  
  261.  
  262.             //sidelines
  263.             glBegin(GL_POLYGON);
  264.                 normal = count_normal(side[i][j], side[i + 1][(j + 1) % stop_j], side[i + 1][j]);
  265.                 glNormal3f(normal.x, normal.y, normal.z);
  266.  
  267.                 glColor3f(0.6, 0.6, 0.6);
  268.                 glTexCoord2f(tex_i, tex_j);
  269.                 glVertex3f(side[i][j].x, side[i][j].y, side[i][j].z);
  270.                 glTexCoord2f(tex_i, tex_j + dtex_j);
  271.                 glVertex3f(side[i][(j + 1) % stop_j].x, side[i][(j + 1) % stop_j].y, side[i][(j + 1) % stop_j].z);
  272.                 glTexCoord2f(tex_i + dtex_i, tex_j + dtex_j);
  273.                 glVertex3f(side[i + 1][(j + 1) % stop_j].x, side[i + 1][(j + 1) % stop_j].y, side[i + 1][(j + 1) % stop_j].z);
  274.                 glTexCoord2f(tex_i + dtex_i, tex_j);
  275.                 glVertex3f(side[i + 1][j].x, side[i + 1][j].y, side[i + 1][j].z);
  276.             glEnd();
  277.  
  278.  
  279.             //bot
  280.             glBegin(GL_POLYGON);
  281.                 normal = count_normal(bot[i][j], bot[i + 1][(j + 1) % stop_j], bot[i + 1][j]);
  282.                 glNormal3f(normal.x, normal.y, normal.z);
  283.  
  284.                 glColor3f(0.3, 0.3, 0.3);
  285.                 glTexCoord2f(0.0f, 0.0f);
  286.                 glVertex3f(bot[i][j].x, bot[i][j].y, bot[i][j].z);
  287.                 glTexCoord2f(0.0f, 1.0f);
  288.                 glVertex3f(bot[i][(j + 1) % stop_j].x, bot[i][(j + 1) % stop_j].y, bot[i][(j + 1) % stop_j].z);
  289.                 glTexCoord2f(1.0f, 1.0f);
  290.                 glVertex3f(bot[i + 1][(j + 1) % stop_j].x, bot[i + 1][(j + 1) % stop_j].y, bot[i + 1][(j + 1) % stop_j].z);
  291.                 glTexCoord2f(1.0f, 0.0f);
  292.                 glVertex3f(bot[i + 1][j].x, bot[i + 1][j].y, bot[i + 1][j].z);
  293.             glEnd();
  294.         }
  295.     }
  296.  
  297.     glBegin(GL_LINES);
  298.         glColor3f(1, 0, 0);
  299.         glVertex3f(light_pos_x, light_pos_y, light_pos_z);
  300.         glVertex3f(0,0,0);
  301.     glEnd();
  302. }
  303.  
  304. GLFWwindow* WINDOW;
  305.  
  306. void tweenking()
  307. {
  308.     int i, j, speed;
  309.     //Bezier cube:
  310.     //(1-t)^3*p0 + 3t(1-t)^2*p1 + 3*t^2*(1-t)*p2 + t^3*p3
  311.  
  312.     for (t = 0, speed = 0; speed <= TWINKIE_SPEED; t += dt, speed++) {
  313.         for (i = 0; i < stop_i; i += 1) {
  314.             for (j = 0; j < stop_j; j += 1) {
  315.                 cone_top[i][j].x = (1 - t)*(1 - t)*(1 - t)*cone_top[i][j].x + 3 * t*(1 - t)*(1 - t)*cone_top[i][j].x + 3 * t*t*(1 - t)*cone_top[i][j].x + t*t*t*cyl_top[i][j].x;
  316.                 cone_top[i][j].y = (1 - t)*(1 - t)*(1 - t)*cone_top[i][j].y + 3 * t*(1 - t)*(1 - t)*cone_top[i][j].y + 3 * t*t*(1 - t)*cone_top[i][j].y + t*t*t*cyl_top[i][j].y;
  317.                 cone_top[i][j].z = (1 - t)*(1 - t)*(1 - t)*cone_top[i][j].z + 3 * t*(1 - t)*(1 - t)*cone_top[i][j].z + 3 * t*t*(1 - t)*cone_top[i][j].z + t*t*t*cyl_top[i][j].z;
  318.  
  319.                 cone_side[i][j].x = (1 - t)*(1 - t)*(1 - t)*cone_side[i][j].x + 3 * t*(1 - t)*(1 - t)*cone_side[i][j].x + 3 * t*t*(1 - t)*cone_side[i][j].x + t*t*t*cyl_side[i][j].x;
  320.                 cone_side[i][j].y = (1 - t)*(1 - t)*(1 - t)*cone_side[i][j].y + 3 * t*(1 - t)*(1 - t)*cone_side[i][j].y + 3 * t*t*(1 - t)*cone_side[i][j].y + t*t*t*cyl_side[i][j].y;
  321.                 cone_side[i][j].z = (1 - t)*(1 - t)*(1 - t)*cone_side[i][j].z + 3 * t*(1 - t)*(1 - t)*cone_side[i][j].z + 3 * t*t*(1 - t)*cone_side[i][j].z + t*t*t*cyl_side[i][j].z;
  322.  
  323.                 cone_bot[i][j].x = (1 - t)*(1 - t)*(1 - t)*cone_bot[i][j].x + 3 * t*(1 - t)*(1 - t)*cone_bot[i][j].x + 3 * t*t*(1 - t)*cone_bot[i][j].x + t*t*t*cyl_bot[i][j].x;
  324.                 cone_bot[i][j].y = (1 - t)*(1 - t)*(1 - t)*cone_bot[i][j].y + 3 * t*(1 - t)*(1 - t)*cone_bot[i][j].y + 3 * t*t*(1 - t)*cone_bot[i][j].y + t*t*t*cyl_bot[i][j].y;
  325.                 cone_bot[i][j].z = (1 - t)*(1 - t)*(1 - t)*cone_bot[i][j].z + 3 * t*(1 - t)*(1 - t)*cone_bot[i][j].z + 3 * t*t*(1 - t)*cone_bot[i][j].z + t*t*t*cyl_bot[i][j].z;
  326.             }
  327.         }
  328.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  329.        
  330.         draw_figure(cone_top, cone_side, cone_bot);
  331.         glfwSwapBuffers(WINDOW);
  332.         Sleep(20);
  333.     }
  334. }
  335.  
  336. void save()
  337. {
  338.     FILE *file;
  339.     file = fopen("save.txt", "w");
  340.     fprintf(file, "%f\n", alpha);
  341.     fprintf(file, "%f\n", beta);
  342.     fprintf(file, "%f\n", gamma);
  343.     fprintf(file, "%f\n", h);
  344.     fprintf(file, "%f\n", r);
  345.     fprintf(file, "%d\n", freq_x);
  346.     fprintf(file, "%d\n", freq_y);
  347.     fprintf(file, "%d\n", type);
  348.     fclose(file);
  349.  
  350.     printf("-> current state saved\n");
  351. }
  352.  
  353. void load()
  354. {
  355.     FILE *file;
  356.     file = fopen("save.txt", "r");
  357.  
  358.     if (!file) {
  359.         printf("-> failed to load!\n");
  360.         return;
  361.     }
  362.  
  363.     fscanf(file, "%f\n", &alpha);
  364.     fscanf(file, "%f\n", &beta);
  365.     fscanf(file, "%f\n", &gamma);
  366.     fscanf(file, "%f\n", &h);
  367.     fscanf(file, "%f\n", &r);
  368.     fscanf(file, "%d\n", &freq_x);
  369.     fscanf(file, "%d\n", &freq_y);
  370.     fscanf(file, "%d\n", &type);
  371.     fclose(file);
  372.  
  373.     printf("-> previous state loaded\n");
  374. }
  375.  
  376. void initial_state()
  377. {
  378.     shift = true;
  379.     tex_flag = false;
  380.     twinkie_flag = false;
  381.  
  382.     t = 0;
  383.     freq_x = 15;
  384.     freq_y = 3;
  385.     type = -1;
  386.     h = 3;
  387.     r = 1;
  388.     alpha = 0;
  389.     beta = 0;
  390.     gamma = 0;
  391.     light_pos_x = 0.0;
  392.     light_pos_y = 2.0;
  393.     light_pos_z = 5.0;
  394.  
  395.     load_textures();
  396.  
  397.     count_cone();
  398.     count_cylinder();
  399. }
  400.  
  401. void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
  402. {
  403.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE);
  404.     if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) { shift = !shift; printf("switched\n"); }
  405.     if (key == GLFW_KEY_Z && action == GLFW_PRESS) initial_state();
  406.     if (GLFW_KEY_UP == key && action == GLFW_PRESS) {
  407.         if (h <= MAXHEIGHT) { h += 2; freq_y++; count_cone(); count_cylinder(); }
  408.     }
  409.     if (GLFW_KEY_DOWN == key && action == GLFW_PRESS) {
  410.         if (h > MINHEIGHT) { h -= 2; freq_y--; count_cone(); count_cylinder(); }
  411.     }
  412.     if (GLFW_KEY_RIGHT == key && action == GLFW_PRESS) {
  413.         if (r < MAXRAD) { r++; count_cone(); count_cylinder(); }
  414.     }
  415.     if (GLFW_KEY_LEFT == key && action == GLFW_PRESS) {
  416.         if (r > MINRAD) { r--; count_cone(); count_cylinder(); }
  417.     }
  418.     if (GLFW_KEY_R == key && action == GLFW_PRESS) {
  419.         if (freq_x <= MAXFREQ_X) { freq_x++; count_cone(); count_cylinder(); }
  420.     }
  421.     if (GLFW_KEY_F == key && action == GLFW_PRESS) {
  422.         if (freq_x > MINFREQ_X) { freq_x--; count_cone(); count_cylinder(); }
  423.     }
  424.     if (GLFW_KEY_V == key && action == GLFW_PRESS) {
  425.         type = -type;
  426.             if (type == 1) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  427.             if (type == -1) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  428.     }
  429.     if (GLFW_KEY_T == key && action == GLFW_PRESS) { twinkie_flag = !twinkie_flag; tweenking(); }
  430.     if (GLFW_KEY_Y == key && action == GLFW_PRESS) {
  431.         tex_flag = !tex_flag;
  432.         if (tex_flag) { glEnable(GL_TEXTURE_2D);} else { glDisable(GL_TEXTURE_2D); }
  433.     }
  434.     if (GLFW_KEY_H == key && action == GLFW_PRESS) { save(); }
  435.     if (GLFW_KEY_G == key && action == GLFW_PRESS) { load(); count_cone(); count_cylinder(); }
  436.     if (GLFW_KEY_L == key && action == GLFW_PRESS) {
  437.         light_flag = !light_flag;
  438.         if (light_flag) {
  439.             glEnable(GL_LIGHTING);
  440.             glEnable(GL_LIGHT0);
  441.             light_enable();
  442.         }
  443.         else {
  444.             glDisable(GL_LIGHTING);
  445.             glDisable(GL_LIGHT0);
  446.         }
  447.     }
  448.    
  449. }
  450.  
  451. GLfloat cabinet_view_matrix[] = {
  452.     1, 0, 0, 0,
  453.     0, 1, 0, 0,
  454.     -0.5*cos(M_PI / 6), -0.5*sin(M_PI / 6), -1, 0,
  455.     0, 0, 0, 1
  456. };
  457.  
  458. void display(GLFWwindow* window)
  459. {
  460.     printf("[H]: save state\n[G]: load state\n[T]:  tweening aninmation\n[Y]: textures\n[L]: lighting\n[Q E W A S D]: rotation\n[Spacebar]:switch model/light rotation\n[Z]: reset state to default\n");
  461.     initial_state();
  462.     while (!glfwWindowShouldClose(window))
  463.     {
  464.         glClearColor(0.4, 0.4, 0.4, 1.0);
  465.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  466.        
  467.         if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS && shift) alpha -= ANGLE;
  468.         if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS && shift) alpha += ANGLE;
  469.         if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS && shift) beta -= ANGLE;
  470.         if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS && shift) beta += ANGLE;
  471.         if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS && shift) gamma -= ANGLE;
  472.         if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS && shift) gamma += ANGLE;
  473.         if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS && !shift) { light_pos_y++; light_enable(); Sleep(120); }
  474.         if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS && !shift) { light_pos_y--; light_enable(); Sleep(120); }
  475.         if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS && !shift) { light_pos_x++; light_enable(); Sleep(120); }
  476.         if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS && !shift) { light_pos_x--; light_enable(); Sleep(120); }
  477.         if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS && !shift) { light_pos_z++; light_enable(); Sleep(120); }
  478.         if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS && !shift) { light_pos_z--; light_enable(); Sleep(120); }
  479.  
  480.         glMatrixMode(GL_PROJECTION);
  481.         glLoadIdentity();
  482.         glLoadMatrixf(cabinet_view_matrix);
  483.         glOrtho(-10, 10, -10, 10, 10, -10);
  484.  
  485.         draw_figure(cone_top, cone_side, cone_bot);
  486.  
  487.         glfwSwapBuffers(window);
  488.         //glfwWaitEvents();
  489.         glfwPollEvents();
  490.     }
  491.  
  492. }
  493.  
  494. int main(int argc, char** argv)
  495. {
  496.     if (!glfwInit())
  497.     {
  498.         printf("glfwInit failed\n");
  499.         return -1;
  500.     }
  501.     glfwWindowHint(GLFW_SAMPLES, PC_MODE);
  502.     glfwSetErrorCallback(error_callback);
  503.  
  504.     GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test app", NULL, NULL);
  505.     WINDOW = window;
  506.     if (window == NULL)
  507.     {
  508.         printf("glfwOpenWindow failed.\n");
  509.         glfwTerminate();
  510.         return -2;
  511.     }
  512.  
  513.     int attrib;
  514.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  515.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  516.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  517.  
  518.     glfwMakeContextCurrent(window);
  519.     glfwSetKeyCallback(window, controls);
  520.  
  521.     //glDepthFunc(GL_LEQUAL);
  522.     glEnable(GL_DEPTH_TEST);
  523.    
  524.     if (NULL != window)
  525.     {
  526.         display(window);
  527.     }
  528.     glfwDestroyWindow(window);
  529.     glfwTerminate();
  530.     return 0;
  531. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement