Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. // opengl_context.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "opengl_context.h"
  6. #include "GLExtensions.h"
  7.  
  8. #include <windows.h> /* must include this before GL/gl.h */
  9. #include <GL/gl.h> /* OpenGL header file */
  10. #include <GL/glu.h> /* OpenGL utilities header file */
  11. #include <stdio.h>
  12. #include <assert.h>
  13.  
  14. #define POLL 1
  15. #define ENABLE_VBO 1
  16. #define RENDER_ITERATIONS 0xf
  17.  
  18. static const GLfloat vertices[] = {
  19. // front
  20. -1.0f, -1.0f, +1.0f,
  21. +1.0f, -1.0f, +1.0f,
  22. -1.0f, +1.0f, +1.0f,
  23. +1.0f, +1.0f, +1.0f,
  24. // back
  25. +1.0f, -1.0f, -1.0f,
  26. -1.0f, -1.0f, -1.0f,
  27. +1.0f, +1.0f, -1.0f,
  28. -1.0f, +1.0f, -1.0f,
  29. // right
  30. +1.0f, -1.0f, +1.0f,
  31. +1.0f, -1.0f, -1.0f,
  32. +1.0f, +1.0f, +1.0f,
  33. +1.0f, +1.0f, -1.0f,
  34. // left
  35. -1.0f, -1.0f, -1.0f,
  36. -1.0f, -1.0f, +1.0f,
  37. -1.0f, +1.0f, -1.0f,
  38. -1.0f, +1.0f, +1.0f,
  39. // top
  40. -1.0f, +1.0f, +1.0f,
  41. +1.0f, +1.0f, +1.0f,
  42. -1.0f, +1.0f, -1.0f,
  43. +1.0f, +1.0f, -1.0f,
  44. // bottom
  45. -1.0f, -1.0f, -1.0f,
  46. +1.0f, -1.0f, -1.0f,
  47. -1.0f, -1.0f, +1.0f,
  48. +1.0f, -1.0f, +1.0f,
  49. };
  50.  
  51. static const GLfloat colors[] = {
  52. // front
  53. 0.0f, 0.0f, 1.0f, // blue
  54. 1.0f, 0.0f, 1.0f, // magenta
  55. 0.0f, 1.0f, 1.0f, // cyan
  56. 1.0f, 1.0f, 1.0f, // white
  57. // back
  58. 1.0f, 0.0f, 0.0f, // red
  59. 0.0f, 0.0f, 0.0f, // black
  60. 1.0f, 1.0f, 0.0f, // yellow
  61. 0.0f, 1.0f, 0.0f, // green
  62. // right
  63. 1.0f, 0.0f, 1.0f, // magenta
  64. 1.0f, 0.0f, 0.0f, // red
  65. 1.0f, 1.0f, 1.0f, // white
  66. 1.0f, 1.0f, 0.0f, // yellow
  67. // left
  68. 0.0f, 0.0f, 0.0f, // black
  69. 0.0f, 0.0f, 1.0f, // blue
  70. 0.0f, 1.0f, 0.0f, // green
  71. 0.0f, 1.0f, 1.0f, // cyan
  72. // top
  73. 0.0f, 1.0f, 1.0f, // cyan
  74. 1.0f, 1.0f, 1.0f, // white
  75. 0.0f, 1.0f, 0.0f, // green
  76. 1.0f, 1.0f, 0.0f, // yellow
  77. // bottom
  78. 0.0f, 0.0f, 0.0f, // black
  79. 1.0f, 0.0f, 0.0f, // red
  80. 0.0f, 0.0f, 1.0f, // blue
  81. 1.0f, 0.0f, 1.0f // magenta
  82. };
  83.  
  84. static const GLfloat normals[] = {
  85. // front
  86. +0.0f, +0.0f, +1.0f, // forward
  87. +0.0f, +0.0f, +1.0f, // forward
  88. +0.0f, +0.0f, +1.0f, // forward
  89. +0.0f, +0.0f, +1.0f, // forward
  90. // back
  91. +0.0f, +0.0f, -1.0f, // backward
  92. +0.0f, +0.0f, -1.0f, // backward
  93. +0.0f, +0.0f, -1.0f, // backward
  94. +0.0f, +0.0f, -1.0f, // backward
  95. // right
  96. +1.0f, +0.0f, +0.0f, // right
  97. +1.0f, +0.0f, +0.0f, // right
  98. +1.0f, +0.0f, +0.0f, // right
  99. +1.0f, +0.0f, +0.0f, // right
  100. // left
  101. -1.0f, +0.0f, +0.0f, // left
  102. -1.0f, +0.0f, +0.0f, // left
  103. -1.0f, +0.0f, +0.0f, // left
  104. -1.0f, +0.0f, +0.0f, // left
  105. // top
  106. +0.0f, +1.0f, +0.0f, // up
  107. +0.0f, +1.0f, +0.0f, // up
  108. +0.0f, +1.0f, +0.0f, // up
  109. +0.0f, +1.0f, +0.0f, // up
  110. // bottom
  111. +0.0f, -1.0f, +0.0f, // down
  112. +0.0f, -1.0f, +0.0f, // down
  113. +0.0f, -1.0f, +0.0f, // down
  114. +0.0f, -1.0f, +0.0f // down
  115. };
  116.  
  117. void
  118. display()
  119. {
  120. /* rotate a triangle around */
  121. glClearColor(0.0, 0.5, 0.5, 1.0);
  122. glClear(GL_COLOR_BUFFER_BIT);
  123.  
  124. #if ENABLE_VBO
  125. glDrawArrays(GL_TRIANGLES, 0, 3);
  126. /*
  127. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  128. glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
  129. glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
  130. glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);
  131. glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
  132. glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
  133. */
  134. #endif
  135.  
  136. glFlush();
  137. }
  138.  
  139.  
  140. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  141. {
  142. static int pass = 0;
  143. static PAINTSTRUCT ps;
  144.  
  145. UNREFERENCED_PARAMETER(pass);
  146.  
  147. switch (uMsg) {
  148. case WM_PAINT:
  149. display();
  150. BeginPaint(hWnd, &ps);
  151. EndPaint(hWnd, &ps);
  152.  
  153. return 0;
  154.  
  155. case WM_SIZE:
  156. glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
  157. PostMessage(hWnd, WM_PAINT, 0, 0);
  158. return 0;
  159.  
  160. case WM_CHAR:
  161. switch (wParam) {
  162. case 27: /* ESC key */
  163. PostQuitMessage(0);
  164. break;
  165. }
  166. return 0;
  167.  
  168. case WM_CLOSE:
  169. PostQuitMessage(0);
  170. return 0;
  171. }
  172.  
  173. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  174. }
  175.  
  176. HWND
  177. CreateOpenGLWindow(char* title, int x, int y, int width, int height,
  178. BYTE type, DWORD flags)
  179. {
  180. int pf;
  181. HDC hDC;
  182. HWND hWnd;
  183. WNDCLASS wc;
  184. PIXELFORMATDESCRIPTOR pfd;
  185. static HINSTANCE hInstance = 0;
  186. const LPCWSTR err1 = L"RegisterClass() failed : Cannot register window class.";
  187. const LPCWSTR err2 = L"CreateWindow() failed : Cannot create a window.";
  188.  
  189. /* only register the window class once - use hInstance as a flag. */
  190. if (!hInstance) {
  191. hInstance = GetModuleHandle(NULL);
  192. wc.style = CS_OWNDC;
  193. wc.lpfnWndProc = (WNDPROC)WndProc;
  194. wc.cbClsExtra = 0;
  195. wc.cbWndExtra = 0;
  196. wc.hInstance = hInstance;
  197. wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  198. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  199. wc.hbrBackground = NULL;
  200. wc.lpszMenuName = NULL;
  201. wc.lpszClassName = L"OpenGL";
  202.  
  203. if (!RegisterClass(&wc)) {
  204. MessageBox(NULL, err1, L"Error", MB_OK);
  205. return NULL;
  206. }
  207. }
  208.  
  209. UNREFERENCED_PARAMETER(title);
  210. hWnd = CreateWindow(L"OpenGL", L"Window Title", WS_OVERLAPPEDWINDOW |
  211. WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  212. x, y, width, height, NULL, NULL, hInstance, NULL);
  213.  
  214. if (hWnd == NULL) {
  215. MessageBox(NULL, err2, L"Error", MB_OK);
  216. return NULL;
  217. }
  218.  
  219. hDC = GetDC(hWnd);
  220.  
  221. /* there is no guarantee that the contents of the stack that become
  222. the pfd are zeroed, therefore _make sure_ to clear these bits. */
  223. memset(&pfd, 0, sizeof(pfd));
  224. pfd.nSize = sizeof(pfd);
  225. pfd.nVersion = 1;
  226. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | flags;
  227. pfd.iPixelType = type;
  228. pfd.cColorBits = 32;
  229.  
  230. pf = ChoosePixelFormat(hDC, &pfd);
  231.  
  232. if (pf == 0) {
  233. MessageBox(NULL, L"ChoosePixelFormat() failed: "
  234. "Cannot find a suitable pixel format.", L"Error", MB_OK);
  235. return 0;
  236. }
  237.  
  238. if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
  239. MessageBox(NULL, L"SetPixelFormat() failed: "
  240. "Cannot set format specified.", L"Error", MB_OK);
  241. return 0;
  242. }
  243.  
  244. DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  245.  
  246. ReleaseDC(hWnd, hDC);
  247.  
  248. return hWnd;
  249. }
  250.  
  251.  
  252. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  253. _In_opt_ HINSTANCE hPrevInstance,
  254. _In_ LPWSTR lpCmdLine,
  255. _In_ int nCmdShow)
  256. {
  257. HDC hDC; /* device context */
  258. HGLRC hRC; /* opengl context */
  259. HWND hWnd; /* window */
  260. MSG msg; /* message */
  261.  
  262. hWnd = CreateOpenGLWindow("minimal", 0, 0, 256, 256, PFD_TYPE_RGBA, 0);
  263. if (hWnd == NULL)
  264. exit(1);
  265.  
  266. hDC = GetDC(hWnd);
  267. assert(hDC && "hDC context is null");
  268.  
  269. hRC = wglCreateContext(hDC);
  270. wglMakeCurrent(hDC, hRC);
  271. glEnable(GL_CULL_FACE);
  272.  
  273. static const GLfloat g_vertex_buffer_data[] = {
  274. -1.0f, -1.0f, 0.0f,
  275. 1.0f, -1.0f, 0.0f,
  276. 0.0f, 1.0f, 0.0f,
  277. };
  278.  
  279. #if ENABLE_VBO
  280. GLuint vertexbuffer;
  281. // Generate 1 buffer, put the resulting identifier in vertexbuffer
  282. glGenBuffers(1, &vertexbuffer);
  283. // The following commands will talk about our 'vertexbuffer' buffer
  284. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  285. // Give our vertices to OpenGL.
  286. glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
  287.  
  288. glEnableVertexAttribArray(0);
  289. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  290. glVertexAttribPointer(
  291. 0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
  292. 3, // size
  293. GL_FLOAT, // type
  294. GL_FALSE, // normalized?
  295. 0, // stride
  296. (void*)0 // array buffer offset
  297. );
  298. #endif
  299.  
  300. ShowWindow(hWnd, nCmdShow);
  301.  
  302. #if POLL
  303. UNREFERENCED_PARAMETER(msg);
  304. for (UINT32 i = 0; i < RENDER_ITERATIONS; i++)
  305. display();
  306. #else
  307. while (GetMessage(&msg, hWnd, 0, 0)) {
  308. TranslateMessage(&msg);
  309. DispatchMessage(&msg);
  310. }
  311. #endif
  312.  
  313. wglMakeCurrent(NULL, NULL);
  314. ReleaseDC(hWnd, hDC);
  315. wglDeleteContext(hRC);
  316. DestroyWindow(hWnd);
  317.  
  318. return static_cast<int>(0);
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement