Accueil > > > JEU DE LA VIE 3D OPENGL AVEC GESTION SOURIS
JEU DE LA VIE 3D OPENGL AVEC GESTION SOURIS
Information sur la source
Description
Jeu de la vie en 3D sous OpenGL avec gestion de la souris. Ma première source en OpenGL donc soyez indulgent :) Espace : avancer la vie r : reset un nouveau cube ESC : exit Etant donné que le jeu de la vie n'a de règles strictes qu'en 2D, on trouve de tout en 3D. Dans cette version, la cellule vit si elle a 3 ou 4 voisins vivants, sinon elle meurt.
Source
- #include "stdio.h"
- #include "windows.h"
- #include <conio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "gl/gl.h"
- #include "gl/glu.h"
- #define FREEGLUT_STATIC
- #include "freeglut/glut.h"
- #include "math.h"
-
- #define DEGREES_PER_PIXEL 0.6f
- #define RADIANS_PER_PIXEL 0.002f
- #define UNITS_PER_PIXEL 0.01f
-
- static float eyex, eyey, eyez; // eye x,y,z values for gluLookAt (location of eye)
- static float focusx, focusy, focusz; // the point the eye is looking at
- struct g_mouseState{
- bool leftButton;
- bool rightButton;
- bool middleButton;
- int x;
- int y;
- } MouseState;
-
- class Cellule
- {
- public:
- short num;
- short col;
- short row;
- short depth;
- short state;
- short neighbour;
-
- Cellule(short nu=0,short c=0,short r=0,short d=0,short s=0, short n=0);
- short Touch_Edge();
- };
-
- Cellule::Cellule(short nu,short c,short r,short d,short s, short n)
- {
- num=nu;
- col=c;
- row=r;
- depth=d;
- state=s;
- neighbour=n;
- }
- //Dynamic creation of the cells tab
- Cellule *Cell = new Cellule [8*8*8];
-
- short Cellule::Touch_Edge()
- {
- if (this->col==0)
- {
- if (this->row==0)
- {
- if (this->depth==0)return 7;
- else if (this->depth==7)return 18;
- else return 10;
- }
- else if (this->row==7)
- {
- if (this->depth==0)return 1;
- else if (this->depth==7)return 24;
- else return 16;
- }
- else
- {
- if (this->depth==0)return 4;
- else if (this->depth==7)return 25;
- else return 17;
- }
- }
- else if (this->col==7)
- {
- if (this->row==0)
- {
- if (this->depth==0)return 9;
- else if (this->depth==7)return 20;
- else return 12;
- }
- else if (this->row==7)
- {
- if (this->depth==0)return 3;
- else if (this->depth==7)return 22;
- else return 14;
- }
- else
- {
- if (this->depth==0)return 6;
- else if (this->depth==7)return 21;
- else return 13;
- }
- }
- else if (this->row==0)
- {
- if (this->depth==0)return 8;
- else if (this->depth==7)return 19;
- else return 11;
- }
- else if (this->row==7)
- {
- if (this->depth==0)return 2;
- else if (this->depth==7)return 23;
- else return 15;
- }
- else
- {
- if (this->depth==0)return 5;
- else if (this->depth==7)return 26;
- else return 0;
- }
-
- }
- //get neighbour depends on the placement of the cell
- //26 different cases in three dimensions ... sigh...
- //there's possibilities to optimize this
- void Get_Neighbour(short number,Cellule *CellTab)
- {
- short Nbr_Neighbour=0;
- short Edge=CellTab[number].Touch_Edge();
- switch (Edge)
- {
- case 0 : Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64+8+1].state+
- CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
- CellTab[number-64-8-1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
- CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
- CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
- CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
- CellTab[number+64-8+1].state+CellTab[number+64+1].state+CellTab[number+64+8+1].state+
- CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 1 : Nbr_Neighbour=CellTab[number+1].state+CellTab[number-8].state+CellTab[number-8+1].state+
- CellTab[number+64-8].state+CellTab[number+64-8+1].state+CellTab[number+64+1].state+
- CellTab[number+64].state;
- break;
- case 2 : Nbr_Neighbour=CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
- CellTab[number+1].state+CellTab[number-1].state+CellTab[number+64-8-1].state+
- CellTab[number+64-8].state+CellTab[number+64-8+1].state+CellTab[number+64+1].state+
- CellTab[number+64-1].state+CellTab[number+64].state;
- break;
- case 3 : Nbr_Neighbour=CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-1].state+
- CellTab[number+64-8-1].state+CellTab[number+64-8].state+CellTab[number+64-1].state+
- CellTab[number+64].state;
- break;
- case 4 : Nbr_Neighbour=CellTab[number-8].state+CellTab[number-8+1].state+CellTab[number+1].state+
- CellTab[number+8+1].state+CellTab[number+64-8].state+CellTab[number+64-8+1].state+
- CellTab[number+64+1].state+CellTab[number+64+8+1].state+CellTab[number+64+8].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 5 : Nbr_Neighbour=CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
- CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
- CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
- CellTab[number+64-8+1].state+CellTab[number+64+1].state+CellTab[number+64+8+1].state+
- CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 6 : Nbr_Neighbour=CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number+8-1].state+
- CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
- CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 7 : Nbr_Neighbour=CellTab[number+1].state+CellTab[number+8+1].state+ CellTab[number+64+1].state+
- CellTab[number+64+8+1].state+CellTab[number+64+8].state+CellTab[number+64].state+
- CellTab[number+8].state;
- break;
- case 8 : Nbr_Neighbour=CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
- CellTab[number-1].state+CellTab[number+64+1].state+CellTab[number+64+8+1].state+
- CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 9 : Nbr_Neighbour=CellTab[number+8-1].state+CellTab[number-1].state+CellTab[number+64+8].state+
- CellTab[number+64+8-1].state+CellTab[number+64-1].state+CellTab[number+64].state+
- CellTab[number+8].state;
- break;
- case 10 :Nbr_Neighbour=CellTab[number-64+8].state+CellTab[number-64+8+1].state+CellTab[number-64].state+
- CellTab[number-64+1].state+CellTab[number+1].state+CellTab[number+8+1].state+
- CellTab[number+64+1].state+CellTab[number+64+8+1].state+CellTab[number+64+8].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 11 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64+8+1].state+
- CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
- CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
- CellTab[number-1].state+CellTab[number+64+1].state+CellTab[number+64+8+1].state+
- CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 12 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64-1].state+
- CellTab[number-64].state+CellTab[number+8-1].state+CellTab[number-1].state+
- CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 13 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64-1].state+
- CellTab[number-64].state+CellTab[number-64-8-1].state+CellTab[number-64-8].state+
- CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number+8-1].state+
- CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
- CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 14 :Nbr_Neighbour=CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64-8-1].state+
- CellTab[number-64-8].state+CellTab[number-8-1].state+CellTab[number-8].state+
- CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
- CellTab[number+64-1].state+CellTab[number+64].state;
- break;
- case 15 :Nbr_Neighbour=CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
- CellTab[number-64-8-1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
- CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
- CellTab[number+1].state+CellTab[number-1].state+CellTab[number+64-8-1].state+
- CellTab[number+64-8].state+CellTab[number+64-8+1].state+CellTab[number+64+1].state+
- CellTab[number+64-1].state+CellTab[number+64].state;
- break;
- case 16 :Nbr_Neighbour=CellTab[number-64].state+CellTab[number-64+1].state+CellTab[number-64-8].state+
- CellTab[number-64-8+1].state+CellTab[number-8].state+CellTab[number-8+1].state+
- CellTab[number+1].state+CellTab[number+64-8].state+CellTab[number+64-8+1].state+
- CellTab[number+64+1].state+CellTab[number+64].state;
- break;
- case 17 :Nbr_Neighbour=CellTab[number-64+8].state+CellTab[number-64+8+1].state+CellTab[number-64].state+
- CellTab[number-64+1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
- CellTab[number-8].state+CellTab[number-8+1].state+CellTab[number+1].state+
- CellTab[number+8+1].state+CellTab[number+64-8].state+CellTab[number+64-8+1].state+
- CellTab[number+64+1].state+CellTab[number+64+8+1].state+CellTab[number+64+8].state+
- CellTab[number+64].state+CellTab[number+8].state;
- break;
- case 18 :Nbr_Neighbour=CellTab[number-64+8].state+CellTab[number-64+8+1].state+CellTab[number-64].state+
- CellTab[number-64+1].state+CellTab[number+1].state+CellTab[number+8+1].state+
- CellTab[number+8].state;
- break;
- case 19 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64+8+1].state+
- CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
- CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
- CellTab[number-1].state+CellTab[number+8].state;
- break;
- case 20 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64-1].state+
- CellTab[number-64].state+CellTab[number+8-1].state+CellTab[number-1].state+
- CellTab[number+8].state;
- break;
- case 21 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64-1].state+
- CellTab[number-64].state+CellTab[number-64-8-1].state+CellTab[number-64-8].state+
- CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number+8-1].state+
- CellTab[number-1].state+CellTab[number+8].state;
- break;
- case 22 :Nbr_Neighbour=
- CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64-8-1].state+
- CellTab[number-64-8].state+CellTab[number-8-1].state+CellTab[number-8].state+
- CellTab[number-1].state;
- break;
- case 23 :Nbr_Neighbour=CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
- CellTab[number-64-8-1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
- CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
- CellTab[number+1].state+CellTab[number-1].state;
- break;
- case 24 :Nbr_Neighbour=CellTab[number-64].state+CellTab[number-64+1].state+CellTab[number-64-8].state+
- CellTab[number-64-8+1].state+CellTab[number-8].state+CellTab[number-8+1].state+
- CellTab[number+1].state;
- break;
- case 25 :Nbr_Neighbour=CellTab[number-64+8].state+CellTab[number-64+8+1].state+CellTab[number-64].state+
- CellTab[number-64+1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
- CellTab[number-8].state+CellTab[number-8+1].state+CellTab[number+1].state+
- CellTab[number+8+1].state+CellTab[number+8].state;
- break;
- case 26 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64+8+1].state+
- CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
- CellTab[number-64-8-1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
- CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
- CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
- CellTab[number-1].state+CellTab[number+8].state;
- break;
- default: break;
- }
- CellTab[number].neighbour=Nbr_Neighbour;
- }
-
- void Random_Tab(Cellule *Cell)
- {
- srand((unsigned)time(NULL));
- for (int k=0;k<8;k++)
- {
- for (int i=0;i<8;i++)
- {
- for (int j=0;j<8;j++)
- {
- Cell[64*k+8*j+i].state=rand()%2;
- }
- }
- }
- }
-
-
- void Init_Tab(Cellule *Cell)
- {
- for (int k=0;k<8;k++)
- {
- for (int j=0;j<8;j++)
- {
- for (int i=0;i<8;i++)
- {
- Cell[64*k+8*j+i].num = 64*k+8*j+i;
- Cell[64*k+8*j+i].col =i;
- Cell[64*k+8*j+i].row =j;
- Cell[64*k+8*j+i].depth =k;
- }
- }
- }
- Random_Tab(Cell);
- }
-
- void Update_Life(Cellule *Cell)
- {
- Cellule CellOld[8*8*8];
- //save states
- for (int k=0;k<8;k++)
- {
- for (int j=0;j<8;j++)
- {
- for (int i=0;i<8;i++)
- {
- CellOld[64*k+8*j+i].state=Cell[64*k+8*j+i].state;
- }
- }
- }
-
- for (int k=0;k<8;k++)
- {
- for (int j=0;j<8;j++)
- {
- for (int i=0;i<8;i++)
- {
- Get_Neighbour(64*k+8*j+i,Cell);
-
- if ((Cell[64*k+8*j+i].neighbour>=3)&&(Cell[64*k+8*j+i].neighbour<=4))CellOld[64*k+8*j+i].state=1;
- else if ((Cell[64*k+8*j+i].neighbour<2)||(Cell[64*k+8*j+i].neighbour>4))CellOld[64*k+8*j+i].state=0;
- }
- }
- }
- //load states
- for (int k=0;k<8;k++)
- {
- for (int j=0;j<8;j++)
- {
- for (int i=0;i<8;i++)
- {
- Cell[64*k+8*j+i].state=CellOld[64*k+8*j+i].state;
- }
- }
- }
- }
-
- float g_xRotation, g_yRotation;
- static float lmodel_ambient[4] = {0.1f, 0.1f, 0.1f, 1.0f};
-
- void HandleMouseState(int button, int state, int x, int y)
- {
- if(button == GLUT_LEFT_BUTTON)
- {
- if(state == GLUT_DOWN)
- MouseState.leftButton = true;
- else
- MouseState.leftButton = false;
- }
- if(button == GLUT_RIGHT_BUTTON)
- {
- if(state == GLUT_DOWN)
- MouseState.rightButton = true;
- else
- MouseState.rightButton = false;
- }
- if(button == GLUT_MIDDLE_BUTTON)
- {
- if(state == GLUT_DOWN)
- MouseState.middleButton = true;
- else
- MouseState.middleButton = false;
- }
- // update our position so we know a delta when the mouse is moved
- MouseState.x = x;
- MouseState.y = y;
- }
-
- void HandleMouseMove(int x, int y)
- {
- // calculate a delta in movement
- int yDelta = MouseState.y - y;
- int xDelta = MouseState.x - x;
- // commit the mouse position
- MouseState.x = x;
- MouseState.y = y;
-
- // when we need to rotate (only the left button is down)
- if(MouseState.leftButton && !MouseState.rightButton && !MouseState.middleButton)
- {
- // rotate by the delta
- g_xRotation -= xDelta * DEGREES_PER_PIXEL;
- g_yRotation -= yDelta * DEGREES_PER_PIXEL;
- }
- // if we need to move translate (left and right buttons are down
- else if(MouseState.leftButton && MouseState.rightButton && !MouseState.middleButton)
- {
- // move our eye
- eyex += xDelta * UNITS_PER_PIXEL;
- eyey -= yDelta * UNITS_PER_PIXEL;
-
- // move our focus point
- focusx += xDelta * UNITS_PER_PIXEL;
- focusy -= yDelta * UNITS_PER_PIXEL;
- }
- glutPostRedisplay();
- }
-
- void handleKeypress(unsigned char key, int x, int y) {
- switch (key) {
- case 27: //Escape key
- delete Cell;
- exit(0);
- break;
- case ' ': //update_life
- Update_Life(Cell);
- glutPostRedisplay();
- break;
- case 'r': //reset
- Init_Tab(Cell);
- glutPostRedisplay();
- break;
- }
- }
- void Draw()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
- //Add positioned light
- GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f};
- GLfloat lightPos0[] = {1.0f, 0.0f, 8.0f, 1.0f};
- glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
- glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
- //Add directed light
- GLfloat lightColor1[] = {1.0f, 1.0f, 0.2f, 1.0f};
- GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
- glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
- glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
-
- gluLookAt(eyex, eyey, eyez, focusx, focusy, focusz, 0,1,0);
- glRotatef(g_xRotation, 0,1,1);
- glRotatef(g_yRotation, 1,1,0);
- glBegin(GL_QUADS);
-
- for (int k=0;k<8;k++){for (int j=0;j<8;j++){for (int i=0;i<8;i++)
- {
- if (Cell[64*k+8*j+i].state==0)glColor3f(0.0f, 0.0f, 0.0f);
- else glColor3f(1.0f, 1.0f, 1.0f);
- //front
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
- //back
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
- //top
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
- //bottom
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
- //left
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
- glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
- //right
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
- glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
- }}}
- glEnd();
- glPopMatrix();
- glutSwapBuffers();
- }
-
- void InitRenderer()
- {
- glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(40.0f, 1.0f, 0.1f, 40.0f);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHT1);
- glEnable(GL_NORMALIZE);
- glEnable(GL_COLOR_MATERIAL);
- // starting rotation.
- g_yRotation = -45.0f;
- g_xRotation = 30.0f;
-
- //initialize the mouse state
- MouseState.leftButton = MouseState.rightButton = MouseState.middleButton = false;
- MouseState.x = MouseState.y = 0;
- // init our eye location
- eyex = -10;
- eyey = -10;
- eyez = 30;
- focusx = focusy = focusz = 0.0f;
- }
-
-
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowSize(800,600);
- glutCreateWindow("Game of Life");
- glutDisplayFunc(Draw);
- glutKeyboardFunc(handleKeypress);
- glutMouseFunc(HandleMouseState);
- glutMotionFunc(HandleMouseMove);
- InitRenderer();
- Init_Tab(Cell);
- glutMainLoop();
- delete Cell;
- return 0;
- }
#include "stdio.h"
#include "windows.h"
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include "gl/gl.h"
#include "gl/glu.h"
#define FREEGLUT_STATIC
#include "freeglut/glut.h"
#include "math.h"
#define DEGREES_PER_PIXEL 0.6f
#define RADIANS_PER_PIXEL 0.002f
#define UNITS_PER_PIXEL 0.01f
static float eyex, eyey, eyez; // eye x,y,z values for gluLookAt (location of eye)
static float focusx, focusy, focusz; // the point the eye is looking at
struct g_mouseState{
bool leftButton;
bool rightButton;
bool middleButton;
int x;
int y;
} MouseState;
class Cellule
{
public:
short num;
short col;
short row;
short depth;
short state;
short neighbour;
Cellule(short nu=0,short c=0,short r=0,short d=0,short s=0, short n=0);
short Touch_Edge();
};
Cellule::Cellule(short nu,short c,short r,short d,short s, short n)
{
num=nu;
col=c;
row=r;
depth=d;
state=s;
neighbour=n;
}
//Dynamic creation of the cells tab
Cellule *Cell = new Cellule [8*8*8];
short Cellule::Touch_Edge()
{
if (this->col==0)
{
if (this->row==0)
{
if (this->depth==0)return 7;
else if (this->depth==7)return 18;
else return 10;
}
else if (this->row==7)
{
if (this->depth==0)return 1;
else if (this->depth==7)return 24;
else return 16;
}
else
{
if (this->depth==0)return 4;
else if (this->depth==7)return 25;
else return 17;
}
}
else if (this->col==7)
{
if (this->row==0)
{
if (this->depth==0)return 9;
else if (this->depth==7)return 20;
else return 12;
}
else if (this->row==7)
{
if (this->depth==0)return 3;
else if (this->depth==7)return 22;
else return 14;
}
else
{
if (this->depth==0)return 6;
else if (this->depth==7)return 21;
else return 13;
}
}
else if (this->row==0)
{
if (this->depth==0)return 8;
else if (this->depth==7)return 19;
else return 11;
}
else if (this->row==7)
{
if (this->depth==0)return 2;
else if (this->depth==7)return 23;
else return 15;
}
else
{
if (this->depth==0)return 5;
else if (this->depth==7)return 26;
else return 0;
}
}
//get neighbour depends on the placement of the cell
//26 different cases in three dimensions ... sigh...
//there's possibilities to optimize this
void Get_Neighbour(short number,Cellule *CellTab)
{
short Nbr_Neighbour=0;
short Edge=CellTab[number].Touch_Edge();
switch (Edge)
{
case 0 : Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64+8+1].state+
CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
CellTab[number-64-8-1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
CellTab[number+64-8+1].state+CellTab[number+64+1].state+CellTab[number+64+8+1].state+
CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 1 : Nbr_Neighbour=CellTab[number+1].state+CellTab[number-8].state+CellTab[number-8+1].state+
CellTab[number+64-8].state+CellTab[number+64-8+1].state+CellTab[number+64+1].state+
CellTab[number+64].state;
break;
case 2 : Nbr_Neighbour=CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
CellTab[number+1].state+CellTab[number-1].state+CellTab[number+64-8-1].state+
CellTab[number+64-8].state+CellTab[number+64-8+1].state+CellTab[number+64+1].state+
CellTab[number+64-1].state+CellTab[number+64].state;
break;
case 3 : Nbr_Neighbour=CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-1].state+
CellTab[number+64-8-1].state+CellTab[number+64-8].state+CellTab[number+64-1].state+
CellTab[number+64].state;
break;
case 4 : Nbr_Neighbour=CellTab[number-8].state+CellTab[number-8+1].state+CellTab[number+1].state+
CellTab[number+8+1].state+CellTab[number+64-8].state+CellTab[number+64-8+1].state+
CellTab[number+64+1].state+CellTab[number+64+8+1].state+CellTab[number+64+8].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 5 : Nbr_Neighbour=CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
CellTab[number+64-8+1].state+CellTab[number+64+1].state+CellTab[number+64+8+1].state+
CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 6 : Nbr_Neighbour=CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number+8-1].state+
CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 7 : Nbr_Neighbour=CellTab[number+1].state+CellTab[number+8+1].state+ CellTab[number+64+1].state+
CellTab[number+64+8+1].state+CellTab[number+64+8].state+CellTab[number+64].state+
CellTab[number+8].state;
break;
case 8 : Nbr_Neighbour=CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
CellTab[number-1].state+CellTab[number+64+1].state+CellTab[number+64+8+1].state+
CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 9 : Nbr_Neighbour=CellTab[number+8-1].state+CellTab[number-1].state+CellTab[number+64+8].state+
CellTab[number+64+8-1].state+CellTab[number+64-1].state+CellTab[number+64].state+
CellTab[number+8].state;
break;
case 10 :Nbr_Neighbour=CellTab[number-64+8].state+CellTab[number-64+8+1].state+CellTab[number-64].state+
CellTab[number-64+1].state+CellTab[number+1].state+CellTab[number+8+1].state+
CellTab[number+64+1].state+CellTab[number+64+8+1].state+CellTab[number+64+8].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 11 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64+8+1].state+
CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
CellTab[number-1].state+CellTab[number+64+1].state+CellTab[number+64+8+1].state+
CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 12 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64-1].state+
CellTab[number-64].state+CellTab[number+8-1].state+CellTab[number-1].state+
CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 13 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64-1].state+
CellTab[number-64].state+CellTab[number-64-8-1].state+CellTab[number-64-8].state+
CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number+8-1].state+
CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
CellTab[number+64+8].state+CellTab[number+64+8-1].state+CellTab[number+64-1].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 14 :Nbr_Neighbour=CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64-8-1].state+
CellTab[number-64-8].state+CellTab[number-8-1].state+CellTab[number-8].state+
CellTab[number-1].state+CellTab[number+64-8-1].state+CellTab[number+64-8].state+
CellTab[number+64-1].state+CellTab[number+64].state;
break;
case 15 :Nbr_Neighbour=CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
CellTab[number-64-8-1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
CellTab[number+1].state+CellTab[number-1].state+CellTab[number+64-8-1].state+
CellTab[number+64-8].state+CellTab[number+64-8+1].state+CellTab[number+64+1].state+
CellTab[number+64-1].state+CellTab[number+64].state;
break;
case 16 :Nbr_Neighbour=CellTab[number-64].state+CellTab[number-64+1].state+CellTab[number-64-8].state+
CellTab[number-64-8+1].state+CellTab[number-8].state+CellTab[number-8+1].state+
CellTab[number+1].state+CellTab[number+64-8].state+CellTab[number+64-8+1].state+
CellTab[number+64+1].state+CellTab[number+64].state;
break;
case 17 :Nbr_Neighbour=CellTab[number-64+8].state+CellTab[number-64+8+1].state+CellTab[number-64].state+
CellTab[number-64+1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
CellTab[number-8].state+CellTab[number-8+1].state+CellTab[number+1].state+
CellTab[number+8+1].state+CellTab[number+64-8].state+CellTab[number+64-8+1].state+
CellTab[number+64+1].state+CellTab[number+64+8+1].state+CellTab[number+64+8].state+
CellTab[number+64].state+CellTab[number+8].state;
break;
case 18 :Nbr_Neighbour=CellTab[number-64+8].state+CellTab[number-64+8+1].state+CellTab[number-64].state+
CellTab[number-64+1].state+CellTab[number+1].state+CellTab[number+8+1].state+
CellTab[number+8].state;
break;
case 19 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64+8+1].state+
CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
CellTab[number-1].state+CellTab[number+8].state;
break;
case 20 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64-1].state+
CellTab[number-64].state+CellTab[number+8-1].state+CellTab[number-1].state+
CellTab[number+8].state;
break;
case 21 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64-1].state+
CellTab[number-64].state+CellTab[number-64-8-1].state+CellTab[number-64-8].state+
CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number+8-1].state+
CellTab[number-1].state+CellTab[number+8].state;
break;
case 22 :Nbr_Neighbour=
CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64-8-1].state+
CellTab[number-64-8].state+CellTab[number-8-1].state+CellTab[number-8].state+
CellTab[number-1].state;
break;
case 23 :Nbr_Neighbour=CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
CellTab[number-64-8-1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
CellTab[number+1].state+CellTab[number-1].state;
break;
case 24 :Nbr_Neighbour=CellTab[number-64].state+CellTab[number-64+1].state+CellTab[number-64-8].state+
CellTab[number-64-8+1].state+CellTab[number-8].state+CellTab[number-8+1].state+
CellTab[number+1].state;
break;
case 25 :Nbr_Neighbour=CellTab[number-64+8].state+CellTab[number-64+8+1].state+CellTab[number-64].state+
CellTab[number-64+1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
CellTab[number-8].state+CellTab[number-8+1].state+CellTab[number+1].state+
CellTab[number+8+1].state+CellTab[number+8].state;
break;
case 26 :Nbr_Neighbour=CellTab[number-64+8-1].state+CellTab[number-64+8].state+CellTab[number-64+8+1].state+
CellTab[number-64-1].state+CellTab[number-64].state+CellTab[number-64+1].state+
CellTab[number-64-8-1].state+CellTab[number-64-8].state+CellTab[number-64-8+1].state+
CellTab[number-8-1].state+CellTab[number-8].state+CellTab[number-8+1].state+
CellTab[number+1].state+CellTab[number+8+1].state+CellTab[number+8-1].state+
CellTab[number-1].state+CellTab[number+8].state;
break;
default: break;
}
CellTab[number].neighbour=Nbr_Neighbour;
}
void Random_Tab(Cellule *Cell)
{
srand((unsigned)time(NULL));
for (int k=0;k<8;k++)
{
for (int i=0;i<8;i++)
{
for (int j=0;j<8;j++)
{
Cell[64*k+8*j+i].state=rand()%2;
}
}
}
}
void Init_Tab(Cellule *Cell)
{
for (int k=0;k<8;k++)
{
for (int j=0;j<8;j++)
{
for (int i=0;i<8;i++)
{
Cell[64*k+8*j+i].num = 64*k+8*j+i;
Cell[64*k+8*j+i].col =i;
Cell[64*k+8*j+i].row =j;
Cell[64*k+8*j+i].depth =k;
}
}
}
Random_Tab(Cell);
}
void Update_Life(Cellule *Cell)
{
Cellule CellOld[8*8*8];
//save states
for (int k=0;k<8;k++)
{
for (int j=0;j<8;j++)
{
for (int i=0;i<8;i++)
{
CellOld[64*k+8*j+i].state=Cell[64*k+8*j+i].state;
}
}
}
for (int k=0;k<8;k++)
{
for (int j=0;j<8;j++)
{
for (int i=0;i<8;i++)
{
Get_Neighbour(64*k+8*j+i,Cell);
if ((Cell[64*k+8*j+i].neighbour>=3)&&(Cell[64*k+8*j+i].neighbour<=4))CellOld[64*k+8*j+i].state=1;
else if ((Cell[64*k+8*j+i].neighbour<2)||(Cell[64*k+8*j+i].neighbour>4))CellOld[64*k+8*j+i].state=0;
}
}
}
//load states
for (int k=0;k<8;k++)
{
for (int j=0;j<8;j++)
{
for (int i=0;i<8;i++)
{
Cell[64*k+8*j+i].state=CellOld[64*k+8*j+i].state;
}
}
}
}
float g_xRotation, g_yRotation;
static float lmodel_ambient[4] = {0.1f, 0.1f, 0.1f, 1.0f};
void HandleMouseState(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON)
{
if(state == GLUT_DOWN)
MouseState.leftButton = true;
else
MouseState.leftButton = false;
}
if(button == GLUT_RIGHT_BUTTON)
{
if(state == GLUT_DOWN)
MouseState.rightButton = true;
else
MouseState.rightButton = false;
}
if(button == GLUT_MIDDLE_BUTTON)
{
if(state == GLUT_DOWN)
MouseState.middleButton = true;
else
MouseState.middleButton = false;
}
// update our position so we know a delta when the mouse is moved
MouseState.x = x;
MouseState.y = y;
}
void HandleMouseMove(int x, int y)
{
// calculate a delta in movement
int yDelta = MouseState.y - y;
int xDelta = MouseState.x - x;
// commit the mouse position
MouseState.x = x;
MouseState.y = y;
// when we need to rotate (only the left button is down)
if(MouseState.leftButton && !MouseState.rightButton && !MouseState.middleButton)
{
// rotate by the delta
g_xRotation -= xDelta * DEGREES_PER_PIXEL;
g_yRotation -= yDelta * DEGREES_PER_PIXEL;
}
// if we need to move translate (left and right buttons are down
else if(MouseState.leftButton && MouseState.rightButton && !MouseState.middleButton)
{
// move our eye
eyex += xDelta * UNITS_PER_PIXEL;
eyey -= yDelta * UNITS_PER_PIXEL;
// move our focus point
focusx += xDelta * UNITS_PER_PIXEL;
focusy -= yDelta * UNITS_PER_PIXEL;
}
glutPostRedisplay();
}
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
delete Cell;
exit(0);
break;
case ' ': //update_life
Update_Life(Cell);
glutPostRedisplay();
break;
case 'r': //reset
Init_Tab(Cell);
glutPostRedisplay();
break;
}
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add positioned light
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat lightPos0[] = {1.0f, 0.0f, 8.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//Add directed light
GLfloat lightColor1[] = {1.0f, 1.0f, 0.2f, 1.0f};
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
gluLookAt(eyex, eyey, eyez, focusx, focusy, focusz, 0,1,0);
glRotatef(g_xRotation, 0,1,1);
glRotatef(g_yRotation, 1,1,0);
glBegin(GL_QUADS);
for (int k=0;k<8;k++){for (int j=0;j<8;j++){for (int i=0;i<8;i++)
{
if (Cell[64*k+8*j+i].state==0)glColor3f(0.0f, 0.0f, 0.0f);
else glColor3f(1.0f, 1.0f, 1.0f);
//front
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
//back
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
//top
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
//bottom
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
//left
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
glVertex3f(Cell[64*k+8*j+i].col,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
//right
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row,Cell[64*k+8*j+i].depth+1);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth+1);
glVertex3f(Cell[64*k+8*j+i].col+1,Cell[64*k+8*j+i].row+1,Cell[64*k+8*j+i].depth);
}}}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void InitRenderer()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0f, 1.0f, 0.1f, 40.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
// starting rotation.
g_yRotation = -45.0f;
g_xRotation = 30.0f;
//initialize the mouse state
MouseState.leftButton = MouseState.rightButton = MouseState.middleButton = false;
MouseState.x = MouseState.y = 0;
// init our eye location
eyex = -10;
eyey = -10;
eyez = 30;
focusx = focusy = focusz = 0.0f;
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800,600);
glutCreateWindow("Game of Life");
glutDisplayFunc(Draw);
glutKeyboardFunc(handleKeypress);
glutMouseFunc(HandleMouseState);
glutMotionFunc(HandleMouseMove);
InitRenderer();
Init_Tab(Cell);
glutMainLoop();
delete Cell;
return 0;
}
Conclusion
La source est vraiment non optimisée mais a l'avantage de n'être qu'un seul fichier. La compréhension est peut-être un peu plus facile. Une optimisation aurait vraiment pu être faite notamment dans la description des 26 cas différents de calculs de voisins mais bref.. La partie gestion de la souris n'est pas de moi. J'espère que ca aidera quand même 2-3 personnes. Hesitez pas à me dire toute erreur de programmation importante, je suis là pour apprendre avant tout. Désolé pour le peu de commentaires aussi, j'ai eu peu de temps pour soigner cette source
Historique
- 07 octobre 2009 10:38:54 :
- aucune
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Camera Opengl [ par sniperfou ]
Salut a tous,j'ai un probleme avec opengl car je voudrais faire tourner ma camera autour d'un cube mais je ne connait pas les fonctions qui permettent
debutant en openGL... [ par dletozeun ]
bonjour,J'ai remarqué qu'avec opengl on peut deplacer le repere avec glTranslatef() ce qui est tres pratique....Mais j'ai remarqué aussi qu'avec glRot
Opengl: fonctions add substract [ par acx01b ]
Bonjour, je n'ai pas un niveau excellent en Opengl donc je m'en remets à vous pour me conseiller: j'aimerais essayer d'implémenter un logique de "a
Cube rotatif sur commande en OpenGL [ par Yoshiji ]
Bonjour, Tout d'abord, je suis sous windows Seven 64, codant sous Code::Blocks. J'ai un projet à faire dans le cadre de mes études : Un cube en C en
Récupération de la Profondeur des Objets OpenGL [ par shorzy ]
Salut J'ai un Probleme sur les Selections-Picking. J'ai une Face (Rectangle) dessiner à une Profondeur de -5 (par Ex.) Comment savoir quand je pose m
Débuter avec OpenGL [ par mohdaef ]
Bonjour à tous J'ai commencé à programmer sous OpenGL 2.6 il y a quelque temps et je souhaiterai passer à la version 3.1 ou bien 3.2, cependant je me
position souris avec GLUT opengl [ par jibons ]
Bonsoir, J'aimerais savoir s'il est possible de récupérer les coordonnées relatives à la position de la souris dans une fenêtre OpenGL avec GLUT. Et
c++ et 3dsmax [ par mikamada ]
Quels sont les limites de GL par rapport à OpenGL? car mwa jai pas open...[^^sad1] Deuxième question: n'est-il pas possible de coder directement sur
Master mind (SDL et OpenGl) [ par mehdighar ]
Bonjour !! Bon je dois créer un mastermind en mode graphique et j'ai un probleme au niveau de la gestion de la souris !! jvoudrai comparer le clic a d
pipeline opengl [ par fleurdelys77 ]
Salut à tous, j'ai une image en entré après avoir exécuté un calcule j'extrairais des parties de cette image (un ensemble de triangle dispersé) et je
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|