Advertisement
Guest User

Visualizer

a guest
Oct 14th, 2012
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.38 KB | None | 0 0
  1. #include <Windows.h>
  2. #include "freeglut.h"
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <time.h>
  6. #include <random>
  7.  
  8. using namespace std;
  9.  
  10. float X = 0, Y = 0, Z = 0, diffX, diffY, camX = 0, camZ = 0;
  11. float rot_x = 0;
  12. float rot_y = 0;
  13. float frustum[6][4];
  14. bool flag = 1;
  15. const int size = 30;
  16. int space[size][size][size], a;
  17. const int scale = 2;
  18. const int stepNum = 200;
  19. int step = 1;
  20. char Name[255] = "";
  21. DWORD real = 0;
  22. char str[255] = "";
  23.  
  24. void ExtractFrustum()
  25. {
  26.     float   proj[16];
  27.     float   modl[16];
  28.     float   clip[16];
  29.     float   t;
  30.     /* Узнаем текущую матрицу PROJECTION */
  31.     glGetFloatv( GL_PROJECTION_MATRIX, proj );
  32.     /* Узнаем текущую матрицу MODELVIEW */
  33.     glGetFloatv( GL_MODELVIEW_MATRIX, modl );
  34.     /* Комбинируем матрицы(перемножаем) */
  35.     clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + \
  36.         modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
  37.     clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + \
  38.         modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
  39.     clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] +\
  40.         modl[ 2] * proj[10] + modl[ 3] * proj[14];
  41.     clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + \
  42.         modl[ 2] * proj[11] + modl[ 3] * proj[15];
  43.     clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] +\
  44.         modl[ 6] * proj[ 8] + modl[ 7] * proj[12];
  45.     clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + \
  46.         modl[ 6] * proj[ 9] + modl[ 7] * proj[13];
  47.     clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] +\
  48.         modl[ 6] * proj[10] + modl[ 7] * proj[14];
  49.     clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + \
  50.         modl[ 6] * proj[11] + modl[ 7] * proj[15];
  51.     clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + \
  52.         modl[10] * proj[ 8] + modl[11] * proj[12];
  53.     clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + \
  54.         modl[10] * proj[ 9] + modl[11] * proj[13];
  55.     clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + \
  56.         modl[10] * proj[10] + modl[11] * proj[14];
  57.     clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + \
  58.         modl[10] * proj[11] + modl[11] * proj[15];
  59.     clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + \
  60.         modl[14] * proj[ 8] + modl[15] * proj[12];
  61.     clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] +
  62.         \modl[14] * proj[ 9] + modl[15] * proj[13];
  63.     clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + \
  64.         modl[14] * proj[10] + modl[15] * proj[14];
  65.     clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + \
  66.         modl[14] * proj[11] + modl[15] * proj[15];
  67.     /* Находим A, B, C, D для ПРАВОЙ плоскости */
  68.     frustum[0][0] = clip[ 3] - clip[ 0];
  69.     frustum[0][1] = clip[ 7] - clip[ 4];
  70.     frustum[0][2] = clip[11] - clip[ 8];
  71.     frustum[0][3] = clip[15] - clip[12];
  72.     /* Приводим уравнение плоскости к нормальному виду */
  73.     t = sqrt( frustum[0][0] * frustum[0][0] + frustum[0][1] * \
  74.         frustum[0][1] + frustum[0][2] * frustum[0][2] );
  75.     frustum[0][0] /= t;
  76.     frustum[0][1] /= t;
  77.     frustum[0][2] /= t;
  78.     frustum[0][3] /= t;
  79.     /* Находим A, B, C, D для ЛЕВОЙ плоскости */
  80.     frustum[1][0] = clip[ 3] + clip[ 0];   
  81.     frustum[1][1] = clip[ 7] + clip[ 4];
  82.     frustum[1][2] = clip[11] + clip[ 8];
  83.     frustum[1][3] = clip[15] + clip[12];
  84.     /* Приводим уравнение плоскости к нормальному виду */
  85.     t = sqrt( frustum[1][0] * frustum[1][0] + frustum[1][1] * \
  86.         frustum[1][1] + frustum[1][2] * frustum[1][2] );
  87.     frustum[1][0] /= t;
  88.     frustum[1][1] /= t;
  89.     frustum[1][2] /= t;
  90.     frustum[1][3] /= t;
  91.     /* Находим A, B, C, D для НИЖНЕЙ плоскости */
  92.     frustum[2][0] = clip[ 3] + clip[ 1];
  93.     frustum[2][1] = clip[ 7] + clip[ 5];
  94.     frustum[2][2] = clip[11] + clip[ 9];
  95.     frustum[2][3] = clip[15] + clip[13];
  96.     /* Приводим уравнение плоскости к нормальному */
  97.     t = sqrt( frustum[2][0] * frustum[2][0] + frustum[2][1] * \
  98.         frustum[2][1] + frustum[2][2] * frustum[2][2] );
  99.     frustum[2][0] /= t;
  100.     frustum[2][1] /= t;
  101.     frustum[2][2] /= t;
  102.     frustum[2][3] /= t;
  103.     /* ВЕРХНЯЯ плоскость */
  104.     frustum[3][0] = clip[ 3] - clip[ 1];
  105.     frustum[3][1] = clip[ 7] - clip[ 5];
  106.     frustum[3][2] = clip[11] - clip[ 9];
  107.     frustum[3][3] = clip[15] - clip[13];
  108.     /* Нормальный вид */
  109.     t = sqrt( frustum[3][0] * frustum[3][0] + frustum[3][1] * \
  110.         frustum[3][1] + frustum[3][2] * frustum[3][2] );
  111.     frustum[3][0] /= t;
  112.     frustum[3][1] /= t;
  113.     frustum[3][2] /= t;
  114.     frustum[3][3] /= t;
  115.     /* ЗАДНЯЯ плоскость */
  116.     frustum[4][0] = clip[ 3] - clip[ 2];
  117.     frustum[4][1] = clip[ 7] - clip[ 6];
  118.     frustum[4][2] = clip[11] - clip[10];
  119.     frustum[4][3] = clip[15] - clip[14];
  120.     /* Нормальный вид */
  121.     t = sqrt( frustum[4][0] * frustum[4][0] + frustum[4][1] * \
  122.         frustum[4][1] + frustum[4][2] * frustum[4][2] );
  123.     frustum[4][0] /= t;
  124.     frustum[4][1] /= t;
  125.     frustum[4][2] /= t;
  126.     frustum[4][3] /= t;
  127.     /* ПЕРЕДНЯЯ плоскость */
  128.     frustum[5][0] = clip[ 3] + clip[ 2];
  129.     frustum[5][1] = clip[ 7] + clip[ 6];
  130.     frustum[5][2] = clip[11] + clip[10];
  131.     frustum[5][3] = clip[15] + clip[14];
  132.     /* Нормальный вид */
  133.     t = sqrt( frustum[5][0] * frustum[5][0] + frustum[5][1] * \
  134.         frustum[5][1] + frustum[5][2] * frustum[5][2] );
  135.     frustum[5][0] /= t;
  136.     frustum[5][1] /= t;
  137.     frustum[5][2] /= t;
  138.     frustum[5][3] /= t;
  139. }
  140.  
  141. bool CubeInFrustum( float x, float y, float z, float size )
  142. {
  143.    int p;
  144.    for( p = 0; p < 6; p++ )
  145.    {
  146.       if( frustum[p][0] * (x - size) + frustum[p][1] * (y - size) + \
  147.           frustum[p][2] * (z - size) + frustum[p][3] > 0 )
  148.          continue;
  149.       if( frustum[p][0] * (x + size) + frustum[p][1] * (y - size) + \
  150.           frustum[p][2] * (z - size) + frustum[p][3] > 0 )
  151.          continue;
  152.       if( frustum[p][0] * (x - size) + frustum[p][1] * (y + size) + \
  153.           frustum[p][2] * (z - size) + frustum[p][3] > 0 )
  154.          continue;
  155.       if( frustum[p][0] * (x + size) + frustum[p][1] * (y + size) + \
  156.           frustum[p][2] * (z - size) + frustum[p][3] > 0 )
  157.          continue;
  158.       if( frustum[p][0] * (x - size) + frustum[p][1] * (y - size) + \
  159.           frustum[p][2] * (z + size) + frustum[p][3] > 0 )
  160.          continue;
  161.       if( frustum[p][0] * (x + size) + frustum[p][1] * (y - size) + \
  162.           frustum[p][2] * (z + size) + frustum[p][3] > 0 )
  163.          continue;
  164.       if( frustum[p][0] * (x - size) + frustum[p][1] * (y + size) + \
  165.           frustum[p][2] * (z + size) + frustum[p][3] > 0 )
  166.          continue;
  167.       if( frustum[p][0] * (x + size) + frustum[p][1] * (y + size) + \
  168.           frustum[p][2] * (z + size) + frustum[p][3] > 0 )
  169.          continue;
  170.       return false;
  171.    }
  172.    return true;
  173. }
  174.  
  175. void keyboard(unsigned char key, int x, int y)
  176. {
  177.     switch (key)
  178.     {
  179.         case 'l':
  180.         {
  181.             HANDLE init  =  \
  182.                 CreateFileA("F:\\Разное\\Проекты\\C++\\life\\InitialLife.init", \
  183.                             GENERIC_READ, FILE_SHARE_READ, NULL,\
  184.                             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  185.             ReadFile(init, space, size * size * size * sizeof(a), &real, NULL);
  186.             CloseHandle(init);
  187.             break;
  188.         }
  189.         case 'w': camZ -= 0.5; printf("X = %f,  Z = %f,  camZ = %f\n", \
  190.                       camX, size * 2 + camZ, camZ); break;
  191.         case 's': camZ += 0.5; printf("X = %f,  Z = %f,  camZ = %f\n", \
  192.                       camX, size * 2 + camZ, camZ); break;
  193.         case 'a': camX -= 0.5; printf("X = %f,  Z = %f,  camZ = %f\n", \
  194.                       camX, size * 2 + camZ, camZ); break;
  195.         case 'd': camX += 0.5; printf("X = %f,  Z = %f,  camZ = %f\n", \
  196.                       camX, size * 2 + camZ, camZ); break;
  197.         case 'z': camZ += 5; break;
  198.         case 'x': camZ -= 5; break;
  199.         case 'n':
  200.         {
  201.             sprintf(Name, "F:\\Разное\\Проекты\\C++\\life\\Step%i.step", step);
  202.             HANDLE stp = CreateFileA(Name, GENERIC_READ, FILE_SHARE_READ, \
  203.                             NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  204.             ReadFile(stp, space, size * size * size * sizeof(a), &real, NULL);
  205.             CloseHandle(stp);
  206.             if (step < stepNum)
  207.                 step++;
  208.         }
  209.     }
  210. }
  211.  
  212. void mouse_f(int button, int state, int x, int y)
  213. {
  214.     if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  215.     {
  216.         X = x;
  217.         Y = y;
  218.     }
  219. }
  220.  
  221. void mouse(int x, int y)
  222. {
  223.     float newX, newY;
  224.     newX = x - 320;
  225.     newY = y - 240;
  226.     diffX = newX - X;
  227.     diffY = newY - Y;
  228.     rot_x -= float(X - x)/2.0f;
  229.     rot_y -= float(Y - y)/2.0f;
  230.     X = x;
  231.     Y = y;
  232. }
  233.  
  234. void resize(int width,int height)
  235. {
  236.    glViewport(0,0,width,height);
  237.    glMatrixMode(GL_PROJECTION);
  238.    glLoadIdentity();
  239.    gluPerspective(50, 4.0/3.0, 1, -10);
  240.    gluLookAt( 0,0,30, 0,0,0, 0,1,0 );
  241.    glMatrixMode( GL_MODELVIEW );
  242. }    
  243.  
  244. void timer (int val)
  245. {
  246.     glutTimerFunc(10, timer, 0);
  247. }
  248.  
  249. void cube (int x, int y, int z, int a, float r)
  250. {
  251.     glPushMatrix();
  252.     glTranslatef(x, y, z);
  253.     if (r == 2)
  254.         glColor3f(1, 1, 0);
  255.     else if (r == 3)
  256.         glColor3f(1, 0, 0);
  257.     else
  258.         glColor3f(0.9, 0.2, 0.5);
  259.     glutSolidCube(a);
  260.     glColor3f(0, 0, 0);
  261.     glutWireCube(a+0.01);
  262.     glPopMatrix();
  263. }
  264.  
  265. void display(void)
  266. {
  267.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  268.     glClearColor(1, 1, 1, 1.0);
  269.     glLineWidth(2);
  270.     glMatrixMode(GL_MODELVIEW);
  271.     glPushMatrix();
  272.     gluLookAt( camX,0,size * 2 + camZ, camX,0,camZ, 0,1,0 );
  273.     glRotatef(rot_x, 0, 1 ,0);
  274.     glRotatef(rot_y, 1, 0, 0);
  275.     int time = clock(), time1;
  276.     ExtractFrustum();
  277.     int Vnum = 0, num = 0;
  278.     if (flag)
  279.     {
  280.         for (int i = 0; i < size; i++)
  281.         {
  282.             for (int j = 0; j < size; j++)
  283.                 for (int k = 0; k < size; k++)
  284.                     if ((float)space[i][j][k] != 0)
  285.                     {
  286.                         {
  287.                         cube((i-size/2) * scale, (j-size/2) * scale, \
  288.                         (k-size/2) * scale, scale, 1 - space[i][j][k]/400.0);
  289.                         Vnum++;
  290.                         }
  291.                         num++;
  292.                     }
  293.         }
  294.     }
  295.     time1 = clock() - time;
  296.     glPopMatrix();
  297.     sprintf(str, "FPS: %6.2f,  Cubes: %i,  Visible_cubes: %i, Step: %i", \
  298.         1000.0 / float(time1), num, Vnum, step);
  299.     float n = 0;
  300.     while (str[int(n)] != '\0')
  301.     {
  302.         glRasterPos2d(-13.5 + n * 0.4, -13.5);
  303.         glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, str[int(n)]);
  304.         n += 1;
  305.     }
  306.     glutSwapBuffers();  
  307. }
  308.  
  309. int main(int argc, char *argv[])
  310. {
  311.     glutInit(&argc,argv);
  312.     glutInitWindowPosition(50, 50);
  313.     glutInitWindowSize(640, 480);
  314.     glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  315.     glutCreateWindow("OpenGL");
  316.     glutTimerFunc(10, timer, 0);
  317.     glutIdleFunc(display);
  318.     glutDisplayFunc(display);
  319.     glutReshapeFunc(resize);
  320.     glutKeyboardFunc(keyboard);
  321.     glutMotionFunc(mouse);
  322.     glutMouseFunc(mouse_f);
  323.     glEnable(GL_DEPTH_TEST);
  324.     glEnable(GL_COLOR_MATERIAL);
  325.     glEnable(GL_LIGHTING);
  326.     glEnable(GL_LIGHT0);
  327.     glutMainLoop();
  328.     return 0;
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement