current position:Home>c++/qt/opencv realizes Rubik's cube restoration [entertainment]

c++/qt/opencv realizes Rubik's cube restoration [entertainment]

2022-10-16 07:09:17Cantonese boy who loves to learn


前言

This article is mainly to test the Rubik's cube reduction,And you can play the Rubik's Cube through the keyboard,Then restore automatically,For this project
到opengl


视频演示

c++/qt/openglRubik's cube recovery function

一、项目工程目录

在这里插入图片描述
cube和GLWidgetThe files are mostly interfaceopengland the definition of the Rubik's Cube
openglTestmain logic


二、重要代码解析

1.Cube方块类

First enumerate the colors of the faces

enum Color{
    white,yellow,blue,green,orage,red};

Initialize the sides of each block

void Cube::initColor()
{
    
    color[Cube::front] = Cube::white;
    color[Cube::back] = Cube::yellow;
    color[Cube::down] = Cube::blue;
    color[Cube::up] = Cube::green;
    color[Cube::right] = Cube::orage;
    color[Cube::left] = Cube::red;
}

Block position settings

 void Cube::setXYZ(GLfloat x1, GLfloat y1, GLfloat z1)
 {
    
     x=x1;
     y=y1;
     z=z1;
 }

2.GLWidget界面构建

这里主要使用opengl来进行设置
初始化opengl

void GLWidget::initializeGL()
{
    
	//初始化进来
    loadGLTextures();             //Load the resource file image
    glEnable( GL_TEXTURE_2D );           //glEnableEnable various functions,Start the target texture,不能写在glBegin和glEnd两个函数中间;
    glShadeModel( GL_SMOOTH );           //设置着色模式     GL_SMOOTHA transition effect will appear,GL_FLAT then just draws all other points in a single color at the specified point
    glClearColor( 0.5,0.5, 0.5, 0.5 );     //设置清除颜色,Also similar to the background color
    glClearDepth( 1.0 );             //Specifies the desired value for each pixel in the depth buffer

    glEnable( GL_DEPTH_TEST );      //启用了之后,OpenGL在绘制的时候就会检查,当前像素前面是否有别的像素,如果别的像素挡道了它,那它就不会绘制,也就是说,OpenGL就只绘制最前面的一层
    //透明操作
	//glDisable(GL_DEPTH_TEST);   //需要关闭它
	//glEnable(GL_BLEND);         //打开混合

	glDepthFunc(GL_LEQUAL);      //GL_LEQUAL:Also renders when the depth is small or equal    GL_LESS:Render only when the depth is small
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );        //第三个参数:GL_FASTESTUse the fastest mode. GL_NICESTFor use the best quality mode,还有一个GL_DONT_CAREIt is determined by the driving device
    glEnable(GL_LIGHT1);      //光照参数
}

绘制方块,Rubik's cube

