演示

                                                     魔方的制作

1.使用glBegin(GL_QUADS)函数,单独绘制出27个正方体,构成一个魔方的框架。
2.然后再分别给魔方的6个面上色,分别是上白,下黄,左绿,右蓝,前红,后橙。
3.最后再给魔方的每个小方格添加边框,这样魔方的原型就做好了。
4.用glutKeyboardFunc()实现键盘的控制,分别用数字键1~9控制魔方的9个面(xyz分别轴3个面)
5.用菜单右键控制魔方的复原与打乱,复原就是把各个面的颜色重置;打乱用了随机数20次随机地选择一个面旋转。

#include<gl/glut.h>
#include<time.h>
#include<stdlib.h>
#define SPEED 10

int face = 0;
int stop = 0;
int    upColor[27];
int  downColor[27];
int  leftColor[27];
int rightColor[27];
int frontColor[27];
int  backColor[27];
int direction[9] = {1,1,1,0,0,0,2,2,2};
GLfloat rotate[27][3];
int cube[9][9] =
{
    { 0, 1, 2, 3, 4, 5, 6, 7, 8},
    { 9,10,11,12,13,14,15,16,17},
    {18,19,20,21,22,23,24,25,26},
    { 0, 3, 6, 9,12,15,18,21,24},
    { 1, 4, 7,10,13,16,19,22,25},
    { 2, 5, 8,11,14,17,20,23,26},
    { 6, 7, 8,15,16,17,24,25,26},
    { 3, 4, 5,12,13,14,21,22,23},
    { 0, 1, 2, 9,10,11,18,19,20}
};
GLfloat block[27][24] =
{
    {-3.0f, 3.0f,-3.0f,-1.0f, 3.0f,-3.0f,-3.0f, 3.0f,-1.0f,-1.0f, 3.0f,-1.0f,-3.0f, 1.0f,-3.0f,-1.0f, 1.0f,-3.0f,-3.0f, 1.0f,-1.0f,-1.0f, 1.0f,-1.0f},
    {-1.0f, 3.0f,-3.0f, 1.0f, 3.0f,-3.0f,-1.0f, 3.0f,-1.0f, 1.0f, 3.0f,-1.0f,-1.0f, 1.0f,-3.0f, 1.0f, 1.0f,-3.0f,-1.0f, 1.0f,-1.0f, 1.0f, 1.0f,-1.0f},
    { 1.0f, 3.0f,-3.0f, 3.0f, 3.0f,-3.0f, 1.0f, 3.0f,-1.0f, 3.0f, 3.0f,-1.0f, 1.0f, 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f, 1.0f, 1.0f,-3.0f,-1.0f, 3.0f,-3.0f,-1.0f, 1.0f,-3.0f, 1.0f, 3.0f,-3.0f, 1.0f},
    {-3.0f,-1.0f, 1.0f,-1.0f,-1.0f, 1.0f,-3.0f,-1.0f, 3.0f,-1.0f,-1.0f, 3.0f,-3.0f,-3.0f, 1.0f,-1.0f,-3.0f, 1.0f,-3.0f,-3.0f, 3.0f,-1.0f,-3.0f, 3.0f},
    {-1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f,-1.0f,-1.0f, 3.0f, 1.0f,-1.0f, 3.0f,-1.0f,-3.0f, 1.0f, 1.0f,-3.0f, 1.0f,-1.0f,-3.0f, 3.0f, 1.0f,-3.0f, 3.0f},
    { 1.0f,-1.0f, 1.0f, 3.0f,-1.0f, 1.0f, 1.0f,-1.0f, 3.0f, 3.0f,-1.0f, 3.0f, 1.0f,-3.0f, 1.0f, 3.0f,-3.0f, 1.0f, 1.0f,-3.0f, 3.0f, 3.0f,-3.0f, 3.0f}
}; 

void SetColor(int color)
{
    switch(color)
    {
    case 0:glColor3f(0.0f,0.0f,0.0f);break;//black
    case 1:glColor3f(0.0f,0.0f,1.0f);break;//blue
    case 2:glColor3f(0.0f,1.0f,0.0f);break;//green
    case 3:glColor3f(0.0f,1.0f,1.0f);break;//cyan
    case 4:glColor3f(1.0f,0.0f,0.0f);break;//red
    case 5:glColor3f(1.0f,0.0f,1.0f);break;//purple
    case 6:glColor3f(1.0f,1.0f,0.0f);break;//yellow
    case 7:glColor3f(1.0f,1.0f,1.0f);break;//white
    case 8:glColor3f(1.0f,0.5f,0.0f);break;//orange
    default:return;
    }
}

void InitRot()
{
    int i;
    for(i = 0;i < 27;i++)
    {
        rotate[i][0] = 0.0f;
        rotate[i][1] = 0.0f;
        rotate[i][2] = 0.0f;
    }
}

void InitColor()
{
    int i;
    for(i = 0;i < 27;i++)
    {
           upColor[i] = 5;
         downColor[i] = 5;
         leftColor[i] = 5;
        rightColor[i] = 5;
        frontColor[i] = 5;
         backColor[i] = 5;
    }
    for(i = 0;i < 9;i++)
    {
           upColor[cube[0][i]] = 7;
         downColor[cube[2][i]] = 6;
         leftColor[cube[3][i]] = 2;
        rightColor[cube[5][i]] = 1;
        frontColor[cube[6][i]] = 4;
         backColor[cube[8][i]] = 8;
    }
}

void ChangeSize(int w,int h)
{
    if(h == 0)h = 1;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(w <= h)glOrtho(-5.0f,5.0f,-5.0f*h/w,5.0f*h/w,-5.0f,5.0f);
    else glOrtho(-5.0f*w/h,5.0f*w/h,-5.0f,5.0f,-5.0f,5.0f);
    gluLookAt(0.2f,0.2f,1.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void SetupRC(void)
{
    glClearColor(0.0f,0.0f,0.0f,1.0f);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
    glLineWidth(5.0f);
}

void RenderScene(void)
{
    int i;
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    for(i = 0;i < 27;i++)
    {
        glPushMatrix();
        glRotatef(rotate[i][0], 1.0f, 0.0f, 0.0f);
        glRotatef(rotate[i][1], 0.0f, 1.0f, 0.0f);
        glRotatef(rotate[i][2], 0.0f, 0.0f,1.0f);
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
        glBegin(GL_QUADS);
        SetColor(upColor[i]);//up
        glVertex3f(block[i][ 0],block[i][ 1],block[i][ 2]);
        glVertex3f(block[i][ 3],block[i][ 4],block[i][ 5]);
        glVertex3f(block[i][ 9],block[i][10],block[i][11]);
        glVertex3f(block[i][ 6],block[i][ 7],block[i][ 8]);
        SetColor(downColor[i]);//down
        glVertex3f(block[i][12],block[i][13],block[i][14]);
        glVertex3f(block[i][15],block[i][16],block[i][17]);
        glVertex3f(block[i][21],block[i][22],block[i][23]);
        glVertex3f(block[i][18],block[i][19],block[i][20]);
        SetColor(leftColor[i]);//left
        glVertex3f(block[i][ 0],block[i][ 1],block[i][ 2]);
        glVertex3f(block[i][ 6],block[i][ 7],block[i][ 8]);
        glVertex3f(block[i][18],block[i][19],block[i][20]);
        glVertex3f(block[i][12],block[i][13],block[i][14]);
        SetColor(rightColor[i]);//right
        glVertex3f(block[i][ 3],block[i][ 4],block[i][ 5]);
        glVertex3f(block[i][ 9],block[i][10],block[i][11]);
        glVertex3f(block[i][21],block[i][22],block[i][23]);
        glVertex3f(block[i][15],block[i][16],block[i][17]);
        SetColor(frontColor[i]);//front
        glVertex3f(block[i][ 6],block[i][ 7],block[i][ 8]);
        glVertex3f(block[i][ 9],block[i][10],block[i][11]);
        glVertex3f(block[i][21],block[i][22],block[i][23]);
        glVertex3f(block[i][18],block[i][19],block[i][20]);
        SetColor(backColor[i]);//back
        glVertex3f(block[i][ 0],block[i][ 1],block[i][ 2]);
        glVertex3f(block[i][ 3],block[i][ 4],block[i][ 5]);
        glVertex3f(block[i][15],block[i][16],block[i][17]);
        glVertex3f(block[i][12],block[i][13],block[i][14]);
        glEnd();
        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
        glBegin(GL_QUADS);
        SetColor(3);
        glVertex3f(block[i][ 0],block[i][ 1],block[i][ 2]);
        glVertex3f(block[i][ 3],block[i][ 4],block[i][ 5]);
        glVertex3f(block[i][ 9],block[i][10],block[i][11]);
        glVertex3f(block[i][ 6],block[i][ 7],block[i][ 8]);
        glVertex3f(block[i][12],block[i][13],block[i][14]);
        glVertex3f(block[i][15],block[i][16],block[i][17]);
        glVertex3f(block[i][21],block[i][22],block[i][23]);
        glVertex3f(block[i][18],block[i][19],block[i][20]);
        glVertex3f(block[i][ 0],block[i][ 1],block[i][ 2]);
        glVertex3f(block[i][ 6],block[i][ 7],block[i][ 8]);
        glVertex3f(block[i][18],block[i][19],block[i][20]);
        glVertex3f(block[i][12],block[i][13],block[i][14]);
        glVertex3f(block[i][ 3],block[i][ 4],block[i][ 5]);
        glVertex3f(block[i][ 9],block[i][10],block[i][11]);
        glVertex3f(block[i][21],block[i][22],block[i][23]);
        glVertex3f(block[i][15],block[i][16],block[i][17]);
        glEnd();
        glPopMatrix();
    }
    glutSwapBuffers();
}

void ChangeColor(int a)
{
    int i;
    int tempColor;
    switch(face)
    {
    case 0:
        for(i = -1;i <= a;i++)
        {
            tempColor =    upColor[ 0];   upColor[ 0] =    upColor[ 6];   upColor[ 6] =    upColor[ 8];   upColor[ 8] =    upColor[ 2];   upColor[ 2] = tempColor;
            tempColor =  leftColor[ 0]; leftColor[ 0] = frontColor[ 6];frontColor[ 6] = rightColor[ 8];rightColor[ 8] =  backColor[ 2]; backColor[ 2] = tempColor;
            tempColor =  backColor[ 0]; backColor[ 0] =  leftColor[ 6]; leftColor[ 6] = frontColor[ 8];frontColor[ 8] = rightColor[ 2];rightColor[ 2] = tempColor;
            tempColor =    upColor[ 1];   upColor[ 1] =    upColor[ 3];   upColor[ 3] =    upColor[ 7];   upColor[ 7] =    upColor[ 5];   upColor[ 5] = tempColor;
            tempColor =  backColor[ 1]; backColor[ 1] =  leftColor[ 3]; leftColor[ 3] = frontColor[ 7];frontColor[ 7] = rightColor[ 5];rightColor[ 5] = tempColor;
        }
        break;
    case 1:
        for(i = -1;i <= a;i++)
        {
            tempColor =  leftColor[ 9]; leftColor[ 9] = frontColor[15];frontColor[15] = rightColor[17];rightColor[17] =  backColor[11]; backColor[11] = tempColor;
            tempColor =  backColor[ 9]; backColor[ 9] =  leftColor[15]; leftColor[15] = frontColor[17];frontColor[17] = rightColor[11];rightColor[11] = tempColor;
            tempColor =  backColor[10]; backColor[10] =  leftColor[12]; leftColor[12] = frontColor[16];frontColor[16] = rightColor[14];rightColor[14] = tempColor;
        }
        break;
    case 2:
        for(i = -1;i <= a;i++)
        {
            tempColor =  downColor[18]; downColor[18] =  downColor[24]; downColor[24] =  downColor[26]; downColor[26] =  downColor[20]; downColor[20] = tempColor;
            tempColor =  leftColor[18]; leftColor[18] = frontColor[24];frontColor[24] = rightColor[26];rightColor[26] =  backColor[20]; backColor[20] = tempColor;
            tempColor =  backColor[18]; backColor[18] =  leftColor[24]; leftColor[24] = frontColor[26];frontColor[26] = rightColor[20];rightColor[20] = tempColor;
            tempColor =  downColor[19]; downColor[19] =  downColor[21]; downColor[21] =  downColor[25]; downColor[25] =  downColor[23]; downColor[23] = tempColor;
            tempColor =  backColor[19]; backColor[19] =  leftColor[21]; leftColor[21] = frontColor[25];frontColor[25] = rightColor[23];rightColor[23] = tempColor;
        }
        break;
    case 3:
        for(i = -1;i <= -a;i++)
        {
            tempColor =  leftColor[ 0]; leftColor[ 0] =  leftColor[18]; leftColor[18] =  leftColor[24]; leftColor[24] =  leftColor[ 6]; leftColor[ 6] = tempColor;
            tempColor =    upColor[ 0];   upColor[ 0] =  backColor[18]; backColor[18] =  downColor[24]; downColor[24] = frontColor[ 6];frontColor[ 6] = tempColor;
            tempColor =  backColor[ 0]; backColor[ 0] =  downColor[18]; downColor[18] = frontColor[24];frontColor[24] =    upColor[ 6];   upColor[ 6] = tempColor;
            tempColor =  leftColor[ 3]; leftColor[ 3] =  leftColor[ 9]; leftColor[ 9] =  leftColor[21]; leftColor[21] =  leftColor[15]; leftColor[15] = tempColor;
            tempColor =    upColor[ 3];   upColor[ 3] =  backColor[ 9]; backColor[ 9] =  downColor[21]; downColor[21] = frontColor[15];frontColor[15] = tempColor;
        }
        break;
    case 4:
        for(i = -1;i <= -a;i++)
        {
            tempColor =    upColor[ 1];   upColor[ 1] =  backColor[19]; backColor[19] =  downColor[25]; downColor[25] = frontColor[ 7];frontColor[ 7] = tempColor;
            tempColor =  backColor[ 1]; backColor[ 1] =  downColor[19]; downColor[19] = frontColor[25];frontColor[25] =    upColor[ 7];   upColor[ 7] = tempColor;
            tempColor =    upColor[ 4];   upColor[ 4] =  backColor[10]; backColor[10] =  downColor[22]; downColor[22] = frontColor[16];frontColor[16] = tempColor;
        }
        break;
    case 5:
        for(i = -1;i <= -a;i++)
        {
            tempColor = rightColor[ 2];rightColor[ 2] = rightColor[20];rightColor[20] = rightColor[26];rightColor[26] = rightColor[ 8];rightColor[ 8] = tempColor;
            tempColor =    upColor[ 2];   upColor[ 2] =  backColor[20]; backColor[20] =  downColor[26]; downColor[26] = frontColor[ 8];frontColor[ 8] = tempColor;
            tempColor =  backColor[ 2]; backColor[ 2] =  downColor[20]; downColor[20] = frontColor[26];frontColor[26] =    upColor[ 8];   upColor[ 8] = tempColor;
            tempColor = rightColor[ 5];rightColor[ 5] = rightColor[11];rightColor[11] = rightColor[23];rightColor[23] = rightColor[17];rightColor[17] = tempColor;
            tempColor =    upColor[ 5];   upColor[ 5] =  backColor[11]; backColor[11] =  downColor[23]; downColor[23] = frontColor[17];frontColor[17] = tempColor;
        }
        break;
    case 6:
        for(i = -1;i <= a;i++)
        {
            tempColor = frontColor[ 6];frontColor[ 6] = frontColor[24];frontColor[24] = frontColor[26];frontColor[26] = frontColor[ 8];frontColor[ 8] = tempColor;
            tempColor =    upColor[ 6];   upColor[ 6] =  leftColor[24]; leftColor[24] =  downColor[26]; downColor[26] = rightColor[ 8];rightColor[ 8] = tempColor;
            tempColor =  leftColor[ 6]; leftColor[ 6] =  downColor[24]; downColor[24] = rightColor[26];rightColor[26] =    upColor[ 8];   upColor[ 8] = tempColor;
            tempColor = frontColor[ 7];frontColor[ 7] = frontColor[15];frontColor[15] = frontColor[25];frontColor[25] = frontColor[17];frontColor[17] = tempColor;
            tempColor =    upColor[ 7];   upColor[ 7] =  leftColor[15]; leftColor[15] =  downColor[25]; downColor[25] = rightColor[17];rightColor[17] = tempColor;
        }
        break;
    case 7:
        for(i = -1;i <= a;i++)
        {
            tempColor =    upColor[ 3];   upColor[ 3] =  leftColor[21]; leftColor[21] =  downColor[23]; downColor[23] = rightColor[ 5];rightColor[ 5] = tempColor;
            tempColor =  leftColor[ 3]; leftColor[ 3] =  downColor[21]; downColor[21] = rightColor[23];rightColor[23] =    upColor[ 5];   upColor[ 5] = tempColor;
            tempColor =    upColor[ 4];   upColor[ 4] =  leftColor[12]; leftColor[12] =  downColor[22]; downColor[22] = rightColor[14];rightColor[14] = tempColor;
        }
        break;
    case 8:
        for(i = -1;i <= a;i++)
        {
            tempColor =  backColor[ 0]; backColor[ 0] =  backColor[18]; backColor[18] =  backColor[20]; backColor[20] =  backColor[ 2]; backColor[ 2] = tempColor;
            tempColor =    upColor[ 0];   upColor[ 0] =  leftColor[18]; leftColor[18] =  downColor[20]; downColor[20] = rightColor[ 2];rightColor[ 2] = tempColor;
            tempColor =  leftColor[ 0]; leftColor[ 0] =  downColor[18]; downColor[18] = rightColor[20];rightColor[20] =    upColor[ 2];   upColor[ 2] = tempColor;
            tempColor =  backColor[ 1]; backColor[ 1] =  backColor[ 9]; backColor[ 9] =  backColor[19]; backColor[19] =  backColor[11]; backColor[11] = tempColor;
            tempColor =    upColor[ 1];   upColor[ 1] =  leftColor[ 9]; leftColor[ 9] =  downColor[19]; downColor[19] = rightColor[11];rightColor[11] = tempColor;
        }
        break;
    default:
        return;
    }
}

void Rotate(int value)
{
    int i;
    for(i = 0;i < 9;i++)
        rotate[cube[face][i]][direction[face]] -= 9.0f;
    glutPostRedisplay();
    if(rotate[cube[face][0]][direction[face]] == -90.0f)
    {
        if(value == 1)
        {
            stop = 0;
            InitRot();
            ChangeColor(-1);
            return;
        }
        if(value >= 2)
        {
            value++;
            InitRot();
            ChangeColor(-1);
            face = rand() % 9;
            if(value == 20)
            {
                stop = 0;
                return;
            }
        }
    }
    glutTimerFunc(SPEED,Rotate,value);
}

void Keyboard(unsigned char key,int x,int y)
{
    if(stop == 1)
        return;
    stop = 1;
    switch(key)
    {
    case '1':face = 0;break;
    case '2':face = 1;break;
    case '3':face = 2;break;
    case '4':face = 3;break;
    case '5':face = 4;break;
    case '6':face = 5;break;
    case '7':face = 6;break;
    case '8':face = 7;break;
    case '9':face = 8;break;
    default:return;
    }
    glutTimerFunc(SPEED,Rotate,1);
}

void ProcessMenu(int value)
{
    switch(value)
    {
    case 1:
        InitRot();
        InitColor();
        break;
    case 2:
        stop = 1;
        glutTimerFunc(SPEED,Rotate,2);
        break;
    default:
        return;
    }
    glutPostRedisplay();
}

int main(int argc,char *argv[])
{
    
    InitRot();
    InitColor();
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(500,200);
    glutCreateWindow("魔方");
    glutReshapeFunc(ChangeSize);
    SetupRC();
    glutDisplayFunc(RenderScene);
    srand(time(0));
    glutKeyboardFunc(Keyboard);
    glutCreateMenu(ProcessMenu);
    glutAddMenuEntry("复原",1);
    glutAddMenuEntry("打乱",2);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutMainLoop();
    return 0;
}

 

posted @ 2016-06-22 09:52  35陈泽展  阅读(193)  评论(0编辑  收藏  举报