1 // WindowsOpengl33.cpp : 定义应用程序的入口点。
2 //
3 #define WIN32_LEAN_AND_MEAN
4 #include "stdafx.h"
5 #include "WindowsOpengl33.h"
6 #include <Windows.h>
7 #include <gl/gl.h>
8 #include <gl/glu.h>
9 #include <gl/glaux.h>
10
11 float angle = 0.0f;
12 HDC g_HDC;
13 bool fullScreen = false;
14 //为设备环境设置像素格式的函数
15 void SetupPixelFormat(HDC hDC)
16 {
17 int nPixelFormat;
18 static PIXELFORMATDESCRIPTOR pfd = {
19 sizeof(PIXELFORMATDESCRIPTOR),
20 1,
21 PFD_DRAW_TO_WINDOW |
22 PFD_SUPPORT_OPENGL |
23 PFD_DOUBLEBUFFER,
24 PFD_TYPE_RGBA,
25 32,
26 0, 0, 0, 0, 0, 0,
27 0,
28 0,
29 0,
30 0, 0, 0, 0,
31 16,
32 0,
33 0,
34 PFD_MAIN_PLANE,
35 0,
36 0, 0, 0
37 };
38 nPixelFormat = ChoosePixelFormat(hDC, &pfd);
39 SetPixelFormat(hDC, nPixelFormat, &pfd);
40 }
41 // 此代码模块中包含的函数的前向声明:
42 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
43 int WINAPI WinMain(HINSTANCE hInstance,
44 HINSTANCE hPrevInstance,
45 LPTSTR lpCmdLine,
46 int nShowCmd)
47 {
48 WNDCLASSEX windowClass;
49 HWND hwnd;
50 MSG msg;
51 bool done;
52 DWORD dwExStyle;
53 DWORD dwStyle;
54 RECT windowRect;
55 int width = 800;
56 int height = 600;
57 int bits = 32;
58 windowRect.left = (long)0;
59 windowRect.right = (long)width;
60 windowRect.top = (long)0;
61 windowRect.bottom = (long)height;
62
63 windowClass.cbSize = sizeof(WNDCLASSEX);
64 windowClass.style = CS_HREDRAW | CS_VREDRAW;
65 windowClass.lpfnWndProc = WndProc;
66 windowClass.cbClsExtra = 0;
67 windowClass.cbWndExtra = 0;
68 windowClass.hInstance = hInstance;
69 windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
70 windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
71 windowClass.hbrBackground = NULL;
72 windowClass.lpszMenuName = NULL;
73 windowClass.lpszClassName = "MyClass";
74 windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
75 if(!RegisterClassEx(&windowClass))
76 return 0;
77 if(fullScreen)
78 {
79 DEVMODE dmScreenSettings;
80 memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
81 dmScreenSettings.dmSize = sizeof(dmScreenSettings);
82 dmScreenSettings.dmPelsWidth = width;
83 dmScreenSettings.dmPelsHeight = height;
84 dmScreenSettings.dmBitsPerPel = bits;
85 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
86 if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) !=
87 DISP_CHANGE_SUCCESSFUL)
88 {
89 MessageBox(NULL, "Display mode failed", NULL, MB_OK);
90 fullScreen = false;
91 }
92 }
93 if(fullScreen)
94 {
95 dwExStyle = WS_EX_APPWINDOW;
96 dwStyle = WS_POPUP;
97 ShowCursor(false);
98 }
99 else
100 {
101 dwExStyle = NULL;
102 dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
103 }
104 AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);
105 hwnd = CreateWindowEx(NULL,
106 "MyClass",
107 "The OpenGl Window Application",
108 WS_OVERLAPPEDWINDOW | WS_VISIBLE |
109 WS_SYSMENU | WS_CLIPCHILDREN |
110 WS_CLIPSIBLINGS,
111 0, 0,
112 windowRect.right - windowRect.left,
113 windowRect.bottom - windowRect.top,
114 NULL,
115 NULL,
116 hInstance,
117 NULL);
118 if(!hwnd)
119 return 0;
120 ShowWindow(hwnd, SW_SHOW);
121 UpdateWindow(hwnd);
122 done = false;
123 while(!done)
124 {
125 PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
126 if(msg.message == WM_QUIT)
127 {
128 done = true;
129 }
130 else //..Draw//
131 //1.创建RC、关联RC与DC.WM_CREATE 中进行设置
132 //2.做具体的绘图工作 myDrawScene().以下将进行
133 //3.SwapBuffers(m_pDC->m_hDC) 把RC绘制的传到当前的DC上以下将进行
134 //4.释放RC,以便其他的DC使用..WM_CLOSE 中进行设置
135 {
136 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
137 glLoadIdentity();
138 angle += 0.1f;
139 if(angle >= 360.0f)
140 angle = 0.0f;
141 glTranslatef(0.0f, 0.0f, -5.0f);
142 glRotatef(angle, 0.0f, 0.0f, 1.0f);
143 glColor3f(1.0f, 1.0f, 0.0f);
144 glBegin(GL_TRIANGLES);
145 glVertex3f(0.0f, 0.0f, 0.0f);
146 glVertex3f(1.0f, 0.0f, 0.0f);
147 glVertex3f(1.0f, 1.0f, 0.0f);
148 glEnd();
149 SwapBuffers(g_HDC);
150 TranslateMessage(&msg);
151 DispatchMessage(&msg);
152 }
153 }
154 if(fullScreen)
155 {
156 ChangeDisplaySettings(NULL, 0);
157 ShowCursor(true);
158 }
159 return msg.wParam;
160 }
161
162 // 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
163 //
164 // 目的: 处理主窗口的消息。
165 //
166 // WM_COMMAND - 处理应用程序菜单
167 // WM_PAINT - 绘制主窗口
168 // WM_DESTROY - 发送退出消息并返回
169 //
170 //
171 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
172 {
173 static HGLRC hRC;
174 static HDC hDC;
175 char string[] = "hello world!";
176 int width, height;
177 switch (message)
178 {
179 case WM_CREATE:
180 hDC = GetDC(hWnd);
181 g_HDC = hDC;
182 SetupPixelFormat(hDC);
183 hRC = wglCreateContext(hDC); //创建RC
184 wglMakeCurrent(hDC, hRC); //关联RC
185 return 0;
186 break;
187 case WM_CLOSE:
188 wglMakeCurrent(hDC, NULL); //释放与hDC对应的RC
189 wglDeleteContext(hRC); //删除RC
190 PostQuitMessage(0);
191 return 0;
192 break;
193 case WM_SIZE:
194 height = HIWORD(lParam);
195 width = LOWORD(lParam);
196 if(height == 0)
197 height = 1;
198 glViewport(0, 0, width, height);
199 glMatrixMode(GL_PROJECTION);
200 glLoadIdentity();
201 //计算窗口尺寸比例
202 gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f ,1000.0f);
203 glMatrixMode(GL_MODELVIEW);
204 glLoadIdentity();
205 return 0;
206 break;
207 default:
208 break;
209 }
210 return(DefWindowProc(hWnd, message, wParam, lParam));
211 }
212
posted on 2010-12-26 22:23
xiwrong 阅读(257)
评论(0) 编辑 收藏 引用