[mapserver-commits] r11479 - trunk/mapserver

svn at osgeo.org svn at osgeo.org
Wed Apr 6 12:47:46 EDT 2011


Author: toby
Date: 2011-04-06 09:47:46 -0700 (Wed, 06 Apr 2011)
New Revision: 11479

Modified:
   trunk/mapserver/mapogl.cpp
   trunk/mapserver/mapoglcontext.cpp
   trunk/mapserver/mapoglcontext.h
   trunk/mapserver/mapoglrenderer.cpp
   trunk/mapserver/mapoglrenderer.h
Log:
Making OpenGL context creation more robust on older video cards and drivers. (#3791)

Modified: trunk/mapserver/mapogl.cpp
===================================================================
--- trunk/mapserver/mapogl.cpp	2011-04-06 14:46:30 UTC (rev 11478)
+++ trunk/mapserver/mapogl.cpp	2011-04-06 16:47:46 UTC (rev 11479)
@@ -16,6 +16,7 @@
 
 imageObj* createImageObjOgl(OglRenderer* renderer)
 {	
+	if (!renderer->isValid()) return NULL;
 	imageObj* pNewImage = (imageObj*)calloc(1, sizeof(imageObj));
 	if (!pNewImage)
 		return pNewImage;	
@@ -30,9 +31,9 @@
 
 int msSaveImageOgl(imageObj *img, FILE *fp, outputFormatObj *format)
 {
-	rasterBufferObj data;
-	OglRenderer* renderer = getOglRenderer(img);
-    renderer->readRasterBuffer(&data);        
+	rasterBufferObj data;
+	OglRenderer* renderer = getOglRenderer(img);
+    renderer->readRasterBuffer(&data);        
     return msSaveRasterBuffer(&data,fp,img->format );
 }
 
@@ -69,7 +70,7 @@
 	return MS_SUCCESS;
 }
 
-int msRenderPixmapOgl(imageObj *img, double x, double y,
+int msRenderPixmapOgl(imageObj *img, double x, double y,
         	symbolObj *symbol, symbolStyleObj *style)
 {
 	OglRenderer* renderer = getOglRenderer(img);
@@ -78,7 +79,7 @@
 }
 
 int msRenderVectorSymbolOgl
-	(imageObj *img, double x, double y,
+	(imageObj *img, double x, double y,
     		symbolObj *symbol, symbolStyleObj *style)
 {
 	OglRenderer* renderer = getOglRenderer(img);
@@ -104,11 +105,17 @@
 
 int msGetTruetypeTextBBoxOgl(rendererVTableObj *renderer, char *font, double size, char *string, rectObj *rect, double **advances)
 {	
-	OglRenderer::getStringBBox(font, size, string, rect, advances);
-	return MS_SUCCESS;
+	if (OglRenderer::getStringBBox(font, size, string, rect, advances))
+	{
+		return MS_SUCCESS;
+	}
+	else
+	{
+		return MS_FAILURE;
+	}
 }
 
-int msRenderGlyphsOgl(imageObj *img, double x, double y,
+int msRenderGlyphsOgl(imageObj *img, double x, double y,
             labelStyleObj *style, char *text)		
 {
 	OglRenderer* renderer = getOglRenderer(img);
@@ -138,7 +145,7 @@
 	return createImageObjOgl(new OglRenderer(width, height, bg));
 }
 
-int msRenderEllipseOgl(imageObj *image, double x, double y, 
+int msRenderEllipseOgl(imageObj *image, double x, double y, 
     		symbolObj *symbol, symbolStyleObj *style)		
 {
 	

Modified: trunk/mapserver/mapoglcontext.cpp
===================================================================
--- trunk/mapserver/mapoglcontext.cpp	2011-04-06 14:46:30 UTC (rev 11478)
+++ trunk/mapserver/mapoglcontext.cpp	2011-04-06 16:47:46 UTC (rev 11479)
@@ -20,14 +20,54 @@
 OglContext* OglContext::current = NULL;
 
 OglContext::OglContext(ms_uint32 width, ms_uint32 height)
-	: width(width), height(height)
+	: valid(false)
 {
-	if (!window) initWindow();
-	if (!sharingContext) initSharingContext();
-	createPBuffer(width, height);
-	makeCurrent();
+	if (!window && !initWindow()) return;
+	if (!sharingContext && !initSharingContext()) return;
+
+	if (!(this->width = getTextureSize(GL_TEXTURE_WIDTH, width))) return;
+	if (!(this->height = getTextureSize(GL_TEXTURE_HEIGHT, height))) return;
+
+	if (!createPBuffer(this->width, this->height)) return;
+	if (!makeCurrent()) return;	
+	valid = true;
 }
 
+ms_uint32 OglContext::getTextureSize(GLuint dimension, ms_uint32 value)
+{
+	glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA,  value, value, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+    GLint check = 0;
+    glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, dimension, &check);
+
+    if (glGetError() != GL_NO_ERROR)
+    {
+    	msSetError(MS_OGLERR, "glGetTexLevelParameteriv failed. glError: %d", "OglContext::getTextureSize()", glGetError());
+    }
+
+    if (check == 0)
+    {
+    	return MS_MAX(MS_MIN(NextPowerOf2(value), OglContext::MAX_TEXTURE_SIZE), OglContext::MIN_TEXTURE_SIZE);
+    }
+    else
+    {
+    	msSetError(MS_OGLERR, "Unable to create opengl texture of map size", "OglContext::getTextureSize()");
+    	return value;
+    }
+}
+
+GLuint OglContext::NextPowerOf2(GLuint in)
+{
+	in -= 1;
+
+	in |= in >> 16;
+	in |= in >> 8;
+	in |= in >> 4;
+	in |= in >> 2;
+	in |= in >> 1;
+
+	return in + 1;
+}
+
 #if defined(_WIN32) && !defined(__CYGWIN__)
 
 HDC OglContext::window = NULL;
@@ -137,19 +177,19 @@
 
 	if (!(window=GetDC(hWnd)))
 	{
-		msSetError(MS_OGLERR, "Can't Create A GL Device Context.", "OglContext::initWindow()");
+		msSetError(MS_OGLERR, "Can't Create A GL Device Context.. Last Error: %d", "OglContext::initWindow()", GetLastError());
 		return FALSE;
 	}
 
 	if (!(windowPixelFormat=ChoosePixelFormat(window,&pfd)))
 	{
-		msSetError(MS_OGLERR, "Can't Find A Suitable windowPixelFormat.", "OglContext::initWindow()");
+		msSetError(MS_OGLERR, "Can't Find A Suitable windowPixelFormat. Last Error: %d", "OglContext::initWindow()", GetLastError());
 		return FALSE;
 	}
 
 	if(!SetPixelFormat(window,windowPixelFormat,&pfd))
 	{
-		msSetError(MS_OGLERR, "Can't Set The windowPixelFormat.", "OglContext::initWindow()");
+		msSetError(MS_OGLERR, "Can't Set The windowPixelFormat. Last Error: %d", "OglContext::initWindow()", GetLastError());
 		return FALSE;
 	}
 	return TRUE;
@@ -159,39 +199,46 @@
 {
 	if (!(sharingContext=wglCreateContext(window)))
 	{
-		msSetError(MS_OGLERR, "Can't Create A GL Rendering Context.", "OglContext::createContext()");
+		msSetError(MS_OGLERR, "Can't Create A GL Rendering Context. glError: %d", "OglContext::createContext()", glGetError());
 		return FALSE;
 	}
 
 	if(!wglMakeCurrent(window,sharingContext))
 	{
-		msSetError(MS_OGLERR, "Can't Activate The GL Rendering Context.", "OglContext::createContext()");
+		msSetError(MS_OGLERR, "Can't Activate The GL Rendering Context. glError: %d", "OglContext::createContext()", glGetError());
 		return FALSE;
 	}
 
-	wglMakeContextCurrentARB = (PFNWGLMAKECONTEXTCURRENTARBPROC)wglGetProcAddress("wglMakeContextCurrentARB");
-	wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
-	wglGetPixelFormatAttribivARB = (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB");
-	wglGetPixelFormatAttribfvARB = (PFNWGLGETPIXELFORMATATTRIBFVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribfvARB");
-	wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
-	wglCreatePbufferARB = (PFNWGLCREATEPBUFFERARBPROC)wglGetProcAddress("wglCreatePbufferARB");
-	wglGetPbufferDCARB = (PFNWGLGETPBUFFERDCARBPROC)wglGetProcAddress("wglGetPbufferDCARB");
-	wglReleasePbufferDCARB = (PFNWGLRELEASEPBUFFERDCARBPROC)wglGetProcAddress("wglReleasePbufferDCARB");
-	wglDestroyPbufferARB = (PFNWGLDESTROYPBUFFERARBPROC)wglGetProcAddress("wglDestroyPbufferARB");
-	wglQueryPbufferARB = (PFNWGLQUERYPBUFFERARBPROC)wglGetProcAddress("wglQueryPbufferARB");
-	wglBindTexImageARB = (PFNWGLBINDTEXIMAGEARBPROC)wglGetProcAddress("wglBindTexImageARB");
-	wglReleaseTexImageARB = (PFNWGLRELEASETEXIMAGEARBPROC)wglGetProcAddress("wglReleaseTexImageARB");
-	wglGetPixelFormatAttribivEXT = (PFNWGLGETPIXELFORMATATTRIBIVEXTPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB");
+	if (!(wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB")))
+	{
+		msSetError(MS_OGLERR, "unable to retreive wglChoosePixelFormatARB method.", "OglContext::createContext()");
+		return FALSE;
+	}
+	
+	if (!(wglCreatePbufferARB = (PFNWGLCREATEPBUFFERARBPROC)wglGetProcAddress("wglCreatePbufferARB")))
+	{
+		msSetError(MS_OGLERR, "unable to retreive wglCreatePbufferARB method.", "OglContext::createContext()");
+		return FALSE;
+	}
+	
+	if (!(wglGetPbufferDCARB = (PFNWGLGETPBUFFERDCARBPROC)wglGetProcAddress("wglGetPbufferDCARB")))
+	{
+		msSetError(MS_OGLERR, "unable to retreive wglGetPbufferDCARB method.", "OglContext::createContext()");
+		return FALSE;
+	}
+	
+	if (!(wglReleasePbufferDCARB = (PFNWGLRELEASEPBUFFERDCARBPROC)wglGetProcAddress("wglReleasePbufferDCARB")))
+	{
+		msSetError(MS_OGLERR, "unable to retreive wglReleasePbufferDCARB method.", "OglContext::createContext()");
+		return FALSE;
+	}
+	
+	if (!(wglMakeContextCurrentARB = (PFNWGLMAKECONTEXTCURRENTARBPROC)wglGetProcAddress("wglMakeContextCurrentARB")))
+	{
+		msSetError(MS_OGLERR, "unable to retreive wglMakeContextCurrentARB method.", "OglContext::createContext()");
+		return FALSE;
+	}
 
-	wglGenBuffersARB = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffersARB");
-	wglBindBufferARB = (PFNGLBINDBUFFERARBPROC)wglGetProcAddress("glBindBufferARB");
-	wglBufferDataARB = (PFNGLBUFFERDATAARBPROC)wglGetProcAddress("glBufferDataARB");
-	wglBufferSubDataARB = (PFNGLBUFFERSUBDATAARBPROC)wglGetProcAddress("glBufferSubDataARB");
-	wglDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)wglGetProcAddress("glDeleteBuffersARB");
-	wglGetBufferParameterivARB = (PFNGLGETBUFFERPARAMETERIVARBPROC)wglGetProcAddress("glGetBufferParameterivARB");
-	wglMapBufferARB = (PFNGLMAPBUFFERARBPROC)wglGetProcAddress("glMapBufferARB");
-	wglUnmapBufferARB = (PFNGLUNMAPBUFFERARBPROC)wglGetProcAddress("glUnmapBufferARB");
-
 	glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, (GLint*) &MAX_ANISOTROPY);
 	glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&MAX_TEXTURE_SIZE);
 
@@ -242,29 +289,35 @@
 
 	if (!(hPBuffer = wglCreatePbufferARB(window, pixelFormat, width, height, 0)))
 	{
-		msSetError(MS_OGLERR, "P-buffer Error: Unable to create P-buffer.", "OglContext::createPBuffer()");
+		msSetError(MS_OGLERR, "P-buffer Error: Unable to create P-buffer. glError: %d", "OglContext::createPBuffer()", glGetError());
 		return FALSE;
 	}
 	if (!(hPBufferDC = wglGetPbufferDCARB(hPBuffer)))
 	{
-		msSetError(MS_OGLERR, "P-buffer Error: Unable to get P-buffer DC.", "OglContext::createPBuffer()");
+		msSetError(MS_OGLERR, "P-buffer Error: Unable to get P-buffer DC. glError: %d", "OglContext::createPBuffer()", glGetError());
 		return FALSE;
 	}
 	if (!(hPBufferRC = wglCreateContext(hPBufferDC)))
 	{
-		msSetError(MS_OGLERR, "P-buffer Error: Unable to get P-buffer DC.", "OglContext::createPBuffer()");
+		msSetError(MS_OGLERR, "P-buffer Error: Unable to get P-buffer DC. glError: %d", "OglContext::createPBuffer()", glGetError());
 		return FALSE;
 	}
 
 	if (wglShareLists(sharingContext,hPBufferRC) == FALSE)
 	{
-		msSetError(MS_OGLERR, "P-buffer Error: Unable to share display lists.", "OglContext::createPBuffer()");
+		msSetError(MS_OGLERR, "P-buffer Error: Unable to share display lists. glError: %d", "OglContext::createPBuffer()", glGetError());
 		return FALSE;
 	}
 
 	return TRUE;
 }
 
+PFNWGLCHOOSEPIXELFORMATARBPROC OglContext::wglChoosePixelFormatARB = NULL;
+PFNWGLCREATEPBUFFERARBPROC OglContext::wglCreatePbufferARB = NULL;
+PFNWGLGETPBUFFERDCARBPROC OglContext::wglGetPbufferDCARB = NULL;
+PFNWGLRELEASEPBUFFERDCARBPROC OglContext::wglReleasePbufferDCARB = NULL;
+PFNWGLMAKECONTEXTCURRENTARBPROC OglContext::wglMakeContextCurrentARB = NULL;
+
 #else /* UNIX */
 
 Display* OglContext::window = NULL;
@@ -282,7 +335,11 @@
 	if (current != this)
 	{
 		current = this;
-		glXMakeCurrent(window, pbuffer, sharingContext);
+		if (!glXMakeCurrent(window, pbuffer, sharingContext))
+		{
+			msSetError(MS_OGLERR, "glXMakeCurrent failed. glError: %d", "OglContext::makeCurrent()", glGetError());
+			return false;
+		}
 	}
 	return true;
 }
@@ -313,14 +370,14 @@
 
 	if (configs == NULL || num_configs == 0)
 	{
-		msSetError(MS_OGLERR, "glXChooseFBConfig could not find any configs.", "OglContext::init()");
+		msSetError(MS_OGLERR, "glXChooseFBConfig could not find any configs. Likely your video card or drivers are not supported. glError: %d", "OglContext::init()", glGetError());
 		return false;
 	}
 
 	sharingContext = glXCreateNewContext(window, *configs, GLX_RGBA_TYPE, NULL, 1);
 	if (sharingContext == NULL)
 	{
-		msSetError(MS_OGLERR, "glXCreateNewContext failed.", "OglContext::initSharingContext()");
+		msSetError(MS_OGLERR, "glXCreateNewContext failed. glError: %d", "OglContext::initSharingContext()", glGetError());
 		return false;
 	}
 
@@ -350,7 +407,7 @@
 	pbuffer = glXCreatePbuffer(window, *configs, iPbufferAttributes);
 	if (pbuffer == 0)
 	{
-		msSetError(MS_OGLERR, "glXCreatePbuffer failed.", "OglContext::init()");
+		msSetError(MS_OGLERR, "glXCreatePbuffer failed. glError: %d", "OglContext::init()", glGetError());
 		return false;
 	}
 
@@ -386,14 +443,14 @@
 
 	if (tempConfigs == NULL || num_configs == 0)
 	{
-		msSetError(MS_OGLERR, "glXChooseFBConfig could not find any configs.", "OglContext::initWindow()");
+		msSetError(MS_OGLERR, "glXChooseFBConfig could not find any configs. Likely your video card or drivers are not supported. glError: %d", "OglContext::initWindow()", glGetError());
 		return false;
 	}
 
 	GLXContext tempContext = glXCreateNewContext(window, *tempConfigs, GLX_RGBA_TYPE, NULL, 1);
 	if (tempContext == NULL)
 	{
-		msSetError(MS_OGLERR, "glXCreateNewContext failed.", "OglContext::initWindow()");
+		msSetError(MS_OGLERR, "glXCreateNewContext failed. glError: %d", "OglContext::initWindow()", glGetError());
 		return false;
 	}
 
@@ -402,11 +459,15 @@
 	GLXPbuffer tempBuffer = glXCreatePbuffer(window, *tempConfigs, iPbufferAttributes);
 	if (tempBuffer == 0)
 	{
-		msSetError(MS_OGLERR, "glXCreatePbuffer failed.", "OglContext::initWindow()");
+		msSetError(MS_OGLERR, "glXCreatePbuffer failed. glError: %d", "OglContext::initWindow()", glGetError());
 		return false;
 	}
 
-	glXMakeCurrent(window, tempBuffer, tempContext);
+	if (!glXMakeCurrent(window, tempBuffer, tempContext))
+	{
+		msSetError(MS_OGLERR, "glXMakeCurrent failed. glError: %d", "OglContext::initWindow()", glGetError());
+		return false;
+	}
 
 	glGetIntegerv(GL_MAX_SAMPLES_EXT, (GLint*)&MAX_MULTISAMPLE_SAMPLES);
 	glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, (GLint*) &MAX_ANISOTROPY);
@@ -419,262 +480,4 @@
 
 #endif /* UNIX */
 
-#if defined(_WIN32) && !defined(__CYGWIN__)
-
-PFNWGLGETEXTENSIONSSTRINGARBPROC OglContext::wglGetExtensionsStringARB = NULL;
-PFNWGLGETPIXELFORMATATTRIBIVARBPROC OglContext::wglGetPixelFormatAttribivARB = NULL;
-PFNWGLGETPIXELFORMATATTRIBFVARBPROC OglContext::wglGetPixelFormatAttribfvARB = NULL;
-PFNWGLCHOOSEPIXELFORMATARBPROC OglContext::wglChoosePixelFormatARB = NULL;
-PFNWGLCREATEPBUFFERARBPROC OglContext::wglCreatePbufferARB = NULL;
-PFNWGLGETPBUFFERDCARBPROC OglContext::wglGetPbufferDCARB = NULL;
-PFNWGLRELEASEPBUFFERDCARBPROC OglContext::wglReleasePbufferDCARB = NULL;
-PFNWGLDESTROYPBUFFERARBPROC OglContext::wglDestroyPbufferARB = NULL;
-PFNWGLQUERYPBUFFERARBPROC OglContext::wglQueryPbufferARB = NULL;
-PFNWGLBINDTEXIMAGEARBPROC OglContext::wglBindTexImageARB = NULL;
-PFNWGLRELEASETEXIMAGEARBPROC OglContext::wglReleaseTexImageARB = NULL;
-PFNWGLMAKECONTEXTCURRENTARBPROC OglContext::wglMakeContextCurrentARB = NULL;
-PFNWGLGETPIXELFORMATATTRIBIVEXTPROC OglContext::wglGetPixelFormatAttribivEXT = NULL;
-
-PFNGLGENBUFFERSARBPROC OglContext::wglGenBuffersARB = 0; // VBO Name Generation Procedure
-PFNGLBINDBUFFERARBPROC OglContext::wglBindBufferARB = 0; // VBO Bind Procedure
-PFNGLBUFFERDATAARBPROC OglContext::wglBufferDataARB = 0; // VBO Data Loading Procedure
-PFNGLBUFFERSUBDATAARBPROC OglContext::wglBufferSubDataARB = 0; // VBO Sub Data Loading Procedure
-PFNGLDELETEBUFFERSARBPROC OglContext::wglDeleteBuffersARB = 0; // VBO Deletion Procedure
-PFNGLGETBUFFERPARAMETERIVARBPROC OglContext::wglGetBufferParameterivARB = 0; // return various parameters of VBO
-PFNGLMAPBUFFERARBPROC OglContext::wglMapBufferARB = 0; // map VBO procedure
-PFNGLUNMAPBUFFERARBPROC OglContext::wglUnmapBufferARB = 0; // unmap VBO procedure
-
-#endif
-
-/*
-HDC OglContext::hWINDOWDC = NULL;
-HGLRC OglContext::hWINDOWRC = NULL;
-int OglContext::windowPixelFormat = 0;
-
-HPBUFFERARB OglContext::hPBuffer = NULL;
-HDC OglContext::hPBufferDC = NULL;
-HGLRC OglContext::hPBufferRC = NULL;
-
-// P-Buffer for texture rendering.
-HPBUFFERARB OglContext::hTexPBuffer = NULL;
-HDC OglContext::hTexPBufferDC = NULL;
-HGLRC OglContext::hTexPBufferRC = NULL;
-
-bool OglContext::init()
-{
-	WNDCLASS wc;
-	DWORD dwExStyle;
-	DWORD dwStyle;
-	RECT WindowRect;
-	HMODULE hInstance = NULL;
-	HWND hWnd = NULL;
-	WindowRect.left=(long)0;
-	WindowRect.right=(long)0;
-	WindowRect.top=(long)0;
-	WindowRect.bottom=(long)0;
-	hInstance = GetModuleHandle(NULL);
-	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
-	wc.cbClsExtra = 0;
-	wc.cbWndExtra = 0;
-	wc.lpfnWndProc = (WNDPROC) WndProc;
-	wc.hInstance = hInstance;
-	wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
-	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
-	wc.hbrBackground = NULL;
-	wc.lpszMenuName = NULL;
-	wc.lpszClassName = L"OpenGL";
-
-	if (!RegisterClass(&wc))
-	{
-		printError("Failed To Register The Window Class.");
-		return FALSE;
-	}
-	dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
-	dwStyle=WS_OVERLAPPEDWINDOW;
-
-	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
-
-	if (!(hWnd=CreateWindowEx( dwExStyle,
-							L"OpenGL",
-							L"temp",
-							dwStyle |
-							WS_CLIPSIBLINGS |
-							WS_CLIPCHILDREN,
-							0, 0,
-							WindowRect.right-WindowRect.left,
-							WindowRect.bottom-WindowRect.top,
-							NULL,
-							NULL,
-							hInstance,
-							NULL)))
-	{
-		printError("Window Creation Error.");
-		return FALSE;
-	}
-
-	static PIXELFORMATDESCRIPTOR pfd=
-	{
-		sizeof(PIXELFORMATDESCRIPTOR),
-		1,
-		PFD_DRAW_TO_WINDOW |
-		PFD_SUPPORT_OPENGL |
-		PFD_DOUBLEBUFFER,
-		PFD_TYPE_RGBA,
-		24, // Select Our Color Depth
-		0, 0, 0, 0, 0, 0, // Color Bits Ignored
-		8, // Alpha Buffer
-		0, // Shift Bit Ignored
-		0, // No Accumulation Buffer
-		0, 0, 0, 0, // Accumulation Bits Ignored
-		16, // 16Bit Z-Buffer (Depth Buffer)
-		0, // No Stencil Buffer
-		0, // No Auxiliary Buffer
-		PFD_MAIN_PLANE, // Main Drawing Layer
-		0, // Reserved
-		0, 0, 0 // Layer Masks Ignored
-	};
-
-	if (!(hWINDOWDC=GetDC(hWnd)))
-	{
-		printError("Can't Create A GL Device Context.");
-		return FALSE;
-	}
-
-	if (!(windowPixelFormat=ChoosePixelFormat(hWINDOWDC,&pfd)))
-	{
-		printError("Can't Find A Suitable windowPixelFormat.");
-		return FALSE;
-	}
-
-	if(!SetPixelFormat(hWINDOWDC,windowPixelFormat,&pfd))
-	{
-		printError("Can't Set The windowPixelFormat.");
-		return FALSE;
-	}
-
-	if (!(hWINDOWRC=wglCreateContext(hWINDOWDC)))
-	{
-		printError("Can't Create A GL Rendering Context.");
-		return FALSE;
-	}
-
-	if(!wglMakeCurrent(hWINDOWDC,hWINDOWRC))
-	{
-		printError("Can't Activate The GL Rendering Context.");
-		return FALSE;
-	}
-	wglMakeContextCurrentARB = (PFNWGLMAKECONTEXTCURRENTARBPROC)wglGetProcAddress("wglMakeContextCurrentARB");
-	wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
-	wglGetPixelFormatAttribivARB = (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB");
-	wglGetPixelFormatAttribfvARB = (PFNWGLGETPIXELFORMATATTRIBFVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribfvARB");
-	wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
-	wglCreatePbufferARB = (PFNWGLCREATEPBUFFERARBPROC)wglGetProcAddress("wglCreatePbufferARB");
-	wglGetPbufferDCARB = (PFNWGLGETPBUFFERDCARBPROC)wglGetProcAddress("wglGetPbufferDCARB");
-	wglReleasePbufferDCARB = (PFNWGLRELEASEPBUFFERDCARBPROC)wglGetProcAddress("wglReleasePbufferDCARB");
-	wglDestroyPbufferARB = (PFNWGLDESTROYPBUFFERARBPROC)wglGetProcAddress("wglDestroyPbufferARB");
-	wglQueryPbufferARB = (PFNWGLQUERYPBUFFERARBPROC)wglGetProcAddress("wglQueryPbufferARB");
-	wglBindTexImageARB = (PFNWGLBINDTEXIMAGEARBPROC)wglGetProcAddress("wglBindTexImageARB");
-	wglReleaseTexImageARB = (PFNWGLRELEASETEXIMAGEARBPROC)wglGetProcAddress("wglReleaseTexImageARB");
-	wglGetPixelFormatAttribivEXT = (PFNWGLGETPIXELFORMATATTRIBIVEXTPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB");
-
-	wglGenBuffersARB = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffersARB");
-	wglBindBufferARB = (PFNGLBINDBUFFERARBPROC)wglGetProcAddress("glBindBufferARB");
-	wglBufferDataARB = (PFNGLBUFFERDATAARBPROC)wglGetProcAddress("glBufferDataARB");
-	wglBufferSubDataARB = (PFNGLBUFFERSUBDATAARBPROC)wglGetProcAddress("glBufferSubDataARB");
-	wglDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)wglGetProcAddress("glDeleteBuffersARB");
-	wglGetBufferParameterivARB = (PFNGLGETBUFFERPARAMETERIVARBPROC)wglGetProcAddress("glGetBufferParameterivARB");
-	wglMapBufferARB = (PFNGLMAPBUFFERARBPROC)wglGetProcAddress("glMapBufferARB");
-	wglUnmapBufferARB = (PFNGLUNMAPBUFFERARBPROC)wglGetProcAddress("glUnmapBufferARB");
-
-	loadShapes();
-
-	hPBufferRC = NULL;
-	hPBufferDC = NULL;
-	hPBuffer = NULL;
-	int pixelFormat;
-	int valid;
-	UINT numFormats;
-	float fAttributes[] =
-	{	0,0};
-	int iAttributes[] =
-	{
-		WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
-		WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
-		WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
-		WGL_COLOR_BITS_ARB,24,
-		WGL_ALPHA_BITS_ARB,8,
-		WGL_DEPTH_BITS_ARB,16,
-		WGL_STENCIL_BITS_ARB,0,
-		WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
-		WGL_SAMPLES_ARB,MUTLISAMPLE_SAMPLES,
-		WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
-		0,0
-	};
-
-	valid = wglChoosePixelFormatARB(hWINDOWDC,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
-	if(numFormats == 0)
-	{
-		printError("P-buffer Error: Unable to find an acceptable pixel format");
-		return FALSE;
-	}
-
-	if (!(hPBuffer = wglCreatePbufferARB(hWINDOWDC, pixelFormat, MAX_WIDTH, MAX_HEIGHT, 0)))
-	{
-		printError("P-buffer Error: Unable to create P-buffer");
-		return FALSE;
-	}
-	if (!(hPBufferDC = wglGetPbufferDCARB(hPBuffer)))
-	{
-		printError("P-buffer Error: Unable to get P-buffer DC");
-		return FALSE;
-	}
-	if (!(hPBufferRC = wglCreateContext(hPBufferDC)))
-	{
-		printError("P-buffer Error: Unable to get P-buffer DC");
-		return FALSE;
-	}
-
-	if (wglShareLists(hWINDOWRC,hPBufferRC) == FALSE)
-	{
-		printError("P-buffer Error: Unable to share display lists");
-		return FALSE;
-	}
-
-	initialised = true;
-}
-
-PFNWGLGETEXTENSIONSSTRINGARBPROC OglContext::wglGetExtensionsStringARB = NULL;
-PFNWGLGETPIXELFORMATATTRIBIVARBPROC OglContext::wglGetPixelFormatAttribivARB = NULL;
-PFNWGLGETPIXELFORMATATTRIBFVARBPROC OglContext::wglGetPixelFormatAttribfvARB = NULL;
-PFNWGLCHOOSEPIXELFORMATARBPROC OglContext::wglChoosePixelFormatARB = NULL;
-PFNWGLCREATEPBUFFERARBPROC OglContext::wglCreatePbufferARB = NULL;
-PFNWGLGETPBUFFERDCARBPROC OglContext::wglGetPbufferDCARB = NULL;
-PFNWGLRELEASEPBUFFERDCARBPROC OglContext::wglReleasePbufferDCARB = NULL;
-PFNWGLDESTROYPBUFFERARBPROC OglContext::wglDestroyPbufferARB = NULL;
-PFNWGLQUERYPBUFFERARBPROC OglContext::wglQueryPbufferARB = NULL;
-PFNWGLBINDTEXIMAGEARBPROC OglContext::wglBindTexImageARB = NULL;
-PFNWGLRELEASETEXIMAGEARBPROC OglContext::wglReleaseTexImageARB = NULL;
-PFNWGLMAKECONTEXTCURRENTARBPROC OglContext::wglMakeContextCurrentARB = NULL;
-PFNWGLGETPIXELFORMATATTRIBIVEXTPROC OglContext::wglGetPixelFormatAttribivEXT = NULL;
-
-PFNGLGENBUFFERSARBPROC OglContext::wglGenBuffersARB = 0; // VBO Name Generation Procedure
-PFNGLBINDBUFFERARBPROC OglContext::wglBindBufferARB = 0; // VBO Bind Procedure
-PFNGLBUFFERDATAARBPROC OglRenderer::wglBufferDataARB = 0; // VBO Data Loading Procedure
-PFNGLBUFFERSUBDATAARBPROC OglContext::wglBufferSubDataARB = 0; // VBO Sub Data Loading Procedure
-PFNGLDELETEBUFFERSARBPROC OglContext::wglDeleteBuffersARB = 0; // VBO Deletion Procedure
-PFNGLGETBUFFERPARAMETERIVARBPROC OglContext::wglGetBufferParameterivARB = 0; // return various parameters of VBO
-PFNGLMAPBUFFERARBPROC OglContext::wglMapBufferARB = 0; // map VBO procedure
-PFNGLUNMAPBUFFERARBPROC OglContext::wglUnmapBufferARB = 0; // unmap VBO procedure
-
-LRESULT CALLBACK WndProc(HWND hWnd,
-		UINT message,
-		WPARAM wParam,
-		LPARAM lParam)
-{
-	return(1L);
-}
-
-#endif
-*/
-
 #endif /* USE_OGL */

Modified: trunk/mapserver/mapoglcontext.h
===================================================================
--- trunk/mapserver/mapoglcontext.h	2011-04-06 14:46:30 UTC (rev 11478)
+++ trunk/mapserver/mapoglcontext.h	2011-04-06 16:47:46 UTC (rev 11479)
@@ -17,14 +17,14 @@
 #include<GL/gl.h>
 #include<GL/glu.h>
 
-#include "opengl/glext.h"
-#include "opengl/wglext.h"
-
-typedef HDC OGL_WINDOW;
-typedef HGLRC OGL_CONTEXT;
+#include "opengl/glext.h"
+#include "opengl/wglext.h"
+
+typedef HDC OGL_WINDOW;
+typedef HGLRC OGL_CONTEXT;
 typedef HGLRC OGL_PBUFFER;
 
-#define CALLBACK __stdcall
+#define CALLBACK __stdcall
 
 #else
 
@@ -47,11 +47,13 @@
 	static OglContext* current;
 	static bool initWindow();
 	static bool initSharingContext();
+	static ms_uint32 getTextureSize(GLuint dimension, ms_uint32 value);
+	static GLuint NextPowerOf2(GLuint in);
 
 #if defined(_WIN32) && !defined(__CYGWIN__)
 	static HDC window;
 	static HGLRC sharingContext;
-	HDC hPBufferDC;
+	HDC hPBufferDC;
 	HGLRC hPBufferRC;
 #else
     static Display* window;
@@ -68,40 +70,24 @@
 
 	OglContext(ms_uint32 width, ms_uint32 height);
 	~OglContext();
-	int getHeight() { return height; }
-	int getWidth() { return width; }
+	int getHeight() const { return height; }
+	int getWidth() const { return width; }
+	bool isValid() const { return valid; }
 	bool makeCurrent();
 	void bindPBufferToTexture();
 private:
 	bool createPBuffer(ms_uint32 width, ms_uint32 height);
 	ms_uint32 width;
 	ms_uint32 height;
-	int windowPixelFormat;
+	bool valid;
+	int windowPixelFormat;	
 
 #if defined(_WIN32) && !defined(__CYGWIN__)
-	static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB ;
-	static PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB ;
-	static PFNWGLGETPIXELFORMATATTRIBFVARBPROC wglGetPixelFormatAttribfvARB ;
-	static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB ;
-	static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB ;
-	static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB ;
-	static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB ;
-	static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB ;
-	static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB ;
-	static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB ;
-	static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB ;
-	static PFNWGLMAKECONTEXTCURRENTARBPROC wglMakeContextCurrentARB;
-	static PFNWGLGETPIXELFORMATATTRIBIVEXTPROC wglGetPixelFormatAttribivEXT;
-
-	static PFNGLGENBUFFERSARBPROC wglGenBuffersARB;                     // VBO Name Generation Procedure
-	static PFNGLBINDBUFFERARBPROC wglBindBufferARB;                     // VBO Bind Procedure
-	static PFNGLBUFFERDATAARBPROC wglBufferDataARB;                     // VBO Data Loading Procedure
-	static PFNGLBUFFERSUBDATAARBPROC wglBufferSubDataARB;               // VBO Sub Data Loading Procedure
-	static PFNGLDELETEBUFFERSARBPROC wglDeleteBuffersARB;               // VBO Deletion Procedure
-	static PFNGLGETBUFFERPARAMETERIVARBPROC wglGetBufferParameterivARB; // return various parameters of VBO
-	static PFNGLMAPBUFFERARBPROC wglMapBufferARB;                       // map VBO procedure
-	static PFNGLUNMAPBUFFERARBPROC wglUnmapBufferARB;                   // unmap VBO procedure
-
+	static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
+	static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB;
+	static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB;
+	static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB;
+	static PFNWGLMAKECONTEXTCURRENTARBPROC wglMakeContextCurrentARB;
 #endif
 };
 

Modified: trunk/mapserver/mapoglrenderer.cpp
===================================================================
--- trunk/mapserver/mapoglrenderer.cpp	2011-04-06 14:46:30 UTC (rev 11478)
+++ trunk/mapserver/mapoglrenderer.cpp	2011-04-06 16:47:46 UTC (rev 11479)
@@ -28,8 +28,11 @@
 GLvoid CALLBACK vertexCallback(GLdouble *vertex);
 
 OglRenderer::OglRenderer(ms_uint32 width, ms_uint32 height, colorObj* color)
+	: width(width), height(height), texture(NULL), transparency(1.0), valid(false)
 {	
-	this->texture = NULL;
+	context = new OglContext(width, height);
+	if (!context->isValid()) return;
+	if (!context->makeCurrent()) return;
 
 	int viewPort[4];
 	glGetIntegerv(GL_VIEWPORT ,viewPort);
@@ -40,12 +43,6 @@
 
 	glPushMatrix();
 
-	this->width = width;
-	this->height = height;
-	context = new OglContext(getTextureSize(GL_TEXTURE_WIDTH, width), getTextureSize(GL_TEXTURE_HEIGHT, height));
-	context->makeCurrent();
-	transparency = 1.0;
-
 	// set read/write context to pbuffer
 	glEnable(GL_MULTISAMPLE_ARB);
 	glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
@@ -66,6 +63,7 @@
 	{
 		msSetError(MS_OGLERR, "P-buffer Error: Unable to create tessalotor",
 				"OglRenderer");
+		return;
 	}
 
 	gluTessCallback(tess, GLU_TESS_VERTEX, (void (CALLBACK*)(void)) vertexCallback);
@@ -93,6 +91,7 @@
 	glOrtho(0.0, width, 0.0, height, -3.0, 3.0);
 
 	createShapes();
+	valid = true;
 }
 
 OglRenderer::~OglRenderer()
@@ -135,25 +134,14 @@
 	return texture;
 }
 
-ms_uint32 OglRenderer::getTextureSize(GLuint dimension, ms_uint32 value)
-{
-	glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA,  value, value, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
-    GLint check;
-    glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, dimension, &check);
-    if (check == 0)
-    {
-    	return MS_MAX(MS_MIN(NextPowerOf2(value), OglContext::MAX_TEXTURE_SIZE), OglContext::MIN_TEXTURE_SIZE);
-    }
-    else
-    {
-    	return value;
-    }
-}
-
-void OglRenderer::getStringBBox(char *font, double size, char *string, rectObj *rect, double** advances)
+bool OglRenderer::getStringBBox(char *font, double size, char *string, rectObj *rect, double** advances)
 {	
 	FTFont* face = getFTFont(font, size);
-	if (!face) return;
+	if (!face) 
+	{
+		msSetError(MS_OGLERR, "Failed to load font (%s).", "OglRenderer::getStringBBox()", font);
+		return false;
+	}
 	
 	float llx =0.0f, lly=0.0f, llz=0.0f, urx=0.0f, ury=0.0f, urz=0.0f;
 	glPushAttrib( GL_ALL_ATTRIB_BITS );
@@ -174,6 +162,8 @@
 			(*advances)[i] = face->Advance(&string[i], 1);
 		}
 	}
+
+	return true;
 }
 
 void OglRenderer::initializeRasterBuffer(rasterBufferObj * rb, int width, int height, bool useAlpha) 
@@ -563,7 +553,11 @@
 {
 	makeCurrent();	
 	FTFont* face = getFTFont(font, size);
-	if (!face) return;
+	if (!face) 
+	{
+		msSetError(MS_OGLERR, "Failed to load font (%s).", "OglRenderer::renderGlyphs()", font);
+		return;
+	}
 
 	glPushAttrib(GL_ALL_ATTRIB_BITS);
 	glPushMatrix();
@@ -715,25 +709,15 @@
 	if (*face == NULL && ifstream(font))
 	{
 		*face = new FTGLTextureFont( font );
-		(*face)->UseDisplayList(true);
-		(*face)->FaceSize(size*SIZE_RES);		
+		if (*face)
+		{
+			(*face)->UseDisplayList(true);
+			(*face)->FaceSize(size*SIZE_RES);		
+		}
 	}	
 	return *face;
 }
 
-GLuint OglRenderer::NextPowerOf2(GLuint in)
-{
-	in -= 1;
-
-	in |= in >> 16;
-	in |= in >> 8;
-	in |= in >> 4;
-	in |= in >> 2;
-	in |= in >> 1;
-
-	return in + 1;
-}
-
 GLvoid CALLBACK beginCallback(GLenum which)
 {
 	glBegin(which);

Modified: trunk/mapserver/mapoglrenderer.h
===================================================================
--- trunk/mapserver/mapoglrenderer.h	2011-04-06 14:46:30 UTC (rev 11478)
+++ trunk/mapserver/mapoglrenderer.h	2011-04-06 16:47:46 UTC (rev 11479)
@@ -10,7 +10,7 @@
 #include <map>
 #include <memory>
 
-#include <FTGL/ftgl.h>
+#include <FTGL/ftgl.h>
 #include <FTGL/FTGLTextureFont.h>
 
 class OglCache
@@ -36,13 +36,13 @@
 	void renderPolyline(shapeObj *p, colorObj *c, double width, int patternlength, double* pattern, int lineCap = MS_CJC_ROUND, int joinStyle = MS_CJC_ROUND, colorObj *outlinecolor = NULL, double outlinewidth = 0);
 	void renderPolygon(shapeObj*, colorObj *color, colorObj *outlinecolor, double outlinewidth, const OglCachePtr& tile = OglCachePtr(), int lineCap=MS_CJC_ROUND, int joinStyle=MS_CJC_ROUND);
 	void renderGlyphs(double x, double y, colorObj *color, colorObj *outlinecolor, double size, char* font, char *thechars, double angle, colorObj *shadowcolor, double shdx, double shdy);
-    void renderPixmap(symbolObj *symbol, double x, double y, double angle, double scale);
+    void renderPixmap(symbolObj *symbol, double x, double y, double angle, double scale);
     void renderEllipse(double x, double y, double angle, double w, double h, colorObj *color, colorObj *outlinecolor, double outlinewidth);
     void renderVectorSymbol(double x, double y, symbolObj *symbol, double scale, double angle, colorObj *c, colorObj *oc, double ow);
-    void renderTile(const OglCachePtr& tile, double x, double y, double angle);
+    void renderTile(const OglCachePtr& tile, double x, double y, double angle);
     void renderPolylineTile(shapeObj *p, const OglCachePtr& tile);
 
-    static void getStringBBox(char *font, double size, char *string, rectObj *rect, double** advances);    
+    static bool getStringBBox(char *font, double size, char *string, rectObj *rect, double** advances);    
 	void setTransparency(double transparency);    
 	
 	void readRasterBuffer(rasterBufferObj * rb);
@@ -50,48 +50,47 @@
 	static void initializeRasterBuffer(rasterBufferObj * rb, int width, int height, bool useAlpha);	
 
 	OglCachePtr getTexture();
-    int getWidth() { return width; }
-    int getHeight() { return height; }
-protected:
+    int getWidth() const { return width; }
+    int getHeight() const { return height; }
+	bool isValid() const { return valid && context->isValid(); }
+protected:
     OglCachePtr texture;
 	
 	ms_uint32 width;
     ms_uint32 height;
 	double transparency;
+	bool valid;
 
 	GLint viewportX;
 	GLint viewportY;
 	GLsizei viewportWidth;
 	GLsizei viewportHeight;
 
-	typedef std::map<char*,std::map<double,FTFont*> > fontCache_t;
-    typedef std::map<symbolObj*,std::map<double,GLuint> > dashCache_t;
+	typedef std::map<char*,std::map<double,FTFont*> > fontCache_t;
+    typedef std::map<symbolObj*,std::map<double,GLuint> > dashCache_t;
 
     static FTFont* getFTFont(char* font, double size);
     bool loadLine(shapeObj* shape, double width, int patternlength, double *pattern);
-    double drawQuad(pointObj *p1, pointObj *p2, double width, double tilelength = 0.0, double textureStart = 0.0);
+    double drawQuad(pointObj *p1, pointObj *p2, double width, double tilelength = 0.0, double textureStart = 0.0);
     double drawTriangles(pointObj *p1, pointObj *p2, double width, double tilelength = 0.0, double textureStart = 0.0);
     void drawVectorLineStrip(symbolObj *symbol, double width);
-    void drawFan(pointObj* center, pointObj* from, pointObj* to, int resolution);
+    void drawFan(pointObj* center, pointObj* from, pointObj* to, int resolution);
     void createShapes();
 	
-	// texture functions	
-	ms_uint32 getTextureSize(GLuint dimension, ms_uint32 value);
-	GLuint NextPowerOf2(GLuint in);
 	GLuint createTexture(ms_uint32 x, ms_uint32 y);
 
 	void makeCurrent();
     void setColor(colorObj *color);
 
     GLUtesselator *tess;
-    enum shapes_t{ circle = 0};
+    enum shapes_t{ circle = 0};
 
     OglContext* context;
 
-    static dashCache_t dashCache;
-    static fontCache_t fontCache;
-    static std::vector<GLuint> shapes;
-    static std::vector<symbolObj*> testSymbols;
+    static dashCache_t dashCache;
+    static fontCache_t fontCache;
+    static std::vector<GLuint> shapes;
+    static std::vector<symbolObj*> testSymbols;
     static ms_uint32 OUTLINE_WIDTH;
 	static ms_uint32 FONT_SIZE;
 	static ms_uint32 FONT_RES;



More information about the mapserver-commits mailing list