好了,现在整篇代码应该是这个样子了:
// GlTest.cpp : 定义应用程序的入口点。 //
#include "stdafx.h" #include "GlTest.h" #define MAX_LOADSTRING 100
// 全局变量: HINSTANCE hInst; // 当前实例 HWND g_hWnd; HDC g_hDC; HGLRC g_glRes;
short nSrcBox[ 3 * 8 ] = { 5, 5, 0, 5, 5, 10, 5, -5, 0, 5, -5, 10, -5, -5, 0, -5, -5, 10, -5, 5, 0, -5, 5, 10, };
BYTE byIndex[36] ={ 0, 4, 6, 0, 2, 4, 0, 6, 7, 0, 7, 1, 0, 3, 2, 0, 1, 3, 5, 2, 3, 5, 4, 2, 5, 6, 4, 5, 7, 6, 5, 1, 7, 5, 3, 1, };
short nTempBox[ 36 * 3 ]; float fNormal[ 36 * 3 ];
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
template <typename Type> HRESULT ReduceToUnit( Type *pVector ) { double dLength = sqrt( (double)pVector[0] * (double)pVector[0] + (double)pVector[1] * (double)pVector[1] + (double)pVector[2] * (double)pVector[2] );
if ( FLOATEQUAL( dLength, 0.0, 1e-8 ) ) { dLength = 1.0; } pVector[0] /= (Type)dLength; pVector[1] /= (Type)dLength; pVector[2] /= (Type)dLength; return S_OK; }
template <typename T1, typename T2> HRESULT CalcNormal( const T1 *pVertical, T2 *pNormal ) { T1 d1[3], d2[3]; d1[0] = pVertical[0] - pVertical[3]; d1[1] = pVertical[1] - pVertical[4]; d1[2] = pVertical[2] - pVertical[5]; d2[0] = pVertical[3] - pVertical[6]; d2[1] = pVertical[4] - pVertical[7]; d2[2] = pVertical[5] - pVertical[8]; pNormal[0] = (T2)( d1[1] * d2[2] - d1[2] * d2[1] ); pNormal[1] = (T2)( d1[2] * d2[0] - d1[0] * d2[2] ); pNormal[2] = (T2)( d1[0] * d2[1] - d1[1] * d2[0] ); return ReduceToUnit( pNormal ); }
// 此代码模块中包含的函数的前向声明: void OnCreate( HWND hWnd ); void OnCreated( void ); void OnDestroy( void ); void OnDraw( void ); void SetProjMatrix( WORD wWidth, WORD wHeight ); void SetModalMatrix( void ); void OnIdle( void );
ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // TODO: 在此放置代码。 MSG msg; HACCEL hAccelTable;
// 初始化全局字符串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_GLTEST, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance);
// 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; }
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_GLTEST);
// 主消息循环: while ( true ) { if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } continue; } if ( WM_QUIT == msg.message ) { break; } OnIdle(); }
return (int) msg.wParam; }
ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = 0; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GLTEST); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCTSTR)IDC_GLTEST; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex); }
// // 函数:InitInstance(HANDLE, int) // // 目的:保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 将实例句柄存储在全局变量中
CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if ( !g_hWnd ) { return FALSE; } OnCreated();
ShowWindow( g_hWnd, nCmdShow ); UpdateWindow( g_hWnd);
return TRUE; }
// // 函数:WndProc(HWND, unsigned, WORD, LONG) // // 目的:处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; switch (message) { case WM_CREATE: OnCreate( hWnd ); break; case WM_KEYDOWN: g_bStartSwap = !g_bStartSwap; break; case WM_MOVE: SetWindowText( g_hWnd, "Move" ); break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 分析菜单选择: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_SIZE: SetProjMatrix( LOWORD( lParam ), HIWORD( lParam ) ); break; case WM_DESTROY: OnDestroy(); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
// “关于”框的消息处理程序。 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE;
case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; }
void OnCreate( HWND hWnd ) { g_hWnd = hWnd; }
void OnDestroy( void ) { ReleaseDC( g_hWnd, g_hDC ); wglDeleteContext( g_glRes ); }
void OnCreated( void ) { g_hDC = GetDC( g_hWnd );
PIXELFORMATDESCRIPTOR pfd; ZeroMemory( &pfd, sizeof(PIXELFORMATDESCRIPTOR) ); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR ); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 32;
SetPixelFormat( g_hDC, ChoosePixelFormat( g_hDC, &pfd ), &pfd );
g_glRes = wglCreateContext( g_hDC ); wglMakeCurrent( g_hDC, g_glRes );
glEnable( GL_CULL_FACE ); glCullFace( GL_BACK );
glEnable( GL_DEPTH_TEST ); glDepthFunc( GL_LEQUAL );
int LightPos[] = { 50, 50, 10, 1 }; float LightColor[] = { 0.3f, 0.3f, 0.3f, 1.0f };
glEnable(GL_LIGHTING); glLightiv( GL_LIGHT0, GL_POSITION, LightPos ); glLightfv( GL_LIGHT0, GL_AMBIENT, LightColor ); glLightfv( GL_LIGHT0, GL_DIFFUSE, LightColor ); glEnable( GL_LIGHT0 ); glEnable( GL_COLOR_MATERIAL ); glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
glShadeModel( GL_SMOOTH );
glClearColor( 255.0f / 255.0f, 255.0f / 255.0f, 200.0f / 255.0f, 0.0 ); for ( int i = 0; i < 36; i++ ) { MoveMemory( &nTempBox[ i * 3 ], &nSrcBox[ byIndex[i] * 3 ], sizeof(nTempBox[0]) * 3 ); } for ( int i = 0; i < 12; i++ ) { CalcNormal( &nTempBox[ i * 9 ], &fNormal[ i * 9 ] ); MoveMemory( &fNormal[ i * 9 + 3 ], &fNormal[ i * 9 ], sizeof(fNormal[0]) * 3 ); MoveMemory( &fNormal[ i * 9 + 6 ], &fNormal[ i * 9 ], sizeof(fNormal[0]) * 3 ); } glEnableClientState( GL_VERTEX_ARRAY ); glVertexPointer( 3, GL_SHORT, 0, nTempBox ); glEnableClientState( GL_NORMAL_ARRAY ); glNormalPointer( GL_FLOAT, 0, fNormal );
glColor3ub( 140, 200, 255 ); }
void OnDraw( void ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glDrawArrays( GL_TRIANGLES, 0, 36 ); SwapBuffers( g_hDC ); }
void SetModalMatrix( void ) { glMatrixMode( GL_MODELVIEW ); glLoadIdentity( ); static float fRadius = 0; fRadius += 0.01f; if ( fRadius > M_PI * 2 ) { fRadius = 0; } gluLookAt( cosf( fRadius ) * 30, sinf( fRadius ) * 30, 15.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 ); }
void SetProjMatrix( WORD wWidth, WORD wHeight ) { glViewport( 0, 0, wWidth, wHeight ); glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); gluPerspective( 45.0, (double)wWidth / (double)wHeight, 1.0, 1000.0 ); }
void OnIdle( void ) { SetModalMatrix(); OnDraw(); } |
现在已经达到我们想要的效果了。
|