1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-10-08 23:27:25 -04:00
glfw/src/opengl.c

450 lines
13 KiB
C
Raw Normal View History

2010-09-07 11:34:51 -04:00
//========================================================================
// GLFW - An OpenGL library
2010-09-07 11:34:51 -04:00
// Platform: Any
2010-09-07 11:41:26 -04:00
// API version: 3.0
2010-09-07 11:34:51 -04:00
// WWW: http://www.glfw.org/
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
// Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
#include <string.h>
#include <limits.h>
2010-09-07 11:34:51 -04:00
2010-09-09 14:59:50 -04:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
//========================================================================
// Return the available framebuffer config closest to the desired values
// This is based on the manual GLX Visual selection from 2.6
//========================================================================
const _GLFWfbconfig* _glfwChooseFBConfig(const _GLFWfbconfig* desired,
const _GLFWfbconfig* alternatives,
unsigned int count)
{
unsigned int i;
unsigned int missing, leastMissing = UINT_MAX;
unsigned int colorDiff, leastColorDiff = UINT_MAX;
unsigned int extraDiff, leastExtraDiff = UINT_MAX;
const _GLFWfbconfig* current;
const _GLFWfbconfig* closest = NULL;
for (i = 0; i < count; i++)
{
current = alternatives + i;
if (desired->stereo > 0 && current->stereo == 0)
{
// Stereo is a hard constraint
continue;
}
// Count number of missing buffers
{
missing = 0;
if (desired->alphaBits > 0 && current->alphaBits == 0)
missing++;
if (desired->depthBits > 0 && current->depthBits == 0)
missing++;
if (desired->stencilBits > 0 && current->stencilBits == 0)
missing++;
if (desired->auxBuffers > 0 && current->auxBuffers < desired->auxBuffers)
missing += desired->auxBuffers - current->auxBuffers;
if (desired->samples > 0 && current->samples == 0)
{
// Technically, several multisampling buffers could be
// involved, but that's a lower level implementation detail and
// not important to us here, so we count them as one
missing++;
}
}
// These polynomials make many small channel size differences matter
// less than one large channel size difference
// Calculate color channel size difference value
{
colorDiff = 0;
if (desired->redBits > 0)
{
colorDiff += (desired->redBits - current->redBits) *
(desired->redBits - current->redBits);
}
if (desired->greenBits > 0)
{
colorDiff += (desired->greenBits - current->greenBits) *
(desired->greenBits - current->greenBits);
}
if (desired->blueBits > 0)
{
colorDiff += (desired->blueBits - current->blueBits) *
(desired->blueBits - current->blueBits);
}
}
// Calculate non-color channel size difference value
{
extraDiff = 0;
if (desired->alphaBits > 0)
{
extraDiff += (desired->alphaBits - current->alphaBits) *
(desired->alphaBits - current->alphaBits);
}
if (desired->depthBits > 0)
{
extraDiff += (desired->depthBits - current->depthBits) *
(desired->depthBits - current->depthBits);
}
if (desired->stencilBits > 0)
{
extraDiff += (desired->stencilBits - current->stencilBits) *
(desired->stencilBits - current->stencilBits);
}
if (desired->accumRedBits > 0)
{
extraDiff += (desired->accumRedBits - current->accumRedBits) *
(desired->accumRedBits - current->accumRedBits);
}
if (desired->accumGreenBits > 0)
{
extraDiff += (desired->accumGreenBits - current->accumGreenBits) *
(desired->accumGreenBits - current->accumGreenBits);
}
if (desired->accumBlueBits > 0)
{
extraDiff += (desired->accumBlueBits - current->accumBlueBits) *
(desired->accumBlueBits - current->accumBlueBits);
}
if (desired->accumAlphaBits > 0)
{
extraDiff += (desired->accumAlphaBits - current->accumAlphaBits) *
(desired->accumAlphaBits - current->accumAlphaBits);
}
if (desired->samples > 0)
{
extraDiff += (desired->samples - current->samples) *
(desired->samples - current->samples);
}
}
// Figure out if the current one is better than the best one found so far
// Least number of missing buffers is the most important heuristic,
// then color buffer size match and lastly size match for other buffers
if (missing < leastMissing)
closest = current;
else if (missing == leastMissing)
{
if ((colorDiff < leastColorDiff) ||
(colorDiff == leastColorDiff && extraDiff < leastExtraDiff))
{
closest = current;
}
}
if (current == closest)
{
leastMissing = missing;
leastColorDiff = colorDiff;
leastExtraDiff = extraDiff;
}
}
return closest;
}
2010-09-07 11:34:51 -04:00
//========================================================================
// Parses the OpenGL version string and extracts the version number
//========================================================================
2010-09-08 09:58:43 -04:00
void _glfwParseGLVersion(int* major, int* minor, int* rev)
2010-09-07 11:34:51 -04:00
{
GLuint _major, _minor = 0, _rev = 0;
2010-09-08 09:58:43 -04:00
const GLubyte* version;
const GLubyte* ptr;
2010-11-15 13:28:06 -05:00
const char* glesPrefix = "OpenGL ES ";
2010-09-07 11:34:51 -04:00
// Get OpenGL version string
2010-09-08 08:45:52 -04:00
version = glGetString(GL_VERSION);
if (!version)
2010-09-07 11:34:51 -04:00
return;
2010-11-17 08:40:17 -05:00
if (strncmp((const char*) version, glesPrefix, strlen(glesPrefix)) == 0)
2010-11-15 13:28:06 -05:00
{
// The version string on OpenGL ES has a prefix before the version
// number, so we skip past it and then continue as normal
version += strlen(glesPrefix);
}
2010-09-07 11:34:51 -04:00
// Parse string
ptr = version;
2010-09-08 08:45:52 -04:00
for (_major = 0; *ptr >= '0' && *ptr <= '9'; ptr++)
_major = 10 * _major + (*ptr - '0');
2010-09-08 08:45:52 -04:00
if (*ptr == '.')
2010-09-07 11:34:51 -04:00
{
2010-09-08 08:45:52 -04:00
ptr++;
for (_minor = 0; *ptr >= '0' && *ptr <= '9'; ptr++)
2010-11-17 08:41:39 -05:00
_minor = 10 * _minor + (*ptr - '0');
2010-09-08 08:45:52 -04:00
if (*ptr == '.')
2010-09-07 11:34:51 -04:00
{
2010-09-08 08:45:52 -04:00
ptr++;
for (_rev = 0; *ptr >= '0' && *ptr <= '9'; ptr++)
2010-11-17 08:41:39 -05:00
_rev = 10 * _rev + (*ptr - '0');
2010-09-07 11:34:51 -04:00
}
}
// Return parsed values
*major = _major;
*minor = _minor;
*rev = _rev;
}
//========================================================================
2010-09-07 11:50:43 -04:00
// Check if a string can be found in an OpenGL extension string
2010-09-07 11:34:51 -04:00
//========================================================================
2010-09-08 09:58:43 -04:00
int _glfwStringInExtensionString(const char* string,
const GLubyte* extensions)
2010-09-07 11:34:51 -04:00
{
2010-09-08 09:58:43 -04:00
const GLubyte* start;
GLubyte* where;
GLubyte* terminator;
2010-09-07 11:34:51 -04:00
// It takes a bit of care to be fool-proof about parsing the
// OpenGL extensions string. Don't be fooled by sub-strings,
// etc.
start = extensions;
2010-09-08 08:45:52 -04:00
for (;;)
2010-09-07 11:34:51 -04:00
{
2010-09-08 09:58:43 -04:00
where = (GLubyte*) strstr((const char*) start, string);
2010-09-08 08:45:52 -04:00
if (!where)
2010-09-07 11:34:51 -04:00
return GL_FALSE;
2010-09-08 08:45:52 -04:00
terminator = where + strlen(string);
if (where == start || *(where - 1) == ' ')
2010-09-07 11:34:51 -04:00
{
2010-09-08 08:45:52 -04:00
if (*terminator == ' ' || *terminator == '\0')
2010-09-07 11:34:51 -04:00
break;
}
2010-09-07 11:34:51 -04:00
start = terminator;
}
return GL_TRUE;
}
2010-09-09 14:59:50 -04:00
//////////////////////////////////////////////////////////////////////////
////// GLFW public API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 11:34:51 -04:00
//========================================================================
// Swap buffers (double-buffering)
//========================================================================
GLFWAPI void glfwSwapBuffers(void)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED, NULL);
return;
}
if (!_glfwLibrary.currentWindow)
{
_glfwSetError(GLFW_NO_CURRENT_WINDOW, NULL);
return;
}
if (_glfwLibrary.currentWindow)
_glfwPlatformSwapBuffers();
}
//========================================================================
// Set double buffering swap interval (0 = vsync off)
//========================================================================
GLFWAPI void glfwSwapInterval(int interval)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED, NULL);
return;
}
if (!_glfwLibrary.currentWindow)
{
_glfwSetError(GLFW_NO_CURRENT_WINDOW, NULL);
return;
}
_glfwPlatformSwapInterval(interval);
}
2010-09-07 11:34:51 -04:00
//========================================================================
// Check if an OpenGL extension is available at runtime
//========================================================================
2010-09-08 09:58:43 -04:00
GLFWAPI int glfwExtensionSupported(const char* extension)
2010-09-07 11:34:51 -04:00
{
2010-09-08 09:58:43 -04:00
const GLubyte* extensions;
2010-09-13 18:17:00 -04:00
_GLFWwindow* window;
2010-09-08 09:58:43 -04:00
GLubyte* where;
2010-09-07 11:34:51 -04:00
GLint count;
int i;
if (!_glfwInitialized)
{
2010-11-23 11:45:23 -05:00
_glfwSetError(GLFW_NOT_INITIALIZED, NULL);
2010-09-07 11:34:51 -04:00
return GL_FALSE;
}
2010-09-07 11:34:51 -04:00
2010-09-13 18:17:00 -04:00
window = _glfwLibrary.currentWindow;
if (!window)
{
2010-11-23 11:45:23 -05:00
_glfwSetError(GLFW_NO_CURRENT_WINDOW, NULL);
return GL_FALSE;
}
2010-09-07 11:34:51 -04:00
// Extension names should not have spaces
2010-09-08 09:58:43 -04:00
where = (GLubyte*) strchr(extension, ' ');
2010-09-08 08:45:52 -04:00
if (where || *extension == '\0')
2010-09-07 11:34:51 -04:00
return GL_FALSE;
2010-09-09 12:15:32 -04:00
if (window->glMajor < 3)
2010-09-07 11:34:51 -04:00
{
// Check if extension is in the old style OpenGL extensions string
2010-09-08 08:45:52 -04:00
extensions = glGetString(GL_EXTENSIONS);
if (extensions != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 08:45:52 -04:00
if (_glfwStringInExtensionString(extension, extensions))
2010-09-07 11:34:51 -04:00
return GL_TRUE;
}
}
else
{
// Check if extension is in the modern OpenGL extensions string list
2010-09-08 08:45:52 -04:00
glGetIntegerv(GL_NUM_EXTENSIONS, &count);
2010-09-07 11:34:51 -04:00
2010-09-08 08:45:52 -04:00
for (i = 0; i < count; i++)
2010-09-07 11:34:51 -04:00
{
2010-09-09 12:15:32 -04:00
if (strcmp((const char*) window->GetStringi(GL_EXTENSIONS, i),
2010-09-08 08:45:52 -04:00
extension) == 0)
2010-09-07 11:34:51 -04:00
{
return GL_TRUE;
}
}
}
// Additional platform specific extension checking (e.g. WGL)
2010-09-08 08:45:52 -04:00
if (_glfwPlatformExtensionSupported(extension))
2010-09-07 11:34:51 -04:00
return GL_TRUE;
return GL_FALSE;
}
//========================================================================
2010-09-07 11:50:43 -04:00
// Get the function pointer to an OpenGL function.
2010-09-07 11:34:51 -04:00
// This function can be used to get access to extended OpenGL functions.
//========================================================================
2010-09-08 09:58:43 -04:00
GLFWAPI void* glfwGetProcAddress(const char* procname)
2010-09-07 11:34:51 -04:00
{
if (!_glfwInitialized)
{
2010-11-23 11:45:23 -05:00
_glfwSetError(GLFW_NOT_INITIALIZED, NULL);
return NULL;
}
if (!_glfwLibrary.currentWindow)
{
2010-11-23 11:45:23 -05:00
_glfwSetError(GLFW_NO_CURRENT_WINDOW, NULL);
2010-09-07 11:34:51 -04:00
return NULL;
}
2010-09-07 11:34:51 -04:00
2010-09-08 08:45:52 -04:00
return _glfwPlatformGetProcAddress(procname);
2010-09-07 11:34:51 -04:00
}
//========================================================================
// Returns the OpenGL version
//========================================================================
2010-09-08 09:58:43 -04:00
GLFWAPI void glfwGetGLVersion(int* major, int* minor, int* rev)
2010-09-07 11:34:51 -04:00
{
2010-09-13 18:20:42 -04:00
_GLFWwindow* window;
if (!_glfwInitialized)
{
2010-11-23 11:45:23 -05:00
_glfwSetError(GLFW_NOT_INITIALIZED, NULL);
2010-09-07 11:34:51 -04:00
return;
}
2010-09-13 18:20:42 -04:00
window = _glfwLibrary.currentWindow;
if (!window)
{
2010-11-23 11:45:23 -05:00
_glfwSetError(GLFW_NO_CURRENT_WINDOW, NULL);
return;
}
2010-09-07 11:34:51 -04:00
2010-09-08 08:45:52 -04:00
if (major != NULL)
2010-09-09 12:15:32 -04:00
*major = window->glMajor;
2010-09-08 08:45:52 -04:00
if (minor != NULL)
2010-09-09 12:15:32 -04:00
*minor = window->glMinor;
2010-09-08 08:45:52 -04:00
if (rev != NULL)
2010-09-09 12:15:32 -04:00
*rev = window->glRevision;
2010-09-07 11:34:51 -04:00
}