From 62462d2734a16c2b355216b98089f405f6a8163f Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Thu, 5 Jan 2012 21:01:19 +0100 Subject: OpenGL 3 context creation --- samples/OpenGL/x11argb_opengl/Makefile | 2 +- samples/OpenGL/x11argb_opengl/x11argb_opengl.c | 68 +++++++++++++++++++++- .../x11argb_opengl_glsl/x11argb_opengl_glsl.c | 24 +++++--- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/samples/OpenGL/x11argb_opengl/Makefile b/samples/OpenGL/x11argb_opengl/Makefile index 658fc06..abafd5c 100644 --- a/samples/OpenGL/x11argb_opengl/Makefile +++ b/samples/OpenGL/x11argb_opengl/Makefile @@ -1,3 +1,3 @@ x11argb_opengl: x11argb_opengl.c Makefile - $(CC) -std=c99 -g3 -o x11argb_opengl x11argb_opengl.c -lX11 -lXrender -lGL -lm + $(CC) -std=c99 -g3 -o x11argb_opengl -DUSE_GLX_CREATE_CONTEXT_ATTRIB=1 x11argb_opengl.c -lX11 -lXrender -lGL -lm diff --git a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c index f7f4c58..77565f0 100644 --- a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c +++ b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c @@ -69,6 +69,37 @@ GLX_DEPTH_SIZE, 16, None }; +static int isExtensionSupported(const char *extList, const char *extension) +{ + + const char *start; + const char *where, *terminator; + + /* Extension names should not have spaces. */ + where = strchr(extension, ' '); + if ( where || *extension == '\0' ) + return 0; + + /* It takes a bit of care to be fool-proof about parsing the + OpenGL extensions string. Don't be fooled by sub-strings, + etc. */ + for ( start = extList; ; ) { + where = strstr( start, extension ); + + if ( !where ) + break; + + terminator = where + strlen( extension ); + + if ( where == start || *(where - 1) == ' ' ) + if ( *terminator == ' ' || *terminator == '\0' ) + return 1; + + start = terminator; + } + return 0; +} + static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg) { return d && e && arg && (e->type == MapNotify) && (e->xmap.window == *(Window*)arg); @@ -222,9 +253,40 @@ static void createTheRenderContext() fatalError("OpenGL not supported by X server\n"); } - render_context = glXCreateNewContext(Xdisplay, fbconfig, GLX_RGBA_TYPE, 0, True); - if (!render_context) { - fatalError("Failed to create a GL context\n"); +#if USE_GLX_CREATE_CONTEXT_ATTRIB + #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 + #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 + render_context = NULL; + if( isExtensionSupported( glXQueryExtensionsString(Xdisplay, DefaultScreen(Xdisplay)), "GLX_ARB_create_context" ) ) { + typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*); + glXCreateContextAttribsARBProc glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" ); + if( glXCreateContextAttribsARB ) { + int context_attribs[] = + { + GLX_CONTEXT_MAJOR_VERSION_ARB, 3, + GLX_CONTEXT_MINOR_VERSION_ARB, 0, + //GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, + None + }; + + render_context = glXCreateContextAttribsARB( Xdisplay, fbconfig, 0, True, context_attribs ); + fputs("glXCreateContextAttribsARB failed", stderr); + } else { + fputs("glXCreateContextAttribsARB could not be retrieved", stderr); + } + } else { + fputs("glXCreateContextAttribsARB not supported", stderr); + } + + if(!render_context) + { +#else + { +#endif + render_context = glXCreateNewContext(Xdisplay, fbconfig, GLX_RGBA_TYPE, 0, True); + if (!render_context) { + fatalError("Failed to create a GL context\n"); + } } if (!glXMakeContextCurrent(Xdisplay, glX_window_handle, glX_window_handle, render_context)) { diff --git a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c index f7fc68e..643c0ca 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c +++ b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c @@ -62,7 +62,7 @@ static const GLchar *fragment_shader_source = " vec2 mod_texcoord = gl_TexCoord[0].st*vec2(1., 2.) + vec2(0, -0.5 + 0.5*sin(T + 1.5*ts*pi));\n" " if( mod_texcoord.t < 0. || mod_texcoord.t > 1. ) { discard; }\n" " gl_FragColor = -texture2D(texCMYK, mod_texcoord) + texture2D(texRGB, gl_TexCoord[0].st);\n" -" gl_FragColor.a = 1.0;\n" +" gl_FragColor.a = -0.5;\n" "}\n\0"; GLuint shaderFragment = 0; @@ -245,11 +245,11 @@ static void createTheWindow() width = DisplayWidth(Xdisplay, DefaultScreen(Xdisplay))/2; height = DisplayHeight(Xdisplay, DefaultScreen(Xdisplay))/2; - x=width/2, y=height/2; + int const dim = width < height ? width : height; window_handle = XCreateWindow( Xdisplay, Xroot, - x, y, width, height, + 0, 0, dim, dim, 0, visual->depth, InputOutput, @@ -275,11 +275,14 @@ static void createTheWindow() textprop.format = 8; textprop.nitems = strlen(title); - hints.x = x; - hints.y = y; - hints.width = width; - hints.height = height; - hints.flags = USPosition|USSize; + hints.width = dim; + hints.height = dim; + hints.min_aspect.x = 1; + hints.min_aspect.y = 1; + hints.max_aspect.x = 1; + hints.max_aspect.y = 1; + + hints.flags = USSize|PAspect; startup_state = XAllocWMHints(); startup_state->initial_state = NormalState; @@ -461,7 +464,10 @@ static void redrawTheWindow(double T) glDisable(GL_SCISSOR_TEST); - glClearColor(0., 0., 0., 0.0); +#if 0 + glClearColor(88./255., 95./255., 160./255., 0.); +#endif + glClearColor(0., 0., 0., 0.); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); -- cgit v1.2.3