首先这个程序是建立的是Windows应用程序,建立控制台程序是不能运行的,另外,项目——项目属性——配置属性——常规-----使用多字节字符集,这样编译才能够通过的,否则如果选择使用 Unicode 字符集,编译会有错误提示:error C2440: “初始化”: 无法从“const char [8]”转换为“LPCTSTR”,另外,链接器----输入----附加依赖项要加入:“opengl32.lib glu32.lib”的lib库。。

cubemanage.h文件为:

#ifndef CUBEMANAGE_H
#define CUBEMANAGE_H
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include "wcgcube.h"
#define CUBE_SIZE  3
#define ORIENTX 0
#define ORIENTY 0
#define ORIENTZ 0
class CubeManage {
public:
CubeManage();
~CubeManage();
void turn(int rotateType);
void turnByXShun(int x);
void turnByXNi(int x);
void turnByYShun(int y);
void turnByYNi(int y);
void turnByZShun(int z);
void turnByZNi(int z);
void output(int scr,int site);
void output();
void draw(int rotateType,GLfloat rotate);
private:
WcgCube *cubes[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
void goStep(int *leftLeg,int *rightLeg,int *goDirection,int step,int leftEdge,int rightEdge);
};
#endif

wcgcube.h文件为:

#ifndef WCGCUBE_H
#define WCGCUBE_H
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include "iostream"
using namespace std;
#define X 1
#define Y 2
#define Z 3
class WcgCube {
public:
WcgCube();
~WcgCube();
void turnByXShun(int x);
void turnByXNi(int x);
void turnByYShun(int y);
void turnByYNi(int y);
void turnByZShun(int z);
void turnByZNi(int z);
void output(int sign);
void output();
void draw(GLfloat x0,GLfloat y0,GLfloat z0);
private:
int direct[6];
GLfloat sideColor[6][3];
void turnByX(int x,int sign);
void turnByY(int y,int sign);
void turnByZ(int z,int sign);
};
#endif

CubeGame.cpp文件为:

#include <windows.h>
#include <winuser.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include "iostream"
using namespace std;
#include "cubemanage.h"
#include "wcgcube.h"
static GLfloat PI=3.1415f;
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
static GLfloat rotate=0.0f;
static int rotateType=0;
static int rotateOK=0;
static int rotateRate=100;
static GLfloat rotateStep=5*PI/180;
CubeManage cm;
HPALETTE hPalette = NULL;
// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;
static LPCTSTR lpszAppName = "WcgCube";
void exitGame(HWND hWnd,HDC hDC,HGLRC hRC);
// Declaration for Window procedure
LRESULT CALLBACK WndProc(   HWND    hWnd,
UINT    message,
WPARAM  wParam,
LPARAM  lParam);
// Set Pixel Format function - forward declaration
void SetDCPixelFormat(HDC hDC);
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat nRange = 350.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Called by timer routine to effect movement of the rectangle.
void IdleFunction(void)
{
if (rotate>=PI/2) {
cm.turn(rotateType);
rotateType=0;
rotateOK=0;
rotate=0.0f;
// Refresh the Window
//      glutPostRedisplay();
return;
}
rotate+=rotateStep;
// Refresh the Window
//  glutPostRedisplay();
}
// Called by AUX library to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
cm.draw(rotateType,rotate);
glPopMatrix();
// Show the graphics
//  glutSwapBuffers();
}
// If necessary, creates a 3-3-2 palette for the device context listed.
HPALETTE GetOpenGLPalette(HDC hDC)
{
HPALETTE hRetPal = NULL;   // Handle to palette to be created
PIXELFORMATDESCRIPTOR pfd;  // Pixel Format Descriptor
LOGPALETTE *pPal;           // Pointer to memory for logical palette
int nPixelFormat;           // Pixel format index
int nColors;                // Number of entries in palette
int i;                      // Counting variable
BYTE RedRange,GreenRange,BlueRange;
// Range for each color entry (7,7,and 3)
// Get the pixel format index and retrieve the pixel format description
nPixelFormat = GetPixelFormat(hDC);
DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
// Does this pixel format require a palette?  If not, do not create a
// palette and just return NULL
if(!(pfd.dwFlags & PFD_NEED_PALETTE))
return NULL;
// Number of entries in palette.  8 bits yeilds 256 entries
nColors = 1 << pfd.cColorBits;
// Allocate space for a logical palette structure plus all the palette entries
pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +nColors*sizeof(PALETTEENTRY));
// Fill in palette header
pPal->palVersion = 0x300;       // Windows 3.0
pPal->palNumEntries = nColors; // table size
// Build mask of all 1's.  This creates a number represented by having
// the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
// pfd.cBlueBits.
RedRange = (1 << pfd.cRedBits) -1;
GreenRange = (1 << pfd.cGreenBits) - 1;
BlueRange = (1 << pfd.cBlueBits) -1;
// Loop through all the palette entries
for(i = 0; i < nColors; i++)
{
// Fill in the 8-bit equivalents for each component
pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
pPal->palPalEntry[i].peRed = (unsigned char)(
(double) pPal->palPalEntry[i].peRed * 255.0 / RedRange);
pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
pPal->palPalEntry[i].peGreen = (unsigned char)(
(double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
pPal->palPalEntry[i].peBlue = (unsigned char)(
(double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
pPal->palPalEntry[i].peFlags = (unsigned char) NULL;
}
// Create the palette
hRetPal = CreatePalette(pPal);
// Go ahead and select and realize the palette for this device context
SelectPalette(hDC,hRetPal,FALSE);
RealizePalette(hDC);
// Free the memory used for the logical palette structure
free(pPal);
// Return the handle to the new palette
return hRetPal;
}
// Select the pixel format for a given device context
void SetDCPixelFormat(HDC hDC)
{
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
1,                              // Version of this structure
PFD_DRAW_TO_WINDOW |            // Draw to Window (not to bitmap)
PFD_SUPPORT_OPENGL |            // Support OpenGL calls in window
PFD_DOUBLEBUFFER,               // Double buffered mode
PFD_TYPE_RGBA,                  // RGBA Color mode
32,                             // Want 32 bit color
0,0,0,0,0,0,                    // Not used to select mode
0,0,                            // Not used to select mode
0,0,0,0,0,                      // Not used to select mode
16,                             // Size of depth buffer
0,                              // Not used to select mode
0,                              // Not used to select mode
0,                              // Not used to select mode
0,                              // Not used to select mode
0,0,0 };                        // Not used to select mode
// Choose a pixel format that best matches that described in pfd
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
// Set the pixel format for the device context
SetPixelFormat(hDC, nPixelFormat, &pfd);
}
void dealKey(HWND hWnd,HDC hDC,HGLRC hRC,int wParam)
{
switch (wParam)
{
case 27:
exitGame(hWnd,hDC,hRC);
break;
case 113:                       //q
if (rotateOK==1)
return;
rotateType=1;
rotateOK=1;
rotate=0.0f;
break;
case 119:                       //w
if (rotateOK==1)
return;
rotateType=2;
rotateOK=1;
rotate=0.0f;
break;
case 101:                       //e
if (rotateOK==1)
return;
rotateType=3;
rotateOK=1;
rotate=0.0f;
break;
case 114:                       //r
if (rotateOK==1)
return;
rotateType=4;
rotateOK=1;
rotate=0.0f;
break;
case 116:                       //t
if (rotateOK==1)
return;
rotateType=5;
rotateOK=1;
rotate=0.0f;
break;
case 121:                       //y
if (rotateOK==1)
return;
rotateType=6;
rotateOK=1;
rotate=0.0f;
break;
case 97:                        //a
if (rotateOK==1)
return;
rotateType=7;
rotateOK=1;
rotate=0.0f;
break;
case 115:                       //s
if (rotateOK==1)
return;
rotateType=8;
rotateOK=1;
rotate=0.0f;
break;
case 100:                       //d
if (rotateOK==1)
return;
rotateType=9;
rotateOK=1;
rotate=0.0f;
break;
case 102:                       //f
if (rotateOK==1)
return;
rotateType=10;
rotateOK=1;
rotate=0.0f;
break;
case 103:                       //g
if (rotateOK==1)
return;
rotateType=11;
rotateOK=1;
rotate=0.0f;
break;
case 104:                       //h
if (rotateOK==1)
return;
rotateType=12;
rotateOK=1;
rotate=0.0f;
break;
case VK_UP:
xRot-= 5.0f;
break;
case VK_DOWN:
xRot += 5.0f;
break;
case VK_LEFT:
yRot -= 5.0f;
break;
case VK_RIGHT:
yRot += 5.0f;
break;
}
if(xRot > 356.0f)
xRot = 0.0f;
if(xRot < -1.0f)
xRot = 355.0f;
if(yRot > 356.0f)
yRot = 0.0f;
if(yRot < -1.0f)
yRot = 355.0f;
}
void exitGame(HWND hWnd,HDC hDC,HGLRC hRC)
{
// Kill the timer that we created
KillTimer(hWnd,101);
// Deselect the current rendering context and delete it
wglMakeCurrent(hDC,NULL);
wglDeleteContext(hRC);
// Delete the palette
if(hPalette != NULL)
DeleteObject(hPalette);
// Tell the application to terminate after the window
// is gone.
PostQuitMessage(0);
}
// Entry point of all Windows programs
int APIENTRY WinMain(   HINSTANCE   hInstance,
HINSTANCE   hPrevInstance,
LPSTR       lpCmdLine,
int         nCmdShow)
{
MSG         msg;        // Windows message structure
WNDCLASS    wc;         // Windows class structure
HWND        hWnd;       // Storeage for window handle
HWND        hDesktopWnd;// Storeage for desktop window handle
HDC         hDesktopDC; // Storeage for desktop window device context
int         nScreenX, nScreenY; // Screen Dimensions
// Register Window style
wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc      = (WNDPROC) WndProc;
wc.cbClsExtra       = 0;
wc.cbWndExtra       = 0;
wc.hInstance        = hInstance;
wc.hIcon            = NULL;
wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
// No need for background brush for OpenGL window
wc.hbrBackground    = NULL;
wc.lpszMenuName     = NULL;
wc.lpszClassName    = lpszAppName;
// Register the window class
if(RegisterClass(&wc) == 0)
return FALSE;
// Get he Window handle and Device context to the desktop
hDesktopWnd = GetDesktopWindow();
hDesktopDC = GetDC(hDesktopWnd);
// Get the screen size
nScreenX = GetDeviceCaps(hDesktopDC, HORZRES);
nScreenY = GetDeviceCaps(hDesktopDC, VERTRES);
// Release the desktop device context
ReleaseDC(hDesktopWnd, hDesktopDC);
// Create the main application window
hWnd = CreateWindow(
lpszAppName,
lpszAppName,
// OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
// Window position and size
0, 0,
nScreenX, nScreenY,
NULL,
NULL,
hInstance,
NULL);
// If window was not created, quit
if(hWnd == NULL)
return FALSE;
// Display the window
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
// Process application messages until the application closes
while( GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
// Window procedure, handles all messages for this program
LRESULT CALLBACK WndProc(   HWND    hWnd,
UINT    message,
WPARAM  wParam,
LPARAM  lParam)
{
static HGLRC hRC;       // Permenant Rendering context
static HDC hDC;         // Private GDI Device context
switch (message)
{
// Window creation, setup for OpenGL
case WM_CREATE:
// Store the device context
hDC = GetDC(hWnd);
// Select the pixel format
SetDCPixelFormat(hDC);
// Create the rendering context and make it current
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
// Create the palette
hPalette = GetOpenGLPalette(hDC);
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
glEnable(GL_DEPTH_TEST);
//  glEnable(GL_DITHER);
glShadeModel(GL_SMOOTH);
// Create a timer that fires 30 times a second
SetTimer(hWnd,33,1,NULL);
break;
// Window is being destroyed, cleanup
case WM_DESTROY:
exitGame(hWnd,hDC,hRC);
break;
case WM_KEYDOWN:
dealKey(hWnd,hDC,hRC,wParam);
InvalidateRect(hWnd,NULL,FALSE);
break;
case WM_CHAR:
dealKey(hWnd,hDC,hRC,wParam);
InvalidateRect(hWnd,NULL,FALSE);
break;
// Window is resized.
case WM_SIZE:
// Call our function which modifies the clipping
// volume and viewport
ChangeSize(LOWORD(lParam), HIWORD(lParam));
break;
// Timer, moves and bounces the rectangle, simply calls
// our previous OnIdle function, then invalidates the
// window so it will be redrawn.
case WM_TIMER:
{
IdleFunction();
InvalidateRect(hWnd,NULL,FALSE);
}
break;
// The painting function.  This message sent by Windows
// whenever the screen needs updating.
case WM_PAINT:
{
// Call OpenGL drawing code
RenderScene();
// Call function to swap the buffers
SwapBuffers(hDC);
// Validate the newly painted client area
ValidateRect(hWnd,NULL);
}
break;
// Windows is telling the application that it may modify
// the system palette.  This message in essance asks the
// application for a new palette.
case WM_QUERYNEWPALETTE:
// If the palette was created.
if(hPalette)
{
int nRet;
// Selects the palette into the current device context
SelectPalette(hDC, hPalette, FALSE);
// Map entries from the currently selected palette to
// the system palette.  The return value is the number
// of palette entries modified.
nRet = RealizePalette(hDC);
// Repaint, forces remap of palette in current window
InvalidateRect(hWnd,NULL,FALSE);
return nRet;
}
break;
// This window may set the palette, even though it is not the
// currently active window.
case WM_PALETTECHANGED:
// Don't do anything if the palette does not exist, or if
// this is the window that changed the palette.
if((hPalette != NULL) && ((HWND)wParam != hWnd))
{
// Select the palette into the device context
SelectPalette(hDC,hPalette,FALSE);
// Map entries to system palette
RealizePalette(hDC);
// Remap the current colors to the newly realized palette
UpdateColors(hDC);
return 0;
}
break;
default:   // Passes it on if unproccessed
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0L);
}

cubemanage.cpp文件为:

#include "iostream"
using namespace std;
#include "cubemanage.h"
CubeManage::CubeManage(){
for (int i=0;i<CUBE_SIZE;i++) {
for (int j=0;j<CUBE_SIZE;j++) {
for (int k=0;k<CUBE_SIZE;k++) {
cubes[i][j][k]=new WcgCube();
}
}
}
}
CubeManage::~CubeManage(){
for (int i=0;i<CUBE_SIZE;i++) {
for (int j=0;j<CUBE_SIZE;j++) {
for (int k=0;k<CUBE_SIZE;k++) {
delete cubes[i][j][k];
}
}
}
}
void CubeManage::turn(int rotateType) {
if (rotateType==1) {
turnByZShun(2);
}
else
if (rotateType==2) {
turnByXShun(2);
}
else
if (rotateType==3) {
turnByZNi(0);
}
else
if (rotateType==4) {
turnByXNi(0);
}
else
if (rotateType==5) {
turnByYShun(2);
}
else
if (rotateType==6) {
turnByYNi(0);
}
else
if (rotateType==7) {
turnByZNi(2);
}
else
if (rotateType==8) {
turnByXNi(2);
}
else
if (rotateType==9) {
turnByZShun(0);
}
else
if (rotateType==10) {
turnByXShun(0);
}
else
if (rotateType==11) {
turnByYNi(2);
}
else
if (rotateType==12) {
turnByYShun(0);
}
}
void CubeManage::draw(int rotateType,GLfloat rotate) {
GLfloat PI=3.1415f;
GLfloat cubeRadium=10.0f;
GLfloat cubeSpace=2.0f;
GLfloat x,y,z;
int i,j,k;
x=ORIENTX-(CUBE_SIZE/2)*(cubeRadium*2+cubeSpace);
y=ORIENTZ-(CUBE_SIZE/2)*(cubeRadium*2+cubeSpace);
z=ORIENTZ-(CUBE_SIZE/2)*(cubeRadium*2+cubeSpace);
if (rotateType==0) {
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
for (k=0;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==1) {
glPushMatrix();
glRotatef(360-180*rotate/PI,0.0f,0.0f,1.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[i][j][CUBE_SIZE-1])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1));
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
for (k=0;k<CUBE_SIZE-1;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==2) {
glPushMatrix();
glRotatef(360-180*rotate/PI,1.0f,0.0f,0.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[CUBE_SIZE-1][i][j])->draw(x+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE-1;i++) {
for (j=0;j<CUBE_SIZE;j++) {
for (k=0;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==3) {
glPushMatrix();
glRotatef(360-180*rotate/PI,0.0f,0.0f,-1.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[i][j][0])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z);
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
for (k=1;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==4) {
glPushMatrix();
glRotatef(360-180*rotate/PI,-1.0f,0.0f,0.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[0][i][j])->draw(x,y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);
}
}
glPopMatrix();
for (i=1;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
for (k=0;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==5) {
glPushMatrix();
glRotatef(360-180*rotate/PI,0.0f,1.0f,0.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[i][CUBE_SIZE-1][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),z+(cubeRadium*2+cubeSpace)*j);
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE-1;j++) {
for (k=0;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==6) {
glPushMatrix();
glRotatef(360-180*rotate/PI,0.0f,-1.0f,0.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[i][0][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y,z+(cubeRadium*2+cubeSpace)*j);
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE;i++) {
for (j=1;j<CUBE_SIZE;j++) {
for (k=0;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==7) {
glPushMatrix();
glRotatef(180*rotate/PI,0.0f,0.0f,1.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[i][j][CUBE_SIZE-1])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1));
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
for (k=0;k<CUBE_SIZE-1;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==8) {
glPushMatrix();
glRotatef(180*rotate/PI,1.0f,0.0f,0.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[CUBE_SIZE-1][i][j])->draw(x+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE-1;i++) {
for (j=0;j<CUBE_SIZE;j++) {
for (k=0;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==9) {
glPushMatrix();
glRotatef(180*rotate/PI,0.0f,0.0f,-1.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[i][j][0])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z);
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
for (k=1;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==10) {
glPushMatrix();
glRotatef(180*rotate/PI,-1.0f,0.0f,0.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[0][i][j])->draw(x,y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);
}
}
glPopMatrix();
for (i=1;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
for (k=0;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==11) {
glPushMatrix();
glRotatef(180*rotate/PI,0.0f,1.0f,0.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[i][CUBE_SIZE-1][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),z+(cubeRadium*2+cubeSpace)*j);
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE-1;j++) {
for (k=0;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
else
if (rotateType==12) {
glPushMatrix();
glRotatef(180*rotate/PI,0.0f,-1.0f,0.0f);
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
(cubes[i][0][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y,z+(cubeRadium*2+cubeSpace)*j);
}
}
glPopMatrix();
for (i=0;i<CUBE_SIZE;i++) {
for (j=1;j<CUBE_SIZE;j++) {
for (k=0;k<CUBE_SIZE;k++) {
(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
}
}
}
}
}
void CubeManage::output() {
for (int i=0;i<CUBE_SIZE;i++) {
for (int j=0;j<CUBE_SIZE;j++) {
cubes[0][i][j]->output();
}
}
}
void CubeManage::output(int scr,int site){
int sign;
int i,j;
if (site==1) {
cout << "site=1,nonsense!" << endl;
return;
}
switch (scr) {
case 1:
if (site==0)
sign=-X;
else
sign=X;
cout << "scr=" << scr << " sign=" << sign << endl;
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
cout << i << "," << j << "=";
cubes[site][i][j]->output(sign);
cout << endl;
}
}
break;
case 2:
if (site==0)
sign=-Y;
else
sign=Y;
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
cout << i << "," << j << "=";
cubes[i][site][j]->output(sign);
cout << endl;
}
}
break;
case 3:
if (site==0)
sign=-Z;
else
sign=Z;
for (i=0;i<CUBE_SIZE;i++) {
for (j=0;j<CUBE_SIZE;j++) {
cout << i << "," << j << "=";
cubes[i][j][site]->output(sign);
cout << endl;
}
}
break;
}
}
void CubeManage::goStep(int *leftLeg,int *rightLeg,int *goDirection,int step,int leftEdge,int rightEdge) {
for (int i=0;i<step;i++) {
switch (*goDirection) {
case 0:
*leftLeg=*leftLeg-1;
if (*leftLeg<leftEdge) {
*leftLeg=*leftLeg+1;
*goDirection=3;
*rightLeg=*rightLeg+1;
}
break;
case 1:
*rightLeg=*rightLeg-1;
if (*rightLeg<leftEdge) {
*rightLeg=*rightLeg+1;
*goDirection=0;
*leftLeg=*leftLeg-1;
}
break;
case 2:
*leftLeg=*leftLeg+1;
if (*leftLeg>=rightEdge) {
*leftLeg=*leftLeg-1;
*goDirection=1;
*rightLeg=*rightLeg-1;
}
break;
case 3:
*rightLeg=*rightLeg+1;
if (*rightLeg>=rightEdge) {
*rightLeg=*rightLeg-1;
*goDirection=2;
*leftLeg=*leftLeg+1;
}
break;
}
}
}
void CubeManage::turnByXShun(int x) {
int step=CUBE_SIZE-1;
int leftEdge=0;
int rightEdge=CUBE_SIZE;
int goDirection0=3;
int goDirection1=3;
int y0=0;
int z0=0;
int y1=0;
int z1=0;
WcgCube *tempcubes[CUBE_SIZE][CUBE_SIZE];
tempcubes[CUBE_SIZE/2][CUBE_SIZE/2]=cubes[x][CUBE_SIZE/2][CUBE_SIZE/2];
cubes[x][CUBE_SIZE/2][CUBE_SIZE/2]->turnByXShun(x);
for (int i=0;i<CUBE_SIZE/2;i++) {
step=CUBE_SIZE-i*2-1;
goDirection0=3;
goDirection1=3;
leftEdge=i;
rightEdge=CUBE_SIZE-i;
y0=leftEdge;
z0=leftEdge;
y1=leftEdge;
z1=leftEdge;
goStep(&y1,&z1,&goDirection1,step,leftEdge,rightEdge);
for (int j=0;j<step*4;j++) {
tempcubes[y1][z1]=cubes[x][y0][z0];
cubes[x][y0][z0]->turnByXShun(x);
goStep(&y0,&z0,&goDirection0,1,leftEdge,rightEdge);
goStep(&y1,&z1,&goDirection1,1,leftEdge,rightEdge);
}
for (int m=0;m<CUBE_SIZE;m++) {
for (int n=0;n<CUBE_SIZE;n++) {
cubes[x][m][n]=tempcubes[m][n];
}
}
}
}
void CubeManage::turnByXNi(int x) {
turnByXShun(x);
turnByXShun(x);
turnByXShun(x);
}
void CubeManage::turnByYShun(int y) {
int step=CUBE_SIZE-1;
int leftEdge=0;
int rightEdge=CUBE_SIZE;
int goDirection0=3;
int goDirection1=3;
int x0=0;
int z0=0;
int x1=0;
int z1=0;
WcgCube *tempcubes[CUBE_SIZE][CUBE_SIZE];
tempcubes[CUBE_SIZE/2][CUBE_SIZE/2]=cubes[CUBE_SIZE/2][y][CUBE_SIZE/2];
cubes[CUBE_SIZE/2][y][CUBE_SIZE/2]->turnByYShun(y);
for (int i=0;i<CUBE_SIZE/2;i++) {
step=CUBE_SIZE-i*2-1;
goDirection0=3;
goDirection1=3;
leftEdge=i;
rightEdge=CUBE_SIZE-i;
x0=leftEdge;
z0=leftEdge;
x1=leftEdge;
z1=leftEdge;
goStep(&z1,&x1,&goDirection1,step,leftEdge,rightEdge);
for (int j=0;j<step*4;j++) {
tempcubes[x1][z1]=cubes[x0][y][z0];
cubes[x0][y][z0]->turnByYShun(y);
goStep(&z0,&x0,&goDirection0,1,leftEdge,rightEdge);
goStep(&z1,&x1,&goDirection1,1,leftEdge,rightEdge);
}
for (int m=0;m<CUBE_SIZE;m++) {
for (int n=0;n<CUBE_SIZE;n++) {
cubes[m][y][n]=tempcubes[m][n];
}
}
}
}
void CubeManage::turnByYNi(int y) {
turnByYShun(y);
turnByYShun(y);
turnByYShun(y);
}
void CubeManage::turnByZShun(int z) {
int step=CUBE_SIZE-1;
int leftEdge=0;
int rightEdge=CUBE_SIZE;
int goDirection0=3;
int goDirection1=3;
int x0=0;
int y0=0;
int x1=0;
int y1=0;
WcgCube *tempcubes[CUBE_SIZE][CUBE_SIZE];
tempcubes[CUBE_SIZE/2][CUBE_SIZE/2]=cubes[CUBE_SIZE/2][CUBE_SIZE/2][z];
cubes[CUBE_SIZE/2][CUBE_SIZE/2][z]->turnByZShun(z);
for (int i=0;i<CUBE_SIZE/2;i++) {
step=CUBE_SIZE-i*2-1;
goDirection0=3;
goDirection1=3;
leftEdge=i;
rightEdge=CUBE_SIZE-i;
x0=leftEdge;
y0=leftEdge;
x1=leftEdge;
y1=leftEdge;
goStep(&x1,&y1,&goDirection1,step,leftEdge,rightEdge);
for (int j=0;j<step*4;j++) {
tempcubes[x1][y1]=cubes[x0][y0][z];
cubes[x0][y0][z]->turnByZShun(z);
goStep(&x0,&y0,&goDirection0,1,leftEdge,rightEdge);
goStep(&x1,&y1,&goDirection1,1,leftEdge,rightEdge);
}
for (int m=0;m<CUBE_SIZE;m++) {
for (int n=0;n<CUBE_SIZE;n++) {
cubes[m][n][z]=tempcubes[m][n];
}
}
}
}
void CubeManage::turnByZNi(int z) {
turnByZShun(z);
turnByZShun(z);
turnByZShun(z);
}

wcgcube.cpp文件为:

#include "iostream"
using namespace std;
#include "wcgcube.h"
WcgCube::WcgCube(){
direct[0]=Z;
direct[1]=X;
direct[2]=-Z;
direct[3]=-X;
direct[4]=Y;
direct[5]=-Y;
sideColor[0][0]=1.0f;
sideColor[0][1]=1.0f;
sideColor[0][2]=1.0f;
sideColor[1][0]=1.0f;
sideColor[1][1]=1.0f;
sideColor[1][2]=0.0f;
sideColor[2][0]=1.0f;
sideColor[2][1]=0.0f;
sideColor[2][2]=0.0f;
sideColor[3][0]=1.0f;
sideColor[3][1]=0.0f;
sideColor[3][2]=1.0f;
sideColor[4][0]=0.0f;
sideColor[4][1]=1.0f;
sideColor[4][2]=1.0f;
sideColor[5][0]=0.0f;
sideColor[5][1]=1.0f;
sideColor[5][2]=0.0f;
}
WcgCube::~WcgCube(){
}
void WcgCube::draw(GLfloat orientX,GLfloat orientY,GLfloat orientZ) {
GLfloat cubeRadium=10.0f;
GLfloat cubeSpace=2.0f;
for (int i=0;i<6;i++) {
glColor3f(sideColor[i][0],sideColor[i][1],sideColor[i][2]);
if (direct[i]==Z) {
// Front face
glBegin(GL_POLYGON);
glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);
glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);
glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);
glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);
glEnd();
}
else
if (direct[i]==-Z) {
// Back Face
glBegin(GL_POLYGON);
glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);
glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);
glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);
glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);
glEnd();
}
else
if (direct[i]==Y) {
// Top Face
glBegin(GL_POLYGON);
glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);
glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);
glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);
glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);
glEnd();
}
else
if (direct[i]==-Y) {
// Bottom Face
glBegin(GL_POLYGON);
glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);
glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);
glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);
glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);
glEnd();
}
else
if (direct[i]==X) {
// Left face
glBegin(GL_POLYGON);
glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);
glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);
glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);
glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);
glEnd();
}
else
if (direct[i]==-X) {
// Right face
glBegin(GL_POLYGON);
glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);
glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);
glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);
glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);
glEnd();
}
}
}
void WcgCube::output() {
for (int i=0;i<6;i++) {
cout << "direct[" << i << "]=" << direct[i] << endl;
}
}
void WcgCube::output(int sign) {
for (int i=0;i<6;i++) {
if (direct[i]==sign)
cout <<  i;
}
}
void WcgCube::turnByXShun(int x) {
turnByX(x,-1);
}
void WcgCube::turnByXNi(int x) {
turnByX(x,1);
}
void WcgCube::turnByX(int x,int sign) {
for (int i=0;i<6;i++) {
switch (direct[i]) {
case Z:
direct[i]=(-1)*sign*Y;
break;
case -Z:
direct[i]=sign*Y;
break;
case Y:
direct[i]=sign*Z;
break;
case -Y:
direct[i]=(-1)*sign*Z;
break;
}
}
}
void WcgCube::turnByYShun(int y) {
turnByY(y,-1);
}
void WcgCube::turnByYNi(int y) {
turnByY(y,1);
}
void WcgCube::turnByY(int y,int sign) {
for (int i=0;i<6;i++) {
switch (direct[i]) {
case Z:
direct[i]=sign*X;
break;
case -Z:
direct[i]=(-1)*sign*X;
break;
case X:
direct[i]=(-1)*sign*Z;
break;
case -X:
direct[i]=sign*Z;
break;
}
}
}
void WcgCube::turnByZShun(int z) {
turnByZ(z,-1);
}
void WcgCube::turnByZNi(int z) {
turnByZ(z,1);
}
void WcgCube::turnByZ(int z,int sign) {
for (int i=0;i<6;i++) {
switch (direct[i]) {
case Y:
direct[i]=(-1)*sign*X;
break;
case -Y:
direct[i]=sign*X;
break;
case X:
direct[i]=sign*Y;
break;
case -X:
direct[i]=(-1)*sign*Y;
break;
}
}
}

