您的位置:首页 > 运维架构

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;
}


 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息