版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 基于openGl的三维旋转魔方源代码/* *This Code Was Created By Jeff Molofee 2000 *A HUGE Thanks To Fredric Echols For Cleaning Up *And Optimizing The Base Code, Making It More Flexible! *If You've Found This Code Useful, Please Let Me Know. * */ Header File For Standard Input/Output #include ""HDChDC
2、=NULL;/ Private GDI Device ContextHGLRChRC=NULL;/ Permanent Rendering ContextHWNDhWnd=NULL;/ Holds Our Window HandleHINSTANCEhInstance;/ Holds The Instance Of The Applicationboolkeys256;/ Array Used For The Keyboard Routineboolactive=TRUE;/ Window Active Flag Set To TRUE By Defaultboolfullscreen=TRU
3、E;/ Fullscreen Flag Set To Fullscreen Mode By DefaultGLfloatxrot;/ X Rotation ( NEW )GLfloatyrot;/ Y Rotation ( NEW )GLfloatzrot;/ Z Rotation ( NEW )GLfloat RX;GLfloat RY;GLuinttexture24;/ Storage For One Texture ( NEW )GLboolean b_RX,b_RY;LRESULTCALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);/ Declar
4、ation For WndProcAUX_RGBImageRec *LoadBMP(char *Filename)/ Loads A Bitmap ImageFILE *File=NULL;/ File Handleif (!Filename)/ Make Sure A Filename Was Givenreturn NULL;/ If Not Return NULLFile=fopen(Filename,"r");/ Check To See If The File Existsif (File)/ Does The File Exist?fclose(File);/
5、Close The Handlereturn auxDIBImageLoad(Filename);/ Load The Bitmap And Return A Pointerreturn NULL;/ If Load Failed Return NULLint LoadGLTextures()/ Load Bitmaps And Convert To Texturesint Status=FALSE;/ Status IndicatorAUX_RGBImageRec *TextureImage1;/ Create Storage Space For The Texturememset(Text
6、ureImage,0,sizeof(void *)*1); / Set The Pointer To NULL/ Load The Bitmap, Check For Errors, If Bitmap's Not Found Quitif (TextureImage0=LoadBMP("Data/NeHe.bmp")Status=TRUE;/ Set The Status To TRUEglGenTextures(1, &texture0);/ Create The Texture/ Typical Texture Generation Using Dat
7、a From The BitmapglBindTexture(GL_TEXTURE_2D, texture0);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(GL_TEXTURE_2D,GL_TEXTURE_MAG
8、_FILTER,GL_LINEAR);if (TextureImage0)/ If Texture Existsif (TextureImage0->data)/ If Texture Image Existsfree(TextureImage0->data);/ Free The Texture Image Memoryfree(TextureImage0);/ Free The Image Structurereturn Status;/ Return The Statusint LoadGLTextures(char *file,int index)/ Load Bitmap
9、s And Convert To Texturesint Status=FALSE;/ Status IndicatorAUX_RGBImageRec *TextureImage1;/ Create Storage Space For The Texturememset(TextureImage,0,sizeof(void *)*1); / Set The Pointer To NULL/ Load The Bitmap, Check For Errors, If Bitmap's Not Found Quitif (TextureImage0=LoadBMP(file)Status=
10、TRUE;/ Set The Status To TRUEglGenTextures(1, &textureindex);/ Create The Texture/ Typical Texture Generation Using Data From The BitmapglBindTexture(GL_TEXTURE_2D, textureindex);glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage0->sizeX, TextureImage0->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Text
11、ureImage0->data);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);if (TextureImage0)/ If Texture Existsif (TextureImage0->data)/ If Texture Image Existsfree(TextureImage0->data);/ Free The Texture Image Memoryfree(
12、TextureImage0);/ Free The Image Structurereturn Status;/ Return The StatusGLvoid ReSizeGLScene(GLsizei width, GLsizei height)/ Resize And Initialize The GL Windowif (height=0)/ Prevent A Divide By Zero Byheight=1;/ Making Height Equal OneglViewport(0,0,width,height);/ Reset The Current ViewportglMat
13、rixMode(GL_PROJECTION);/ Select The Projection MatrixglLoadIdentity();/ Reset The Projection Matrix/ Calculate The Aspect Ratio Of The WindowgluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);/ Select The Modelview MatrixglLoadIdentity();/ Reset The Modelview
14、 Matrixint InitGL(GLvoid)/ All Setup For OpenGL Goes Hereif (!LoadGLTextures("Data/y1.bmp",0)/ Jump To Texture Loading Routine ( NEW )return FALSE;/ If Texture Didn't Load Return FALSEif (!LoadGLTextures("Data/y2.bmp",1)/ Jump To Texture Loading Routine ( NEW )return FALSE;/
15、If Texture Didn't Load Return FALSEif (!LoadGLTextures("Data/y3.bmp",2)/ Jump To Texture Loading Routine ( NEW )return FALSE;/ If Texture Didn't Load Return FALSEif (!LoadGLTextures("Data/y4.bmp",3)/ Jump To Texture Loading Routine ( NEW )return FALSE;/ If Texture Didn
16、9;t Load Return FALSEif (!LoadGLTextures("Data/y5.bmp",4)/ Jump To Texture Loading Routine ( NEW )return FALSE;/ If Texture Didn't Load Return FALSEif (!LoadGLTextures("Data/y6.bmp",5)/ Jump To Texture Loading Routine ( NEW )return FALSE;/ If Texture Didn't Load Return FA
17、LSEif (!LoadGLTextures("Data/y7.bmp",6)/ Jump To Texture Loading Routine ( NEW )return FALSE;/ If Texture Didn't Load Return FALSEif (!LoadGLTextures("Data/y8.bmp",7)/ Jump To Texture Loading Routine ( NEW )return FALSE;/ If Texture Didn't Load Return FALSE glEnable(GL_TE
18、XTURE_2D);/ Enable Texture Mapping ( NEW )glShadeModel(GL_SMOOTH);/ Enable Smooth ShadingglClearColor(0.0f, 0.0f, 0.0f, 0.5f);/ Black BackgroundglClearDepth(1.0f);/ Depth Buffer SetupglEnable(GL_DEPTH_TEST);/ Enables Depth TestingglDepthFunc(GL_LEQUAL);/ The Type Of Depth Testing To DoglHint(GL_PERS
19、PECTIVE_CORRECTION_HINT, GL_NICEST);/ Really Nice Perspective Calculationsreturn TRUE;/ Initialization Went OKvoid DrawObject1(GLfloat X,GLfloat Y,GLfloat Z)glBegin(GL_QUADS);/ Front FaceglTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f + X, -1.0f + Y, 1.0f + Z);glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f +
20、 X, -1.0f + Y, 1.0f + Z);glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f + X, 1.0f + Y, 1.0f + Z);glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f + X, 1.0f + Y, 1.0f + Z);/ Back FaceglTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f + X, -1.0f + Y, -1.0f + Z);glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f + X, 1.0f + Y,
21、-1.0f + Z);glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f + X, 1.0f + Y, -1.0f + Z);glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f + X, -1.0f + Y, -1.0f + Z);/ Top FaceglTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f + X, 1.0f + Y, -1.0f + Z);glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f + X, 1.0f + Y, 1.0f + Z);glT
22、exCoord2f(1.0f, 0.0f); glVertex3f( 1.0f + X, 1.0f + Y, 1.0f + Z);glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f + X, 1.0f + Y, -1.0f + Z);/ Bottom FaceglTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f + X, -1.0f + Y, -1.0f + Z);glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f + X, -1.0f + Y, -1.0f + Z);glTexCoord2f(
23、0.0f, 0.0f); glVertex3f( 1.0f + X, -1.0f + Y, 1.0f + Z);glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f + X, -1.0f + Y, 1.0f + Z);/ Right faceglTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f + X, -1.0f + Y, -1.0f + Z);glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f + X, 1.0f + Y, -1.0f + Z);glTexCoord2f(0.0f, 1.0f)
24、; glVertex3f( 1.0f + X, 1.0f + Y, 1.0f + Z);glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f + X, -1.0f + Y, 1.0f + Z);/ Left FaceglTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f + X, -1.0f + Y, -1.0f + Z);glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f + X, -1.0f + Y, 1.0f + Z);glTexCoord2f(1.0f, 1.0f); glVertex3f(
25、-1.0f + X, 1.0f + Y, 1.0f + Z);glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f + X, 1.0f + Y, -1.0f + Z);glEnd(); void DrawObject(int ID)stPoint *CubePoint = CubeID.CubePoint;glBegin(GL_QUADS);/ Front FaceglTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint0.p);glTexCoord2f(1.0f, 0.0f); glVertex3fv( CubePoin
26、t1.p);glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint2.p);glTexCoord2f(0.0f, 1.0f); glVertex3fv( CubePoint3.p);/ Back FaceglTexCoord2f(1.0f, 0.0f); glVertex3fv( CubePoint4.p);glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint5.p);glTexCoord2f(0.0f, 1.0f); glVertex3fv( CubePoint6.p);glTexCoord2f(0.0f,
27、 0.0f); glVertex3fv( CubePoint7.p);/ Top FaceglTexCoord2f(0.0f, 1.0f); glVertex3fv( 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);/ Bottom FaceglTexCoord2f(1.0f, 1.0f); glVe
28、rtex3fv( CubePoint4.p);glTexCoord2f(0.0f, 1.0f); glVertex3fv( CubePoint7.p);glTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint1.p);glTexCoord2f(1.0f, 0.0f); glVertex3fv( CubePoint0.p);/ Right faceglTexCoord2f(1.0f, 0.0f); glVertex3fv( CubePoint7.p);glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint6.p);
29、glTexCoord2f(0.0f, 1.0f); glVertex3fv( CubePoint2.p);glTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint1.p);/ Left FaceglTexCoord2f(0.0f, 0.0f); glVertex3fv( CubePoint4.p);glTexCoord2f(1.0f, 0.0f); glVertex3fv( CubePoint0.p);glTexCoord2f(1.0f, 1.0f); glVertex3fv( CubePoint3.p);glTexCoord2f(0.0f, 1.0f)
30、; glVertex3fv( CubePoint5.p);glEnd(); int DrawGLScene(GLvoid)/ Here's Where We Do All The DrawingglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);/ Clear The Screen And The Depth BufferglLoadIdentity();/ Reset The ViewglTranslatef(0.0f,0.0f,-12.0f);glTranslatef(RX,0.0f,0.0f); glTranslatef(0.0,
31、RY,0.0); glRotatef(xrot,1.0f,0.0f,0.0f); glRotatef(yrot,0.0f,1.0f,0.0f); glRotatef(zrot,0.0f,0.0f,1.0f); /glBindTexture(GL_TEXTURE_2D, texture0); DrawObject(0);glBindTexture(GL_TEXTURE_2D, texture1); DrawObject(1);glBindTexture(GL_TEXTURE_2D, texture2); DrawObject(2);glBindTexture(GL_TEXTURE_2D, tex
32、ture3); DrawObject(3);glBindTexture(GL_TEXTURE_2D, texture4); DrawObject(4);glBindTexture(GL_TEXTURE_2D, texture5); DrawObject(5);glBindTexture(GL_TEXTURE_2D, texture6); DrawObject(6);glBindTexture(GL_TEXTURE_2D, texture7); DrawObject(7);/*/ xrot+=0.15f; yrot+=0.1f; zrot+=0.2f;return TRUE;/ Keep Goi
33、ngGLvoid KillGLWindow(GLvoid)/ Properly Kill The Windowif (fullscreen)/ Are We In Fullscreen Mode?ChangeDisplaySettings(NULL,0);/ If So Switch Back To The DesktopShowCursor(TRUE);/ Show Mouse Pointerif (hRC)/ Do We Have A Rendering Context?if (!wglMakeCurrent(NULL,NULL)/ Are We Able To Release The D
34、C 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?MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATI
35、ON);hRC=NULL;/ Set RC To NULLif (hDC && !ReleaseDC(hWnd,hDC)/ Are We Able To Release The DCMessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);hDC=NULL;/ Set DC To NULLif (hWnd && !DestroyWindow(hWnd)/ Are We Able To Destr
36、oy The Window?MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);hWnd=NULL;/ Set hWnd To NULLif (!UnregisterClass("OpenGL",hInstance)/ Are We Able To Unregister ClassMessageBox(NULL,"Could Not Unregister Class.","SHUTDO
37、WN 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 Mode* *bits- Nu
38、mber 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)GLuintPixelFormat;/ Holds The Results After Searching For A MatchWNDCLASSwc;/ Windows Class Structu
39、reDWORDdwExStyle;/ Window Extended StyleDWORDdwStyle;/ Window StyleRECTWindowRect;/ Grabs Rectangle Upper Left / Lower Right Values=(long)0;/ Set Left Value To 0=(long)width;/ Set Right Value To Requested Width=(long)0;/ Set Top Value To 0=(long)height;/ Set Bottom Value To Requested Heightfullscree
40、n=fullscreenflag;/ Set The Global Fullscreen FlaghInstance= GetModuleHandle(NULL);/ Grab An Instance For Our Window= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;/ Redraw On Size, And Own DC For Window.= (WNDPROC) WndProc;/ WndProc Handles Messages= 0;/ No Extra Window Data= 0;/ No Extra Window Data= hInstanc
41、e;/ Set The Instance= LoadIcon(NULL, IDI_WINLOGO);/ Load The Default Icon= LoadCursor(NULL, IDC_ARROW);/ Load The Arrow Pointer= NULL;/ No Background Required For GL= NULL;/ We Don't Want A Menu= "OpenGL"/ Set The Class Nameif (!RegisterClass(&wc)/ Attempt To Register The Window Cl
42、assMessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;/ Return FALSEif (fullscreen)/ Attempt Fullscreen Mode?DEVMODE dmScreenSettings;/ Device Modememset(&dmScreenSettings,0,sizeof(dmScreenSettings);/ Makes Sure Memory's
43、Cleared=sizeof(dmScreenSettings);/ Size Of The Devmode Structure= width;/ Selected Screen Width= height;/ Selected Screen Height= bits;/ Selected Bits Per PixeldmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;/ Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid
44、 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. Use Windowed Mode Instead?","
45、NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)=IDYES)fullscreen=FALSE;/ Windowed Mode Selected. Fullscreen = FALSEelse/ 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 FALSEif (fu
46、llscreen)/ Are We Still In Fullscreen Mode?dwExStyle=WS_EX_APPWINDOW;/ Window Extended StyledwStyle=WS_POPUP;/ Windows StyleShowCursor(FALSE);/ Hide Mouse PointerelsedwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;/ Window Extended StyledwStyle=WS_OVERLAPPEDWINDOW;/ Windows StyleAdjustWindowRectEx(&
47、;WindowRect, dwStyle, FALSE, dwExStyle);/ Adjust Window To True Requested Size/ Create The Windowif (!(hWnd=CreateWindowEx(dwExStyle,/ Extended Style For The Window"OpenGL",/ Class Nametitle,/ Window TitledwStyle |/ Defined Window StyleWS_CLIPSIBLINGS |/ Required Window StyleWS_CLIPCHILDRE
48、N,/ Required Window Style0, 0,/ Window Position,/ Calculate Window Width,/ Calculate Window HeightNULL,/ No Parent WindowNULL,/ No MenuhInstance,/ InstanceNULL)/ Dont Pass Anything To WM_CREATEKillGLWindow();/ Reset The DisplayMessageBox(NULL,"Window Creation Error.","ERROR",MB_O
49、K|MB_ICONEXCLAMATION);return FALSE;/ Return FALSEstaticPIXELFORMATDESCRIPTOR pfd=/ pfd Tells Windows How We Want Things To Besizeof(PIXELFORMATDESCRIPTOR),/ Size Of This Pixel Format Descriptor1,/ Version NumberPFD_DRAW_TO_WINDOW |/ Format Must Support WindowPFD_SUPPORT_OPENGL |/ Format Must Support
50、 OpenGLPFD_DOUBLEBUFFER,/ Must Support Double BufferingPFD_TYPE_RGBA,/ Request An RGBA Formatbits,/ Select Our Color Depth0, 0, 0, 0, 0, 0,/ Color Bits Ignored0,/ No Alpha Buffer0,/ Shift Bit Ignored0,/ No Accumulation Buffer0, 0, 0, 0,/ Accumulation Bits Ignored16,/ 16Bit Z-Buffer (Depth Buffer) 0,/ No Stencil Buffer0,/ No Auxiliary BufferPFD_MAIN_PLANE,/ Main Drawing Layer0,/ Reserved0, 0, 0/ Layer Masks Ignored;if (!(hDC=GetDC(hWnd)/ Did We Get A Device Context?KillGLWindow();/ Reset The DisplayMessageBox(NULL,"Can'
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024物业管理委托合同范本
- 河北省房屋租赁合同书范文标准版
- 健身房租赁合同范本
- 2024年度建筑材料质量纠纷处理合同3篇
- 2024年度航空公司与机场的航空服务合同
- 2024个人借款的合同模板
- 门窗铝型材加工专利技术使用权转让合同2024
- 家电产品购销合同范本
- 2024年度网站建设合同标的及功能要求
- 2024年动迁地公共交通改善合同
- 2022年全国统一高考化学试卷和答案解析(全国甲卷)
- 平行结转分步法
- 高考英语单词3500记忆短文40篇
- 子宫异常出血病例分析报告
- 特殊使用级抗菌药物申请表
- 管理学课件决策与决策方法2
- 小学美术艺术测评分析报告
- 医疗器械培训教学培训课件
- 电气测试技术46热电阻
- 陕西省西安市高新重点中学2023-2024学年高一上学期第二次月考数学试题(含答案)
- 他律重于自律的四辩总结陈词
评论
0/150
提交评论