#include "rubik.h"
rubik::rubik()
{
static color init_colors[6];
init_colors[0].r = 1.0f; init_colors[0].g = 1.0f; init_colors[0].b = 1.0f;
init_colors[1].r = 1.0f; init_colors[1].g = 0.0f; init_colors[1].b = 0.0f;
init_colors[2].r = 0.0f; init_colors[2].g = 1.0f; init_colors[2].b = 0.0f;
init_colors[3].r = 0.0f; init_colors[3].g = 0.0f; init_colors[3].b = 1.0f;
init_colors[4].r = 0.0f; init_colors[4].g = 1.0f; init_colors[4].b = 1.0f;
init_colors[5].r = 1.0f; init_colors[5].g = 1.0f; init_colors[5].b = 0.0f;
static surface *init_surfaces[6];
for (int i=0; i<6; i++)
{
init_surfaces[i] = new surface;
init_surfaces[i]->m_color = init_colors[i];
}
for (int i=0; i<6; i++)
{
faces[i] = new face((COVER)i);
}
for (int x=0; x<JIE; x++)
for (int y=0; y<JIE; y++)
for (int z=0; z<JIE; z++)
{
cubes[x][y][z] = new cube(x, y, z);
}
for (int i=0; i<JIE; i++)
for (int j=0; j<JIE; j++)
{
faces[FRONT]->cubes[i][j] = cubes[i][j][0];
faces[FRONT]->cubes[i][j]->surfaces[FRONT] = init_surfaces[0];
faces[BACK]->cubes[i][j] = cubes[i][JIE-1-j][JIE-1];
faces[BACK]->cubes[i][j]->surfaces[BACK] = init_surfaces[1];
faces[LEFT]->cubes[i][j] = cubes[0][i][j];
faces[LEFT]->cubes[i][j]->surfaces[LEFT] = init_surfaces[2];
faces[RIGHT]->cubes[i][j] = cubes[JIE-1][i][JIE-1-j];
faces[RIGHT]->cubes[i][j]->surfaces[RIGHT] = init_surfaces[3];
faces[DOWN]->cubes[i][j] = cubes[i][0][j];
faces[DOWN]->cubes[i][j]->surfaces[DOWN] = init_surfaces[4];
faces[UP]->cubes[i][j] = cubes[i][JIE-1][JIE-1-j];
faces[UP]->cubes[i][j]->surfaces[UP] = init_surfaces[5];
}
}
void rubik::twist(COVER cover, int clockwise)
{
if (clockwise)
clockwise = 1;
else
clockwise = -1;
for (int i=0; i<9; i++)
{
faces[cover]->set_angle(10*i*clockwise);
draw();
_sleep(20);
}
faces[cover]->twist(clockwise);
faces[cover]->set_none();
}
#define DIS_X (8.0f)
#define DIS_Y (8.0f)
#define DIS_Z (8.0f)
int eye_index = 7;
static float eye[8][3] =
{
{-0.5, -0.5, -0.5}, /*0 l d f*/
{-0.5, -0.5, 0.5}, /*1 l d b*/
{-0.5, 0.5, -0.5}, /*2 l u f*/
{-0.5, 0.5, 0.5}, /*3 l u b*/
{0.5, -0.5, -0.5}, /*4 r d f*/
{0.5, -0.5, 0.5}, /*5 r d b*/
{0.5, 0.5, -0.5}, /*6 r u f*/
{0.5, 0.5, 0.5} /*7 r u b*/
};
void rubik::draw()
{
glClearColor(0.4f, 0.4f, 0.4f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(DIS_X*eye[eye_index][0],
DIS_Y*eye[eye_index][1],
DIS_Z*eye[eye_index][2],
0.0,1.0,0.0,
0.0,
1.0,
0.0);
switch(axis)
{
case AXIS_X:
glRotatef(angle, 1.0f, 0.0f, 0.0f);
break;
case AXIS_Y:
glRotatef(angle, 0.0f, 1.0f, 0.0f);
break;
case AXIS_Z:
glRotatef(angle, 0.0f, 0.0f, 1.0f);
break;
}
for (int x=0; x<JIE; x++)
for (int y=0; y<JIE; y++)
for (int z=0; z<JIE; z++)
if (cubes[x][y][z]!=0)
{
cubes[x][y][z]->draw(x, y, z);
}
SwapBuffers( hDC );
}
//enum COVER{LEFT,RIGHT,DOWN,UP,FRONT,BACK};
static COVER rotate_rule[3][2][4] =
{
{{FRONT,DOWN,BACK,UP},{FRONT,UP,BACK,DOWN}},/*X*/
{{FRONT,LEFT,BACK,RIGHT},{FRONT,RIGHT,BACK,LEFT}},/*Y*/
{{DOWN,RIGHT,UP,LEFT},{DOWN,LEFT,UP,RIGHT}}/*Z,OPENGL*/
};
static int rotate_eye_rule[8][3][2] =
{
{{1,2},{4,1},{2,4}},
{{3,0},{0,5},{3,5}},
{{0,3},{6,3},{6,0}},
{{2,1},{2,7},{7,1}},
{{5,6},{5,0},{0,6}},
{{7,4},{1,4},{1,7}},
{{4,7},{7,2},{4,2}},
{{6,5},{3,6},{5,3}},
};
void get_next(int jie, AXIS axis, int clockwise, int in_x, int in_y, int in_z, int *out_x, int *out_y, int *out_z)
{
if (clockwise)
{
switch(axis)
{
case AXIS_X:
*out_x = in_x;
*out_y = in_z;
*out_z = jie - 1 - in_y;
break;
case AXIS_Y:
*out_x = jie - 1 - in_z;
*out_y = in_y;
*out_z = in_x;
break;
case AXIS_Z:
*out_x = jie - 1 - in_y;
*out_y = in_x;
*out_z = in_z;
break;
}
}
else
{
switch(axis)
{
case AXIS_X:
*out_x = in_x;
*out_y = jie - 1 - in_z;
*out_z = in_y;
break;
case AXIS_Y:
*out_x = in_z;
*out_y = in_y;
*out_z = jie - 1 - in_x;
break;
case AXIS_Z:
*out_x = in_y;
*out_y = jie - 1 - in_x;
*out_z = in_z;
break;
}
}
}
void rubik::rotate(AXIS axis, int clockwise)
{
if (clockwise)
clockwise = 1;
else
clockwise = -1;
for (int i=0; i<9; i++)
{
for (int x=0; x<JIE; x++)
for (int y=0; y<JIE; y++)
for (int z=0; z<JIE; z++)
if (cubes[x][y][z]!=0)
{
cubes[x][y][z]->axis = axis;
cubes[x][y][z]->angle = clockwise*i*10;
}
draw();
_sleep(20);
}
//
/*此处发生实际的翻转*/
//
if (clockwise == -1)
clockwise = 0;
cube temp[JIE][JIE][JIE];
int next_x,next_y,next_z;
for (int x=0; x<JIE; x++)
for (int y=0; y<JIE; y++)
for (int z=0; z<JIE; z++)
if (cubes[x][y][z]!=0)
{
int next_x,next_y,next_z;
get_next(JIE, axis, clockwise, x, y, z, &next_x, &next_y, &next_z);
temp[next_x][next_y][next_z] = *cubes[x][y][z];
}
if (axis == AXIS_Y)
clockwise = !clockwise;
for (int x=0; x<JIE; x++)
for (int y=0; y<JIE; y++)
for (int z=0; z<JIE; z++)
if (cubes[x][y][z]!=0)
{
*cubes[x][y][z] = temp[x][y][z];
cubes[x][y][z]->rotate(axis, clockwise);
}
for (int x=0; x<JIE; x++)
for (int y=0; y<JIE; y++)
for (int z=0; z<JIE; z++)
if (cubes[x][y][z]!=0)
{
cubes[x][y][z]->axis = NONE;
cubes[x][y][z]->angle = 0.0f;
}
}
windows下通过opengl实现的魔方
4星 · 超过85%的资源 需积分: 10 130 浏览量
2010-04-05
17:01:29
上传
评论
收藏 588KB ZIP 举报
linnbuai
- 粉丝: 0
- 资源: 1
最新资源
- 基于STM32使用HAL库实现USB组合设备之多路CDC源码+说明文档.zip
- 金融贸易项目springboot
- mybatis动态sqlSQL 映射 XML 文件是所有 sql 语句
- 基于基于STM32的智能家居系统源码+qt上位机源码.zip
- 深圳房地产资源数据报告
- 基于stm32的智能门禁系统源码+设计文档+演示视频.zip
- cef + chromium 完整源码支持h265和h264
- 基于SpringBoot的API管理平台源代码+数据库,以项目的形式管理API文档,可以进行API的编辑、测试、Mock等操作
- protobuf 3.11版本,静态编译
- 2023NOC创客智慧编程赛项真题图形化-选拔赛(有解析)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