void GLWidget::drawOneCube(Cube *b, GLfloat x, GLfloat y, GLfloat z)//
{
    
    x=x-0.5;
    y=y-0.5;
    z=z-0.5;
    const Cube::Color *color = b->getColor();
	
	//后面 黄色
    glBindTexture( GL_TEXTURE_2D, texture[color[1]] );
    glBegin( GL_QUADS );    //填充颜色
    glNormal3f( x, y, z );     //Sets where the normal vector points to
	//glTexCoord2f:第一个参数代表X坐标. 0.0f 是纹理的左侧. 0.5f 是纹理的中点, 1.0f 是纹理的右侧
	//             第二个参数代表Y坐标. 0.0f 是纹理的底部. 0.5f 是纹理的中点, 1.0f 是纹理的顶部
	//glTexCoord2f()is the configuration texture coordinate,glVertex3f()are the configuration graphics coordinates,绘制图形时指定纹理的坐标
    glTexCoord2f( 0,   0  );glVertex3f(x,   y, z);     
    glTexCoord2f( 0+1, 0  );glVertex3f(x+1, y, z);
    glTexCoord2f( 0+1, 0+1);glVertex3f(x+1, y+1, z);
    glTexCoord2f( 0,   0+1);glVertex3f(x, y+1, z);
    glEnd();

    glBindTexture( GL_TEXTURE_2D, texture[color[0]] );
    glBegin( GL_QUADS );
    //前面 白色
    glNormal3f( x, y, z+1 );
    glTexCoord2f( 0,   0  );glVertex3f(x,   y,   z+1);
    glTexCoord2f( 0+1, 0  );glVertex3f(x+1, y,   z+1);
    glTexCoord2f( 0+1, 0+1);glVertex3f(x+1,   y+1, z+1);
    glTexCoord2f( 0,   0+1);glVertex3f(x, y+1, z+1);

    glEnd();
    glBindTexture( GL_TEXTURE_2D, texture[color[2]] );
    glBegin( GL_QUADS );

    //下面 蓝色
    glNormal3f( x, y, z);
    glTexCoord2f( 0,   0  );glVertex3f(x,   y,   z);
    glTexCoord2f( 0+1, 0  );glVertex3f(x+1, y,   z);
    glTexCoord2f( 0+1, 0+1);glVertex3f(x+1,   y, z+1);
    glTexCoord2f( 0,   0+1);glVertex3f(x, y, z+1);

    glEnd();
    glBindTexture( GL_TEXTURE_2D, texture[color[3]] );
    glBegin( GL_QUADS );

    //上面 绿色
    glNormal3f( x, y+1, z);
    glTexCoord2f( 0,   0  );glVertex3f(x,   y+1,   z);
    glTexCoord2f( 0+1, 0  );glVertex3f(x+1, y+1,   z);
    glTexCoord2f( 0+1, 0+1);glVertex3f(x+1,   y+1, z+1);
    glTexCoord2f( 0,   0+1);glVertex3f(x, y+1, z+1);

    glEnd();
    glBindTexture( GL_TEXTURE_2D, texture[color[5]] );
    glBegin( GL_QUADS );

    //左面 橙色
    glNormal3f( x, y, z);
    glTexCoord2f( 0,   0  );glVertex3f(x,   y,   z);
    glTexCoord2f( 0+1, 0  );glVertex3f(x, y+1,   z);
    glTexCoord2f( 0+1, 0+1);glVertex3f(x,   y+1, z+1);
    glTexCoord2f( 0,   0+1);glVertex3f(x, y, z+1);

    glEnd();

    glBindTexture( GL_TEXTURE_2D, texture[color[4]] );
    glBegin( GL_QUADS );

    //右面 红色
    glNormal3f( x+1, y, z);
    glTexCoord2f( 0,   0  );glVertex3f(x+1,   y,   z);
    glTexCoord2f( 0+1, 0  );glVertex3f(x+1, y+1,   z);
    glTexCoord2f( 0+1, 0+1);glVertex3f(x+1,   y+1, z+1);
    glTexCoord2f( 0,   0+1);glVertex3f(x+1, y, z+1);

    glEnd();
}

3.魔方还原

Restore the main entrance

void OpenGLTest::on_QAction_AutoFinish_triggered()
{
    
	StartRunFlag = true;
	ui.statusBar->showMessage(QStringLiteral("Start recovery"));
	listRotateTempTask.clear();
	//The first step is a white cross
    autoBackCenter();
    while(!calAutoBack1())
    {
    
        if(autoBack1zthird())break;
        if(autoBack1xthird())break;
        if(autoBack1xfirst())break;
        if(autoBack1yfirst())break;
        if(autoBack1ythird())break;
        sleep(10);
    }
    autoBack1Last();

	//The second step is to add the four sides to the first sideT字形
    autoBack2();
    //The third step deals with the four prismatic color blocks of the second layer,Align the first two layers
    autoBack3();
    //Step 4 Draw a cross on the new yellow top
    autoBack4();
    //The fifth step is to adjust the orientation of the top-level character block,Align the top yellow side
    autoBack5();
    //第六步 Adjust the top-level character block order
    autoBack6();
    //The seventh step adjusts the order of the top edge color blocks,The Rubik's Cube is finally restored
    autoBack7();
    ui.statusBar->showMessage(QStringLiteral("完成"));
	StartRunFlag =false;
}

Here is the need for Rubik's Cube restoration7步,Entering the loop at each step has been judging the color


总结

The specific code can be downloaded directly
应用程序体验
源码

copyright notice
author[Cantonese boy who loves to learn],Please bring the original link to reprint, thank you.
https://en.bfun.fun/2022/289/202210160700099217.html

Random recommended