#include "Application.h"
HDC Application::hDC;
int Application::m_WinWidth = 640;
int Application::m_WinHeigh = 480;
HGLRC Application::hGLRC;
const UINT_PTR Application::TIMER_SEC = 1;
const UINT_PTR Application::TIMER_MILLISEC = 2;
Camera Application::camera;
Application::Application()
{
hWnd = NULL;
hDC = NULL;
hGLRC = NULL;
ZeroMemory(&m_WinClassEx,sizeof(m_WinClassEx));
ZeroMemory(&m_Msg,sizeof(m_Msg));
}
Application::~Application()
{
UnregisterClass(m_WinClassEx.lpszClassName,m_WinClassEx.hInstance);
}
HWND Application::InitWindow()
{
m_WinClassEx.cbSize = sizeof(WNDCLASSEX);
m_WinClassEx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
m_WinClassEx.lpfnWndProc = (WNDPROC)MsgProc;
m_WinClassEx.cbClsExtra = 0;
m_WinClassEx.cbWndExtra = 0;
m_WinClassEx.hInstance = GetModuleHandle(NULL);
m_WinClassEx.hIcon = NULL;
m_WinClassEx.hCursor = NULL;
m_WinClassEx.hbrBackground = NULL;
m_WinClassEx.lpszMenuName = NULL;
m_WinClassEx.lpszClassName = TEXT("Application");
m_WinClassEx.hIconSm = NULL;
RegisterClassEx(&m_WinClassEx);
//创建窗口
HWND hWnd = CreateWindowEx(NULL,
TEXT("Application"),
TEXT("Demo"),
WS_OVERLAPPEDWINDOW,
100,100,
m_WinWidth,m_WinHeigh,
NULL,NULL,
m_WinClassEx.hInstance,
NULL);
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
return hWnd;
}
LRESULT CALLBACK Application::MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
hDC = GetDC(hWnd);
Application::SetupPixelFormat();
hGLRC = wglCreateContext(hDC);
wglMakeCurrent(hDC,hGLRC);
Application::Init(hWnd);
break;
case WM_DESTROY:
wglMakeCurrent(hDC,NULL);
wglDeleteContext(hGLRC);
ReleaseDC(hWnd,hDC);
PostQuitMessage(0);
break;
case WM_SIZE:
m_WinWidth = LOWORD(lParam);
m_WinHeigh = HIWORD(lParam);
Application::SetProjection();
break;
case WM_PAINT:
Application::Draw();
break;
case WM_TIMER:
switch(wParam)
{
case TIMER_MILLISEC:
camera.OperationProc();
Application::SetProjection();
InvalidateRect(hWnd,NULL,false);
break;
case TIMER_SEC:
break;
}
break;
default:
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
//为环境设备设置像素格式
void Application::SetupPixelFormat()
{
PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cRedBits = 0;
pfd.cRedShift = 0;
pfd.cGreenBits = 0;
pfd.cGreenShift = 0;
pfd.cBlueBits = 0;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 32;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0;
int pixelFormat = ChoosePixelFormat(hDC,&pfd);
SetPixelFormat(hDC,pixelFormat,&pfd);
}
void Application::SetProjection()
{
if(0 == m_WinHeigh)
{
m_WinHeigh = 1;
}
double ratio = 1.0 * m_WinWidth/m_WinHeigh;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,ratio,0.01,100);
glViewport(0, 0,m_WinWidth,m_WinHeigh);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera.camera_x,camera.camera_y,camera.camera_z,
camera.lookat_x,camera.lookat_y,camera.lookat_z,
0.0f,1.0f,0.0f);
}
Application* Application::Instance()
{
static Application ins;
return &ins;
}
int Application::Run()
{
if(!InitWindow())
return 0;
Application::SetProjection();
while(GetMessage(&m_Msg,NULL,0,0))
{
TranslateMessage(&m_Msg);
DispatchMessage(&m_Msg);
}
return 0;
}
void Application::InitTimer(HWND hWnd)
{
SetTimer(hWnd,TIMER_SEC,1000,NULL);
SetTimer(hWnd,TIMER_MILLISEC,1,NULL);
}
void Application::Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glutSolidTeapot(1);
glFlush();
SwapBuffers(hDC);
}
void Application::SetLight()
{
GLfloat ambient[] = {1.0,1.0,1.0,1.0};
GLfloat diffuse[] = {1.0,1.0,1.0,1.0};
GLfloat position[] = {10.0,10.0,0.0,1.0};
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
glLightfv(GL_LIGHT0,GL_POSITION,position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}
void Application::Init(HWND hWnd)
{
Application::SetProjection();
Application::InitTimer(hWnd);
Application::SetLight();
}
opengl的一个摄像机类
4星 · 超过85%的资源 需积分: 12 172 浏览量
2010-08-12
22:54:44
上传
评论 2
收藏 2.2MB RAR 举报
hacson
- 粉丝: 5
- 资源: 11
最新资源
- 基于ThinkPHP的轻量级PHP开发框架设计源码
- 基于CSS的响应式鲜花网站全屏效果设计源码
- 基于JavaScript的访客预约系统设计源码
- 基于Vue和ECharts的工作租房数据可视化系统设计源码
- 1040g0cg310ravpiu6ibg5pg00tsipsln3ju2d0g 2
- 基于Python的SAR图像去噪CNN-NLM设计源码
- redhat6升级到redhat7,过程redhat6.x-> redhat6.10->rehat7.9 主版本最高版本
- 基于Django的流程引擎设计源码
- 基于Node.js的Express框架与MySQL的后台管理系统设计源码
- 基于Java的Flink流批一体数据处理快速集成开发框架设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页