Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artem #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion RubiksCubons/.cproject
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
<listOptionValue builtIn="false" srcPrefixMapping="" srcRootPath="" value="opengl32"/>
</option>
<option id="gnu.cpp.link.option.paths.345120568" name="Library search path (-L)" superClass="gnu.cpp.link.option.paths" valueType="libPaths">
<listOptionValue builtIn="false" value="&quot;C:\MinGW\lib&quot;"/>
<listOptionValue builtIn="false" value="&quot;C:\MinGW\freeglut\lib&quot;"/>
</option>
<option id="gnu.cpp.link.option.flags.2023794677" name="Linker flags" superClass="gnu.cpp.link.option.flags" value="-static-libgcc -static-libstdc++" valueType="string"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1239948239" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
Expand Down
2 changes: 2 additions & 0 deletions RubiksCubons/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding//src/main.cpp=UTF-8
1 change: 1 addition & 0 deletions RubiksCubons/src/Cube.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using namespace std;
class Cube {
private:
vector<Face*> _tbFace;

public:
Cube(GLfloat x, GLfloat y, GLfloat z);
Cube(float size, Point p0);
Expand Down
80 changes: 78 additions & 2 deletions RubiksCubons/src/RubikCube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,48 @@ RubikCube::RubikCube(int size) {
float zdepart = -0.5;
float taille = 1.0 / size;//largeur d'un cube(size est le nombre de cube de large)


this->rotationAngles[0] = 0;
this->rotationAngles[1] = 0;
this->rotationAngles[2] = 0;

for (float x = xdepart; x < -xdepart; x += taille + decalage) {
for (float y = ydepart; y > -ydepart; y -= taille + decalage) {
for (float z = zdepart; z < -zdepart; z += taille + decalage) {
_tbCube.push_back(new Cube(taille, Point(x, y, z)));

}
}
}


for (int i = 0; i < 9; i++) {
Tranches[0][i] = i;
}

for (int i = 0, c = 9; i < 9; i++, c++) {
Tranches[1][i] = c;
}
for (int i = 0, c = 18; i < 9; i++, c++) {
Tranches[2][i] = c;
}



//_tbTranche est un tableau de tranches (size*3 car x,y,z)
//_tbTranche[0] = new Tranche(0, _size, _tbCouleur);//axe des X
//_tbTranche[1] = new Tranche(1, _size, _tbCouleur);//axe des Y
//_tbTranche[2] = new Tranche(2, _size, _tbCouleur);//axe des Z
}



int RubikCube::getSize() const {
return _size;
}



void RubikCube::setSize(int size) {
_size = size;
}
Expand All @@ -63,8 +87,50 @@ RubikCube::~RubikCube() {
}

void RubikCube::afficher() {
for (unsigned int i = 0; i < _tbCube.size(); ++i)
_tbCube[i]->afficher();
bool fl = false;
for (int i = 0; i < _tbCube.size(); ++i) {

for (int var = 0; var < 9; ++var) {//traitement de la tranche 1
if (this->Tranches[0][var] == i) {
fl = true;
glPushMatrix();
glRotatef(this->rotationAngles[0], 1.0, 0.0, 0.0);
_tbCube[i]->afficher();
glPopMatrix();
break;
}
}
for (int var = 0; var < 9; ++var) {//traitement de la tranche 2
if (this->Tranches[1][var] == i) {
fl = true;
glPushMatrix();
glRotatef(this->rotationAngles[1], 1.0, 0.0, 0.0);
_tbCube[i]->afficher();
glPopMatrix();
break;
}
}

for (int var = 0; var < 9; ++var) {//traitement de la tranche 3
if (this->Tranches[2][var] == i) {
fl = true;
glPushMatrix();
glRotatef(this->rotationAngles[2], 1.0, 0.0, 0.0);
_tbCube[i]->afficher();
glPopMatrix();
break;
}
}

if (fl == false) {//si le cube n'appartient pas a tranche on fais pas de rotation
_tbCube[i]->afficher();

} else
fl = true;


}

}

const Point* RubikCube::getCentre() const {
Expand Down Expand Up @@ -99,3 +165,13 @@ void RubikCube::Deplacement(GLfloat x, GLfloat y, GLfloat z){
_centre->setZ(Zcentre);
glTranslatef(x,-y,z);
}

void RubikCube::RotateTranche(int angle, int tranche){
this->rotationAngles[tranche] = angle;
}

int RubikCube::getAngle(int tranche){

return this->rotationAngles[tranche];

}
6 changes: 6 additions & 0 deletions RubiksCubons/src/RubikCube.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class RubikCube {
Point *_centre;
//Tranche* _tbTranche[];//pointeur de tableaux de tranches (3 car x,y,z)
vector<Cube *>_tbCube;
int rotationAngles[3];
int NTranches = 3;
int Tranches[3][9];
public:
RubikCube(int size);
virtual ~RubikCube();
Expand All @@ -20,6 +23,9 @@ class RubikCube {
void setCentre(const Point* centre);
void Rotation(char XYZ, int angle);
void Deplacement(GLfloat x, GLfloat y, GLfloat z);
void RotateTranche( int angle, int tranche);
int getAngle(int tranche);

};

#endif /* RUBIKCUBE_H_ */
99 changes: 85 additions & 14 deletions RubiksCubons/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ int Win_height = 800;
char ClicGauchePresse;//Gestion des clics de la souris

//rotation
int RotationAngleOfTranche = 0; //l'angle de rotation d'une tranche
int Mouse_x,Mouse_y;//NEW : Position actuelle de la souris
int Oldmouse_x,Oldmouse_y;//OLD: Gestion des clics de la souris
int Angle_x,Angle_y;//ACTION:Gestion de l'angle de vue (pour faire tourner l'objet)
//deplacement
GLfloat Mouse_plan_x, Mouse_plan_y;//NEW: Nouvelles coordonn�es de la souris dans le plan
GLfloat Oldmouse_plan_x, Oldmouse_plan_y;//OLD: Anciennes coordonn�es de la souris dans le plan
GLfloat Mouse_plan_x, Mouse_plan_y;//NEW: Nouvelles coordonnйes de la souris dans le plan
GLfloat Oldmouse_plan_x, Oldmouse_plan_y;//OLD: Anciennes coordonnйes de la souris dans le plan
GLfloat Deplac_x, Deplac_y, Deplac_z;//ACTION:Position actuelle du centre de l'objet dans l'espace plan
/*GESTION DES MENUS*/
int Dimension = 3;//taille du cube (3 par defaut)
Expand All @@ -33,6 +34,7 @@ void clavier(unsigned char touche,int x,int y);
void reshape(int x,int y);
void mouse(int bouton,int etat,int x,int y);
void mousemotion(int x,int y);
void trancheRotation(int );
//Creation de notre RubikCube 3x3 (par default)
RubikCube *rc = new RubikCube(Dimension);

Expand Down Expand Up @@ -64,6 +66,7 @@ void selectMode(int selection) {//selection du mode ROTATION/DEPLACEMENT
case 22 :
Mode = "deplacement";
break;

glutPostRedisplay();
}
}
Expand Down Expand Up @@ -105,7 +108,7 @@ void affichage()

//Affichage
rc->afficher();
//r�cup�re la valeur du centre de l'objet, pour pouvoir le d�placer dans l'espace plan
//rйcupиre la valeur du centre de l'objet, pour pouvoir le dйplacer dans l'espace plan
/*posx = rc->getCentre()->getX();
posy = rc->getCentre()->getY();
posz = rc->getCentre()->getZ();
Expand Down Expand Up @@ -143,12 +146,22 @@ void clavier(unsigned char touche,int x,int y)
}
}

void reshape(int x,int y)
void reshape(int w, int l) // Reshape function
{
if (x<y)
glViewport(0,(y-x)/2,x,x);
else
glViewport((x-y)/2,0,y,y);
glViewport(0, 0, w, l);
/* On veut changer la matrice de projection */
glMatrixMode(GL_PROJECTION);
/* chargement de la matrice identité */
glLoadIdentity();
/* On modifie les valeurs de projection */
if (w > l)
glOrtho((float)-w / l, (float)w / l, -1.0, 1.0, -1.0, 1.0);
else
glOrtho(-1.0, 1.0, (float)-l / w, (float)l / w, -1.0, 1.0);
/* On applique la modification de matrice */
glMatrixMode(GL_MODELVIEW);
/*On revient sur la matrice initiale */
glLoadIdentity();
}

void mouse(int button, int state,int x,int y)
Expand Down Expand Up @@ -189,7 +202,7 @@ void mousemotion(int x,int y)
//position actuelle de la souris dans le plan[-1;1]
Mouse_plan_x = (2.0 * x) / Win_width - 1.0;//(theGlutMouseXCoordinate / theGlutWindowWidth) - 1.0
Mouse_plan_y = (2.0 * y) / Win_height - 1.0;
//calcul du d�placement par rapport l'ancienne position
//calcul du dйplacement par rapport а l'ancienne position
Deplac_x += Mouse_plan_x - Oldmouse_plan_x;
Deplac_y += Mouse_plan_y - Oldmouse_plan_y;
Deplac_z = 0;
Expand All @@ -204,12 +217,53 @@ void mousemotion(int x,int y)
Oldmouse_plan_x = Mouse_plan_x;
Oldmouse_plan_y = Mouse_plan_y;
}

