《基于openGL的三阶魔方程序代码.docx》由会员分享,可在线阅读,更多相关《基于openGL的三阶魔方程序代码.docx(34页珍藏版)》请在三一办公上搜索。
1、基于openGL的三阶魔方程序代码/*Magic2D.cpp文件: 可以播放爱你一万年歌曲,需要在工程文件夹中新建文件夹“sound”并存放“爱你一万年.wav”文件 需要在Data文件夹中上传y2.bmp到y7.bmp的图片,当然也可以在程序中相应部分自己修改 该程序有两个文件,Magic2D.cpp和MoFang.cpp,可以实现三阶魔方任意层面的旋转并能使魔方在界面来回移动反弹,具体的建工程的步骤就不说了,祝大家好运*/ #include MoFang.h #include /提供与多媒体有关的接口 #pragma comment(lib, WINMM.LIB) /导入winmm.lib
2、库,实现对多媒体编程的支持 HDC hDC=NULL; / Private GDI Device Context HGLRC hRC=NULL; / Permanent Rendering Context HWND hWnd=NULL; / Holds Our Window Handle bool keys256; / Array Used For The Keyboard Routine HINSTANCE hInstance; / Holds The Instance Of The Application bool active=TRUE; / Window Active Flag Set
3、 To TRUE By Default bool fullscreen=TRUE; / Fullscreen Flag Set To Fullscreen Mode By Default GLfloat xrot; / X Rotation ( NEW ) GLfloat yrot; / Y Rotation ( NEW ) GLfloat zrot; / Z Rotation ( NEW ) GLfloat RX=0; GLfloat RY=0; GLuint texture7; / Storage For One Texture ( NEW ) GLboolean b_RX,b_RY; G
4、Lfloat turn_x=1,turn_y=1; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); / CALLBACK代表回调函数 Declaration For WndProc,函数声明,监听windows AUX_RGBImageRec *LoadBMP(char *Filename) / Loads A Bitmap Image FILE *File=NULL; / File Handle if (!Filename) / Make Sure A Filename Was Given return NULL; / If Not
5、 Return NULL File=fopen(Filename,r); / Check To See If The File Exists if (File) / Does The File Exist? fclose(File); / Close The Handle return auxDIBImageLoad(Filename); / Load The Bitmap And Return A Pointer return NULL; / If Load Failed Return NULL void loadsound PlaySound(sound爱你一万年.wav,NULL,SND
6、_LOOP|SND_ASYNC|SND_FILENAME); int LoadGLTextures / Load Bitmaps And Convert To Textures int Status=FALSE; / Status Indicator AUX_RGBImageRec *TextureImage1; / Create Storage Space For The Texture memset(TextureImage,0,sizeof(void *)*1); / Set The Pointer To NULL / Load The Bitmap, Check For Errors,
7、 If Bitmaps Not Found Quit if (TextureImage0=LoadBMP(Data/NeHe.bmp) Status=TRUE; / Set The Status To TRUE glGenTextures(1, &texture0); / Create The Texture / Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture0); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage0-
8、sizeX, TextureImage0-sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage0-data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if (TextureImage0) if (TextureImage0-data) free(TextureImage0-data); Memory free(TextureImage0
9、); return Status; int LoadGLTextures(char *file,int index) And Convert To Textures int Status=FALSE; / If Texture Exists / If Texture Image Exists / Free The Texture Image / Free The Image Structure / Return The Status / Load Bitmaps / Status Indicator AUX_RGBImageRec *TextureImage1; / 创建纹理储存空间 mems
10、et(TextureImage,0,sizeof(void *)*1); / Set The Pointer To NULL / Load The Bitmap, Check For Errors, If Bitmaps Not Found Quit if (TextureImage0=LoadBMP(file) Status=TRUE; / Set The Status To TRUE glGenTextures(1, &textureindex); / 生成纹理 / Typical Texture Generation Using Data From The Bitmap glBindTe
11、xture(GL_TEXTURE_2D, textureindex); /glBindTexture函数实现了将调用glGenTextures函数生成的纹理的名字绑定到对应的目标纹理上。 glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage0-sizeX, TextureImage0-sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage0-data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(
12、GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if (TextureImage0) / If Texture Exists if (TextureImage0-data) / If Texture Image Exists free(TextureImage0-data); Memory free(TextureImage0); return Status; GLvoid ReSizeGLScene(GLsizei width, GLsizei height) Window if (height=0) height=1; glViewport(
13、0,0,width,height); Reset The Current Viewport glMatrixMode(GL_PROJECTION); glLoadIdentity; / Calculate The Aspect Ratio Of The Window / Free The Texture Image / Free The Image Structure / Return The Status / Resize And Initialize The GL / Prevent A Divide By Zero By / Making Height Equal One / 按照指定的
14、高和宽显示图像 / Select The Projection Matrix / Reset The Projection Matrix gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); /使图像显示时自身比例相同,不会产生扭曲,图像在z轴方向上的坐标必须介于-0.1到-100之间,不然显示不出来 glMatrixMode(GL_MODELVIEW); / Select The Modelview Matrix glLoadIdentity; Reset The Modelview Matrix int Init
15、GL(GLvoid) Here if (!LoadGLTextures(Data/y2.bmp,1) texture1 return FALSE; FALSE if (!LoadGLTextures(Data/y3.bmp,2) / 使原点重新回到屏幕中心 / All Setup For OpenGL Goes / 生成纹理 / If Texture Didnt Load Return / Jump To Texture Loading Routine ( NEW ) return FALSE; FALSE if (!LoadGLTextures(Data/y4.bmp,3) Texture
16、Loading Routine ( NEW ) return FALSE; FALSE if (!LoadGLTextures(Data/y5.bmp,4) Texture Loading Routine ( NEW ) return FALSE; FALSE if (!LoadGLTextures(Data/y6.bmp,5) Texture Loading Routine ( NEW ) return FALSE; FALSE if (!LoadGLTextures(Data/y7.bmp,6) Texture Loading Routine ( NEW ) return FALSE; F
17、ALSE glEnable(GL_TEXTURE_2D); ( NEW ) / If Texture Didnt Load Return / Jump To / If Texture Didnt Load Return / Jump To / If Texture Didnt Load Return / Jump To / If Texture Didnt Load Return / Jump To / If Texture Didnt Load Return / Enable Texture Mapping glShadeModel(GL_SMOOTH); / Enable Smooth S
18、hading,启用阴影平滑 glClearColor(0.5f, 0.0f, 0.0f, 0.5f); / Black Background,背景设为黑色 glClearDepth(1.0f); / Depth Buffer Setup,设置深度缓存 glEnable(GL_DEPTH_TEST); / Enables Depth Testing glDepthFunc(GL_LEQUAL); / The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); / Really Nice Pe
19、rspective Calculations loadsound; return TRUE; / Initialization Went OK void DrawObject(int ID) /画出一个立方体,六个面,贴图 stPoint *CubePoint = CubeID.CubePoint; / Front Face glBindTexture(GL_TEXTURE_2D, texture1); glBegin(GL_QUADS); /发布四边形绘图指令 glTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint0.p);/glTexCoord2f
20、指定平面纹理坐标,glVertex3fv是四边形的顶点坐标 glTexCoord2f(1.0f, 0.0f); glVertex3fv( CubePoint1.p); glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint2.p); glTexCoord2f(0.0f, 1.0f); glVertex3fv( CubePoint3.p); glEnd; / Back Face glBindTexture(GL_TEXTURE_2D, texture2); glBegin(GL_QUADS); glTexCoord2f(1.0f, 0.0f); glVe
21、rtex3fv( CubePoint4.p); glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint5.p); glTexCoord2f(0.0f, 1.0f); glVertex3fv( CubePoint6.p); glTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint7.p); glEnd; / Top Face glBindTexture(GL_TEXTURE_2D, texture3); glBegin(GL_QUADS); glTexCoord2f(0.0f, 1.0f); glVertex3fv
22、( CubePoint5.p); glTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint3.p); glTexCoord2f(1.0f, 0.0f); glVertex3fv( CubePoint2.p); glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint6.p); glEnd; / Bottom Face glBindTexture(GL_TEXTURE_2D, texture4); glBegin(GL_QUADS); glTexCoord2f(1.0f, 0.0f); glVertex3fv( Cu
23、bePoint4.p); glTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint7.p); glTexCoord2f(0.0f, 1.0f); glVertex3fv( CubePoint1.p); glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint0.p); glEnd; / Right face glBindTexture(GL_TEXTURE_2D, texture5); glBegin(GL_QUADS); glTexCoord2f(1.0f, 0.0f); glVertex3fv( CubePoi
24、nt7.p); glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint6.p); glTexCoord2f(0.0f, 1.0f); glVertex3fv( CubePoint2.p); glTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint1.p); glEnd; / Left Face glBindTexture(GL_TEXTURE_2D, texture6); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint4.p)
25、; glTexCoord2f(1.0f, 0.0f); glVertex3fv( CubePoint0.p); glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint3.p); glTexCoord2f(0.0f, 1.0f); glVertex3fv( CubePoint5.p); glEnd; int DrawGLScene(GLvoid) / Heres Where We Do All The Drawing int i; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); / Clear Th
26、e Screen And The Depth Buffer glLoadIdentity; / Reset The View glTranslatef(0.0f,0.0f,-12.0f); / glTranslatef为坐标系原点平移函数,相当于相对平面向里面平移12, glTranslatef(RX,0.0f,0.0f); glTranslatef(0.0,RY,0.0); glRotatef(xrot,1.0f,0.0f,0.0f); / 整体的旋转靠库函数来实现,glRotatef为坐标系旋转函数 glRotatef(yrot,0.0f,1.0f,0.0f); glRotatef(zro
27、t,0.0f,0.0f,1.0f); for (i=0;i27;i+) DrawObject(i); xrot+=0.2f; yrot+=0.2f; zrot+=0.2f; /控制魔方整体的旋转 return TRUE; / Keep Going GLvoid KillGLWindow(GLvoid) / Properly Kill The Window if (fullscreen) / Are We In Fullscreen Mode? ChangeDisplaySettings(NULL,0); / If So Switch Back To The Desktop ShowCursor
28、(TRUE); / Show Mouse Pointer if (hRC) / Do We Have A Rendering Context? if (!wglMakeCurrent(NULL,NULL) / Are We Able To Release The DC And RC Contexts? MessageBox(NULL,Release Of DC And RC Failed.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION); if (!wglDeleteContext(hRC) / Are We Able To Delete The RC?
29、MessageBox(NULL,Release Rendering Context Failed.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION); hRC=NULL; / Set RC To NULL if (hDC & !ReleaseDC(hWnd,hDC) / Are We Able To Release The DC MessageBox(NULL,Release Device Context Failed.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION); hDC=NULL; / Set DC To NUL
30、L if (hWnd & !DestroyWindow(hWnd) / Are We Able To Destroy The Window? MessageBox(NULL,Could Not Release hWnd.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION); hWnd=NULL; / Set hWnd To NULL if (!UnregisterClass(OpenGL,hInstance) / Are We Able To Unregister Class MessageBox(NULL,Could Not Unregister Class
31、.,SHUTDOWN ERROR,MB_OK | MB_ICONINFORMATION); hInstance=NULL; / Set hInstance To NULL /* This Code Creates Our OpenGL Window. Parameters Are: * * title - Title To Appear At The Top Of The Window * * width - Width Of The GL Window Or Fullscreen Mode * * height - Height Of The GL Window Or Fullscreen
32、Mode * * bits - Number Of Bits To Use For Color (8/16/24/32) * * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */ BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) GLuint PixelFormat; / Holds The Results After Searching For A Match WNDCLASS
33、 wc; / Windows Class Structure DWORD dwExStyle; / Window Extended Style DWORD dwStyle; / Window Style RECT WindowRect; / Grabs Rectangle Upper Left / Lower Right Values WindowRect.left=(long)0; / Set Left Value To 0 WindowRect.right=(long)width; / Set Right Value To Requested Width WindowRect.top=(l
34、ong)0; / Set Top Value To 0 WindowRect.bottom=(long)height; / Set Bottom Value To Requested Height fullscreen=fullscreenflag; / Set The Global Fullscreen Flag hInstance = GetModuleHandle(NULL); / Grab An Instance For Our Window wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; / Redraw On Size, And Own
35、 DC For Window. wc.lpfnWndProc = (WNDPROC) WndProc; / WndProc Handles Messages wc.cbClsExtra = 0; / No Extra Window Data wc.cbWndExtra = 0; / No Extra Window Data wc.hInstance = hInstance; / Set The Instance wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); / Load The Default Icon wc.hCursor = LoadCursor(NULL
36、, IDC_ARROW); / Load The Arrow Pointer wc.hbrBackground = NULL; / No Background Required For GL wc.lpszMenuName = NULL; / We Dont Want A Menu wc.lpszClassName = OpenGL; / Set The Class Name if (!RegisterClass(&wc) / Attempt To Register The Window Class MessageBox(NULL,Failed To Register The Window C
37、lass.,ERROR,MB_OK|MB_ICONEXCLAMATION); return FALSE; / Return FALSE if (fullscreen) / Attempt Fullscreen Mode? DEVMODE dmScreenSettings; / Device Mode memset(&dmScreenSettings,0,sizeof(dmScreenSettings); / Makes Sure Memorys Cleared dmScreenSettings.dmSize=sizeof(dmScreenSettings); / Size Of The Dev
38、mode Structure dmScreenSettings.dmPelsWidth = width; / Selected Screen Width dmScreenSettings.dmPelsHeight = height; / Selected Screen Height dmScreenSettings.dmBitsPerPel = bits; / Selected Bits Per Pixel dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; / Try To Set Selected Mode
39、 And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) / If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode. if (MessageBox(NULL,The Requested Fullscreen Mode Is Not Supported BynYour Video Card.
40、 Use Windowed Mode Instead?,NeHe GL,MB_YESNO|MB_ICONEXCLAMATION)=IDYES) fullscreen=FALSE; / Windowed Mode Selected. Fullscreen = FALSE else / Pop Up A Message Box Letting User Know The Program Is Closing. MessageBox(NULL,Program Will Now Close.,ERROR,MB_OK|MB_ICONSTOP); return FALSE; / Return FALSE
41、if (fullscreen) / Are We Still In Fullscreen Mode? dwExStyle=WS_EX_APPWINDOW; / Window Extended Style dwStyle=WS_POPUP; / Windows Style ShowCursor(FALSE); / Hide Mouse Pointer全屏则隐去光标 else dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; Extended Style dwStyle=WS_OVERLAPPEDWINDOW; AdjustWindowRectEx(&Wi
42、ndowRect, dwStyle, FALSE, dwExStyle); 非全屏,都将屏幕调整到要求的大小 / Create The Window if (!(hWnd=CreateWindowEx( dwExStyle, For The Window OpenGL, Name / Window / Windows Style / 不管全屏或者/ Extended Style / Class title, / Window Title dwStyle | / Defined Window Style WS_CLIPSIBLINGS | / Required Window Style WS_CLIPCHILDREN, / Required Window Style 0, 0, / Window Position WindowRect.right-WindowRect.left, / Calculate Window Width WindowRect.bottom-WindowRect.top, / Calculate Window Height NULL,