Advertisement
Ladies_Man

#CG LAB7 (optimization) COMPLETE

Jun 5th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.37 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. #include "stdafx.h"
  4. #define _CRT_SECURE_NO_DEPRECATE
  5. #include <time.h>
  6. #define _USE_MATH_DEFINES
  7. #include <cmath>
  8. #include <GL/glew.h>
  9. #include <GL/glut.h>
  10. #include <GL/GL.h>
  11. #include <GL/glaux.h>
  12. #pragma comment(lib, "glaux.lib")
  13. #include <GLFW/glfw3.h>
  14. #include <vector>
  15. #include <Windows.h>
  16. #include <iostream>
  17.  
  18. #define SCREEN_WIDTH 1000
  19. #define SCREEN_HEIGHT 1000
  20. #define PC_MODE 16
  21. #define CONSOLE_MODE 0
  22.  
  23. #define MAXRAD 4
  24. #define MINRAD 1
  25. #define MAXHEIGHT 7
  26. #define MINHEIGHT 2
  27. #define MAXFREQ_X 40
  28. #define MINFREQ_X 3
  29. #define ANGLE 2
  30. #define TWINKIE_SPEED 20
  31. #define MAIN_LIST 1
  32.  
  33.  
  34. //Optimization goes here:
  35. //enable PERFORM_TESTS and enable optimization
  36. //results (FPS after optimization) after 10 tests will be stored in x_file.txt
  37.  
  38. //#define PERFORM_TESTS
  39.  
  40. //#define X_NOSMOOTH            //disable built-in antialiasing
  41. //#define X_CULLFACE            //hide surfaces that cant be seen
  42. //#define X_SCREENSIZE          //reduce screen size
  43. //#define X_NORAMLIZE           //disable built-in normalization of normals
  44. //#define X_TEXSIZE         //reduce texture size
  45. //#define X_LIGHTDECREASE       //disable 2-sided light model
  46. //#define X_DISPLIST            //use display list for drawing
  47.  
  48.  
  49. typedef struct {
  50.     GLfloat x, y, z;
  51. } point;
  52.  
  53. std::vector<std::vector<point>> cone_top;
  54. std::vector<std::vector<point>> cone_bot;
  55. std::vector<std::vector<point>> cone_side;
  56.  
  57. std::vector<std::vector<point>> cyl_top;
  58. std::vector<std::vector<point>> cyl_bot;
  59. std::vector<std::vector<point>> cyl_side;
  60.  
  61. bool shift, twinkie_flag, tex_flag, light_flag;
  62. int type, stop_i, stop_j, freq_x, freq_y, list;
  63. float t, twinkie_steps = 100, dt = 5 / twinkie_steps;
  64. GLfloat light_pos_x, light_pos_y, light_pos_z;
  65. static float alpha, beta, gamma, h, r;
  66.  
  67.  
  68. static void error_callback(int error, const char* description)
  69. {
  70.     fputs(description, stderr);
  71. }
  72.  
  73. point count_normal(point p1, point p2, point p3)
  74. {
  75.     point a, b, n;
  76.     GLfloat l;
  77.  
  78.     a.x = p2.x - p1.x;
  79.     a.y = p2.y - p1.y;
  80.     a.z = p2.z - p1.z;
  81.  
  82.     b.x = p3.x - p1.x;
  83.     b.y = p3.y - p1.y;
  84.     b.z = p3.z - p1.z;
  85.  
  86.     n.x = (a.y * b.z) - (a.z * b.y);
  87.     n.y = (a.z * b.x) - (a.x * b.z);
  88.     n.z = (a.x * b.y) - (a.y * b.x);
  89.  
  90.     // Normalize (divide by root of dot product)
  91.     l = sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
  92.     n.x /= l;
  93.     n.y /= l;
  94.     n.z /= l;
  95.  
  96.     return n;
  97. }
  98.  
  99. GLuint texture[1];
  100.  
  101. void load_textures()
  102. {
  103.     AUX_RGBImageRec *texture1;
  104.  
  105. #ifdef X_TEXSIZE
  106.     texture1 = auxDIBImageLoadA("tex_cage_small.bmp");
  107. #endif
  108.  
  109. #ifndef X_TEXSIZE
  110.     texture1 = auxDIBImageLoadA("tex_cage.bmp");
  111. #endif
  112.  
  113.     glGenTextures(1, &texture[0]);
  114.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  115.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  116.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  117.     glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
  118.  
  119.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  120. }
  121.  
  122. void light_enable()
  123. {
  124.  
  125. #ifdef X_LIGHTDECRESE
  126.     glShadeModel(GL_FLAT);
  127. #endif
  128.  
  129. #ifndef X_LIGHTDECREASE
  130.     glShadeModel(GL_SMOOTH);
  131. #endif
  132.  
  133. #ifndef X_LIGHTDECREASE
  134.     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  135. #endif
  136.  
  137.     GLfloat material_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  138.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
  139.  
  140.  
  141.     GLfloat light0_diffuse[] = { 0.4, 0.7, 0.2 };
  142.     GLfloat light0_position[] = { light_pos_x, light_pos_y, light_pos_z, 1.0 };
  143.     //GLfloat spot_direction[] = { -1.0, -1.0, 0.0 };
  144.  
  145.  
  146.     //glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
  147.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  148.     glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  149.     //glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
  150.     //glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT, );
  151.  
  152.  
  153.     glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.5);
  154.     glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
  155.     glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.05);
  156.  
  157. #ifdef X_NORMALIZE
  158.     glEnable(GL_NORMALIZE);
  159. #endif
  160. }
  161.  
  162. void count_cone()
  163. {
  164.     int i, j, top_rad = r, bot_rad = r * 2;
  165.     float angle, dangle, height, dheight, drad0, drad1, drad2;
  166.  
  167.  
  168.  
  169.     height = h;
  170.     dheight = height / (float)freq_y;
  171.     dangle = 2 * M_PI / (float)freq_x;
  172.     drad0 = top_rad / (float)freq_y;
  173.     drad1 = (bot_rad - top_rad) / (float)freq_y;
  174.     drad2 = bot_rad / (float)freq_y;
  175.  
  176.  
  177.  
  178.     cone_top.resize(freq_y + 1);
  179.     for (i = 0; i < freq_y + 1; i++) cone_top[i].resize(freq_x + 1);
  180.  
  181.     cone_bot.resize(freq_y + 1);
  182.     for (i = 0; i < freq_y + 1; i++) cone_bot[i].resize(freq_x + 1);
  183.  
  184.     cone_side.resize(freq_y + 1);
  185.     for (i = 0; i < freq_y + 1; i++) cone_side[i].resize(freq_x + 1);
  186.  
  187.  
  188.  
  189.     for (i = 0; i < freq_y + 1; i += 1) {
  190.         for (j = 0, angle = 0; angle <= 2 * M_PI; j += 1, angle += dangle) {
  191.             cone_top[i][j].x = drad0 * i * cos(angle);
  192.             cone_top[i][j].y = height;
  193.             cone_top[i][j].z = drad0 * i * sin(angle);
  194.  
  195.             cone_side[i][j].x = (top_rad + drad1 * i) * cos(angle);
  196.             cone_side[i][j].y = height - dheight * i;
  197.             cone_side[i][j].z = (top_rad + drad1 * i) * sin(angle);
  198.  
  199.             cone_bot[i][j].x = drad2 * i * cos(angle);
  200.             cone_bot[i][j].y = 0;
  201.             cone_bot[i][j].z = drad2 * i * sin(angle);
  202.         }
  203.     }
  204.     stop_i = i;
  205.     stop_j = j;
  206.  
  207.     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);
  208. }
  209.  
  210. void count_cylinder()
  211. {
  212.     int i, j, rad = r;
  213.     float angle, dangle, height, dheight, drad0;
  214.  
  215.  
  216.     height = h;
  217.     dheight = height / (float)freq_y;
  218.     dangle = 2 * M_PI / (float)freq_x;
  219.     drad0 = rad / (float)freq_y;
  220.  
  221.  
  222.  
  223.     cyl_top.resize(freq_y + 1);
  224.     for (i = 0; i < freq_y + 1; i++) cyl_top[i].resize(freq_x + 1);
  225.  
  226.     cyl_bot.resize(freq_y + 1);
  227.     for (i = 0; i < freq_y + 1; i++) cyl_bot[i].resize(freq_x + 1);
  228.  
  229.     cyl_side.resize(freq_y + 1);
  230.     for (i = 0; i < freq_y + 1; i++) cyl_side[i].resize(freq_x + 1);
  231.  
  232.  
  233.  
  234.     for (i = 0; i < freq_y + 1; i += 1) {
  235.         for (j = 0, angle = 0; angle <= 2 * M_PI; j += 1, angle += dangle) {
  236.             cyl_top[i][j].x = drad0 * i * cos(angle);
  237.             cyl_top[i][j].y = height;
  238.             cyl_top[i][j].z = drad0 * i * sin(angle);
  239.  
  240.             cyl_side[i][j].x = rad * cos(angle);
  241.             cyl_side[i][j].y = height - dheight * i;
  242.             cyl_side[i][j].z = rad * sin(angle);
  243.  
  244.             cyl_bot[i][j].x = drad0 * i * cos(angle);
  245.             cyl_bot[i][j].y = 0;
  246.             cyl_bot[i][j].z = drad0 * i * sin(angle);
  247.         }
  248.     }
  249.     stop_i = i;
  250.     stop_j = j;
  251.  
  252.     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);
  253. }
  254.  
  255. void draw_figure(
  256.     std::vector<std::vector<point>>& top,
  257.     std::vector<std::vector<point>>& side,
  258.     std::vector<std::vector<point>>& bot
  259.     )
  260. {
  261.     int i, j;
  262.     point normal;
  263.     GLfloat tex_i, tex_j, dtex_i, dtex_j;
  264.  
  265.     dtex_i = 1.0 / (float)(freq_y);
  266.     dtex_j = 1.0 / (float)(freq_x);
  267.  
  268.     glRotatef(alpha, 1, 0, 0);
  269.     glRotatef(beta, 0, 1, 0);
  270.     glRotatef(gamma, 0, 0, 1);
  271.  
  272.     glMatrixMode(GL_MODELVIEW);
  273.     glLoadIdentity();
  274.  
  275.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  276.  
  277. #ifdef X_DISPLIST
  278.     list = glGenLists(1);
  279.     if (list != 0) {
  280.         glNewList(list, GL_COMPILE);
  281. #endif
  282.         for (i = 0, tex_i = 0; i < stop_i - 1; i += 1, tex_i += dtex_i) {
  283.             for (j = 0, tex_j = 0; j < stop_j; j += 1, tex_j += dtex_j) {
  284.                 //top
  285.                 glBegin(GL_POLYGON);
  286.                 normal = count_normal(top[i][j], top[i + 1][(j + 1) % stop_j], top[i + 1][j]);
  287.                 glNormal3f(normal.x, normal.y, normal.z);
  288.  
  289.                 glColor3f(1, 1, 1);
  290.                 glVertex3f(top[i][j].x, top[i][j].y, top[i][j].z);
  291.                 glVertex3f(top[i][(j + 1) % stop_j].x, top[i][(j + 1) % stop_j].y, top[i][(j + 1) % stop_j].z);
  292.                 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);
  293.                 glVertex3f(top[i + 1][j].x, top[i + 1][j].y, top[i + 1][j].z);
  294.                 glEnd();
  295.  
  296.  
  297.                 //sidelines
  298.                 glBegin(GL_POLYGON);
  299.                 normal = count_normal(side[i][j], side[i + 1][(j + 1) % stop_j], side[i + 1][j]);
  300.                 glNormal3f(normal.x, normal.y, normal.z);
  301.  
  302.                 glColor3f(0.6, 0.6, 0.6);
  303.                 glTexCoord2f(tex_i, tex_j);
  304.                 glVertex3f(side[i][j].x, side[i][j].y, side[i][j].z);
  305.                 glTexCoord2f(tex_i, tex_j + dtex_j);
  306.                 glVertex3f(side[i][(j + 1) % stop_j].x, side[i][(j + 1) % stop_j].y, side[i][(j + 1) % stop_j].z);
  307.                 glTexCoord2f(tex_i + dtex_i, tex_j + dtex_j);
  308.                 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);
  309.                 glTexCoord2f(tex_i + dtex_i, tex_j);
  310.                 glVertex3f(side[i + 1][j].x, side[i + 1][j].y, side[i + 1][j].z);
  311.                 glEnd();
  312.  
  313.  
  314.                 //bot
  315.                 glBegin(GL_POLYGON);
  316.                 normal = count_normal(bot[i][j], bot[i + 1][(j + 1) % stop_j], bot[i + 1][j]);
  317.                 glNormal3f(normal.x, normal.y, normal.z);
  318.  
  319.                 glColor3f(0.3, 0.3, 0.3);
  320.                 glTexCoord2f(0.0f, 0.0f);
  321.                 glVertex3f(bot[i][j].x, bot[i][j].y, bot[i][j].z);
  322.                 glTexCoord2f(0.0f, 1.0f);
  323.                 glVertex3f(bot[i][(j + 1) % stop_j].x, bot[i][(j + 1) % stop_j].y, bot[i][(j + 1) % stop_j].z);
  324.                 glTexCoord2f(1.0f, 1.0f);
  325.                 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);
  326.                 glTexCoord2f(1.0f, 0.0f);
  327.                 glVertex3f(bot[i + 1][j].x, bot[i + 1][j].y, bot[i + 1][j].z);
  328.                 glEnd();
  329.             }
  330.         }
  331.  
  332.         glBegin(GL_LINES);
  333.         glColor3f(1, 0, 0);
  334.         glVertex3f(light_pos_x, light_pos_y, light_pos_z);
  335.         glVertex3f(0, 0, 0);
  336.         glEnd();
  337.  
  338. #ifdef X_DISPLIST
  339.     }
  340.     glEndList();
  341. #endif
  342. }
  343.  
  344. GLFWwindow* WINDOW;
  345.  
  346. void tweenking()
  347. {
  348.     int i, j, speed;
  349.     //Bezier cube:
  350.     //(1-t)^3*p0 + 3t(1-t)^2*p1 + 3*t^2*(1-t)*p2 + t^3*p3
  351.  
  352.     for (t = 0, speed = 0; speed <= 20; t += dt, speed++) {
  353.         for (i = 0; i < stop_i; i += 1) {
  354.             for (j = 0; j < stop_j; j += 1) {
  355.                 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;
  356.                 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;
  357.                 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;
  358.  
  359.                 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;
  360.                 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;
  361.                 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;
  362.  
  363.                 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;
  364.                 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;
  365.                 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;
  366.             }
  367.         }
  368.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  369.        
  370.         draw_figure(cone_top, cone_side, cone_bot);
  371.         //glCallList(1);
  372.  
  373.         glfwSwapBuffers(WINDOW);
  374.         Sleep(20);
  375.     }
  376. }
  377.  
  378. void save()
  379. {
  380.     FILE *file;
  381.     file = fopen("save.txt", "w");
  382.     fprintf(file, "%f\n", alpha);
  383.     fprintf(file, "%f\n", beta);
  384.     fprintf(file, "%f\n", gamma);
  385.     fprintf(file, "%f\n", h);
  386.     fprintf(file, "%f\n", r);
  387.     fprintf(file, "%d\n", freq_x);
  388.     fprintf(file, "%d\n", freq_y);
  389.     fprintf(file, "%d\n", type);
  390.     fclose(file);
  391.  
  392.     printf("-> current state saved\n");
  393. }
  394.  
  395. void load()
  396. {
  397.     FILE *file;
  398.     file = fopen("save.txt", "r");
  399.  
  400.     if (!file) {
  401.         printf("-> failed to load!\n");
  402.         return;
  403.     }
  404.  
  405.     fscanf(file, "%f\n", &alpha);
  406.     fscanf(file, "%f\n", &beta);
  407.     fscanf(file, "%f\n", &gamma);
  408.     fscanf(file, "%f\n", &h);
  409.     fscanf(file, "%f\n", &r);
  410.     fscanf(file, "%d\n", &freq_x);
  411.     fscanf(file, "%d\n", &freq_y);
  412.     fscanf(file, "%d\n", &type);
  413.     fclose(file);
  414.  
  415.     printf("-> previous state loaded\n");
  416. }
  417.  
  418. void initial_state()
  419. {
  420.     shift = true;
  421.     tex_flag = true;
  422.     twinkie_flag = false;
  423.     light_flag = true;
  424.  
  425.     t = 0;
  426.     freq_x = 15;
  427.     freq_y = 3;
  428.     type = -1;
  429.     h = 3;
  430.     r = 1;
  431.     alpha = 0;
  432.     beta = 0;
  433.     gamma = 0;
  434.     light_pos_x = 0.0;
  435.     light_pos_y = 2.0;
  436.     light_pos_z = 5.0;
  437.  
  438.     load_textures();
  439.  
  440.     count_cone();
  441.     count_cylinder();
  442.  
  443. #ifdef X_LIGHTDEC
  444.     light_flag = true;
  445.     glEnable(GL_LIGHTING);
  446.     glEnable(GL_LIGHT0);
  447.     light_enable();
  448. #endif
  449.  
  450. #ifdef X_TEXSIZE
  451.     tex_flag = true;
  452.     glEnable(GL_TEXTURE_2D);
  453. #endif
  454.  
  455. }
  456.  
  457. void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
  458. {
  459.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE);
  460.     if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) { shift = !shift; printf("switched\n"); }
  461.     if (key == GLFW_KEY_Z && action == GLFW_PRESS) initial_state();
  462.     if (GLFW_KEY_UP == key && action == GLFW_PRESS) {
  463.         if (h <= MAXHEIGHT) { h += 2; freq_y++; count_cone(); count_cylinder(); }
  464.     }
  465.     if (GLFW_KEY_DOWN == key && action == GLFW_PRESS) {
  466.         if (h > MINHEIGHT) { h -= 2; freq_y--; count_cone(); count_cylinder(); }
  467.     }
  468.     if (GLFW_KEY_RIGHT == key && action == GLFW_PRESS) {
  469.         if (r < MAXRAD) { r++; count_cone(); count_cylinder(); }
  470.     }
  471.     if (GLFW_KEY_LEFT == key && action == GLFW_PRESS) {
  472.         if (r > MINRAD) { r--; count_cone(); count_cylinder(); }
  473.     }
  474.     if (GLFW_KEY_R == key && action == GLFW_PRESS) {
  475.         if (freq_x <= MAXFREQ_X) { freq_x++; count_cone(); count_cylinder(); }
  476.     }
  477.     if (GLFW_KEY_F == key && action == GLFW_PRESS) {
  478.         if (freq_x > MINFREQ_X) { freq_x--; count_cone(); count_cylinder(); }
  479.     }
  480.     if (GLFW_KEY_V == key && action == GLFW_PRESS) {
  481.         type = -type;
  482.             if (type == 1) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  483.             if (type == -1) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  484.     }
  485.     if (GLFW_KEY_T == key && action == GLFW_PRESS) { twinkie_flag = !twinkie_flag; tweenking(); }
  486.     if (GLFW_KEY_Y == key && action == GLFW_PRESS) {
  487.         tex_flag = !tex_flag;
  488.         if (tex_flag) { glEnable(GL_TEXTURE_2D);} else { glDisable(GL_TEXTURE_2D); }
  489.     }
  490.     if (GLFW_KEY_H == key && action == GLFW_PRESS) { save(); }
  491.     if (GLFW_KEY_G == key && action == GLFW_PRESS) { load(); count_cone(); count_cylinder(); }
  492.     if (GLFW_KEY_L == key && action == GLFW_PRESS) {
  493.         light_flag = !light_flag;
  494.         if (light_flag) {
  495.             glEnable(GL_LIGHTING);
  496.             glEnable(GL_LIGHT0);
  497.             light_enable();
  498.         }
  499.         else {
  500.             glDisable(GL_LIGHTING);
  501.             glDisable(GL_LIGHT0);
  502.         }
  503.     }
  504.  
  505. }
  506.  
  507. GLfloat cabinet_view_matrix[] = {
  508.     1, 0, 0, 0,
  509.     0, 1, 0, 0,
  510.     -0.5*cos(M_PI / 6), -0.5*sin(M_PI / 6), -1, 0,
  511.     0, 0, 0, 1
  512. };
  513.  
  514. int test, fps;
  515. FILE *X_FILE;
  516.  
  517. void display(GLFWwindow* window)
  518. {
  519.     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");
  520.    
  521.     initial_state();
  522.  
  523.     fps = 0;
  524.  
  525.     LARGE_INTEGER timerFrequency, timerStart, timerStop;
  526.     QueryPerformanceFrequency(&timerFrequency);
  527.     QueryPerformanceCounter(&timerStart);
  528.  
  529. #ifdef X_DISPLIST
  530.     draw_figure(cone_top, cone_side, cone_bot);
  531. #endif
  532.  
  533.     while (!glfwWindowShouldClose(window))
  534.     {
  535.         glClearColor(0.4, 0.4, 0.4, 1.0);
  536.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  537.  
  538.         if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS && shift) alpha -= ANGLE;
  539.         if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS && shift) alpha += ANGLE;
  540.         if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS && shift) beta -= ANGLE;
  541.         if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS && shift) beta += ANGLE;
  542.         if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS && shift) gamma -= ANGLE;
  543.         if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS && shift) gamma += ANGLE;
  544.         if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS && !shift) { light_pos_y++; light_enable(); Sleep(120); }
  545.         if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS && !shift) { light_pos_y--; light_enable(); Sleep(120); }
  546.         if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS && !shift) { light_pos_x++; light_enable(); Sleep(120); }
  547.         if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS && !shift) { light_pos_x--; light_enable(); Sleep(120); }
  548.         if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS && !shift) { light_pos_z++; light_enable(); Sleep(120); }
  549.         if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS && !shift) { light_pos_z--; light_enable(); Sleep(120); }
  550.  
  551.         glMatrixMode(GL_PROJECTION);
  552.         glLoadIdentity();
  553.         glLoadMatrixf(cabinet_view_matrix);
  554.         glOrtho(-10, 10, -10, 10, 10, -10);
  555.  
  556. #ifdef X_DISPLIST
  557.         glCallList(list);
  558. #endif
  559.  
  560. #ifndef X_DISPLIST
  561.         draw_figure(cone_top, cone_side, cone_bot);
  562. #endif
  563.         fps++;
  564.  
  565.         QueryPerformanceCounter(&timerStop);
  566.         double const t(static_cast<double>(timerStop.QuadPart - timerStart.QuadPart) / timerFrequency.QuadPart);
  567.  
  568. #ifdef PERFORM_TESTS
  569.         if (t >= 1.0) {
  570.             if (fps < 100) printf("are you perfroming tests on conole?\ndrakeface");
  571.             fprintf(X_FILE, "%d\n", fps);
  572.             return;
  573.         }
  574. #endif
  575.         glfwSwapBuffers(window);
  576.         //glfwWaitEvents();
  577.         glfwPollEvents();
  578.     }
  579. }
  580.  
  581.  
  582.  
  583. int main(int argc, char** argv)
  584. {
  585.  
  586. #ifdef PERFORM_TESTS
  587.     X_FILE = fopen("x_file.txt", "w");
  588.     fps = 0;
  589.  
  590.     for (test = 0; test < 10; test++) {
  591. #endif
  592.  
  593.         if (!glfwInit())
  594.         {
  595.             printf("glfwInit failed\n");
  596.             return -1;
  597.         }
  598.  
  599.     #ifdef X_NOSMOOTH
  600.         glfwWindowHint(GLFW_SAMPLES, CONSOLE_MODE);
  601.     #endif
  602.  
  603.     #ifndef X_NOSMOOTH
  604.         glfwWindowHint(GLFW_SAMPLES, PC_MODE);
  605.     #endif
  606.  
  607.         glfwSetErrorCallback(error_callback);
  608.  
  609.     #ifdef X_SCREENSIZE
  610.         GLFWwindow* window = glfwCreateWindow(850, 850, "Test app", NULL, NULL);
  611.     #endif
  612.  
  613.     #ifndef X_SCREENSIZE
  614.         GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test app", NULL, NULL);
  615.     #endif
  616.  
  617.  
  618.             WINDOW = window;
  619.             if (window == NULL)
  620.             {
  621.                 printf("glfwOpenWindow failed.\n");
  622.                 glfwTerminate();
  623.                 return -2;
  624.             }
  625.  
  626.             int attrib;
  627.             attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  628.             attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  629.             attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  630.  
  631.             glfwMakeContextCurrent(window);
  632.             glfwSetKeyCallback(window, controls);
  633.  
  634.             //glDepthFunc(GL_LEQUAL);
  635.             glEnable(GL_DEPTH_TEST);
  636.  
  637.     #ifdef X_CULLFACE
  638.             glEnable(GL_CULL_FACE);
  639.     #endif
  640.  
  641.             if (NULL != window)
  642.             {
  643.                 display(window);
  644.             }
  645.  
  646.             glfwDestroyWindow(window);
  647.             glfwTerminate();
  648.  
  649. #ifdef PERFORM_TESTS
  650.     }
  651.  
  652.     fclose(X_FILE);
  653.     getchar();
  654. #endif
  655.  
  656.     return 0;
  657. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement