版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、/* *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. *Visit My Site At */#include <windows.h>/ Header File For Windows#include <glgl.
2、h>/ Header File For The OpenGL32 Library#include <glglu.h>/ Header File For The GLu32 Library#include <glglaux.h>/ Header File For The Glaux LibraryHDChDC=NULL;/ Private GDI Device ContextHGLRChRC=NULL;/ Permanent Rendering ContextHWNDhWnd=NULL;/ Holds Our Window HandleHINSTANCEhInsta
3、nce;/ Holds The Instance Of The Applicationboolkeys256;/ Array Used For The Keyboard Routineboolactive=TRUE;/ Window Active Flag Set To TRUE By Defaultboolfullscreen=TRUE;/ Fullscreen Flag Set To Fullscreen Mode By DefaultGLfloatrtri;/ Angle For The Triangle ( NEW )GLfloatrquad;/ Angle For The Quad
4、( NEW )LRESULTCALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);/ Declaration For WndProcGLvoid 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 Vie
5、wportglMatrixMode(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
6、 Modelview Matrixint InitGL(GLvoid)/ All Setup For OpenGL Goes HereglShadeModel(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 Te
7、sting To DoglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);/ Really Nice Perspective Calculationsreturn TRUE;/ Initialization Went OKint DrawGLScene(GLvoid)/ Here's Where We Do All The DrawingglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);/ Clear Screen And Depth BufferglLoadIdentity();/ R
8、eset The Current Modelview MatrixglTranslatef(-1.5f,0.0f,-6.0f);/ Move Left 1.5 Units And Into The Screen 6.0glRotatef(rtri,0.0f,1.0f,0.0f);/ Rotate The Triangle On The Y axis ( NEW )glBegin(GL_TRIANGLES);/ Start Drawing A TriangleglColor3f(1.0f,0.0f,0.0f);/ RedglVertex3f( 0.0f, 1.0f, 0.0f);/ Top Of
9、 Triangle (Front)glColor3f(0.0f,1.0f,0.0f);/ GreenglVertex3f(-1.0f,-1.0f, 1.0f);/ Left Of Triangle (Front)glColor3f(0.0f,0.0f,1.0f);/ BlueglVertex3f( 1.0f,-1.0f, 1.0f);/ Right Of Triangle (Front)glColor3f(1.0f,0.0f,0.0f);/ RedglVertex3f( 0.0f, 1.0f, 0.0f);/ Top Of Triangle (Right)glColor3f(0.0f,0.0f
10、,1.0f);/ BlueglVertex3f( 1.0f,-1.0f, 1.0f);/ Left Of Triangle (Right)glColor3f(0.0f,1.0f,0.0f);/ GreenglVertex3f( 1.0f,-1.0f, -1.0f);/ Right Of Triangle (Right)glColor3f(1.0f,0.0f,0.0f);/ RedglVertex3f( 0.0f, 1.0f, 0.0f);/ Top Of Triangle (Back)glColor3f(0.0f,1.0f,0.0f);/ GreenglVertex3f( 1.0f,-1.0f
11、, -1.0f);/ Left Of Triangle (Back)glColor3f(0.0f,0.0f,1.0f);/ BlueglVertex3f(-1.0f,-1.0f, -1.0f);/ Right Of Triangle (Back)glColor3f(1.0f,0.0f,0.0f);/ RedglVertex3f( 0.0f, 1.0f, 0.0f);/ Top Of Triangle (Left)glColor3f(0.0f,0.0f,1.0f);/ BlueglVertex3f(-1.0f,-1.0f,-1.0f);/ Left Of Triangle (Left)glCol
12、or3f(0.0f,1.0f,0.0f);/ GreenglVertex3f(-1.0f,-1.0f, 1.0f);/ Right Of Triangle (Left)glEnd();/ Done Drawing The PyramidglLoadIdentity();/ Reset The Current Modelview MatrixglTranslatef(1.5f,0.0f,-7.0f);/ Move Right 1.5 Units And Into The Screen 7.0glRotatef(rquad,1.0f,1.0f,1.0f);/ Rotate The Quad On
13、The X axis ( NEW )glBegin(GL_QUADS);/ Draw A QuadglColor3f(0.0f,1.0f,0.0f);/ Set The Color To GreenglVertex3f( 1.0f, 1.0f,-1.0f);/ Top Right Of The Quad (Top)glVertex3f(-1.0f, 1.0f,-1.0f);/ Top Left Of The Quad (Top)glVertex3f(-1.0f, 1.0f, 1.0f);/ Bottom Left Of The Quad (Top)glVertex3f( 1.0f, 1.0f,
14、 1.0f);/ Bottom Right Of The Quad (Top)glColor3f(1.0f,0.5f,0.0f);/ Set The Color To OrangeglVertex3f( 1.0f,-1.0f, 1.0f);/ Top Right Of The Quad (Bottom)glVertex3f(-1.0f,-1.0f, 1.0f);/ Top Left Of The Quad (Bottom)glVertex3f(-1.0f,-1.0f,-1.0f);/ Bottom Left Of The Quad (Bottom)glVertex3f( 1.0f,-1.0f,
15、-1.0f);/ Bottom Right Of The Quad (Bottom)glColor3f(1.0f,0.0f,0.0f);/ Set The Color To RedglVertex3f( 1.0f, 1.0f, 1.0f);/ Top Right Of The Quad (Front)glVertex3f(-1.0f, 1.0f, 1.0f);/ Top Left Of The Quad (Front)glVertex3f(-1.0f,-1.0f, 1.0f);/ Bottom Left Of The Quad (Front)glVertex3f( 1.0f,-1.0f, 1.
16、0f);/ Bottom Right Of The Quad (Front)glColor3f(1.0f,1.0f,0.0f);/ Set The Color To YellowglVertex3f( 1.0f,-1.0f,-1.0f);/ Top Right Of The Quad (Back)glVertex3f(-1.0f,-1.0f,-1.0f);/ Top Left Of The Quad (Back)glVertex3f(-1.0f, 1.0f,-1.0f);/ Bottom Left Of The Quad (Back)glVertex3f( 1.0f, 1.0f,-1.0f);
17、/ Bottom Right Of The Quad (Back)glColor3f(0.0f,0.0f,1.0f);/ Set The Color To BlueglVertex3f(-1.0f, 1.0f, 1.0f);/ Top Right Of The Quad (Left)glVertex3f(-1.0f, 1.0f,-1.0f);/ Top Left Of The Quad (Left)glVertex3f(-1.0f,-1.0f,-1.0f);/ Bottom Left Of The Quad (Left)glVertex3f(-1.0f,-1.0f, 1.0f);/ Botto
18、m Right Of The Quad (Left)glColor3f(1.0f,0.0f,1.0f);/ Set The Color To VioletglVertex3f( 1.0f, 1.0f,-1.0f);/ Top Right Of The Quad (Right)glVertex3f( 1.0f, 1.0f, 1.0f);/ Top Left Of The Quad (Right)glVertex3f( 1.0f,-1.0f, 1.0f);/ Bottom Left Of The Quad (Right)glVertex3f( 1.0f,-1.0f,-1.0f);/ Bottom
19、Right Of The Quad (Right)glEnd();/ Done Drawing The Quadrtri+=0.2f;/ Increase The Rotation Variable For The Triangle ( NEW )rquad-=0.15f;/ Decrease The Rotation Variable For The Quad ( NEW )return TRUE;/ Keep GoingGLvoid KillGLWindow(GLvoid)/ Properly Kill The Windowif (fullscreen)/ Are We In Fullsc
20、reen 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 DC And RC Contexts?MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOW
21、N 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_ICONINFORMATION);hRC=NULL;/ Set RC To NULLif (hDC && !ReleaseDC(hWnd,hDC)/ Are We Able To Rel
22、ease 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 Destroy The Window?MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR&q
23、uot;,MB_OK | MB_ICONINFORMATION);hWnd=NULL;/ Set hWnd To NULLif (!UnregisterClass("OpenGL",hInstance)/ Are We Able To Unregister ClassMessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);hInstance=NULL;/ Set hInstance To NULL/*This
24、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- Number Of Bits To Use For Color (8/16/24/32)* *fullscreenflag- Use Fullscreen Mode (TRUE)
25、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 StructureDWORDdwExStyle;/ Window Extended StyleDWORDdwStyle;/ Window StyleRECTWindowRect;/ Grab
26、s Rectangle Upper Left / Lower Right ValuesWindowRect.left=(long)0;/ Set Left Value To 0WindowRect.right=(long)width;/ Set Right Value To Requested WidthWindowRect.top=(long)0;/ Set Top Value To 0WindowRect.bottom=(long)height;/ Set Bottom Value To Requested Heightfullscreen=fullscreenflag;/ Set The
27、 Global Fullscreen FlaghInstance= GetModuleHandle(NULL);/ Grab An Instance For Our Windowwc.style= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;/ Redraw On Size, And Own DC For Window.wc.lpfnWndProc= (WNDPROC) WndProc;/ WndProc Handles Messageswc.cbClsExtra= 0;/ No Extra Window Datawc.cbWndExtra= 0;/ No Extra
28、 Window Datawc.hInstance= hInstance;/ Set The Instancewc.hIcon= LoadIcon(NULL, IDI_WINLOGO);/ Load The Default Iconwc.hCursor= LoadCursor(NULL, IDC_ARROW);/ Load The Arrow Pointerwc.hbrBackground= NULL;/ No Background Required For GLwc.lpszMenuName= NULL;/ We Don't Want A Menuwc.lpszClassName= &
29、quot;OpenGL"/ Set The Class Nameif (!RegisterClass(&wc)/ Attempt To Register The Window ClassMessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;/ Return FALSEif (fullscreen)/ Attempt Fullscreen Mode?DEVMODE dmScreenSetti
30、ngs;/ Device Modememset(&dmScreenSettings,0,sizeof(dmScreenSettings);/ Makes Sure Memory's CleareddmScreenSettings.dmSize=sizeof(dmScreenSettings);/ Size Of The Devmode StructuredmScreenSettings.dmPelsWidth= width;/ Selected Screen WidthdmScreenSettings.dmPelsHeight= height;/ Selected Screen
31、 HeightdmScreenSettings.dmBitsPerPel= 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 Of Start Bar.if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_S
32、UCCESSFUL)/ 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?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)=IDYES)fullscreen=FALSE;/ Windowed Mode Selected.
33、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 (fullscreen)/ Are We Still In Fullscreen Mode?dwExStyle=WS_EX_APPWINDOW;/ Window Extended Style
34、dwStyle=WS_POPUP;/ Windows StyleShowCursor(FALSE);/ Hide Mouse PointerelsedwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;/ Window Extended StyledwStyle=WS_OVERLAPPEDWINDOW;/ Windows StyleAdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);/ Adjust Window To True Requested Size/ Create The W
35、indowif (!(hWnd=CreateWindowEx(dwExStyle,/ Extended Style For The Window"OpenGL",/ Class Nametitle,/ Window TitledwStyle |/ Defined Window StyleWS_CLIPSIBLINGS |/ Required Window StyleWS_CLIPCHILDREN,/ Required Window Style0, 0,/ Window PositionWindowRect.right-WindowRect.left,/ Calculate
36、Window WidthWindowRect.bottom-WindowRect.top,/ Calculate Window HeightNULL,/ No Parent WindowNULL,/ No MenuhInstance,/ InstanceNULL)/ Dont Pass Anything To WM_CREATEKillGLWindow();/ Reset The DisplayMessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);return
37、 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 OpenGLPFD_DOUBLEBUFFER,/ Mu
38、st 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 Au
39、xiliary 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't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;/
40、Return FALSEif (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)/ Did Windows Find A Matching Pixel Format?KillGLWindow();/ Reset The DisplayMessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;/ Return FALSEif(!SetPixelFormat(hDC,P
41、ixelFormat,&pfd)/ Are We Able To Set The Pixel Format?KillGLWindow();/ Reset The DisplayMessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;/ Return FALSEif (!(hRC=wglCreateContext(hDC)/ Are We Able To Get A Rendering Context?KillGL
42、Window();/ Reset The DisplayMessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;/ Return FALSEif(!wglMakeCurrent(hDC,hRC)/ Try To Activate The Rendering ContextKillGLWindow();/ Reset The DisplayMessageBox(NULL,"Can't
43、Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;/ Return FALSEShowWindow(hWnd,SW_SHOW);/ Show The WindowSetForegroundWindow(hWnd);/ Slightly Higher PrioritySetFocus(hWnd);/ Sets Keyboard Focus To The WindowReSizeGLScene(width, height);/ Set Up Our Pe
44、rspective GL Screenif (!InitGL()/ Initialize Our Newly Created GL WindowKillGLWindow();/ Reset The DisplayMessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;/ Return FALSEreturn TRUE;/ SuccessLRESULT CALLBACK WndProc(HWNDhWnd,/ Handle For This
45、 WindowUINTuMsg,/ Message For This WindowWPARAMwParam,/ Additional Message InformationLPARAMlParam)/ Additional Message Informationswitch (uMsg)/ Check For Windows Messagescase WM_ACTIVATE:/ Watch For Window Activate Message/ LoWord Can Be WA_INACTIVE, WA_ACTIVE, WA_CLICKACTIVE,/ The High-Order Word
46、 Specifies The Minimized State Of The Window Being Activated Or Deactivated./ A NonZero Value Indicates The Window Is Minimized.if (LOWORD(wParam) != WA_INACTIVE) && !(BOOL)HIWORD(wParam)active=TRUE;/ Program Is Activeelseactive=FALSE;/ Program Is No Longer Activereturn 0;/ Return To The Mes
47、sage Loopcase WM_SYSCOMMAND:/ Intercept System Commandsswitch (wParam)/ Check System Callscase SC_SCREENSAVE:/ Screensaver Trying To Start?case SC_MONITORPOWER:/ Monitor Trying To Enter Powersave?return 0;/ Prevent From Happeningbreak;/ Exitcase WM_CLOSE:/ Did We Receive A Close Message?PostQuitMess
48、age(0);/ Send A Quit Messagereturn 0;/ Jump Backcase WM_KEYDOWN:/ Is A Key Being Held Down?keyswParam = TRUE;/ If So, Mark It As TRUEreturn 0;/ Jump Backcase WM_KEYUP:/ Has A Key Been Released?keyswParam = FALSE;/ If So, Mark It As FALSEreturn 0;/ Jump Backcase WM_SIZE:/ Resize The OpenGL WindowReSizeGLScene(LOWORD(lParam),HIWORD(lParam); / LoWord=Width, HiWord=Heightreturn 0;/ Jump Back/ Pass All Unhandled Messages To DefWindowProcreturn DefWindowProc(hWnd,uMsg,wParam,lParam);int WINAPI WinMain(HINSTANCEhInstance,/ InstanceHINS
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025房屋贈與合同范文模板
- 2025年度智能節(jié)能暖氣系統(tǒng)安裝與維護服務(wù)承包合同3篇
- 2024版云計算平臺運營服務(wù)合同
- 2025版企業(yè)實習(xí)生就業(yè)保障與職業(yè)培訓(xùn)協(xié)議6篇
- 二零二五年度二手房抵押貸款與智能家居系統(tǒng)集成合同3篇
- 2024版實驗室試劑耗材購置采購合同
- 2024年股權(quán)轉(zhuǎn)讓協(xié)議:科技公司股份交易
- 2024版分享69家政服務(wù)合同書
- 2024年銅精煉項目合作協(xié)議范本3篇
- 2024橋梁工程照明系統(tǒng)設(shè)計與施工承包合同樣本3篇
- 水泥行業(yè)數(shù)字化轉(zhuǎn)型服務(wù)方案
- 深圳市南山區(qū)2024-2025學(xué)年第一學(xué)期期末教學(xué)質(zhì)量檢測九年級物理 24-25上九年級物理
- 應(yīng)急設(shè)施設(shè)備和物資儲備管理制度(4篇)
- 團委書記個人工作總結(jié)
- 高危多發(fā)性骨髓瘤診斷與治療中國專家共識(2024年版)解讀
- 2024版房屋市政工程生產(chǎn)安全重大事故隱患判定標準內(nèi)容解讀
- 江蘇省鎮(zhèn)江市實驗學(xué)校2023-2024學(xué)年九年級上學(xué)期期末考試化學(xué)試卷
- 期末 (試題) -2024-2025學(xué)年人教PEP版(2024)英語三年級上冊
- GB/T 32066-2024煤基費托合成液體石蠟
- 食品工程原理課程設(shè)計攪拌器的設(shè)計
- 傷口傷口愈合以及影響愈合因素PPT文檔
評論
0/150
提交評論