/************************************************************************* GameInit() Performs one-time game-specific setup. Returns false on any failure. *************************************************************************/ bool GameInit() { if (!InitializeExtensions()) return false; // disable VSYNCH if possible if (pwglSwapIntervalEXT) { pwglSwapIntervalEXT(0); } glEnable(GL_DEPTH_TEST); LoadTGATexture(g_floorTexture, "floor.tga", GL_REPEAT); LoadTGATexture(g_lightmap, "lightmap.tga", (g_useEdgeClamp ? GL_CLAMP_TO_EDGE : GL_CLAMP)); // set up the settings needed to render the light as a point glPointSize(12.0); glEnable(GL_POINT_SMOOTH); glHint(GL_POINT_SMOOTH, GL_NICEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // vary the light point size by the distance from the camera if possible if (pglPointParameterfvEXT) { GLfloat attenuation[3] = { 0.0, 0.5, 0.0 }; pglPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, attenuation); } return true; } // end GameInit()
bool Device::Create(void) { PIXELFORMATDESCRIPTOR pixelFormatDesc; ZeroMemory(&pixelFormatDesc, sizeof PIXELFORMATDESCRIPTOR); pixelFormatDesc.nSize = sizeof PIXELFORMATDESCRIPTOR; // Size of this pixel format descriptor. pixelFormatDesc.nVersion = 1; // Version number. pixelFormatDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pixelFormatDesc.iPixelType = PFD_TYPE_RGBA; // Request an RGBA format. pixelFormatDesc.cColorBits = 24; pixelFormatDesc.cDepthBits = 16; pixelFormatDesc.iLayerType = PFD_MAIN_PLANE; // Main drawing layer. _deviceContext = GetDC(_windowHandle); int pixelFormatIdx = ChoosePixelFormat(_deviceContext, &pixelFormatDesc); if (pixelFormatIdx == 0) { ReleaseDC(_windowHandle, _deviceContext); return false; } if (SetPixelFormat(_deviceContext, pixelFormatIdx, &pixelFormatDesc)) { _renderContext = wglCreateContext(_deviceContext); if (_renderContext) MakeCurrentContext(); else { ReleaseDC(_windowHandle, _deviceContext); wglDeleteContext(_renderContext); return false; } } else { ReleaseDC(_windowHandle, _deviceContext); return false; } _version = (const char*)glGetString(GL_VERSION); _vendorName = (const char*)glGetString(GL_VENDOR); _rendererName = (const char*)glGetString(GL_RENDERER); _GLSLVersion = (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION); glGetIntegerv(GL_MAJOR_VERSION, &_majorVersion); glGetIntegerv(GL_MINOR_VERSION, &_minorVersion); // Инициализируем расширения. InitializeExtensions(); return true; }
bool GL::Initialize(HWND hwnd, int screenWidth, int screenHeight, float screenDepth, float screenNear, bool vsync) { int attributeListInt[19]; int pixelFormat[1]; unsigned int formatCount; int result; PIXELFORMATDESCRIPTOR pixelFormatDescriptor; int attributeList[5]; float fieldOfView, screenAspect; char *vendorString, *rendererString; InitializeExtensions(hwnd); auto e = glGetError(); // Get the device context for this window. device_context_ = GetDC(hwnd); if(!device_context_) { return false; } // Support for OpenGL rendering. attributeListInt[0] = WGL_SUPPORT_OPENGL_ARB; attributeListInt[1] = TRUE; // Support for rendering to a window. attributeListInt[2] = WGL_DRAW_TO_WINDOW_ARB; attributeListInt[3] = TRUE; // Support for hardware acceleration. attributeListInt[4] = WGL_ACCELERATION_ARB; attributeListInt[5] = WGL_FULL_ACCELERATION_ARB; // Support for 24bit color. attributeListInt[6] = WGL_COLOR_BITS_ARB; attributeListInt[7] = 32; attributeListInt[8] = WGL_PIXEL_TYPE_ARB; attributeListInt[9] = WGL_TYPE_RGBA_ARB; /* // Support for 24 bit depth buffer. attributeListInt[8] = WGL_DEPTH_BITS_ARB; attributeListInt[9] = 24; // Support for double buffer. attributeListInt[10] = WGL_DOUBLE_BUFFER_ARB; attributeListInt[11] = TRUE; // Support for swapping front and back buffer. attributeListInt[8] = WGL_SWAP_METHOD_ARB; attributeListInt[9] = WGL_SWAP_COPY_ARB; // Support for the RGBA pixel type. // Support for a 8 bit stencil buffer. attributeListInt[16] = WGL_STENCIL_BITS_ARB; attributeListInt[17] = 8; */ // Null terminate the attribute list. attributeListInt[10] = 0; // Query for a pixel format that fits the attributes we want. result = 1;//wglChoosePixelFormatARB(device_context_, attributeListInt, NULL, 1, pixelFormat, &formatCount); if(result != 1) { return false; } // If the video card/display can handle our desired pixel format then we set it as the current one. //result = SetPixelFormat(device_context_, pixelFormat[0], &pixelFormatDescriptor); PIXELFORMATDESCRIPTOR pfd; int iFormat; ZeroMemory( &pfd, sizeof( pfd ) ); pfd.nSize = sizeof( pfd ); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 24; pfd.iLayerType = PFD_MAIN_PLANE; iFormat = ChoosePixelFormat( device_context_, &pfd ); e = glGetError(); result = SetPixelFormat( device_context_, iFormat, &pfd ); e = glGetError(); if(result != 1) { return false; } // Set the 4.0 version of OpenGL in the attribute list. attributeList[0] = WGL_CONTEXT_MAJOR_VERSION_ARB; attributeList[1] = 3; attributeList[2] = WGL_CONTEXT_MINOR_VERSION_ARB; attributeList[3] = 0; // Null terminate the attribute list. attributeList[4] = 0; // Create a OpenGL 4.0 rendering context. rendering_context_ = wglCreateContext(device_context_);//wglCreateContextAttribsARB(device_context_, 0, attributeList); if(rendering_context_ == NULL) { return false; } // Set the rendering context to active. result = wglMakeCurrent(device_context_, rendering_context_); if(result != 1) { return false; } // Set the depth buffer to be entirely cleared to 1.0 values. glClearDepth(1.0f); // Enable depth testing. glEnable(GL_DEPTH_TEST); // Set the polygon winding to front facing for the left handed system. glFrontFace(GL_CW); // Enable back face culling. glEnable(GL_CULL_FACE); glCullFace(GL_BACK); // Initialize the world/model matrix to the identity matrix. BuildIdentityMatrix(worldMatrix); // Set the field of view and screen aspect ratio. fieldOfView = 3.14159265358979323846f / 4.0f; screenAspect = (float)screenWidth / (float)screenHeight; // Build the perspective projection matrix. BuildPerspectiveFovLHMatrix(projectionMatrix, fieldOfView, screenAspect, screenNear, screenDepth); // Get the name of the video card. vendorString = (char*)glGetString(GL_VENDOR); rendererString = (char*)glGetString(GL_RENDERER); // Store the video card name in a class member variable so it can be retrieved later. strcpy_s(videoCardDescription, vendorString); strcat_s(videoCardDescription, " - "); strcat_s(videoCardDescription, rendererString); // Turn on or off the vertical sync depending on the input bool value. if(vsync) { result = 1;//wglSwapIntervalEXT(1); } else { result = 1;//wglSwapIntervalEXT(0); } // Check if vsync was set correctly. if(result != 1) { return false; } return true; }