begin process at 2012 05 27 20:13:47
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

OpenGL

 > JEU DE LA VIE 3D OPENGL AVEC GESTION SOURIS

JEU DE LA VIE 3D OPENGL AVEC GESTION SOURIS


 Information sur la source

Note :
9 / 10 - par 1 personne
9,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :OpenGL Classé sous :jeu vie, gestion souris, OpenGL, cube Niveau :Débutant Date de création :07/10/2009 Date de mise à jour :07/10/2009 10:38:54 Vu / téléchargé :3 328 / 330

Auteur : fratleym

Ecrire un message privé
Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
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

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Historique

07 octobre 2009 10:38:54 :
aucune

 Sources de la même categorie

Source avec Zip Source avec une capture AFFICHER DES COURBES DE BEZIER par shorzy
Source avec Zip Source avec une capture BASE/MOTEUR 3D EN QT/OPENGL (COMPLET ET FONCTIONNEL!) POUR U... par envi33
Source avec Zip Source avec une capture CLASSE AVEC OPENGL - OBJETS 3D ET ANIMATIONS par rasta63
Source avec Zip Source avec une capture LETTRES 3D AVEC OPENGL ET QT par opossum_farceur
Source avec Zip CUBE 3D GLUT32 VC++ ET DEVC++ par bobby03

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture AFFIMOFF : UNE VISIONNEUSE 3D AVEC PARAMÉTRISATION ET TEXTUR... par pgl10
Source avec Zip Source avec une capture AFFICHER DES COURBES DE BEZIER par shorzy
Source avec Zip Source avec une capture BASE/MOTEUR 3D EN QT/OPENGL (COMPLET ET FONCTIONNEL!) POUR U... par envi33
Source avec Zip Source avec une capture OPENCL : CALCUL ET AFFICHAGE DU GRADIENT COULEUR D'UNE WEBCA... par ciddiju
Source avec Zip Source avec une capture CLASSE AVEC OPENGL - OBJETS 3D ET ANIMATIONS par rasta63

Commentaires et avis

Commentaire de jeron le 08/11/2009 18:52:10 9/10

9/10 parce que c'est impréssionnant, mais que , comme la perfection n'existe pas , ...Bravo pour cette source, les automates cellulaires sont un monde passionnant, que beaucoups d'artistes ont repris , dans la musique ou dans l'art graphique , bref , tu as vraimment misé juste , je t'encourage à continuer dans ce sens.

meilleures salutations

 Ajouter un commentaire


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


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

Photothèque

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 8,518 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales