Example #1
0
void
setupWindowForOpenGL(MSWinSurface *surf, ISurface::Attributes attribs)
{
    initGlew();

    // Get a DC
    HDC hDC = GetDC(surf->windowHandle());  // TODO: is it ok to call GetDC() every time a DC is needed?

    // Select and set a pixel format
    int pixel_format = selectPixelFormat(hDC, !attribs.test(ISurface::SINGLE_BUFFERED));
    if (! SetPixelFormat(hDC, pixel_format, NULL))
        throw EMSWinError(GetLastError(), "SetPixelFormat");

    // Create an OpenGL context
    HGLRC hRC = wglCreateContext(hDC);
    if (hRC == 0) throw EMSWinError(GetLastError(), "wglCreateContext");
    surf->setOpenGLContext(hRC);

    // Try to make the Rendering Context part of a Video Context
    surf->setVideoContextID( assignToVideoContext(hRC) );

#ifdef NOT_DEFINED
    // Initialize GLEW for this context
    glewExperimental = TRUE;
    if (glewInit() != GLEW_OK)
        throw EMSWinError(GetLastError(), "glewInit()");
#endif
}
Example #2
0
bool OpenGLApp::initAPI(){
	PixelFormat pf;
	initPixelFormat(pf);
	selectPixelFormat(pf);

	int bpp = pf.alphaBits > 0? 32 : 24;
	wa = (pf.accumBits > 0);

    static PIXELFORMATDESCRIPTOR pfd = {
        sizeof (PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		bpp,
		0, 0, 0, 0, 0, 0, 0, 0,
		pf.accumBits, pf.accumBits / 4, pf.accumBits / 4, pf.accumBits / 4, pf.accumBits / 4,
		pf.depthBits,
		pf.stencilBits,
		pf.alphaBits,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
    };


	WNDCLASS wincl;
	HINSTANCE hInst = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE);

	wincl.hInstance = hInst;
	wincl.lpszClassName = "PFrmt";
	wincl.lpfnWndProc = PFWinProc;
	wincl.style = 0;
	wincl.hIcon = NULL;
	wincl.hCursor = NULL;
	wincl.lpszMenuName = NULL;
	wincl.cbClsExtra = 0;
	wincl.cbWndExtra = 0;
	wincl.hbrBackground = NULL;
	RegisterClass(&wincl);

	HWND hPFwnd = CreateWindow("PFrmt", "PFormat", WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 8, 8, HWND_DESKTOP, NULL, hInst, NULL);
	initEntryPoints(hPFwnd, pfd);
	SendMessage(hPFwnd, WM_CLOSE, 0, 0);

	hdc = GetDC(hwnd);

	int pixelFormat;
	float fAttribs[256];
	int iAttribs[256] = {
		WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
		WGL_ACCELERATION_ARB,   WGL_FULL_ACCELERATION_ARB,
		WGL_DOUBLE_BUFFER_ARB,  GL_TRUE,

		WGL_RED_BITS_ARB,       pf.redBits,
		WGL_GREEN_BITS_ARB,     pf.greenBits,
		WGL_BLUE_BITS_ARB,      pf.blueBits,
		WGL_ALPHA_BITS_ARB,     pf.alphaBits,
		WGL_DEPTH_BITS_ARB,     pf.depthBits,
		WGL_STENCIL_BITS_ARB,   pf.stencilBits,
		WGL_ACCUM_BITS_ARB,     pf.accumBits,
	};

	while (true){
		int   *iAtt = iAttribs + 20;
		float *fAtt = fAttribs;

		if (WGL_ARB_multisample_supported && pf.fsaaLevel > 0){
			*iAtt++ = WGL_SAMPLE_BUFFERS_ARB;
			*iAtt++ = GL_TRUE;

			*iAtt++ = WGL_SAMPLES_ARB;
			*iAtt++ = pf.fsaaLevel;
		}

		*iAtt++ = 0;
		*fAtt++ = 0;

		unsigned int nMatchingPixelFormats;
		if (!WGL_ARB_pixel_format_supported || !wglChoosePixelFormatARB(hdc, iAttribs, fAttribs, 1, &pixelFormat, &nMatchingPixelFormats) || nMatchingPixelFormats == 0){
			if (pf.fsaaLevel > 0){
				pf.fsaaLevel -= 2;
			} else {
				pixelFormat = ChoosePixelFormat(hdc, &pfd);
				break;
			}
		} else break;
	}

    SetPixelFormat(hdc, pixelFormat, &pfd);

	hglrc = wglCreateContext(hdc);
	wglMakeCurrent(hdc, hglrc);

	initExtensions(hdc);

	if (WGL_ARB_multisample_supported && pf.fsaaLevel > 0){
		glEnable(GL_MULTISAMPLE_ARB);
	}


	renderer = new OpenGLRenderer(hdc, hglrc);

	return true;
}
Example #3
0
bool OpenGLApp::initAPI(){
	PixelFormat pf;
	initPixelFormat(pf);
	selectPixelFormat(pf);

	int iAttribs[256] = {
		GLX_RGBA,
		GLX_DOUBLEBUFFER,
		GLX_RED_SIZE,     pf.redBits,
		GLX_GREEN_SIZE,   pf.greenBits,
		GLX_BLUE_SIZE,    pf.blueBits,
		GLX_ALPHA_SIZE,   pf.alphaBits,
		GLX_DEPTH_SIZE,   pf.depthBits,
		GLX_STENCIL_SIZE, pf.stencilBits,
		GLX_ACCUM_RED_SIZE,   pf.accumBits / 4,
		GLX_ACCUM_GREEN_SIZE, pf.accumBits / 4,
		GLX_ACCUM_BLUE_SIZE,  pf.accumBits / 4,
		GLX_ACCUM_ALPHA_SIZE, pf.accumBits / 4,
	};

	XVisualInfo *vi;
	while (true){
		int *iAtt = iAttribs + 22;
		if (/*GLX_ARB_multisample_supported && */pf.fsaaLevel > 0){
			*iAtt++ = 100000;
			*iAtt++ = GL_TRUE;

			*iAtt++ = 100001;
			*iAtt++ = pf.fsaaLevel;
		}

		*iAtt++ = None;

		vi = glXChooseVisual(display, screen, iAttribs);
		if (vi == NULL){
			if (pf.fsaaLevel > 0){
				pf.fsaaLevel -= 2;
			} else {
				printf("Error: Couldn't set visual\n");
				return false;
			}
		} else break;
	}


	int x, y, w, h;
	if (fullscreen){
		x = 0;
		y = 0;
		w = fullscreenWidth;
		h = fullscreenHeight;
	} else {
		x = windowedLeft;
		y = windowedTop;
		w = windowedRight - windowedLeft;
		h = windowedBottom - windowedTop;
	}
	middleX = w / 2;
	middleY = h / 2;

    ctx = glXCreateContext(display, vi, 0, GL_TRUE);


	XSetWindowAttributes attr;
	attr.colormap = XCreateColormap(display, RootWindow(display, screen), vi->visual, AllocNone);

	attr.border_pixel = 0;
	attr.override_redirect = fullscreen;
    attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;

	window = XCreateWindow(display, RootWindow(display, vi->screen),
			x, y, w, h, 0, vi->depth, InputOutput, vi->visual,
			CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect, &attr);

	if (!fullscreen){
	    Atom wmDelete;
        wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True);
        XSetWMProtocols(display, window, &wmDelete, 1);
		char *title = "OpenGL";
        XSetStandardProperties(display, window, title, title, None, NULL, 0, NULL);
	}
    XMapRaised(display, window);

	Pixmap blank;
	XColor dummy;
	char data = 0;

	// Create a blank cursor for cursor hiding
	blank = XCreateBitmapFromData(display, window, &data, 1, 1);
	blankCursor = XCreatePixmapCursor(display, blank, blank, &dummy, &dummy, 0, 0);
	XFreePixmap(display, blank);


	XGrabKeyboard(display, window, True, GrabModeAsync, GrabModeAsync, CurrentTime);
	//if (captureMouse) showCursor(false);//XGrabPointer(display, window, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, window, blankCursor, CurrentTime);
	cursorVisible = true;


    glXMakeCurrent(display, window, ctx);

	//printf((char *) glXGetClientString(display, GLX_EXTENSIONS));


	initExtensions(display, screen);

	// Set some of my preferred defaults
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glFrontFace(GL_CW);

	if (/*GLX_ARB_multisample_supported && */pf.fsaaLevel > 0){
		glEnable(GL_MULTISAMPLE_ARB);
	}

	renderer = new OpenGLRenderer(ctx, display, screen);

//	renderer->setAnisotropic(anisotropic);

	return true;
}