Ember

oglinit

Feb 13th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1.     // OpenGL context initialization ------------------------------------------------------
  2.     bool Device::Initialize(HWND hwnd, int screenWidth, int screenHeight, float screenDepth, float screenNear, bool vsync)
  3.     {
  4.         int attributeListInt[19];
  5.         int pixelFormat;
  6.         unsigned int formatCount;
  7.         int result;
  8.         PIXELFORMATDESCRIPTOR pixelFormatDescriptor;
  9.         int attributeList[5];
  10.         float fieldOfView, screenAspect;
  11.         char *vendorString, *rendererString;
  12.  
  13.         // Get the device context for this window
  14.         mDeviceContext = GetDC(hwnd);
  15.         if(!mDeviceContext) {return false;}
  16.  
  17.         pixelFormatDescriptor = {
  18.             sizeof(PIXELFORMATDESCRIPTOR),    // size of this pfd
  19.             1,                                // version number
  20.             PFD_DRAW_TO_WINDOW |              // support window
  21.             PFD_SUPPORT_OPENGL |              // support OpenGL
  22.             PFD_DOUBLEBUFFER,                 // double buffered
  23.             PFD_TYPE_RGBA,                    // RGBA type
  24.             24,                               // 24-bit color depth
  25.             0, 0, 0, 0, 0, 0,                 // color bits ignored
  26.             0,                                // no alpha buffer
  27.             0,                                // shift bit ignored
  28.             0,                                // no accumulation buffer
  29.             0, 0, 0, 0,                       // accum bits ignored
  30.             32,                               // 32-bit z-buffer
  31.             0,                                // no stencil buffer
  32.             0,                                // no auxiliary buffer
  33.             PFD_MAIN_PLANE,                   // main layer
  34.             0,                                // reserved
  35.             0, 0, 0                           // layer masks ignored
  36.         };
  37.  
  38.         // get the device context's best, available pixel format match
  39.         pixelFormat = ChoosePixelFormat(mDeviceContext, &pixelFormatDescriptor);
  40.  
  41.         // If the video card/display can handle our desired pixel format then we set it as the current one.
  42.         result = SetPixelFormat(mDeviceContext, pixelFormat, &pixelFormatDescriptor);
  43.         if(result != 1) {return false;}
  44.  
  45.         // Create a OpenGL 4.0 rendering context.
  46.         mRenderContext = wglCreateContext(mDeviceContext);
  47.         if(mRenderContext == NULL) {return false;}
  48.  
  49.         // Set the rendering context to active
  50.         result = wglMakeCurrent(mDeviceContext, mRenderContext);
  51.         if(result != 1) {return false;}
  52.  
  53.         // Set the depth buffer to be entirely cleared to 1.0 values
  54.         glClearDepth(1.0f);
  55.  
  56.         // Enable depth testing
  57.         glEnable(GL_DEPTH_TEST);
  58.  
  59.         // Set the polygon winding to front facing for the left handed system
  60.         glFrontFace(GL_CW);
  61.  
  62.         // Enable back face culling
  63.         glEnable(GL_CULL_FACE);
  64.         glCullFace(GL_BACK);
  65.  
  66.         // Initialize the world/model matrix to the identity matrix
  67.         BuildIdentityMatrix(mWorldMatrix);
  68.  
  69.         // Set the field of view and screen aspect ratio
  70.         fieldOfView = 3.14159265358979323846f / 4.0f;
  71.         screenAspect = (float)screenWidth / (float)screenHeight;
  72.  
  73.         // Build the perspective projection matrix
  74.         BuildPerspectiveFovLHMatrix(mProjectionMatrix, fieldOfView, screenAspect, screenNear, screenDepth);
  75.  
  76.         // Get the name of the video card
  77.         vendorString = (char*)glGetString(GL_VENDOR);
  78.         rendererString = (char*)glGetString(GL_RENDERER);
  79.  
  80.         // Store the video card name in a class member variable so it can be retrieved later
  81.         strcpy_s(mVideoCardDescription, vendorString);
  82.         strcat_s(mVideoCardDescription, " - ");
  83.         strcat_s(mVideoCardDescription, rendererString);
  84.  
  85.         return true;
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment