#pragma once #include "stdafx.h" #include "template.h" float angle = 0.0; float red=1.0, blue=1.0, green=1.0; GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window { glViewport(0,0,width,height); // Reset The Current Viewport glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix // Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); //gluOrtho2D(0.0, width, 0.0,height);// to be explained glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix } int InitGL(GLvoid) // All Setup For OpenGL Goes Here { glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f, 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 Perspective Calculations return TRUE; // Initialization Went OK } int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The View glTranslatef(0.0f,0.0f,-5.0f); glRotatef(angle,0.0,1.0,0.0); glColor3f(red,green,blue); glBegin(GL_TRIANGLES); glVertex3f(-0.5,-0.5,0.0); glVertex3f(0.5,0.0,0.0); glVertex3f(0.0,0.5,0.0); glEnd(); angle++; return TRUE; } VOID Cleanup() { } //----------------------------------------------------------------------------- // Desc: 消息处理 //----------------------------------------------------------------------------- LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { static POINT ptLastMousePosit; static POINT ptCurrentMousePosit; static bool bMousing; switch( msg ) { case WM_COMMAND: int wmId, wmEvent; wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case ID_COLOR_RED: red = 1.0; green = 0.0; blue = 0.0; break; case ID_COLOR_BLUE: red = 0.0; green = 0.0; blue = 1.0; break; case ID_COLOR_GREEN: red = 0.0; green = 1.0; blue = 0.0; break; case ID_COLOR_WHITE: red = 1.0; green = 1.0; blue = 1.0; break; default: return DefWindowProc(hWnd, msg, wParam, lParam); } break; case WM_SIZE: // Resize The OpenGL Window { ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height break; // Jump Back } case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); break; case WM_PAINT: DrawGLScene(); ValidateRect( hWnd, NULL ); break; case WM_RBUTTONDOWN: { POINT point; point.x=LOWORD(lParam); point.y=HIWORD(lParam); ClientToScreen(hWnd,&point); HINSTANCE hInstance=GetModuleHandle(NULL); HMENU hMenu = LoadMenu (hInstance, MAKEINTRESOURCE (IDR_COLOR)) ; HMENU hMenuTrackPopup = GetSubMenu(hMenu,0); TrackPopupMenu(hMenuTrackPopup,TPM_RIGHTBUTTON,point.x,point.y,0,hWnd,NULL); } break; return DefWindowProc( hWnd, msg, wParam, lParam ); } //----------------------------------------------------------------------------- // Desc: 程序入口 //----------------------------------------------------------------------------- int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { WNDCLASSEX winClass; MSG uMsg; memset(&uMsg,0,sizeof(uMsg)); winClass.lpszClassName = L"MY_WINDOWS_CLASS"; winClass.cbSize = sizeof(WNDCLASSEX); winClass.style = CS_HREDRAW | CS_VREDRAW; winClass.lpfnWndProc = MsgProc; winClass.hInstance = hInstance; winClass.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON); winClass.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON); winClass.hCursor = LoadCursor(NULL, IDC_ARROW); winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); winClass.lpszMenuName = NULL; winClass.cbClsExtra = 0; winClass.cbWndExtra = 0; if( !RegisterClassEx(&winClass) ) return E_FAIL; HWND hWnd = CreateWindowEx( NULL, L"MY_WINDOWS_CLASS", L"OpenGL - Initialization", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT , CW_USEDEFAULT , NULL, NULL, hInstance, NULL ); SetWindowPos(hWnd,NULL,0,0,640,480,SWP_NOMOVE); HDC hDC=GetDC(hWnd); static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 16, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 16, // 16Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; GLuint PixelFormat; // Holds The Results After Searching For A Match PixelFormat=ChoosePixelFormat(hDC,&pfd); SetPixelFormat(hDC,PixelFormat,&pfd); HGLRC hRC=wglCreateContext(hDC); wglMakeCurrent(hDC,hRC); RECT clientrc; GetClientRect(hWnd,&clientrc); ReSizeGLScene(clientrc.right-clientrc.left, clientrc.bottom-clientrc.top); //初始化Direct3D if(SUCCEEDED(InitGL())) { //if( SUCCEEDED(InitGeometry())) { //显示主窗口 ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); //进入消息循环 MSG msg; ZeroMemory( &msg, sizeof(msg) ); while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { DrawGLScene(); // Draw The Scene SwapBuffers(hDC); } } } } UnregisterClass( L"ClassName", hInstance); return 0; }