Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. #include <windows.h>
  2. #include <GL/gl.h>
  3.  
  4. #include <cmath>
  5.  
  6. int lineWidth = 1;
  7.  
  8. LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  9. {
  10. if (message == WM_CLOSE)
  11. {
  12. ExitProcess(0);
  13. }
  14. else if (message == WM_KEYDOWN)
  15. {
  16. if (wParam == VK_UP)
  17. {
  18. lineWidth++;
  19. }
  20. else if (wParam == VK_DOWN)
  21. {
  22. lineWidth--;
  23. if (lineWidth <= 0) lineWidth = 1;
  24. }
  25. }
  26.  
  27. return DefWindowProc(hWnd, message, wParam, lParam);
  28. }
  29.  
  30. int main()
  31. {
  32. WNDCLASSEX wndClass;
  33. ZeroMemory(&wndClass, sizeof wndClass);
  34. wndClass.cbSize = sizeof wndClass;
  35. wndClass.style = CS_OWNDC;
  36. wndClass.hInstance = GetModuleHandle(NULL);
  37. wndClass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  38. wndClass.lpszClassName = TEXT("SuperBasicGLWindowClass");
  39. wndClass.lpfnWndProc = MyWndProc;
  40. RegisterClassEx(&wndClass);
  41.  
  42. HWND hWnd = CreateWindowEx(
  43. 0, TEXT("SuperBasicGLWindowClass"),
  44. TEXT("SuperBasicGL"),
  45. WS_OVERLAPPEDWINDOW,
  46. 0, 0, 640, 480,
  47. 0, 0, GetModuleHandle(NULL),
  48. 0);
  49.  
  50. ShowWindow(hWnd, SW_SHOWNORMAL);
  51.  
  52. PIXELFORMATDESCRIPTOR pfd = {
  53. sizeof pfd,
  54. 1,
  55. PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
  56. PFD_TYPE_RGBA,
  57. 32,
  58. 0, 0, 0, 0, 0, 0,
  59. 0,
  60. 0,
  61. 0,
  62. 0, 0, 0, 0,
  63. 24,
  64. 8,
  65. 0,
  66. PFD_MAIN_PLANE,
  67. 0,
  68. 0, 0, 0
  69. };
  70.  
  71. HDC hDC = GetDC(hWnd);
  72.  
  73. int chosenPixelFormat = ChoosePixelFormat(hDC, &pfd);
  74. SetPixelFormat(hDC, chosenPixelFormat, &pfd);
  75.  
  76. HGLRC hGLRC = wglCreateContext(hDC);
  77.  
  78. wglMakeCurrent(hDC, hGLRC);
  79.  
  80. while (1)
  81. {
  82. MSG msg;
  83. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  84. {
  85. TranslateMessage(&msg);
  86. DispatchMessage(&msg);
  87. }
  88.  
  89. glClear(GL_COLOR_BUFFER_BIT);
  90.  
  91. static float x = 0;
  92. x++;
  93.  
  94. float translationX = std::sin(x / 30.0f) * 0.5f;
  95.  
  96. glLoadIdentity();
  97. glTranslatef(translationX, 0, 0);
  98.  
  99. glBegin(GL_TRIANGLES);
  100. glColor3ub(255, 0, 0);
  101. glVertex2f(0.0f, 0.0f);
  102. glVertex2f(0.0f,-1.0f);
  103. glVertex2f(-1.0f, -1.0f);
  104. glEnd();
  105.  
  106. static float rot = 0;
  107. rot++;
  108.  
  109. glRotatef(rot, 0, 0, 1);
  110.  
  111. glBegin(GL_TRIANGLES);
  112. glColor3ub(255, 255, 255);
  113. glVertex2f(0.0f, 0.0f);
  114. glVertex2f(0.5f, 0.0f);
  115. glVertex2f(0.5f, 0.5f);
  116. glEnd();
  117.  
  118. glTranslatef(0.5f, 0.5f, 0.0f);
  119. glRotatef(rot *2, 0, 0, 1);
  120.  
  121. glBegin(GL_TRIANGLES);
  122. glColor3ub(0, 255, 0);
  123. glVertex2f(0.0f, 0.0f);
  124. glVertex2f(1.0f, 0.0f);
  125. glVertex2f(1.0f, 1.0f);
  126. glEnd();
  127.  
  128. glLoadIdentity();
  129.  
  130. glPointSize(lineWidth);
  131. glBegin(GL_POINTS);
  132. int numPoints = 1000;
  133. for (int i = 0; i < numPoints; i++)
  134. {
  135. float p = (float) i / numPoints;
  136.  
  137. static float lineOffset = 0.0f;
  138. lineOffset++;
  139.  
  140. glColor3f(p, 1 - p, std::sin(p * lineOffset * 5 / 10000.0f) * 2);
  141. glVertex2f(-1.0f + p * 2.0f,
  142. std::sin(p * M_PI * 20) * 0.5f + std::sin(lineOffset / numPoints * M_PI * 2.01f) * 0.5f);
  143. }
  144. glEnd();
  145.  
  146. glEnable(GL_BLEND);
  147. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  148.  
  149. glBegin(GL_TRIANGLES);
  150. glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
  151. glVertex2f(0.0f, -0.5f);
  152. glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
  153. glVertex2f(0.5f, 0.5f);
  154. glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
  155. glVertex2f(-0.5f, 0.5f);
  156. glEnd();
  157.  
  158. glDisable(GL_BLEND);
  159.  
  160. SwapBuffers(hDC);
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement