Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include "freeglut.h"
- #include <cmath>
- #include <cstdio>
- #include <time.h>
- #include <random>
- using namespace std;
- float X = 0, Y = 0, Z = 0, diffX, diffY, camX = 0, camZ = 0;
- float rot_x = 0;
- float rot_y = 0;
- float frustum[6][4];
- bool flag = 1;
- const int size = 30;
- int space[size][size][size], a;
- const int scale = 2;
- const int stepNum = 200;
- int step = 1;
- char Name[255] = "";
- DWORD real = 0;
- char str[255] = "";
- void ExtractFrustum()
- {
- float proj[16];
- float modl[16];
- float clip[16];
- float t;
- /* Узнаем текущую матрицу PROJECTION */
- glGetFloatv( GL_PROJECTION_MATRIX, proj );
- /* Узнаем текущую матрицу MODELVIEW */
- glGetFloatv( GL_MODELVIEW_MATRIX, modl );
- /* Комбинируем матрицы(перемножаем) */
- clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + \
- modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
- clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + \
- modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
- clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] +\
- modl[ 2] * proj[10] + modl[ 3] * proj[14];
- clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + \
- modl[ 2] * proj[11] + modl[ 3] * proj[15];
- clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] +\
- modl[ 6] * proj[ 8] + modl[ 7] * proj[12];
- clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + \
- modl[ 6] * proj[ 9] + modl[ 7] * proj[13];
- clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] +\
- modl[ 6] * proj[10] + modl[ 7] * proj[14];
- clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + \
- modl[ 6] * proj[11] + modl[ 7] * proj[15];
- clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + \
- modl[10] * proj[ 8] + modl[11] * proj[12];
- clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + \
- modl[10] * proj[ 9] + modl[11] * proj[13];
- clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + \
- modl[10] * proj[10] + modl[11] * proj[14];
- clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + \
- modl[10] * proj[11] + modl[11] * proj[15];
- clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + \
- modl[14] * proj[ 8] + modl[15] * proj[12];
- clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] +
- \modl[14] * proj[ 9] + modl[15] * proj[13];
- clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + \
- modl[14] * proj[10] + modl[15] * proj[14];
- clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + \
- modl[14] * proj[11] + modl[15] * proj[15];
- /* Находим A, B, C, D для ПРАВОЙ плоскости */
- frustum[0][0] = clip[ 3] - clip[ 0];
- frustum[0][1] = clip[ 7] - clip[ 4];
- frustum[0][2] = clip[11] - clip[ 8];
- frustum[0][3] = clip[15] - clip[12];
- /* Приводим уравнение плоскости к нормальному виду */
- t = sqrt( frustum[0][0] * frustum[0][0] + frustum[0][1] * \
- frustum[0][1] + frustum[0][2] * frustum[0][2] );
- frustum[0][0] /= t;
- frustum[0][1] /= t;
- frustum[0][2] /= t;
- frustum[0][3] /= t;
- /* Находим A, B, C, D для ЛЕВОЙ плоскости */
- frustum[1][0] = clip[ 3] + clip[ 0];
- frustum[1][1] = clip[ 7] + clip[ 4];
- frustum[1][2] = clip[11] + clip[ 8];
- frustum[1][3] = clip[15] + clip[12];
- /* Приводим уравнение плоскости к нормальному виду */
- t = sqrt( frustum[1][0] * frustum[1][0] + frustum[1][1] * \
- frustum[1][1] + frustum[1][2] * frustum[1][2] );
- frustum[1][0] /= t;
- frustum[1][1] /= t;
- frustum[1][2] /= t;
- frustum[1][3] /= t;
- /* Находим A, B, C, D для НИЖНЕЙ плоскости */
- frustum[2][0] = clip[ 3] + clip[ 1];
- frustum[2][1] = clip[ 7] + clip[ 5];
- frustum[2][2] = clip[11] + clip[ 9];
- frustum[2][3] = clip[15] + clip[13];
- /* Приводим уравнение плоскости к нормальному */
- t = sqrt( frustum[2][0] * frustum[2][0] + frustum[2][1] * \
- frustum[2][1] + frustum[2][2] * frustum[2][2] );
- frustum[2][0] /= t;
- frustum[2][1] /= t;
- frustum[2][2] /= t;
- frustum[2][3] /= t;
- /* ВЕРХНЯЯ плоскость */
- frustum[3][0] = clip[ 3] - clip[ 1];
- frustum[3][1] = clip[ 7] - clip[ 5];
- frustum[3][2] = clip[11] - clip[ 9];
- frustum[3][3] = clip[15] - clip[13];
- /* Нормальный вид */
- t = sqrt( frustum[3][0] * frustum[3][0] + frustum[3][1] * \
- frustum[3][1] + frustum[3][2] * frustum[3][2] );
- frustum[3][0] /= t;
- frustum[3][1] /= t;
- frustum[3][2] /= t;
- frustum[3][3] /= t;
- /* ЗАДНЯЯ плоскость */
- frustum[4][0] = clip[ 3] - clip[ 2];
- frustum[4][1] = clip[ 7] - clip[ 6];
- frustum[4][2] = clip[11] - clip[10];
- frustum[4][3] = clip[15] - clip[14];
- /* Нормальный вид */
- t = sqrt( frustum[4][0] * frustum[4][0] + frustum[4][1] * \
- frustum[4][1] + frustum[4][2] * frustum[4][2] );
- frustum[4][0] /= t;
- frustum[4][1] /= t;
- frustum[4][2] /= t;
- frustum[4][3] /= t;
- /* ПЕРЕДНЯЯ плоскость */
- frustum[5][0] = clip[ 3] + clip[ 2];
- frustum[5][1] = clip[ 7] + clip[ 6];
- frustum[5][2] = clip[11] + clip[10];
- frustum[5][3] = clip[15] + clip[14];
- /* Нормальный вид */
- t = sqrt( frustum[5][0] * frustum[5][0] + frustum[5][1] * \
- frustum[5][1] + frustum[5][2] * frustum[5][2] );
- frustum[5][0] /= t;
- frustum[5][1] /= t;
- frustum[5][2] /= t;
- frustum[5][3] /= t;
- }
- bool CubeInFrustum( float x, float y, float z, float size )
- {
- int p;
- for( p = 0; p < 6; p++ )
- {
- if( frustum[p][0] * (x - size) + frustum[p][1] * (y - size) + \
- frustum[p][2] * (z - size) + frustum[p][3] > 0 )
- continue;
- if( frustum[p][0] * (x + size) + frustum[p][1] * (y - size) + \
- frustum[p][2] * (z - size) + frustum[p][3] > 0 )
- continue;
- if( frustum[p][0] * (x - size) + frustum[p][1] * (y + size) + \
- frustum[p][2] * (z - size) + frustum[p][3] > 0 )
- continue;
- if( frustum[p][0] * (x + size) + frustum[p][1] * (y + size) + \
- frustum[p][2] * (z - size) + frustum[p][3] > 0 )
- continue;
- if( frustum[p][0] * (x - size) + frustum[p][1] * (y - size) + \
- frustum[p][2] * (z + size) + frustum[p][3] > 0 )
- continue;
- if( frustum[p][0] * (x + size) + frustum[p][1] * (y - size) + \
- frustum[p][2] * (z + size) + frustum[p][3] > 0 )
- continue;
- if( frustum[p][0] * (x - size) + frustum[p][1] * (y + size) + \
- frustum[p][2] * (z + size) + frustum[p][3] > 0 )
- continue;
- if( frustum[p][0] * (x + size) + frustum[p][1] * (y + size) + \
- frustum[p][2] * (z + size) + frustum[p][3] > 0 )
- continue;
- return false;
- }
- return true;
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 'l':
- {
- HANDLE init = \
- CreateFileA("F:\\Разное\\Проекты\\C++\\life\\InitialLife.init", \
- GENERIC_READ, FILE_SHARE_READ, NULL,\
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- ReadFile(init, space, size * size * size * sizeof(a), &real, NULL);
- CloseHandle(init);
- break;
- }
- case 'w': camZ -= 0.5; printf("X = %f, Z = %f, camZ = %f\n", \
- camX, size * 2 + camZ, camZ); break;
- case 's': camZ += 0.5; printf("X = %f, Z = %f, camZ = %f\n", \
- camX, size * 2 + camZ, camZ); break;
- case 'a': camX -= 0.5; printf("X = %f, Z = %f, camZ = %f\n", \
- camX, size * 2 + camZ, camZ); break;
- case 'd': camX += 0.5; printf("X = %f, Z = %f, camZ = %f\n", \
- camX, size * 2 + camZ, camZ); break;
- case 'z': camZ += 5; break;
- case 'x': camZ -= 5; break;
- case 'n':
- {
- sprintf(Name, "F:\\Разное\\Проекты\\C++\\life\\Step%i.step", step);
- HANDLE stp = CreateFileA(Name, GENERIC_READ, FILE_SHARE_READ, \
- NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- ReadFile(stp, space, size * size * size * sizeof(a), &real, NULL);
- CloseHandle(stp);
- if (step < stepNum)
- step++;
- }
- }
- }
- void mouse_f(int button, int state, int x, int y)
- {
- if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
- {
- X = x;
- Y = y;
- }
- }
- void mouse(int x, int y)
- {
- float newX, newY;
- newX = x - 320;
- newY = y - 240;
- diffX = newX - X;
- diffY = newY - Y;
- rot_x -= float(X - x)/2.0f;
- rot_y -= float(Y - y)/2.0f;
- X = x;
- Y = y;
- }
- void resize(int width,int height)
- {
- glViewport(0,0,width,height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(50, 4.0/3.0, 1, -10);
- gluLookAt( 0,0,30, 0,0,0, 0,1,0 );
- glMatrixMode( GL_MODELVIEW );
- }
- void timer (int val)
- {
- glutTimerFunc(10, timer, 0);
- }
- void cube (int x, int y, int z, int a, float r)
- {
- glPushMatrix();
- glTranslatef(x, y, z);
- if (r == 2)
- glColor3f(1, 1, 0);
- else if (r == 3)
- glColor3f(1, 0, 0);
- else
- glColor3f(0.9, 0.2, 0.5);
- glutSolidCube(a);
- glColor3f(0, 0, 0);
- glutWireCube(a+0.01);
- glPopMatrix();
- }
- void display(void)
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glClearColor(1, 1, 1, 1.0);
- glLineWidth(2);
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- gluLookAt( camX,0,size * 2 + camZ, camX,0,camZ, 0,1,0 );
- glRotatef(rot_x, 0, 1 ,0);
- glRotatef(rot_y, 1, 0, 0);
- int time = clock(), time1;
- ExtractFrustum();
- int Vnum = 0, num = 0;
- if (flag)
- {
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- for (int k = 0; k < size; k++)
- if ((float)space[i][j][k] != 0)
- {
- {
- cube((i-size/2) * scale, (j-size/2) * scale, \
- (k-size/2) * scale, scale, 1 - space[i][j][k]/400.0);
- Vnum++;
- }
- num++;
- }
- }
- }
- time1 = clock() - time;
- glPopMatrix();
- sprintf(str, "FPS: %6.2f, Cubes: %i, Visible_cubes: %i, Step: %i", \
- 1000.0 / float(time1), num, Vnum, step);
- float n = 0;
- while (str[int(n)] != '\0')
- {
- glRasterPos2d(-13.5 + n * 0.4, -13.5);
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, str[int(n)]);
- n += 1;
- }
- glutSwapBuffers();
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc,argv);
- glutInitWindowPosition(50, 50);
- glutInitWindowSize(640, 480);
- glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow("OpenGL");
- glutTimerFunc(10, timer, 0);
- glutIdleFunc(display);
- glutDisplayFunc(display);
- glutReshapeFunc(resize);
- glutKeyboardFunc(keyboard);
- glutMotionFunc(mouse);
- glutMouseFunc(mouse_f);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement