首页 / 操作系统 / Linux / OpenGL实现3D魔方游戏源代码
首先这个程序是建立的是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