opengl template(win32)
2011-09-26 13:31
148 查看
#pragma once #include "stdafx.h" #include "template.h" #include <gl/GL.h> #include <gl/GLU.h> #include <gl/glut.h> #pragma comment( lib, "opengl32.lib" ) #pragma comment( lib, "glu32.lib" ) #pragma comment( lib, "glaux.lib" ) GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window { if (height==0) // Prevent A Divide By Zero By { height=1; // Making Height Equal One } 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); 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 Screen And Depth Buffer glLoadIdentity(); // Reset The Current Modelview Matrix glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0 glBegin(GL_TRIANGLES); // Drawing Using Triangles glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); // Finished Drawing The Triangle glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); // Done Drawing The Quad return TRUE; // Keep Going } 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_SIZE: // Resize The OpenGL Window { ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0; case WM_PAINT: DrawGLScene(); //SwapBuffers(hDC); ValidateRect( hWnd, NULL ); return 0; } 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; }
相关文章推荐
- 在Win32下使用OpenGL
- 使用GLUT for win32 学习openGL
- WIN32 opengl绘制一个点
- 【译】TetroGL: An OpenGL Game Tutorial in C++ for Win32 Platforms - Part 1
- 比较简单的win32 OpenGL 程序
- [OpenGL] 2、企业版VC6.0自带的Win32-OpenGL工程浅析
- 在Win32中使用OpenGL
- Win32下使用OpenGL实现渲染
- OpenGL(Win32控制台应用程序框架)
- win32+opengl编程框架
- win32 opengl画线
- Nehe OpenGL教程第一课-创建一个OpenGL窗口(Win32)
- Win32 OpenGL编程(10) 视口变换
- OpenGL(Win32 窗体应用程序框架)
- coco2dx-2.2.2 win32启动过程(opengl 和 窗口大小初始化部分) - 学习笔记 1
- Win32 OpenGL编程系列 2D例子 -- 七巧板图形绘制
- OpenGL1-创建窗口(建立Win32的OpenGL开发环境 )(代码上传)
- 关于Nehe‘s OpenGL tutorial on Win32 Multisampling Application Creation的一些代码分析。
- Win32+OpenGL计算机图形学光照阴影算法(参照超级宝典4改编)
- WIN32 opengl缩放、旋转、移动图形