void trancheRotation(int tranche){


RotationAngleOfTranche = rc->getAngle(tranche);
RotationAngleOfTranche++;

if (RotationAngleOfTranche % 90 != 0 || RotationAngleOfTranche == 0) {

rc->RotateTranche(RotationAngleOfTranche,tranche);
glutTimerFunc(50, trancheRotation, tranche);
glutPostRedisplay();

} else {
if (RotationAngleOfTranche == 360) {
RotationAngleOfTranche = 0;
}
rc->RotateTranche(RotationAngleOfTranche, tranche);
glutPostRedisplay();

}
printf("RotationAngleOfTranche: %d \n", RotationAngleOfTranche);

}
void rotateSelection(int arg){

switch (arg) {
case 1:
glutTimerFunc(100,trancheRotation,0);
break;
case 2:
glutTimerFunc(100,trancheRotation,1);
break;
case 3:
glutTimerFunc(100,trancheRotation,2);
break;
default:
break;
}

}
int main(int argc,char **argv)
{
/* initialisation de glut et creation de la fenetre */
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);//Active le double buffering
glutInitWindowPosition(200,200); //Poisitionne la fenetre
glutInitWindowPosition(50,50); //Poisitionne la fenetre
glutInitWindowSize(Win_width,Win_height); //Taille de la fenetre
glutCreateWindow("RubiksCubons"); //Nomme la fenetre

Expand All @@ -222,20 +276,37 @@ int main(int argc,char **argv)

/*creation du menu */

//on commence par cr�er les sous-menus
//on commence par creer les sous-menus
int menuDimension = glutCreateMenu(selectDimension);
glutAddMenuEntry("3x3",11);
glutAddMenuEntry("5x5",12);

int menuMode = glutCreateMenu(selectMode);
glutAddMenuEntry("Rotation",21);
glutAddMenuEntry("D�placement",22);
/* on cr�e ensuite le menu superieur et on lie les sous-menus */
glutCreateMenu(select);
glutAddMenuEntry("Deplacement",22);

int menuRotate = glutCreateMenu(rotateSelection);
glutAddMenuEntry("Tranche 1",1);
glutAddMenuEntry("Tranche 2",2);
glutAddMenuEntry("Tranche 3",3);

int Menuanimation = glutCreateMenu(selectMode);
glutAddSubMenu("Rotate",menuRotate);

/* on cree ensuite le menu superieur et on lie les sous-menus */
glutCreateMenu(select);

glutAddSubMenu("Dimension",menuDimension);

glutAddSubMenu("Mode",menuMode);
glutAddSubMenu("Animation",Menuanimation);

glutAddMenuEntry("Quitter",0);





/* On associe le choix du bouton gauche de la souris */

glutAttachMenu(GLUT_RIGHT_BUTTON);
Expand Down