GFXGLDevice::GFXGLDevice(U32 adapterIndex) : mAdapterIndex(adapterIndex), mNeedUpdateVertexAttrib(false), mCurrentPB(NULL), mDrawInstancesCount(0), mCurrentShader( NULL ), m_mCurrentWorld(true), m_mCurrentView(true), mContext(NULL), mPixelFormat(NULL), mPixelShaderVersion(0.0f), mMaxShaderTextures(2), mMaxFFTextures(2), mMaxTRColors(1), mClip(0, 0, 0, 0), mWindowRT(NULL), mUseGlMap(true) { for(int i = 0; i < VERTEX_STREAM_COUNT; ++i) { mCurrentVB[i] = NULL; mCurrentVB_Divisor[i] = 0; } // Initiailize capabilities to false. memset(&mCapabilities, 0, sizeof(GLCapabilities)); loadGLCore(); GFXGLEnumTranslate::init(); GFXVertexColor::setSwizzle( &Swizzles::rgba ); // OpenGL have native RGB, no need swizzle mDeviceSwizzle32 = &Swizzles::rgba; mDeviceSwizzle24 = &Swizzles::rgb; mTextureManager = new GFXGLTextureManager(); gScreenShot = new ScreenShot(); for(U32 i = 0; i < TEXTURE_STAGE_COUNT; i++) mActiveTextureType[i] = GL_ZERO; mNumVertexStream = 2; for(int i = 0; i < GS_COUNT; ++i) mModelViewProjSC[i] = NULL; mOpenglStateCache = new GFXGLStateCache; }
void GFXGLDevice::init( const GFXVideoMode &mode, PlatformWindow *window ) { AssertFatal(window, "GFXGLDevice::init - no window specified, can't init device without a window!"); PlatformWindowSDL* sdlWindow = dynamic_cast<PlatformWindowSDL*>(window); AssertFatal(sdlWindow, "Window is not a valid PlatformWindowSDL object"); // Create OpenGL context mContext = PlatformGL::CreateContextGL( sdlWindow ); PlatformGL::MakeCurrentGL( sdlWindow, mContext ); loadGLCore(); loadGLExtensions(mContext); // It is very important that extensions be loaded before we call initGLState() initGLState(); mProjectionMatrix.identity(); mInitialized = true; deviceInited(); }
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(); }
void GFXGLDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList ) { AssertFatal( SDL_WasInit(SDL_INIT_VIDEO), ""); PlatformGL::init(); // for hints about context creation // Create a dummy window & openGL context so that gl functions can be used here SDL_Window* tempWindow = SDL_CreateWindow( "", // window title SDL_WINDOWPOS_UNDEFINED, // initial x position SDL_WINDOWPOS_UNDEFINED, // initial y position 640, // width, in pixels 480, // height, in pixels SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN // flags - see below ); SDL_ClearError(); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GLContext tempContext = SDL_GL_CreateContext( tempWindow ); if( !tempContext ) { const char *err = SDL_GetError(); Con::printf( err ); AssertFatal(0, err ); return; } SDL_ClearError(); SDL_GL_MakeCurrent( tempWindow, tempContext ); const char *err = SDL_GetError(); if( err && err[0] ) { Con::printf( err ); AssertFatal(0, err ); } // Init GL loadGLCore(); loadGLExtensions(tempContext); //check minimun Opengl 3.2 int major, minor; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); if( major < 3 || ( major == 3 && minor < 2 ) ) { return; } 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: EnumerateVideoModes(toAdd->mAvailableModes); // Add to the list of available adapters. adapterList.push_back(toAdd); // Cleanup window & open gl context SDL_DestroyWindow( tempWindow ); SDL_GL_DeleteContext( tempContext ); }