通过键盘上的按键q、w、e、r、t、a、s、d、f、g、h来旋转改变魔方的各种组合。

最终效果图如下所示:

OpenGL实现3D魔方游戏源代码相关推荐

  1. 基于C++的3D魔方游戏+完整源代码

    VC++ 6.0 编写的3D魔方游戏附代码,开发智力的益智小游戏.游戏界面和游戏控制是很值得学习的.这款魔方游戏玩法:用鼠标选中需要调整的魔方方块,点击右侧的对应操作,即可改变魔方状态. 项目源代码: ...

  2. Android 3D 魔方游戏的设计与开发

    Android 3D 魔方游戏的设计与开发 5.1 Feature 定义 魔方是一个有趣的益智游戏,相信很多人都玩过.本次毕业设计,欲完成的主要的功能如下: (1) 开始游戏:开始一个新的游戏 (2) ...

  3. 用JAVA FX制作3D魔方游戏

    最近正在翻译一篇老外的文章,用JAVA FX制作的3D魔方游戏. 翻译完成后,会在本博客贴出. 运行的效果见图片.

  4. 基于opengl的3d漫游游戏 - 古堡危机之丧尸围城

    作品名称: <古堡危机> 小组团队名称: 拾荒三人组 日期:2018年12月 目录 第一章 简介 3 前言 3 项目的创意设想.游戏类型.实现的功能.项目意义 3 Opengl 4 作品代 ...

  5. java魔方游戏代码_Java 3D魔方

    很强的JAVA 3D魔方游戏,这是源码及中文注释包,作者的开发文档和思绪都在里面,对学习很有帮助! Java 3D魔方游戏源码及中文注释包 (1 folders, 2 files, 1.38 KB, ...

  6. DirectX11--实现一个3D魔方(1)

    前言 可以说,魔方跟我的人生也有一定的联系. 在高中的学校接触到了魔方社,那时候的我虽然也能够还原魔方,可看到大神们总是可以非常快地还原,为此我也走上了学习高级公式CFOP的坑.当初学习的网站是在魔方 ...

  7. 软件工程第二次作业:基于Unity的3D魔方实现

    基于Unity的3D魔方实现 王化明(2012211757).吴静(2012211523)  一.背景: 魔方,英文名为Rubik's Cube,又叫魔术方块,也称鲁比克方块.是匈牙利布达佩斯建筑学院 ...

  8. Canvas 3D魔方小游戏

    目录 一.案列效果 二.案列思路 三.代码目录 四.代码实现 一.案列效果 二.案列思路 1.先将平面上的6个DIV拼接在一起.形成一张类似于3d立方体图形展开的平面图. 2.我们需要将每一个面旋转到 ...

  9. 爆肝一周,完成了一款第一人称3D射击游戏,现在把源代码分享给大家,适合新手跟着学习

    一.项目背景 大家好,我是小飞 我之前给大家分享了一些网上现成的游戏模板资源,但是很多小伙伴后来反馈说网上的资源有些不能用了,导入进去就会报错等等的问题. 还有一些小伙伴私信问我,有没有什么3D类的游 ...

最新文章

  1. oracle多久断开连接,Oracle自动断开连接
  2. mysql jpa 批注 视图_通过JPA注解映射视图的实体类 jpa 视图 无主键 @Query注解的用法(Spring Data JPA) jpa 使用sql语句...
  3. MySQL日志详细说明
  4. 华为海思年内恐超越联发科 成亚洲最大芯片设计公司
  5. idea 使用 springboot框架开发时,自定义创建 yml文件,系统无法识别。yml文件没有spring的小叶子。完美解决
  6. 高德上线 “家人地图”功能 家人可随时查看彼此的位置
  7. R语言:使用openxlsx读取和写入数据
  8. 绿色版DNF(地下城勇士)全键盘连发工具,支持DNF双开
  9. 匿名管道 与 命名管道
  10. 图像分割并存储 matlab,用于面积增长的Matlab图像分割程序
  11. Ubuntu16.04安装youtub_dl
  12. ​【​观察】云栖大会共话JDM模式 揭秘创新背后的价值和启示
  13. Fire (poj 2152 树形dp)
  14. ST_Intersects
  15. 连续复利 continuous compounding
  16. 基于STM32的超声波雷达项目【可拟合构建平面地图】(代码开源)
  17. 我的学习node.js心得(一)
  18. 360正式造车,网友:自带杀毒软件吗?
  19. 微软市值一周缩水400亿美元 相当于收购雅虎报价
  20. python 大众 同城

热门文章

  1. 一文读懂阿里云数据库Autoscaling是如何工作的
  2. 阿里云开源PolarDB数据库,与社区共建云原生分布式数据库生态
  3. 阿里云邀您参加2020年数据湖高峰会议
  4. 米哈游CEO蔡浩宇GDC演讲:《原神》如何打造动人的开放世界?
  5. 游戏中的整容术! 《Honey Select》捏人系统剖析
  6. Java_小球自由落体_小球下落问题
  7. 域渗透基础之环境搭建(单域到组件域林)
  8. 使用iframe+postMessage跨域操作和通信
  9. C:如何分解整数(从末位往前取以及从首位往后取)
  10. Python基础之(面向对象初识)