Guest User

Untitled

a guest
Jul 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. // Headers
  2. #include <windows.h>
  3. #include "GLee.h"
  4.  
  5. // Globals
  6. bool g_WindowOpen = true;
  7.  
  8. // Window event handler
  9. LRESULT CALLBACK windowEvent( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  10. {
  11. if ( msg == WM_DESTROY ) g_WindowOpen = false;
  12.  
  13. return DefWindowProc( hWnd, msg, wParam, lParam );
  14. }
  15.  
  16. // Application entry point
  17. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  18. {
  19. // Create the window
  20. WNDCLASSEX windowClass;
  21. ZeroMemory( &windowClass, sizeof( windowClass ) );
  22. windowClass.cbSize = sizeof( WNDCLASSEX );
  23. windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  24. windowClass.lpfnWndProc = windowEvent;
  25. windowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  26. windowClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
  27. windowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
  28. windowClass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
  29. windowClass.lpszClassName = L"OpenGLWindow";
  30. RegisterClassEx( &windowClass );
  31.  
  32. RECT windowSize;
  33. windowSize.left = 0;
  34. windowSize.top = 0;
  35. windowSize.right = 800;
  36. windowSize.bottom = 600;
  37. AdjustWindowRect( &windowSize, WS_POPUPWINDOW | WS_CAPTION, false );
  38.  
  39. HWND window = CreateWindowEx( WS_EX_OVERLAPPEDWINDOW, L"OpenGLWindow", L"OpenGL", WS_POPUPWINDOW | WS_CAPTION, 250, 50, windowSize.right - windowSize.left, windowSize.bottom - windowSize.top, NULL, NULL, NULL, NULL );
  40. HDC dc = GetDC( window );
  41. ShowWindow( window, SW_SHOWNORMAL );
  42. UpdateWindow( window );
  43.  
  44. // Choose appropriate pixel format
  45. PIXELFORMATDESCRIPTOR pfd;
  46. ZeroMemory( &pfd, sizeof( pfd ) );
  47. pfd.nSize = sizeof( pfd );
  48. pfd.nVersion = 1;
  49. pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  50. pfd.iPixelType = PFD_TYPE_RGBA;
  51. pfd.cColorBits = 32;
  52. pfd.cDepthBits = 32;
  53. pfd.iLayerType = PFD_MAIN_PLANE;
  54. int pixelFormat = ChoosePixelFormat( dc, &pfd );
  55. SetPixelFormat( dc, pixelFormat, &pfd );
  56.  
  57. // Create OpenGL context
  58. HGLRC context = wglCreateContext( dc );
  59. wglMakeCurrent( dc, context );
  60.  
  61. // Set viewport and clear color
  62. glViewport( 0, 0, 800, 600 );
  63. glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
  64.  
  65. // Load shaders and specify input sources
  66. const char* vertexShaderSource = "in vec2 in_Position;\
  67. in vec3 in_Color;\
  68. varying vec3 ex_Color;\
  69. \
  70. void main()\
  71. {\
  72. gl_Position = vec4( ( in_Position.x - 400.0 ) / 400.0, ( 300.0 - in_Position.y ) / 300.0, 0.0, 1.0 );\
  73. ex_Color = in_Color;\
  74. }";
  75. const char* fragmentShaderSource = "varying vec3 ex_Color;\
  76. \
  77. void main()\
  78. {\
  79. gl_FragColor = vec4( ex_Color, 1.0 );\
  80. }";
  81.  
  82. int vertexShader = glCreateShader( GL_VERTEX_SHADER );
  83. glShaderSource( vertexShader, 1, &vertexShaderSource, 0 );
  84. glCompileShader( vertexShader );
  85.  
  86. int fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
  87. glShaderSource( fragmentShader, 1, &fragmentShaderSource, 0 );
  88. glCompileShader( fragmentShader );
  89.  
  90. int shaderProgram = glCreateProgram();
  91. glAttachShader( shaderProgram, vertexShader );
  92. glAttachShader( shaderProgram, fragmentShader );
  93. glBindAttribLocation( shaderProgram, 0, "in_Position" );
  94. glBindAttribLocation( shaderProgram, 1, "in_Color" );
  95. glLinkProgram( shaderProgram );
  96. glUseProgram( shaderProgram );
  97.  
  98. // Put vertex data in a VBO and set the position and color pointers
  99. float vertices[] = {
  100. 50.0f, 50.0f, 1.0f, 0.0f, 0.0f,
  101. 750.0f, 50.0f, 0.0f, 1.0f, 0.0f,
  102. 750.0f, 550.0f, 0.0f, 0.0f, 1.0f,
  103.  
  104. 750.0f, 550.0f, 0.0f, 0.0f, 1.0f,
  105. 50.0f, 550.0f, 1.0f, 0.0f, 1.0f,
  106. 50.0f, 50.0f, 1.0f, 0.0f, 0.0f,
  107. };
  108. unsigned int vbo;
  109. glGenBuffers( 1, &vbo );
  110. glBindBuffer( GL_ARRAY_BUFFER, vbo );
  111. glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );
  112.  
  113. glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof( float ) * 5, 0 );
  114. glEnableVertexAttribArray( 0 );
  115. glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, sizeof( float ) * 5, (void*)( 2 * sizeof( float ) ) );
  116. glEnableVertexAttribArray( 1 );
  117.  
  118. // Main loop
  119. MSG msg;
  120. while ( g_WindowOpen )
  121. {
  122. while ( PeekMessage( &msg, window, 0, 0, PM_REMOVE ) )
  123. {
  124. TranslateMessage( &msg );
  125. DispatchMessage( &msg );
  126. }
  127.  
  128. // Clear and draw
  129. glClear( GL_COLOR_BUFFER_BIT );
  130.  
  131. glDrawArrays( GL_TRIANGLES, 0, 6 );
  132.  
  133. // Present
  134. SwapBuffers( dc );
  135. }
  136. }
Add Comment
Please, Sign In to add comment