$40 off Craftsman 5 Drawer Homeowner Tool Center with Riser and Parts Bins $99.99 on sale 10/2-10/8
Sears Deal of the Day: Thule Car Top Carrier, Quest
Figure 49
======================
Note: You can rotate it with the mouse.
Please press 1 - 9 to choose the side you want to rotate.
Please pres 's' for shuffle
Please press 'a' for arrow direction rotation
Please press 'c' for anti-arrow direction rotation
Please press 'o' to reset the cube
Please enter 'r' to go back to original setting or setting after shuffle
Please press 'q' to exit
==============================
/*
//Author: Cheikh Drame
//To support, please do one of the following
//- Visit irap123.com many times a day
//- Join irap123.com
//- Buy my app irap123 (rhyming dictionary) available for android, and iphone. On windows phone, it is called iLyrics. If not, you can get the desktop version at www.irap123.com/apps/webstore/
//- Include irap123.com in your blogs, forums, facebook walls, email footers, comments
//- send a donation at my paypal address at cbdsigned.vevo@gmail.com
//- Buy a story at http://shop.pparzygnat.com
*/
// Project3Classmate.cpp : Defines the entry point for the console application.
//
//
//This program is to play with the magic cube
//click and drag the mouse to move the magic cube.
//It let user choose from 1 to 9 to select the side want to rotate
//press 's' will do the shuffle
//press 'o' will go back to the original setting of the magic cube
//press 'a' will rotate according to the arrow direction
//press 'c' will do counter-arrow direction rotation
//press 'r' will go back to the original setting of the magic cube if no shuffle occurs, if shuffle happened before, then go back to the setting after shuffle
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
#define MAXRECORD 300
class prerecord
{
public:
int side, direction;
prerecord(){}
prerecord(int side,int direction)
{
this->side=side;
this->direction=direction;
}
};
class Matrix
{
public:
double m[3][3];
};
class Point
{
public:
double X,Y,Z;
Point (){}
Point (double x, double y, double z)
{
setPoint(x,y,z);
}
void setPoint(double x, double y, double z)
{
this->X=x;
this->Y=y;
this->Z=z;
}
void MultiplyMatrix(Matrix m)
{
double x,y,z;
x=m.m[0][0]*X + m.m[1][0]*Y + m.m[2][0]*Z;
y=m.m[0][1]*X + m.m[1][1]*Y + m.m[2][1]*Z;
z=m.m[0][2]*X + m.m[1][2]*Y + m.m[2][2]*Z;
X=x;
Y=y;
Z=z;
}
};
class Arrow
{
private:
Point p;
double width;
public:
void setCenterPoint(Point p)
{
this->p=p;
}
void setWidth(double width)
{
this->width=width*5/2;
}
void Draw()
{
glLineWidth(5);
glColor3d(0.0,1.0,1.0);
glPushMatrix();
glTranslated(p.X,p.Y,p.Z);
glScaled(width,width,width);
drawArrow();
glRotated(90,1,0,0);
drawArrow();
glRotated(180,1,0,0);
drawArrow();
glRotated(270,1,0,0);
drawArrow();
glPopMatrix();
}
void drawArrow()
{
glBegin(GL_LINES);
glVertex3d(0,0.2,.45);
glVertex3d(0,-0.455,.45);
glVertex3d(0,.45,0.30);
glVertex3d(0,.45,-.455);
glVertex3d(0.1,0.15,.45);
glVertex3d(0,0,.45);
glVertex3d(0,0,.45);
glVertex3d(-0.1,0.15,.45);
glEnd();
}
};
class Face
{
public:
Point p[4];
Point center;
Point normal;
double r,g,b;
void Draw()
{
glColor3d(r,g,b);
int i=0;
center.setPoint(0,0,0);
normal = cross(p[3],p[1],p[2]);
glNormal3d(normal.X,normal.Y,normal.Z);
GLfloat diffuse[]={.5,.5,.5,1};
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
glEnable(GL_LIGHTING);
glBegin(GL_QUADS);
glVertex3d(p[0].X,p[0].Y,p[0].Z); glTexCoord2d(1,0);
glVertex3d(p[1].X,p[1].Y,p[1].Z); glTexCoord2d(1,1);
glVertex3d(p[2].X,p[2].Y,p[2].Z); glTexCoord2d(0,1);
glVertex3d(p[3].X,p[3].Y,p[3].Z); glTexCoord2d(0,0);
center.X=p[0].X/4+p[1].X/4+p[2].X/4+p[3].X/4;
center.Y=p[0].Y/4+p[1].Y/4+p[2].Y/4+p[3].Y/4;
center.Z=p[0].Z/4+p[1].Z/4+p[2].Z/4+p[3].Z/4;
glEnd();
glDisable(GL_LIGHTING);
}
Point cross (Point a, Point b, Point c)
{
Point result;
result.X = (b.Y-a.Y)*(c.Z-a.Z)-(b.Z-a.Z)*(c.Y-a.Y);
result.Y = (b.Z-a.Z)*(c.X-a.X)-(b.X-a.X)*(c.Z-a.Z);
result.Z = (b.X-a.X)*(c.Y-a.Y)-(b.Y-a.Y)*(c.X-a.X);
return result;
}
void init(Point center, double width, int i)
{
switch (i)
{
case 0:
p[0].setPoint(center.X, center.Y+width*2, center.Z);
p[1].setPoint(center.X+width*2, center.Y+width*2, center.Z);
p[2].setPoint(center.X+width*2, center.Y, center.Z);
p[3].setPoint(center.X, center.Y, center.Z);
break;
case 1:
p[0].setPoint(center.X, center.Y, center.Z);
p[1].setPoint(center.X, center.Y, center.Z+width*2);
p[2].setPoint(center.X, center.Y+width*2, center.Z+width*2);
p[3].setPoint(center.X, center.Y+width*2, center.Z);
break;
case 2:
p[0].setPoint(center.X, center.Y, center.Z);
p[1].setPoint(center.X+width*2, center.Y, center.Z);
p[2].setPoint(center.X+width*2, center.Y, center.Z+width*2);
p[3].setPoint(center.X, center.Y, center.Z+width*2);
break;
case 3:
p[3].setPoint(center.X, center.Y+width*2, center.Z);
p[2].setPoint(center.X+width*2, center.Y+width*2, center.Z);
p[1].setPoint(center.X+width*2, center.Y, center.Z);
p[0].setPoint(center.X, center.Y, center.Z);
break;
case 4:
p[3].setPoint(center.X, center.Y, center.Z);
p[2].setPoint(center.X, center.Y, center.Z+width*2);
p[1].setPoint(center.X, center.Y+width*2, center.Z+width*2);
p[0].setPoint(center.X, center.Y+width*2, center.Z);
break;
case 5:
p[3].setPoint(center.X, center.Y, center.Z);
p[2].setPoint(center.X+width*2, center.Y, center.Z);
p[1].setPoint(center.X+width*2, center.Y, center.Z+width*2);
p[0].setPoint(center.X, center.Y, center.Z+width*2);
break;
}
}
void moveFace(Matrix m)
{
int i;
for (i=0; i<4; i++)
p[i].MultiplyMatrix(m);
}
void setColor(double r, double g, double b)
{
this->r=r;
this->g=g;
this->b=b;
}
};
class Cube
{
public:
double width;
Face f[6];
Point center;
void Draw()
{
int i;
center.setPoint(0,0,0);
for (i=0; i<6; i++)
{
f[i].Draw();
center.X+=f[i].center.X/6;
center.Y+=f[i].center.Y/6;
center.Z+=f[i].center.Z/6;
}
}
void setCenterPoint(Point p)
{
f[0].init(Point(p.X,p.Y,p.Z),width,3);
f[0].setColor(1.0,0.0,1.0);
f[1].init(Point(p.X,p.Y,p.Z-width*2),width,0);
f[1].setColor(1.0,0.0,0.0);
f[2].init(Point(p.X+width*2,p.Y,p.Z-width*2),width,4);
f[2].setColor(1.0,1.0,0.0);
f[3].init(Point(p.X,p.Y,p.Z-width*2),width,1);
f[3].setColor(0.8,0.3,0.1);
f[4].init(Point(p.X,p.Y+width*2,p.Z-width*2),width,5);
f[4].setColor(0.0,1.0,0.0);
f[5].init(Point(p.X,p.Y,p.Z-width*2),width,2);
f[5].setColor(0.0,0.0,1.0);
}
void setWidth(double width)
{
this->width=width;
}
void moveCube(Matrix m)
{
int i;
for (i=0; i<6; i++)
f[i].moveFace(m);
}
};
class myCube
{
public:
double width;
double margin;
Cube c[27];
Arrow a;
Matrix m;
void init()
{
int i,j,k;
for (j=0; j<3; j++)
{ for (k=0; k<3; k++)
for (i=0; i<3; i++)
{
c[k*3*3+j*3+i].setWidth(width);
c[k*3*3+j*3+i].setCenterPoint(Point(margin*i-margin-margin/2+0.03,margin*j-margin-margin/2+0.03,margin*k-margin + margin/2-0.03));
}
}
Draw();
setBlackFaces();
}
void setBlackFaces()
{
int i,j;
for (i=0; i<27; i++)
for(j=0; j<6; j++)
if(c[i].f[j].center.X>-margin-margin/2+0.03 && c[i].f[j].center.X
}
void Draw()
{
int i;
for (i=0; i<27; i++)
c[i].Draw();
}
void setWidth(double width)
{
this->width=width/3;
margin=this->width*2+0.06;
}
Point MultiplyMatrix(Matrix m,Point in)
{
Point result;
result.X=m.m[0][0]*in.X + m.m[1][0]*in.Y + m.m[2][0]*in.Z;
result.Y=m.m[0][1]*in.X + m.m[1][1]*in.Y + m.m[2][1]*in.Z;
result.Z=m.m[0][2]*in.X + m.m[1][2]*in.Y + m.m[2][2]*in.Z;
return result;
}
void moveCube(int direction,double angle, int side)
{
int i;
a.setWidth(width*3);
for(int x=0;x<3;++x)
for(int y=0;y<3;++y)
m.m[x][y]=0;
switch(side)
{
case 1:
case 2:
case 3:
m.m[0][0]=1;
m.m[1][1]=cos(angle);
m.m[1][2]=direction*sin(angle);
m.m[2][1]=-direction*sin(angle);
m.m[2][2]=cos(angle);
break;
case 4:
case 5:
case 6:
m.m[2][2]=1;
m.m[0][0]=cos(angle);
m.m[0][1]=-direction*sin(angle);
m.m[1][0]=direction*sin(angle);
m.m[1][1]=cos(angle);
break;
case 7:
case 8:
case 9:
m.m[1][1]=1;
m.m[0][0]=cos(angle);
m.m[0][2]=-direction*sin(angle);
m.m[2][0]=direction*sin(angle);
m.m[2][2]=cos(angle);
break;
}
switch(side)
{
case 1:
for (i=0; i<27; i++)
if (int(c[i].center.X)==int(-margin))
c[i].moveCube(m);
a.setCenterPoint(Point(-margin,0,0));
a.Draw();
break;
case 2:
for (i=0; i<27; i++)
if (int(c[i].center.X)==0)
c[i].moveCube(m);
a.setCenterPoint(Point(0,0,0));
a.Draw();
break;
case 3:
for (i=0; i<27; i++)
if (int(c[i].center.X)==int(margin))
c[i].moveCube(m);
a.setCenterPoint(Point(margin,0,0));
a.Draw();
break;
case 4:
for (i=0; i<27; i++)
if (int(c[i].center.Z)==int(margin))
c[i].moveCube(m);
glPushMatrix();
glRotated(90,0,1,0);
a.setCenterPoint(Point(-margin,0,0));
a.Draw();
glPopMatrix();
break;
case 5:
for (i=0; i<27; i++)
if (int(c[i].center.Z)==0)
c[i].moveCube(m);
glPushMatrix();
glRotated(90,0,1,0);
a.setCenterPoint(Point(0,0,0));
a.Draw();
glPopMatrix();
break;
case 6:
for (i=0; i<27; i++)
if (int(c[i].center.Z)==int(-margin))
c[i].moveCube(m);
glPushMatrix();
glRotated(90,0,1,0);
a.setCenterPoint(Point(margin,0,0));
a.Draw();
glPopMatrix();
break;
case 7:
for (i=0; i<27; i++)
if (int(c[i].center.Y)==int(-margin))
c[i].moveCube(m);
glPushMatrix();
glRotated(90,0,0,1);
a.setCenterPoint(Point(-margin,0,0));
a.Draw();
glPopMatrix();
break;
case 8:
for (i=0; i<27; i++)
if (int(c[i].center.Y)==0)
c[i].moveCube(m);
glPushMatrix();
glRotated(90,0,0,1);
a.setCenterPoint(Point(0,0,0));
a.Draw();
glPopMatrix();
break;
case 9:
for (i=0; i<27; i++)
if (int(c[i].center.Y)==int(margin))
c[i].moveCube(m);
glPushMatrix();
glRotated(90,0,0,1);
a.setCenterPoint(Point(margin,0,0));
a.Draw();
glPopMatrix();
break;
}
}
};
#define angle 3.1415926/180
double ea=0.8;
double eb=0.8;
double ea1;
double eb1;
int mousex,mousey;
myCube cube;
int direction=0;
double ani=-1;
int side=0;
bool start;
int pos=0;
prerecord items[MAXRECORD];
void add(int side, int direction)
{
if(pos
items[pos]=prerecord(side,direction);
pos++;
}
}
void prev(int &side, int &direction)
{
if(pos!=0)
pos--;
side=items[pos].side;
direction=-items[pos].direction;
}
void clear()
{
pos=0;
}
bool isDone()
{
if(pos==0)
{
start=false;
return true;
}
return false;
}
void animation()
{
if (!isDone() && start && ani==-1)
{
prev(side,direction);
ani=0;
}
if (ani>=0 && ani<8.9)
{
cube.moveCube(direction,angle,side);
cube.Draw();
ani+=0.1;
}
else
{
cube.moveCube(0,0,side);
cube.Draw();
ani=-1;
}
}
void reshape(int x,int y)
{
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50,1,1,100);
glMatrixMode(GL_MODELVIEW);
if(x > y)
y = x;
else
x= y;
glutReshapeWindow(x,y);
glViewport(0,0,x,y);
}
void Draw()
{
glClearColor(0.5,0.5,0.5,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPopMatrix();
GLfloat EYEX= 10*cos(ea)*sin(eb);
GLfloat EYEY = -10*cos(ea)*cos(eb);
GLfloat EYEZ = 10*sin(ea);
gluLookAt(EYEX,EYEY,EYEZ,0,0,0,0,0,1);
animation();
glFlush();
glutSwapBuffers();
}
void motion(int x,int y)
{
ea=(y-mousey)/100.0+ea1;
eb=(mousex-x)/50.0+eb1;
if(ea<-1.57) ea=-1.57;
if(ea>1.57) ea=1.57;
glutPostRedisplay();
}
void shuffle()
{
srand(time(0));
int randnumber=rand();
int randside[]={1,2,3,4,5,6,7,8,9};
int randdirection[]={1,-1};
int randcount=rand()%12+1;
for (unsigned int i=0; i
add(randside[randnumber%9],randdirection[randnumber%2]);
randnumber+=5;
}
start=true;
}
void keyboard(unsigned char k,int x,int y)
{
if(ani==-1)
switch (k)
{
case 'a':
direction=1;
ani=0;
add(side,direction);
break;
case 'c':
direction=-1;
ani=0;
add(side,direction);
break;
case 'r':
start=true;
break;
case 'o':
cube.init();
clear();
side=0;
break;
case 's':
shuffle();
break;
case 'q':
exit(0);
case '1':
side=1;
break;
case '2':
side=2;
break;
case '3':
side=3;
break;
case '4':
side=4;
break;
case '5':
side=5;
break;
case '6':
side=6;
break;
case '7':
side=7;
break;
case '8':
side=8;
break;
case '9':
side=9;
break;
}
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
mousex=x;
mousey=y;
ea1=ea;
eb1=eb;
}
}
void Idle()
{
Draw();
}
void Display(void)
{
glFlush();
}
void lighting() {
GLfloat direction[] = { 1.0, 2.0, 1.0, 1.0 };
GLfloat intensity[] = { 1, 1, 1, 1 };
GLfloat ambient[] = { 0.8, 0.8, 0.8, 1.0 };
glEnable(GL_LIGHTING);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, direction);
glLightfv(GL_LIGHT0, GL_DIFFUSE, intensity);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(200,100);
glutCreateWindow("Project 3 by irap123.com");
lighting();
cout<<"Please press 1 - 9 to choose the side you want to rotate."<
cube.init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutIdleFunc(Idle);
glutDisplayFunc(Display);
glutMainLoop();
}
No comments:
Post a Comment