Jeu de la vie 3d opengl avec gestion souris

Soyez le premier à donner votre avis sur cette source.

Vue 5 955 fois - Téléchargée 737 fois

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 / Exemple :


#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

Codes Sources

A voir également

Ajouter un commentaire

Commentaire

Messages postés
29
Date d'inscription
lundi 30 juin 2003
Statut
Membre
Dernière intervention
25 juillet 2014
2
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

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.