Advertisement
Ladies_Man

omw to the dark/light side

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