Пример #1
0
void Scene3D::InitializeOpenGL(int width, int height) 
{  
    hdc = GetDC(*hwnd);//  sets  global HDC

    if (!CreatePixelFormat(hdc))//  sets  pixel format
        PostQuitMessage (0);

    hrc = wglCreateContext(hdc);	//  creates  rendering context from  hdc
    wglMakeCurrent(hdc, hrc);		//	Use this HRC.

	ResizeGLWindow(width, height);	// Setup the Screen
}
Пример #2
0
GLContext::GLContext(HWND hWindow, U32 width, U32 height)
	: m_hWindow(hWindow)
	, m_hDeviceContext(0)
	, m_hRenderContext(0)
{
	m_hDeviceContext = GetDC(hWindow);

	CreatePixelFormat();
	CreateContext();

	// Set clear color
	gl::ClearColor(0.4f, 0.6f, 0.9f, 1.0f);

	gl::Viewport(0, 0, width, height);
}
void GFXGLDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
{
//   GL_ERROR_CHECK();
   WNDCLASS windowclass;
   dMemset( &windowclass, 0, sizeof( WNDCLASS ) );

   windowclass.lpszClassName = L"GFX-OpenGL";
   windowclass.style         = CS_OWNDC;
   windowclass.lpfnWndProc   = DefWindowProc;
   windowclass.hInstance     = winState.appInstance;

   if( !RegisterClass( &windowclass ) )
      AssertFatal( false, "Failed to register the window class for the GL test window." );

   // Now create a window
   HWND hwnd = CreateWindow( L"GFX-OpenGL", L"", WS_POPUP, 0, 0, 640, 480, 
                             NULL, NULL, winState.appInstance, NULL );
   AssertFatal( hwnd != NULL, "Failed to create the window for the GL test window." );

   // Create a device context
   HDC tempDC = GetDC( hwnd );
   AssertFatal( tempDC != NULL, "Failed to create device context" );

   // Create pixel format descriptor...
   PIXELFORMATDESCRIPTOR pfd;
   CreatePixelFormat( &pfd, 32, 0, 0, false );
   if( !SetPixelFormat( tempDC, ChoosePixelFormat( tempDC, &pfd ), &pfd ) )
      AssertFatal( false, "I don't know who's responcible for this, but I want caught..." );

   // Create a rendering context!
   HGLRC tempGLRC = wglCreateContext( tempDC );
   if( !wglMakeCurrent( tempDC, tempGLRC ) )
      AssertFatal( false, "I want them caught and killed." );

   // Add the GL renderer
   loadGLCore();
   loadGLExtensions(tempDC);

   GFXAdapter *toAdd = new GFXAdapter;
   toAdd->mIndex = 0;

   const char* renderer = (const char*) glGetString( GL_RENDERER );
   AssertFatal( renderer != NULL, "GL_RENDERER returned NULL!" );

   if (renderer)
   {
      dStrcpy(toAdd->mName, renderer);
      dStrncat(toAdd->mName, " OpenGL", GFXAdapter::MaxAdapterNameLen);
   }
   else
      dStrcpy(toAdd->mName, "OpenGL");

   toAdd->mType = OpenGL;
   toAdd->mShaderModel = 0.f;
   toAdd->mCreateDeviceInstanceDelegate = mCreateDeviceInstance;

   // Enumerate all available resolutions:
   DEVMODE devMode;
   U32 modeNum = 0;
   U32 stillGoing = true;
   while ( stillGoing )
   {
      dMemset( &devMode, 0, sizeof( devMode ) );
      devMode.dmSize = sizeof( devMode );

      stillGoing = EnumDisplaySettings( NULL, modeNum++, &devMode );

      if (( devMode.dmPelsWidth >= 480) && (devMode.dmPelsHeight >= 360 )
         && ( devMode.dmBitsPerPel == 16 || devMode.dmBitsPerPel == 32 )) 
      {
         GFXVideoMode vmAdd;

         vmAdd.bitDepth     = devMode.dmBitsPerPel;
         vmAdd.fullScreen   = true;
         vmAdd.refreshRate  = devMode.dmDisplayFrequency;
         vmAdd.resolution.x = devMode.dmPelsWidth;
         vmAdd.resolution.y = devMode.dmPelsHeight;

         // Only add this resolution if it is not already in the list:
         bool alreadyInList = false;
         for (Vector<GFXVideoMode>::iterator i = toAdd->mAvailableModes.begin(); i != toAdd->mAvailableModes.end(); i++)
         {
            if (vmAdd == *i)
            {
               alreadyInList = true;
               break;
            }
         }

         if(alreadyInList)
            continue;

         toAdd->mAvailableModes.push_back( vmAdd );
      }
   }

   // Add to the list of available adapters.
   adapterList.push_back(toAdd);

   // Cleanup our window
   wglMakeCurrent(NULL, NULL);
   wglDeleteContext(tempGLRC);
   ReleaseDC(hwnd, tempDC);
   DestroyWindow(hwnd);
   UnregisterClass(L"GFX-OpenGL", winState.appInstance);
}
void GFXGLDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
{
   AssertFatal(window, "GFXGLDevice::init - no window specified, can't init device without a window!");
   AssertFatal(dynamic_cast<Win32Window*>(window), "Invalid window class type!");
   HWND hwnd = GETHWND(window);

   mWindowRT = &static_cast<Win32Window*>(window)->mTarget;

   RECT rect;
   GetClientRect(hwnd, &rect);

   Point2I resolution;
   resolution.x = rect.right - rect.left;
   resolution.y = rect.bottom - rect.top;

   // Create a device context
   HDC hdcGL = GetDC( hwnd );
   AssertFatal( hdcGL != NULL, "Failed to create device context" );

   // Create pixel format descriptor...
   PIXELFORMATDESCRIPTOR pfd;
   CreatePixelFormat( &pfd, 32, 0, 0, false ); // 32 bit color... We do not need depth or stencil, OpenGL renders into a FBO and then copy the image to window
   if( !SetPixelFormat( hdcGL, ChoosePixelFormat( hdcGL, &pfd ), &pfd ) )
   {
      AssertFatal( false, "GFXGLDevice::init - cannot get the one and only pixel format we check for." );
   }

   int OGL_MAJOR = 3;
   int OGL_MINOR = 2;
   
#if TORQUE_DEBUG
   int debugFlag = WGL_CONTEXT_DEBUG_BIT_ARB;
#else
   int debugFlag = 0;
#endif

   if( gglHasWExtension(ARB_create_context) )
   {
      int const create_attribs[] = {
               WGL_CONTEXT_MAJOR_VERSION_ARB, OGL_MAJOR,
               WGL_CONTEXT_MINOR_VERSION_ARB, OGL_MINOR,
               WGL_CONTEXT_FLAGS_ARB, /*WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB |*/ debugFlag,
               WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
               //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
               0
           };

      mContext = wglCreateContextAttribsARB(hdcGL, 0, create_attribs);
      if(!mContext)
      {
         AssertFatal(0,"");
      }
   } 
   else
      mContext = wglCreateContext( hdcGL );

   if( !wglMakeCurrent( hdcGL, (HGLRC)mContext ) )
      AssertFatal( false , "GFXGLDevice::init - cannot make our context current. Or maybe we can't create it." );

   loadGLCore();
   loadGLExtensions(hdcGL);

   wglSwapIntervalEXT(0);
   
   // It is very important that extensions be loaded
   // before we call initGLState()
   initGLState();

   mProjectionMatrix.identity();

   mInitialized = true;
   deviceInited();
}
Пример #5
0
    Bool RenderDevice::Initialize( HINSTANCE hInstance, HWND window )
    {
        CARBON_ASSERT( m_window == 0 );
        CARBON_ASSERT( m_deviceContext == 0 );
        CARBON_ASSERT( m_renderContext == 0 );

        m_window = window;

        int pfAttribs[] =
        {
            WGL_DRAW_TO_WINDOW_ARB          , GL_TRUE,
            WGL_SUPPORT_OPENGL_ARB          , GL_TRUE,
            WGL_DOUBLE_BUFFER_ARB           , GL_TRUE,
            WGL_PIXEL_TYPE_ARB              , WGL_TYPE_RGBA_ARB,
            WGL_COLOR_BITS_ARB              , 24,
            WGL_DEPTH_BITS_ARB              , 24,
            WGL_STENCIL_BITS_ARB            , 8,
            WGL_SAMPLE_BUFFERS_ARB          , 1,
            WGL_SAMPLES_ARB                 , 4, // MSAA x4
            0 //End
        };

        S32 pixelFormat = CreatePixelFormat( hInstance, pfAttribs );
        if ( !pixelFormat )
        {
            m_window = 0;
            return false;
        }

        m_deviceContext = GetDC( m_window );
        if ( !m_deviceContext )
        {
            m_window = 0;
            return false;
        }

        PIXELFORMATDESCRIPTOR pfd =
        {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
            PFD_TYPE_RGBA,                                              //The kind of framebuffer. RGBA or palette.
            24,                                                         //Colordepth of the framebuffer.
            0, 0, 0, 0, 0, 0,
            0,
            0,
            0,
            0, 0, 0, 0,
            24,                                                         //Number of bits for the depthbuffer
            8,                                                          //Number of bits for the stencilbuffer
            0,                                                          //Number of Aux buffers in the framebuffer.
            PFD_MAIN_PLANE,
            0,
            0, 0, 0
        };

        if ( !SetPixelFormat( m_deviceContext, pixelFormat, &pfd ) )
        {
            m_deviceContext = 0;
            m_window = 0;
            return false;
        }

        int rcAttribs[] =
        {
            WGL_CONTEXT_MAJOR_VERSION_ARB   , 4,
            WGL_CONTEXT_MINOR_VERSION_ARB   , 2,
            WGL_CONTEXT_PROFILE_MASK_ARB    , WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
            0 //End
        };

        m_renderContext = OpenGL::CreateContext( m_deviceContext, rcAttribs );
        if ( !m_renderContext )
        {
            m_deviceContext = 0;
            m_window = 0;
            return false;
        }

        return true;
    }